2020-12-21 20:06:03 +00:00
|
|
|
from datetime import datetime
|
2020-07-15 19:23:40 +00:00
|
|
|
from itertools import islice
|
2020-12-21 20:06:03 +00:00
|
|
|
import utils
|
|
|
|
|
2020-07-15 19:23:40 +00:00
|
|
|
|
|
|
|
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
|
2020-12-21 20:06:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|