| 1 | import json, time, ConfigParser |
|---|
| 2 | #from copy import deepcopy |
|---|
| 3 | |
|---|
| 4 | # Incident Activity log Watcher |
|---|
| 5 | # Look for changes in the IncidentActivity log. |
|---|
| 6 | # The comments from the Incident Activity log be will appended to the log |
|---|
| 7 | # as they arrive from the web app. |
|---|
| 8 | # We only need to keep track of the log length in order to |
|---|
| 9 | # determine if a new comment has been added. We will output the |
|---|
| 10 | # new messages that arrived during the last wait interval. |
|---|
| 11 | # jdalbey 7/24/2019 |
|---|
| 12 | |
|---|
| 13 | lastLineNum = 0 |
|---|
| 14 | |
|---|
| 15 | # Utility functions |
|---|
| 16 | def isEmpty(cmsitem): |
|---|
| 17 | return cmsitem == ",,,,," |
|---|
| 18 | def isFull(cmsitem): |
|---|
| 19 | return not isEmpty(cmsitem) |
|---|
| 20 | |
|---|
| 21 | # Read the log |
|---|
| 22 | def readFile(): |
|---|
| 23 | # get path to input file from configuration |
|---|
| 24 | config = ConfigParser.ConfigParser() |
|---|
| 25 | config.read('config/logging_service.cfg') |
|---|
| 26 | logfilepath = config.get('Paths', 'ActivityLogPath') |
|---|
| 27 | logfilename = "data.json" #"IncidentActivity.log" |
|---|
| 28 | data_summary = "data_summary.json" |
|---|
| 29 | lines = [] |
|---|
| 30 | output = [] |
|---|
| 31 | try: |
|---|
| 32 | json_file = open (logfilepath + logfilename,'r') |
|---|
| 33 | except IOError: |
|---|
| 34 | print "Error: missing "+logfilepath+logfilename+" file." |
|---|
| 35 | else: |
|---|
| 36 | jsonData=json_file.read() |
|---|
| 37 | # implement ticket #192 |
|---|
| 38 | output = json.loads(jsonData)['data'] |
|---|
| 39 | # read in data_summary_json file |
|---|
| 40 | # assuming the data_summary.json is in the trunk foldera |
|---|
| 41 | data_summary = open(logfilepath + data_summary, 'r') |
|---|
| 42 | summary_json = data_summary.read() |
|---|
| 43 | data_lst = json.loads(summary_json)['data'] |
|---|
| 44 | for entry in data_lst: |
|---|
| 45 | # put the first 3 entries as place holder to be consistent with other type of logging |
|---|
| 46 | output.append([entry[0], entry[1] , entry[2], entry[3] , ", Incident Created, ", ""]) |
|---|
| 47 | json_file.close() |
|---|
| 48 | return output |
|---|
| 49 | |
|---|
| 50 | def setup(): |
|---|
| 51 | # nothing needed for setup |
|---|
| 52 | return |
|---|
| 53 | |
|---|
| 54 | # Retrieve new messages from activity log |
|---|
| 55 | def getLogEntries(): |
|---|
| 56 | global lastLineNum |
|---|
| 57 | msgList = readFile() |
|---|
| 58 | currList = [] |
|---|
| 59 | currList = msgList[lastLineNum:] # new items since last file read |
|---|
| 60 | lastLineNum = len(msgList) |
|---|
| 61 | resultList = [] |
|---|
| 62 | # Format messages into desired result format |
|---|
| 63 | for item in currList: |
|---|
| 64 | # implement ticket #188 |
|---|
| 65 | # extract desired fields |
|---|
| 66 | incident_num = item[0].split("-")[1] |
|---|
| 67 | # extract the incident number from the date time field |
|---|
| 68 | name = item[3].replace(',','') |
|---|
| 69 | code = item[4].replace(',','') |
|---|
| 70 | msg = item[5].replace(',','') |
|---|
| 71 | desiredFields = "Activity Log,"+name+"," + incident_num + " " + code+" "+msg |
|---|
| 72 | # Append to results list |
|---|
| 73 | resultList.append(desiredFields) |
|---|
| 74 | return resultList |
|---|
| 75 | |
|---|
| 76 | # Local main for unit testing |
|---|
| 77 | def main(): |
|---|
| 78 | setup() |
|---|
| 79 | # Loop Forever, checking every five seconds |
|---|
| 80 | while True: |
|---|
| 81 | # Look for new messages |
|---|
| 82 | answer = getLogEntries() |
|---|
| 83 | # Output results |
|---|
| 84 | for item in answer: |
|---|
| 85 | print item |
|---|
| 86 | # wait |
|---|
| 87 | time.sleep(5) |
|---|
| 88 | |
|---|
| 89 | if __name__ == "__main__": |
|---|
| 90 | main() |
|---|