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

Revision 645, 5.2 KB checked in by jdalbey, 4 years ago (diff)

activitylog_watcher.py updated to read log filenames from config file.

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# get data filenames from configuration
49# returns incident data filename, incident summary filename
50# jdalbey 2022.9.1
51def getLogFilenames():
52    config = ConfigParser.ConfigParser()
53    config.read('config/logging_service.cfg')
54    file1 = config.get('Files','ActivityLogDataFilename')
55    file2 = config.get('Files','ActivityLogSummaryFilename')
56    return file1,file2
57   
58# Read the activty log entries
59def readFile1():
60    logfilename = getLogFilenames()[0]  #"IncidentActivity.log"
61    output = []
62    try: 
63        log_file = open (getLogFilePath() + logfilename,'r')
64    except IOError:
65        print "Error: missing "+logfilepath + logfilename + " file."
66    else:
67        logData=log_file.read()  # Read the activity log
68        # implement ticket #192
69        output = json.loads(logData)['data']  # parse the log data into a dict
70        log_file.close()
71    return output
72
73# Read the log of new incidents
74# It has a different format than incident_activity.json, so we put it in a separate function.
75def readFile2():
76    data_summary = getLogFilenames()[1] #"incident_description.json"
77
78    output = []
79    try: 
80        data_summary = open(getLogFilePath() + data_summary, 'r')
81    except IOError:
82        print "Error: missing "+logfilepath + data_summary + " file."
83    else:
84        # read in incident_description json file that contains Incident summary data
85        # assuming this file is in the same folder
86        summary_json = data_summary.read()
87        data_lst = json.loads(summary_json)['data'] # parse the new incident data
88        # Append each 'incident created' info to the results
89        for entry in data_lst:
90            # put the first 3 fields as place holder to be consistent with other type of logging
91            output.append([entry[0], entry[1] , entry[2], entry[3] , ", Incident Created, ", ""]) 
92        data_summary.close()
93    return output
94   
95# Retrieve new messages from activity log
96def getLogEntries():
97    global file1length, file2length
98    # Read activity log of new activity
99    msgList = readFile1()
100    file1items = []
101    file1items = msgList[file1length:]    # new items since last file read
102    file1length = len(msgList)
103    # Read activity log of new incidents
104    msgList = readFile2()
105    file2items = []
106    file2items = msgList[file2length:]    # new items since last file read
107    file2length = len(msgList)
108    # Concatenate the two logs
109    currList = file1items + file2items
110
111    resultList = []
112    # Format messages into desired result format
113    for item in currList:
114        # implement ticket #188
115        # extract desired fields
116        incident_num = item[0].split("-")[1] 
117        # extract the incident number from the date time field
118        name = item[3].replace(',','')
119        code = item[4].replace(',','')
120        msg = item[5].replace(',','')
121        desiredFields = "Activity Log,"+name+"," + incident_num + " " + code+" "+msg
122        # Append to results list
123        resultList.append(desiredFields)
124    return resultList
125
126def setup():
127    # nothing needed for setup
128    return
129
130# Local main for unit testing
131def main():
132    setup()
133    # Loop Forever, checking every five seconds
134    while True:
135        # Look for new messages
136        answer = getLogEntries()
137        # Output results
138        for item in answer:
139            print item
140        # wait
141        time.sleep(5)
142
143if __name__ == "__main__":
144    main()
Note: See TracBrowser for help on using the repository browser.