Skip to content

Commit

Permalink
Moved functions to modules. Added more optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
naitmare01 committed Sep 9, 2018
1 parent 14ec541 commit 9e9a66f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 57 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.gitignore
README.md
Private
__pycache__
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
__pycache__
How to run.txt
76 changes: 23 additions & 53 deletions GetDriftlaget.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import requests, json, time, sched, sys, argparse
import requests, json, sched, sys, argparse, time
from sys import argv
from flata import Flata, Query, where
from Modules import cisco
from Modules import flataDb
from Modules import driftlagetApi
from flata import Flata, where
from Modules import cisco, flataDb, driftlagetApi, log


#Handle command line arguments
Expand All @@ -14,6 +12,9 @@ def arguments():
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-b', '--bottoken', help='Access token for your bot.', required=True)
requiredNamed.add_argument('-r', '--roomid', help='The room ID.', required=True)
parser.add_argument('-p', '--pollinginterval', help='The polling intervall in seconds. If left untouched default is 30.', type=int, default=30)
parser.add_argument('-u', '--url', help='API URL to for the Church of Sweden current IT operation status(goo.gl/XXKFxQ). If left untouched default is https://webapp.svenskakyrkan.se/driftlaget/v2/api/news', default='https://webapp.svenskakyrkan.se/driftlaget/v2/api/news')
parser.add_argument('-lt', '--logthreshold', help='Number of entries to be keep in the log database before the databse is purged. If left untouched default is 100', type=int, default=100)

args = parser.parse_args()

Expand All @@ -23,81 +24,50 @@ def arguments():

return args

#build log message
def log(msg):
timestamp = time.ctime()
loggmsg = {'Timestamp': str(timestamp), 'Message': msg}
return loggmsg


#Main
def main(sc):

args = arguments()
#args = arguments()

#Var declaration
DriftlagetUrl = "https://webapp.svenskakyrkan.se/driftlaget/v2/api/news"
DriftlagetUrl = args.url
botToken = args.bottoken
roomId = args.roomid
pollingInterval = args.pollinginterval
logthreshold = args.logthreshold
dbPath = "mydb.json"
dbTableName = "driftlaget"
logdbTableName = "log"
roomId = args.roomid

#Get data from driftlaget.
result = driftlagetApi.apiCallPublished(DriftlagetUrl)
finalResult = cisco.buildJson(result)

#Start and initialize database
#Start and initialize database with two tables
db = flataDb.initDb(dbPath, dbTableName)
logdb = flataDb.initDb(dbPath, logdbTableName)
q = Query()

#Log to database
logdb.insert(log("Starting script"))
logdb.insert(log.log("Starting script"))

#Post into Webex space and update database.
for n in finalResult:
incidentIddb = n["IncidentId"]
lastUpdateddb = n["LastUpdated"]
entryExist = db.search((q.IncidentId == incidentIddb))

#If entry is missing in the database. Insert into database and send to Webex.
if not entryExist:
db.insert(n)
cisco.send_it(botToken, roomId, n)

logdb.insert(log("New insert"))
else:
newEntries = db.search((q.IncidentId == incidentIddb) & (q.LastUpdated < lastUpdateddb))

#Update new entries that have newer lastUpdated and send to Webex.
if newEntries:
db.update(n, where('IncidentId') == incidentIddb)
cisco.send_it(botToken, roomId, n)

logdb.insert(log("New update"))

#Remove objects from database that doesnt exist on the Driftlage.
for i in db.all():
IncId = (i["IncidentId"])
entryInDb = filter(lambda x : x['IncidentId'] == IncId, finalResult)
entryInDb = list(entryInDb)

if not entryInDb:
db.remove(q.IncidentId == IncId)
logdb.insert(log("New remove"))

cisco.postToSpace(finalResult, db, logdb, botToken, roomId)

#Remove objects from database that doesnt exist on the Driftlage and log action to database.
flataDb.removeStaleRecords(finalResult, db, logdb)

#Purge logdata if over 100 entries.
flataDb.cleanLogDb(logdb, 100)
flataDb.cleanLogDb(logdb, logthreshold)

#Restart the script.
logdb.insert(log("Restarting script"))
s.enter(30, 1, main, (sc,))
logdb.insert(log.log("Restarting script"))
s.enter(pollingInterval, 1, main, (sc,))

if __name__ == '__main__':
args = arguments()
# sched is used to schedule main function every 30 seconds.
# for that once main function executes in end,
# we again schedule it to run in 30 seconds
s = sched.scheduler(time.time, time.sleep)
s.enter(30, 1, main, (s,))
s.enter(args.pollinginterval, 1, main, (s,))
s.run()
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ More info about Webex Teams: https://developer.webex.com/
7. docker run get-driftlaget --bottoken 'secret bot access token' --roomid 'secret room id'
```

### **Optional parameters when running image **
```
--pollinginterval
The polling intervall in seconds. If left untouched default is 30.
I.e. how often the bot should poll the API.
```
```
--url
API URL to for the Church of Sweden current IT operation status(goo.gl/XXKFxQ). If left untouched default is https://webapp.svenskakyrkan.se/driftlaget/v2/api/news
```
Empty file added modules/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions modules/cisco.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import requests, json
from datetime import datetime
from flata import Query, where
from . import log


#Send data to Cisco Teams space.
Expand Down Expand Up @@ -64,3 +66,28 @@ def formatTime(time):
formatedTime = datetime.strptime(formatedTime, "%Y%m%d%H%M%S")

return formatedTime


#Post into Webex space and update database.
def postToSpace(objectset, database, logdb, botToken, roomId):
q = Query()
for n in objectset:
incidentIddb = n["IncidentId"]
lastUpdateddb = n["LastUpdated"]
entryExist = database.search((q.IncidentId == incidentIddb))

#If entry is missing in the database. Insert into database and send to Webex.
if not entryExist:
database.insert(n)
send_it(botToken, roomId, n)

logdb.insert(log.log("New insert on object with incidentID of: " + incidentIddb))
else:
newEntries = database.search((q.IncidentId == incidentIddb) & (q.LastUpdated < lastUpdateddb))

#Update new entries that have newer lastUpdated and send to Webex.
if newEntries:
database.update(n, where('IncidentId') == incidentIddb)
send_it(botToken, roomId, n)

logdb.insert(log.log("New update on object with incidentID of: " + incidentIddb))
14 changes: 13 additions & 1 deletion modules/flataDb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from flata import Flata, Query, where
from flata.storages import JSONStorage
import json
from . import log

def initDb(dbPath, tableName):
#Create db
Expand All @@ -26,3 +27,14 @@ def countLog(data, numberOfLogs):
else:
return False

#Remove stale records from db that doesnt exist on the Driftlage.
def removeStaleRecords(objectset, database, logdb):
q = Query()
for i in database.all():
IncId = i["IncidentId"]
entryInDb = filter(lambda x : x['IncidentId'] == IncId, objectset)
entryInDb = list(entryInDb)

if not entryInDb:
database.remove(q.IncidentId == IncId)
logdb.insert(log.log("New remove on object with incidentID of: " + IncId))
8 changes: 8 additions & 0 deletions modules/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import time


#build log message
def log(msg):
timestamp = time.ctime()
loggmsg = {'Timestamp': str(timestamp), 'Message': msg}
return loggmsg
6 changes: 3 additions & 3 deletions read_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def main():
for i in tables:
db = flataDb.initDb(dbPath, i)
response = db.all()
print(json.dumps(response, indent=2))
print("Reading data from table:", i, '\n', json.dumps(response, indent=2))

if args.log:
tables = 'log'
db = flataDb.initDb(dbPath, tables)
response = db.all()
print(json.dumps(response, indent=2))
print("Reading data from table:", tables, '\n', json.dumps(response, indent=2))

if args.driftlaget:
tables = 'driftlaget'
db = flataDb.initDb(dbPath, tables)
response = db.all()
print(json.dumps(response, indent=2))
print("Reading data from table:", tables, '\n', json.dumps(response, indent=2))


if __name__ == '__main__':
Expand Down

0 comments on commit 9e9a66f

Please sign in to comment.