source: tmcsimulator/trunk/src/python/unifiedlogger/activitylog_watcher.py @ 644

Revision 644, 4.8 KB checked in by jdalbey, 5 years ago (diff)

activitylog_watcher.py modified to fix defect #255.

Line 
1import json, time, ConfigParser
2# Incident Activity log Watcher
3# Look for changes in the IncidentActivity log.
4# The comments from the Incident Activity log be will appended to the log
5# as they arrive from the web app.
6# We only need to keep track of the log length in order to
7# determine if a new comment has been added.  We will output the
8# new messages that arrived during the last wait interval.
9# jdalbey  7/24/2019
10# Refactored and revised to fix defect #255 5/27/2021
11# Notes:
12# incident_description.json contains the summary details of the incident
13# incident_activity.json contains the incident detail entries for all created incidents.
14# incident_description.json fields
15#      "20190523-008",  # Incident ID
16#      "05\\/23",                # Date
17#      "15:55.25",       # Time
18#      "Hall, Kacie",    # User ID
19#      "SR-73",          # Route
20#      "BAKER ",                 # Location
21#      "SB",             # Direction
22#      "Advisory",               # Incident Category
23#      "Advisory: National Weather Service Advisory",  # Incident Type
24#      "RV FIRE "                # Description
25#incident_activity fields:
26#      "20190523-004",   # Incident ID
27#      "05\\/23",               # Date
28#      "14:14.04",              # Time
29#      "Sarker, Afrid", # User ID
30#      "CMS Activation",         # Activity Code
31#      "UPDATED CMS CMS ID: 1214504   LOCATION: N I-5 12.86  Avery Pkwy"          Description
32
33# Global variables to remember length of file between readings
34file1length = 0
35file2length = 0
36
37# Utility functions
38def isEmpty(cmsitem):
39    return cmsitem == ",,,,,"
40def isFull(cmsitem):
41    return not isEmpty(cmsitem)
42# get path to input file from configuration
43def getLogFilePath():
44    config = ConfigParser.ConfigParser()
45    config.read('config/logging_service.cfg')
46    return config.get('Paths', 'ActivityLogPath')
47   
48# Read the activty log entries
49def readFile1():
50    logfilename = "incident_activity.json"   #"IncidentActivity.log"
51    output = []
52    try: 
53        log_file = open (getLogFilePath() + logfilename,'r')
54    except IOError:
55        print "Error: missing "+logfilepath + logfilename + " file."
56    else:
57        logData=log_file.read()  # Read the activity log
58        # implement ticket #192
59        output = json.loads(logData)['data']  # parse the log data into a dict
60        log_file.close()
61    return output
62
63# Read the log of new incidents
64# It has a different format than incident_activity.json, so we put it in a separate function.
65def readFile2():
66    data_summary = "incident_description.json"
67    output = []
68    try: 
69        data_summary = open(getLogFilePath() + data_summary, 'r')
70    except IOError:
71        print "Error: missing "+logfilepath + data_summary + " file."
72    else:
73        # read in incident_description json file that contains Incident summary data
74        # assuming this file is in the same folder
75        summary_json = data_summary.read()
76        data_lst = json.loads(summary_json)['data'] # parse the new incident data
77        # Append each 'incident created' info to the results
78        for entry in data_lst:
79            # put the first 3 fields as place holder to be consistent with other type of logging
80            output.append([entry[0], entry[1] , entry[2], entry[3] , ", Incident Created, ", ""]) 
81        data_summary.close()
82    return output
83   
84# Retrieve new messages from activity log
85def getLogEntries():
86    global file1length, file2length
87    # Read activity log of new activity
88    msgList = readFile1()
89    file1items = []
90    file1items = msgList[file1length:]    # new items since last file read
91    file1length = len(msgList)
92    # Read activity log of new incidents
93    msgList = readFile2()
94    file2items = []
95    file2items = msgList[file2length:]    # new items since last file read
96    file2length = len(msgList)
97    # Concatenate the two logs
98    currList = file1items + file2items
99
100    resultList = []
101    # Format messages into desired result format
102    for item in currList:
103        # implement ticket #188
104        # extract desired fields
105        incident_num = item[0].split("-")[1] 
106        # extract the incident number from the date time field
107        name = item[3].replace(',','')
108        code = item[4].replace(',','')
109        msg = item[5].replace(',','')
110        desiredFields = "Activity Log,"+name+"," + incident_num + " " + code+" "+msg
111        # Append to results list
112        resultList.append(desiredFields)
113    return resultList
114
115def setup():
116    # nothing needed for setup
117    return
118
119# Local main for unit testing
120def main():
121    setup()
122    # Loop Forever, checking every five seconds
123    while True:
124        # Look for new messages
125        answer = getLogEntries()
126        # Output results
127        for item in answer:
128            print item
129        # wait
130        time.sleep(5)
131
132if __name__ == "__main__":
133    main()
Note: See TracBrowser for help on using the repository browser.