Changeset 634 in tmcsimulator for branches/LCSv2/controllers/default.py.bak


Ignore:
Timestamp:
02/25/2021 10:00:55 AM (5 years ago)
Author:
jdalbey
Message:

LCS implement #252 Add logging for new closures and status updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/LCSv2/controllers/default.py.bak

    r626 r634  
    11# added comments for testing 
    22# Constants 
     3kLogfile = "samplelogfile.txt" 
     4kSimtimefile = "sim_elapsedtime.json" 
    35hwys = ['','1','5', '22', '55', '57', '73', '74', '91', '133', '142', '241', '261', '405', '605'] 
    46hwyDirections = ['','NB', 'SB', 'NB/SB','EB','WB','EB/WB'] 
    5 # Names to appeaer in the username dropdown box 
     7# Names to appear in the username dropdown box 
    68users = [] 
    79# Show the login page 
     
    189191            fieldname = "s"+session.statustype[0:4]+"time" 
    190192            db(db.closures.closureid == session.statustype[4:]).update(**{fieldname:now.strftime("%H%M")}) 
     193            # Log the update to external file 
     194            logmessage = getSimTime() + " LCS status update: " + session.statustype[4:] + ", " + session.statustype[0:4] + ", " + session.statuser + " " + "\n" 
     195            text_file = open(kLogfile, "a") 
     196            text_file.write(logmessage) 
     197            text_file.close() 
    191198        else: 
    192199            msg = "error because only checking one box is allowed." 
     
    201208    else: 
    202209        return "" 
     210 
     211# Fetch simulation time and format it into a timestamp 
     212def getSimTime(): 
     213    import json,datetime 
     214    try: 
     215        jsontime = json.load(open(kSimtimefile,'r')) 
     216        currentSimTime = jsontime["elapsedtime"] 
     217        timestamp = str(datetime.timedelta(seconds = int(currentSimTime))) 
     218        return timestamp 
     219    except: 
     220        # Fallback if missing file, use current time  
     221        now = datetime.datetime.today() 
     222        return now.strftime("%H:%M:%S") 
    203223 
    204224# Create a new record 
     
    296316        newrec = db.closures.insert(closureid=newID, lognum=newLognum, route=form.vars.route, direction=form.vars.direction, facility=form.vars.facility, startcounty=form.vars.startcounty, endcounty=form.vars.endcounty, startlocation=form.vars.startlocation, endlocation=form.vars.endlocation, startdate=form.vars.startdate, enddate=form.vars.enddate, starttime=form.vars.starttime+form.vars.starttimemin, endtime=form.vars.endtime+form.vars.endtimemin, closuretype=form.vars.closuretype, closedlanes=selectedlanes, worktype=form.vars.worktype, estdelay=form.vars.estdelay, tmpcozeep=getCheckbox(form.vars.cozeep), tmpdetour=getCheckbox(form.vars.detour), supervisor=supervisor_name, fieldrep=fieldrep_name, s1097date='', s1098date='', s1022date='' ) 
    297317        session.flash = 'New lane closure added: ' + newID + ' ' + newLognum + ': ' + selectedlanes 
     318        # Log the new closure to external file. Username, closureID, route, dir, type of closure, type of work 
     319        logmessage = getSimTime() + " LCS new closure: " + session.username + newID + '.' + newLognum + ', ' + form.vars.route + form.vars.direction + ', ' + form.vars.closuretype + ', ' + form.vars.worktype + "\n" 
     320        text_file = open(kLogfile, "a") 
     321        text_file.write(logmessage) 
     322        text_file.close() 
    298323        redirect(URL('search.html')) 
    299324    return dict(form=form) 
     
    303328    form = FORM("Press this button to reset the closure database to its original state at the start of a simulation.",BR(), 
    304329                "Warning: this will delete all the current closures.",BR(), 
     330                "Import filename is 'db_closures_start.csv'.",BR(), 
    305331                INPUT(_value="Reset Database", _type='submit', _class="btn btn-primary btn-default")) 
    306332    if form.process().accepted: 
     
    308334        with open('db_closures_start.csv', 'rb') as dumpfile: 
    309335            db.closures.import_from_csv_file(dumpfile)  # import from starting state 
     336        session.flash = 'Closure database has been reset' 
    310337        redirect(URL('search.html')) 
    311338    return dict(form=form) 
Note: See TracChangeset for help on using the changeset viewer.