localwaka/utils.py

35 lines
949 B
Python

from datetime import datetime
from itertools import islice
import utils
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def get_seconds_coded(conn, start_time, end_time, timeout):
start_stamp = datetime.timestamp(start_time)
end_stamp = datetime.timestamp(end_time)
# get all the entries
c = conn.cursor()
curs = c.execute("""
select time from heartbeats where time > ? and time < ? order by time;
""", [start_stamp, end_stamp])
coded_secs = 0
for (first, second) in utils.window(curs, 2):
diff = second[0] - first[0]
if diff < timeout:
coded_secs += diff
return coded_secs