Added support to get presence data from db

parent 2ad24bc2
from datetime import datetime, timedelta, time
from typing import Tuple, List
from sqlalchemy import asc
from bitsd.persistence.engine import session_scope
from bitsd.persistence.models import Status
from .graph import PresenceGraph
WINDOW_WIDTH = timedelta(weeks=6)
START_TIME = time(hour=8)
END_TIME = time(hour=21)
DELTA_SAMPLE = timedelta(minutes=30)
class Presence():
def _get_raw_data_from_db(self) -> List[List[List[Tuple[datetime, bool]]]]:
"""Return a list of day, each day is a list of week, each week is a list of tuples of timestamp and status"""
window_date = (
datetime.combine(datetime.now().date(), time()) - WINDOW_WIDTH,
datetime.combine(datetime.now().date(), time()))
data = [[], [], [], [], [], []]
with session_scope() as session:
for i in range(7):
current_day = window_date[1] - timedelta(days=(1 + i))
if (current_day.weekday() == 6): continue
while current_day > window_date[0]:
current_limit = (
datetime.combine(current_day, START_TIME),
datetime.combine(current_day, END_TIME),
)
day = []
for row in session.query(Status) \
.filter(Status.timestamp.between(*current_limit)).order_by(asc(Status.timestamp)).all():
day.append((row.timestamp, True if row.value == row.OPEN else False))
data[current_day.weekday()].append(day)
current_day = current_day - timedelta(weeks=1)
return data
def get_graph(self) -> PresenceGraph:
g = PresenceGraph([[0, 1], [1, .5], [.5, 0], [0, .5, 1, .2], [1, .2], [.2, .6, .1, 1, 0]])
g.generate()
return g
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment