Skip to content

Commit

Permalink
adjusted on new specs
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Jan 8, 2024
1 parent fcd04e8 commit a3ce9e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
41 changes: 31 additions & 10 deletions comptages/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from datetime import datetime
from typing import Any

from django.db.models.expressions import F


from qgis.core import Qgis
from qgis.PyQt.uic import loadUiType
Expand Down Expand Up @@ -72,23 +74,42 @@ def connect_to_db():
return db


def partition_by_season(count: models.Count) -> dict[str, Any]:
"""Break down count details by season"""
def get_count_details_by_season(count: models.Count) -> dict[str, Any]:
"""Break down count details by season x section x class"""
seasons = {
"spring": [3, 4, 5],
"summer": [6, 7, 8],
"fall": [9, 10, 11],
"winter": [12, 1, 2],
}
accumulator = {k: {"_range": v, "times": 0} for k, v in seasons.items()}

def reducer(acc: dict, detail: models.CountDetail) -> dict:
d = detail.timestamp
for name, season in acc.items():
if d.month in season["_range"]:
acc[name]["times"] += detail.times
count_details = (
models.CountDetail.objects.filter(id_count=count.id)
.annotate(section=F("id_lane__id_section"), _class=F("id_count__id_class"))
.values("id", "section", "_class", "times", "timestamp")
)

def reducer(acc: dict, detail) -> dict:
timestamp = detail["timestamp"]

for season, _range in seasons.items():
if timestamp.month in _range:
section_id = detail["section"]
_class = detail["_class"]
times = detail["times"]

if section_id not in acc:
acc[section_id] = {}

if _class not in acc[section_id]:
acc[section_id][_class] = {}

if season not in acc[section_id][_class]:
acc[section_id][_class][season] = 0

acc[section_id][_class][season] += times
break

return acc

count_details = models.CountDetail.objects.filter(id_count=count.id)
return reduce(reducer, count_details, accumulator)
return reduce(reducer, count_details, {})
13 changes: 11 additions & 2 deletions comptages/test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,14 @@ def test_busiest_by_season(self):

print(f"Imported {len(files)} count files!")

partitioned_by_season = utils.partition_by_season(count)
self.assertTrue(any(v["times"] > 0 for v in partitioned_by_season.values()))
details = utils.get_count_details_by_season(count)

def inspect_leaves(d, res) -> list[int]:
for v in d.values():
if isinstance(v, int):
res.append(v)
elif isinstance(v, dict):
inspect_leaves(v, res)
return res

self.assertTrue(all(value > 0 for value in inspect_leaves(details, [])))

0 comments on commit a3ce9e7

Please sign in to comment.