def callback(ch, method, properties, body): print('= = = = =') print(body) dt = datetime.utcnow() tsraw.write('{}\t{:6f}\t{}\t{}\n'.format(dt.isoformat(), dt2ts(dt), method.routing_key, body.strip())) tsraw.flush() ch.basic_ack(delivery_tag=method.delivery_tag)
def read_time_range(table,col_list,begin,end=None): if end is None: end = dt2ts(datetime.utcnow()) time_range = 'WHERE {time_col} BETWEEN "{begin}" AND "{end}"'.\ format(time_col=time_col,begin=begin,end=end) cmd = 'SELECT {col_list} FROM {table} {time_range} ORDER BY {time_col} DESC'.\ format(col_list=','.join(col_list), table=table, time_col=time_col, time_range=time_range) tmp = engine.execute(cmd) tmp = zip(*tmp) if (len(tmp)): return {v:tmp[k] for k,v in enumerate(col_list)} return {v:[] for k,v in enumerate(col_list)}
def taskDisp(): try: os.system('cls' if os.name == 'nt' else 'clear') #os.system('du -sh /root/logging/data /root/logging/log') #os.system('date && uptime') #print('Data from {}'.format(config.kmet1_ip)) for tag in sorted(D.keys()): ago = dt2ts(datetime.utcnow()) - D[tag]['ts'] s = '{}, {:.1f}s ago'.format(tag, ago) period = float('nan') if len(Dt[tag]) > 1: period = sum([ p[0] - p[1] for p in zip(Dt[tag][1:], Dt[tag][0:-1]) ]) / (len(Dt[tag]) - 1) s = s + ', {:.1f}s per sample'.format(period) # ago should be <= period. if ago > 2 * period: s = s + ' (dead?)' print('') print(s) for col in sorted(D[tag].keys()): print('\t{}={}'.format(col, D[tag][col])) except: traceback.print_exc()
engine = create_engine('sqlite:///'+dbfile,echo=False) #meta = MetaData() #meta.bind = engine time_col = 'ts' def read_time_range(table,col_list,begin,end=None): if end is None: end = dt2ts(datetime.utcnow()) time_range = 'WHERE {time_col} BETWEEN "{begin}" AND "{end}"'.\ format(time_col=time_col,begin=begin,end=end) cmd = 'SELECT {col_list} FROM {table} {time_range} ORDER BY {time_col} DESC'.\ format(col_list=','.join(col_list), table=table, time_col=time_col, time_range=time_range) tmp = engine.execute(cmd) tmp = zip(*tmp) if (len(tmp)): return {v:tmp[k] for k,v in enumerate(col_list)} return {v:[] for k,v in enumerate(col_list)} if '__main__' == __name__: table = 'PIR' begin = dt2ts(datetime.utcnow() - timedelta(minutes=5)) end = None print(read_time_range(table,['ts','ir_mV','t_dome_V'],begin,end))
def taskLiveliness(): # remove old entries global D for k in D.keys(): if dt2ts(datetime.utcnow()) - D[k]['ts'] > 60 * 60: del D[k]