def __setrun(self,dbname,t0=None,t1=None): # print 'Set Run for ', dbname, t0, t1 if t0 is None: t0 = time.time() if t1 is None: t1 = MAX_EPOCH where = "db='%s'" % dbname r = self.runs.select_one(where = where) if r == {}: self.runs.insert(db=dbname) notes = "%s to %s" % (tformat(t0), tformat(t1)) where = "db='%s'" % (dbname) # print 'update runs: ', where, t0, t1, notes self.runs.update(start_time=t0, where=where) self.runs.update(stop_time=t1, where=where) self.runs.update(notes=notes, where=where)
def manage_positions(self,**kw): ' view details of positions saved for an instrument ' wr = self.write mykw = {'inst_id':'-1'} mykw.update(kw) inst_id = int(mykw['inst_id']) if mykw.has_key('submit'): for k,v in mykw.items(): hide = ('hidden' == v and k not in (None,'')) self.arch.hide_position(inst_id=inst_id,name=k,hide=hide) if 'remove' == v and k not in (None,''): self.arch.delete_position(inst_id=inst_id,name=k) positions = self.arch.get_positions(inst_id=inst_id,get_hidden=True) instrument,station = self.arch.get_instrument_names_from_id(inst_id) self.starthtml() self.show_links(active_tab='Instruments') flink = "%s/manage_positions?inst_id=%i" % (mainpage,inst_id) if DEBUG: self.show_dict(mykw) wr("""<form action ='%s' name='inst_form' enctype='multipart/form-data' method ='POST'> <h3> Positions for Instrument %s in Station %s </h3><table> <tr><td align='center'> Position Name </td> <td align='center'> Time Saved </td> <td align='center'> Status </td></tr> <tr><td colspan =3 ><hr></td></tr>""" % (flink,instrument,station)) for p in positions: pname = p[1] ptime = tformat(p[2], format="%Y-%m-%d %H:%M:%S") + ' ' cshow,chide = ("checked='true'",'') if p[3] =='no': cshow,chide = ('',"checked='true'") wr("""<tr><td>%s</td><td align='center'>%s</td><td> <input type='radio' %s name='%s' value='active'>active <input type='radio' %s name='%s' value='hidden'>hidden <input type='radio' name='%s' value='remove'>delete forever </td> </tr>""" % (pname,ptime,cshow,pname,chide,pname,pname)) wr("""<tr><td colspan =3 ><hr></td></tr> <tr><td><input type='submit' name='submit' value='Update Positions'></td> <td colspan=2> <a href='%s?station=%s&instrument=%s'>View Positions for %s</a> </td></tr></table>""" % (instpage,station,instrument,instrument)) self.endhtml() return self.get_buffer()
def runs_report(self, n=10): """return a report (list of text lines) for the archiving runs showing the time ranges for the (at most) 10 most recent runs. """ r = [] for i in self.runs.select(where='1=1 order by start_time desc limit %i' % n): timefmt = "%6.2f " if i['db'] == self.arch_db: timefmt = "%6.1f*" i['stop_time'] = time.time() days = timefmt % ((i['stop_time'] - i['start_time'])/(24*3600.0)) drange = "%s to %s" %(tformat(i['start_time'],format="%Y-%m-%d"), tformat(i['stop_time'],format="%Y-%m-%d")) r.append("| %16s | %24s | %10s |" % (i['db'],drange,days)) r.reverse() out = [self.runs_line,self.runs_title,self.runs_line] for i in r: out.append(i) out.append(self.runs_line) return out
def cache_report(self,brief=False,dt=60): """return a report (list of text lines) for caching process, giving number of values cached in the past dt seconds. Use 'brief=False' to show which PVs have been cached. """ out = [] pid = self.get_cache_pid() ret = self.cache.select(where="ts> %i order by ts" % (time.time()-dt)) fmt = " %s %.25s = %s" if not brief: for r in ret: out.append(fmt % (tformat(t=r['ts'],format="%H:%M:%S"), r['pvname']+' '*20, r['value']) ) fmt = '%i PVs had values updated in the past %i seconds. pid=%i' out.append(fmt % (len(ret),dt,pid)) return out
def show(self,**kw): self.kw.update(kw) pv = self.kw['pv'] station = self.kw['station'] instrument = self.kw['instrument'] wr = self.write self.starthtml() self.show_links(pv=pv,help='instruments',active_tab='Instruments') if DEBUG: self.show_dict(self.kw) inst_id = -1 if self.kw.has_key('inst_id'): try: inst_id = int(self.kw['inst_id']) except: pass newpos_name = '' if self.kw.has_key('newpos_name'): newpos_name =clean_input(self.kw.get('newpos_name','').strip()) tsec = -1 if self.kw.has_key('date'): try: tsec = time_str2sec(self.kw.get('date','').strip()) except: pass try: use_newstation = int(self.kw.get('newstation','0').strip())==1 except: use_newstation = False if not use_newstation and inst_id>0: # we may act on new position name or lookup-by-date if newpos_name!='': # save new position name self.arch.save_instrument_position(inst_id = inst_id, name=newpos_name) instrument,station = self.arch.get_instrument_names_from_id(inst_id) elif tsec>0: # look up position by date return self.view_position(position= self.POS_DATE, inst= inst_id, date=tsec) self.kw['station_sel'] = '' # wr(" <h3> Instruments </h3> ") self.show_station_choices() self.startform(action=instpage,hiddenkeys=('pv',)) if station == '': station_list = self.stations.keys() ; station_list.sort() station = station_list[0] if station == '': wr("Please select a station.") else: # a large outer 2 column table: ## self.starttable wr("""<table cellpadding=2 border=0 rules='cols'><tr valign='top'><td width=30%% align='top'> <table bgcolor='#F9F9F5' border=0 frame='box'><tr><td>Instruments for %s:</td></tr> <tr><td><a href='%s/add_instrument?station=%s'> <add an instrument> </a></td></tr>""" % (station,mainpage,station)) self.instruments = {} for s in self.arch.list_instruments(station=station): self.instruments[s['name']] = (s['id'],s['notes']) inst_list = self.instruments.keys() ; inst_list.sort() for inst in inst_list: ilink = "<a href='%s?station=%s&instrument=%s'>%s</a>" % (instpage,station,inst,inst) wr("<tr><td>%s</td></tr>" % ilink) wr("</table></td><td><table align='center' valign='top' frame='border'>") # right side : positions for this instrument if instrument == '': wr("<tr><td colspan=2>Please select an instrument</td></tr>") else: inst_id = self.instruments[instrument][0] positions = self.arch.get_positions(inst=instrument,station=station, get_hidden=False) pvlist = self.arch.get_instrument_pvs(name=instrument,station=station) pvlist.sort() wr("<input type='hidden' name='instrument_name' value='%s'>" % instrument) wr("<input type='hidden' name='inst_id' value=%i>" % inst_id) wr("<tr><td align='center' colspan=2>%s: %s</td></tr>" % (station,instrument)) wr("""<tr><td colspan=2> Save Current Position As: <input type='text' name='newpos_name' value='' size=35/> <input type='submit' name='save_position' value='Save'/></td><tr> <tr><td colspan=2> Look up position by date: <input type='text' width=33 id='date' name='date' value='%s'/> <button id='date_trig'>...</button> <button id='date_search' name='search_position' value='Search'>Search</button> </td></tr> <tr><td colspan=2> </td></tr> """ % (tformat(time.time(),format="%Y-%m-%d %H:%M:%S"))) wr(jscal_get_date) if len(positions)==0: wr("<tr><td colspan=2 align='center'> No saved positions </td></tr>") else: wr(""" <tr><td colspan=2><hr></td></tr> <tr><td colspan=2>Saved Positions: (<a href='%s/manage_positions?inst_id=%i'>Manage Positions</a>)</td></tr> <tr><td>Name</td><td>Time Saved</td></tr>""" % (mainpage,inst_id)) for p in positions: plink = "<a href='%s/view_position?inst=%i&position=%s'>%s" % (mainpage,inst_id,p[1],p[1]) wr("<tr><td>%s </td><td>%s</td></tr>" % (plink,tformat(p[2],format="%Y-%m-%d %H:%M:%S"))) wr("""<tr><td colspan=2><hr></td></tr><tr><td colspan=2>PVs in instrument: (<a href='%s/modify_instrument?inst_id=%i'>View/Change)</td></tr>""" % (mainpage,inst_id)) ix = 0 for pvn in pvlist: self.write('<td><a href="%s?pv=%s">%s</a></td>'% (pvinfopage,pvn,pvn)) ix = ix + 1 if ix % 2 == 0: wr("</tr><tr>") wr("</table>") self.endform() self.endhtml() return self.get_buffer()
def view_position(self,**kw): ' view details of a saved position ' wr = self.write mykw = {'inst':'','position':self.POS_DATE,'desc':'','date':'-1'} mykw.update(kw) inst_id = mykw['inst'] if inst_id == '': inst_id = '0' inst_id = int(inst_id) date = int(mykw['date']) instrument,station = self.arch.get_instrument_names_from_id(inst_id) position = mykw['position'] if position == self.POS_DATE and date > 0: pv_vals,save_time = self.arch.get_instrument_values(inst_id=inst_id,ts=date) pname = 'Position' else: pv_vals,save_time = self.arch.get_instrument_values(inst_id=inst_id,position=position) pname = 'Position %s ' % position save_ctime = tformat(save_time,format="%Y-%m-%d %H:%M:%S") if mykw.has_key('submit'): form = 'plain' if mykw['submit'].startswith('IDL'): form = 'idl' elif mykw['submit'].startswith('Python'): form = 'py' if position == self.POS_DATE: position = '(not named : retrieved by date)' headers = ["station / instrument: %s / %s" % (station,instrument), "position name: %s " % position, "saved: %s " % save_ctime] wr(write_saverestore(pv_vals,format=form,header=headers)) return self.get_buffer() elif mykw.has_key('save_position') and mykw.has_key('newpos_name') and date>0: try: inst_id = int(mykw['inst']) except: inst_id = -1 position =clean_input(mykw['newpos_name'].strip()) self.arch.save_instrument_position(inst_id = inst_id, name=position, ts=date) pname = 'Position %s ' % position self.starthtml() self.show_links(active_tab='Instruments') # self.show_dict(mykw) flink = "%s/view_position?inst=%i&position=%s&date=%i" % (mainpage,inst_id,position,save_time) wr("""<form action ='%s' enctype='multipart/form-data' method ='POST'> <h3> %s for Instrument %s / Station %s<p> Position Saved %s </h3><table> <tr><td> PV Name</td> <td> Saved Value </td><td> Current Value </td></tr> <tr><td colspan =3 ><hr></td></tr>""" % (flink,pname,instrument,station,save_ctime)) for pvname,val in pv_vals: curval = 'Unknown' cacheval = self.arch.cache.select_one(where="pvname='%s'"%pvname) if cacheval.has_key('value'): curval = str(cacheval['value']) wr("<tr><td width=20%% >%s</td><td width=30%% >%s</td><td width=30%% >%s</td></tr>" % (pvname,str(val),curval)) wr("<tr><td colspan =3 ><hr></td></tr></table>") if pname == 'Position': #(this is an unsaved position!) wr("""Save this position as:<input type='text' name='newpos_name' value='' size=35/> <input type='submit' name='save_position' value='Save'/><p>""") wr("""To restore to these settings, select one of these output formats:<p> <input type='submit' name='submit' value='IDL script'> <input type='submit' name='submit' value='Python script'> <input type='submit' name='submit' value='Save/Restore file'><p> and run the script. <br> <a href='%s?station=%s&instrument=%s'>View All Positions for %s</a>""" % (instpage, station, instrument, instrument)) self.endhtml() return self.get_buffer()