Warning: Can't use blame annotator:
svn blame failed on trunk/src/python/unifiedlogger/activitylog_watcher.py: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 687, 5.3 KB checked in by jdalbey, 4 years ago (diff)

logging_service.py, et.al. Revise how it handles a missing CADcomments.log file - just skip the file read but continue. Also update all watchers to use config dir.

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