Commit 6b7cd5bd authored by JackV's avatar JackV

Merge branch 'presence-graph' into 'master'

Improved graph calculus

See merge request b4/bits-server!4
parents cb0532e2 3f7a27ed
from datetime import time from datetime import time
from typing import List from typing import List
from colour import Color
from svgwrite import Drawing from svgwrite import Drawing
from svgwrite.container import Group, Style from svgwrite.container import Group, Style
from svgwrite.gradients import LinearGradient from svgwrite.gradients import LinearGradient
from svgwrite.shapes import Line, Rect from svgwrite.shapes import Line, Rect
from svgwrite.text import Text from svgwrite.text import Text
from bitsd.server.presence_graph.parameters import START_TIME, END_TIME, COLOR_CLOSE, COLOR_OPEN from bitsd.server.presence_graph.parameters import GRAPH_START_DAY, GRAPH_END_DAY, COLOR_CLOSE, COLOR_OPEN
HEIGHT = 1000 HEIGHT = 1000
WIDTH = 1000 WIDTH = 1000
...@@ -122,7 +121,7 @@ class PresenceGraph(Drawing): ...@@ -122,7 +121,7 @@ class PresenceGraph(Drawing):
font_weight="bold" font_weight="bold"
)) ))
number_hours = END_TIME.hour - START_TIME.hour number_hours = GRAPH_END_DAY.hour - GRAPH_START_DAY.hour
# Calculate the vertical position for the labels # Calculate the vertical position for the labels
for i, y in enumerate([j * (HEIGHT - HEADER_SIZE) / number_hours for j in range(1, number_hours)]): for i, y in enumerate([j * (HEIGHT - HEADER_SIZE) / number_hours for j in range(1, number_hours)]):
# Create the lines for the hours # Create the lines for the hours
...@@ -132,7 +131,7 @@ class PresenceGraph(Drawing): ...@@ -132,7 +131,7 @@ class PresenceGraph(Drawing):
(WIDTH, HEADER_SIZE + y))) (WIDTH, HEADER_SIZE + y)))
# Create the labels of the hours # Create the labels of the hours
g.add(Text(time(hour=(i + START_TIME.hour + 1)).strftime("%H:%M"), g.add(Text(time(hour=(i + GRAPH_START_DAY.hour + 1)).strftime("%H:%M"),
x=[WIDTH / 2], y=[HEADER_SIZE + y], dy=[9])) x=[WIDTH / 2], y=[HEADER_SIZE + y], dy=[9]))
def _add_header(self) -> None: def _add_header(self) -> None:
......
from datetime import timedelta, time from datetime import timedelta, time
# Time of the start of the day
from colour import Color from colour import Color
START_TIME = time(hour=8) # Time of the start of the day
START_DAY = time(hour=6)
# Time of the end of the day # Time of the end of the day
END_TIME = time(hour=21) END_DAY = time(hour=21)
# Time of the start of the day in the graph
GRAPH_START_DAY = time(hour=8)
# Time of the end of the day in the graph
GRAPH_END_DAY = time(hour=21)
# Delta time between two sample # Delta time between two sample
DELTA_SAMPLE = timedelta(minutes=15) DELTA_SAMPLE = timedelta(minutes=15)
...@@ -22,4 +26,4 @@ GRAPH_CACHE_DURATION = timedelta(hours=6) ...@@ -22,4 +26,4 @@ GRAPH_CACHE_DURATION = timedelta(hours=6)
# The color for the graph # The color for the graph
COLOR_OPEN = Color("#387002") COLOR_OPEN = Color("#387002")
COLOR_CLOSE = Color("#d32f2f") COLOR_CLOSE = Color("#d32f2f")
\ No newline at end of file
...@@ -7,10 +7,11 @@ from sqlalchemy import asc ...@@ -7,10 +7,11 @@ from sqlalchemy import asc
from bitsd.persistence.engine import session_scope from bitsd.persistence.engine import session_scope
from bitsd.persistence.models import Status from bitsd.persistence.models import Status
from .graph import PresenceGraph from .graph import PresenceGraph
from .parameters import WINDOW_WIDTH, START_TIME, END_TIME, DELTA_SAMPLE, DATA_WEIGHTS, GRAPH_CACHE_DURATION from .parameters import WINDOW_WIDTH, START_DAY, END_DAY, DELTA_SAMPLE, DATA_WEIGHTS, GRAPH_CACHE_DURATION, \
GRAPH_END_DAY, GRAPH_START_DAY
class Presence(): class Presence:
def __init__(self): def __init__(self):
# On start the cache is not valid # On start the cache is not valid
self._cache_expiration = datetime(year=1970, month=1, day=1) self._cache_expiration = datetime(year=1970, month=1, day=1)
...@@ -35,14 +36,15 @@ class Presence(): ...@@ -35,14 +36,15 @@ class Presence():
current_day = window_date[1] - timedelta(days=(1 + i)) current_day = window_date[1] - timedelta(days=(1 + i))
# Skip saturday and sunday (the data are useless) # Skip saturday and sunday (the data are useless)
if (current_day.weekday() == 5 or current_day.weekday() == 6): continue if current_day.weekday() == 5 or current_day.weekday() == 6:
continue
# For each day of the week in the window # For each day of the week in the window
while current_day > window_date[0]: while current_day > window_date[0]:
# Calculate the limit of the day # Calculate the limit of the day
current_limit = ( current_limit = (
datetime.combine(current_day, START_TIME), datetime.combine(current_day, START_DAY),
datetime.combine(current_day, END_TIME), datetime.combine(current_day, END_DAY),
) )
day = [] day = []
...@@ -75,10 +77,10 @@ class Presence(): ...@@ -75,10 +77,10 @@ class Presence():
day_data = raw_data[i] day_data = raw_data[i]
# Set the current time to the start of the day # Set the current time to the start of the day
current_time = datetime.combine(date.today(), START_TIME) current_time = datetime.combine(date.today(), GRAPH_START_DAY)
# Until the current time is earlier than the end of the day # Until the current time is earlier than the end of the day
while current_time.time() <= END_TIME: while current_time.time() <= GRAPH_END_DAY:
# Create a list of weeks' values for a single sample in a single day # Create a list of weeks' values for a single sample in a single day
status_hour = [] status_hour = []
......
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