Added cache for the graph

parent 06a56470
......@@ -14,3 +14,6 @@ WINDOW_WIDTH = timedelta(weeks=8)
# Weights of the data of the weeks from the closer to the farther
# They must be at least as many as the number of the weeks in the window
DATA_WEIGHTS = [1000, 300, 200, 150, 100, 70, 30, 20]
# The valid in time for the cache of the graph
GRAPH_CACHE_DURATION = timedelta(days=1)
\ No newline at end of file
......@@ -7,10 +7,13 @@ from sqlalchemy import asc
from bitsd.persistence.engine import session_scope
from bitsd.persistence.models import Status
from .graph import PresenceGraph
from .parameters import WINDOW_WIDTH, START_TIME, END_TIME, DELTA_SAMPLE, DATA_WEIGHTS
from .parameters import WINDOW_WIDTH, START_TIME, END_TIME, DELTA_SAMPLE, DATA_WEIGHTS, GRAPH_CACHE_DURATION
class Presence():
def __init__(self):
self._cache_expiration = datetime(year=1970, month=1, day=1)
self._cache = None
def _get_raw_data_from_db(self) -> List[List[List[Tuple[time, bool]]]]:
"""Return a list of day, each day is a list of week, each week is a list of tuples of timestamp and status"""
......@@ -111,6 +114,10 @@ class Presence():
return status
def get_graph(self) -> PresenceGraph:
g = PresenceGraph(*self.get_prevision(True))
g.generate()
return g
if self._cache_expiration < datetime.now():
self._cache = PresenceGraph(*self.get_prevision(True))
self._cache.generate()
self._cache_expiration = datetime.now() + GRAPH_CACHE_DURATION
return self._cache
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