Merge branch 'master' of https://gitlab.com/ademant/pysql
commit
26f486f632
33
dump_bin.py
33
dump_bin.py
|
@ -25,9 +25,10 @@ except:
|
|||
print("no connection to database")
|
||||
else:
|
||||
mycursor=mydb.cursor()
|
||||
mycursor.execute("select distinct id from ids;")
|
||||
mycursor.execute('select id, max(time) as time,count(time) as count from measures group by id')
|
||||
# mycursor.execute("select distinct id from ids;")
|
||||
ti=mycursor.fetchall()
|
||||
for ids in ti:
|
||||
for ids,lt in ti:
|
||||
myids=str(ids[0])
|
||||
data_dir="data/"+myids
|
||||
file_timestamp=data_dir+"/timestamp"
|
||||
|
@ -37,24 +38,30 @@ else:
|
|||
except:
|
||||
print("could not create data storage")
|
||||
else:
|
||||
brun=False
|
||||
try:
|
||||
stored_timestamps=int(os.path.getsize(file_timestamp)/8)
|
||||
except:
|
||||
select_string="select measures.time,measures.value from measures where id='"+myids+"' order by measures.time"
|
||||
brun=True
|
||||
else:
|
||||
f=open(file_timestamp,"rb")
|
||||
f.seek((stored_timestamps-1)*8)
|
||||
lasttime=int.from_bytes(f.read(8),byteorder="big")
|
||||
print(myids+" last timestamp: "+str(lasttime))
|
||||
select_string="select measures.time,measures.value from measures where id='"+myids+"' and measures.time > "+str(lasttime)+" order by measures.time"
|
||||
no_rows=mycursor.execute(select_string)
|
||||
if(no_rows>0):
|
||||
tm=mycursor.fetchall()
|
||||
ts=open("data/"+myids+"/timestamp","ab")
|
||||
tv=open("data/"+myids+"/measures","ab")
|
||||
for tims,meas in tm:
|
||||
ts.write(int(tims).to_bytes(8,byteorder="big",signed=False))
|
||||
tv.write(int(meas).to_bytes(3,byteorder="big",signed=False))
|
||||
ts.close()
|
||||
tv.close()
|
||||
if lasttime > lt:
|
||||
print("new values since "+str(lt))
|
||||
select_string="select measures.time,measures.value from measures where id='"+myids+"' and measures.time > "+str(lasttime)+" order by measures.time"
|
||||
brun=True
|
||||
if brun:
|
||||
no_rows=mycursor.execute(select_string)
|
||||
if(no_rows>0):
|
||||
tm=mycursor.fetchall()
|
||||
ts=open("data/"+myids+"/timestamp","ab")
|
||||
tv=open("data/"+myids+"/measures","ab")
|
||||
for tims,meas in tm:
|
||||
ts.write(int(tims).to_bytes(8,byteorder="big",signed=False))
|
||||
tv.write(int(meas).to_bytes(4,byteorder="big",signed=False))
|
||||
ts.close()
|
||||
tv.close()
|
||||
|
||||
|
|
Loading…
Reference in New Issue