Skip to content

Commit 5e515a9

Browse files
authored
fix(charts): properly format week of year (#314)
1 parent e2f4c02 commit 5e515a9

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/agent_toolkit/forestadmin/agent_toolkit/resources/collections/stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class StatsResource(BaseCollectionResource, ContextVariableInjectorResourceMixin):
2929
FREQUENCIES = {"Day": "d", "Week": "W-MON", "Month": "BMS", "Year": "BYS"}
3030

31-
FORMAT = {"Day": "%d/%m/%Y", "Week": "W%V-%Y", "Month": "%b %Y", "Year": "%Y"}
31+
FORMAT = {"Day": "%d/%m/%Y", "Week": "W%V-%G", "Month": "%b %Y", "Year": "%Y"}
3232

3333
def stats_method(self, type: str):
3434
return {

src/agent_toolkit/tests/resources/collections/test_stats_resources.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,33 @@ def test_line_should_return_chart_with_week_filter(self):
636636
{"label": "W02-2022", "values": {"value": 15}},
637637
)
638638

639+
def test_line_week_label_should_be_with_iso_year(self):
640+
request = self.mk_request("Week")
641+
with patch.object(
642+
self.book_collection,
643+
"aggregate",
644+
new_callable=AsyncMock,
645+
return_value=[
646+
{"value": 10, "group": {"date": "2024-12-23 00:00:00"}},
647+
{"value": 15, "group": {"date": "2024-12-30 00:00:00"}},
648+
{"value": 20, "group": {"date": "2025-01-06 00:00:00"}},
649+
],
650+
):
651+
response = self.loop.run_until_complete(self.stat_resource.line(request))
652+
653+
content_body = json.loads(response.body)
654+
self.assertEqual(response.status, 200)
655+
self.assertEqual(content_body["data"]["type"], "stats")
656+
self.assertEqual(len(content_body["data"]["attributes"]["value"]), 3)
657+
self.assertEqual(
658+
content_body["data"]["attributes"]["value"],
659+
[
660+
{"label": "W52-2024", "values": {"value": 10}},
661+
{"label": "W01-2025", "values": {"value": 15}},
662+
{"label": "W02-2025", "values": {"value": 20}},
663+
],
664+
)
665+
639666
def test_line_should_return_chart_with_month_filter(self):
640667
request = self.mk_request("Month")
641668
with patch.object(

src/datasource_toolkit/forestadmin/datasource_toolkit/decorators/chart/result_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ResultBuilder:
5555

5656
FORMATS: Dict[DateOperation, str] = {
5757
DateOperation.DAY: "%d/%m/%Y",
58-
DateOperation.WEEK: "W%V-%Y",
58+
DateOperation.WEEK: "W%V-%G",
5959
DateOperation.MONTH: "%b %Y",
6060
DateOperation.YEAR: "%Y",
6161
}

src/datasource_toolkit/tests/decorators/chart/test_chart_result_builder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ def test_time_based_should_return_correct_format_week(self):
109109
{"label": "W02-1986", "values": {"value": 7}},
110110
]
111111

112+
def test_time_based_should_return_correct_format_week_iso_year(self):
113+
result = ResultBuilder.time_based(
114+
DateOperation.WEEK,
115+
{
116+
"2024-12-23": 1,
117+
"2024-12-30": 0,
118+
"2025-01-06": 7,
119+
},
120+
)
121+
self.assertEqual(
122+
result,
123+
[
124+
{"label": "W52-2024", "values": {"value": 1}},
125+
{"label": "W01-2025", "values": {"value": 0}},
126+
{"label": "W02-2025", "values": {"value": 7}},
127+
],
128+
)
129+
112130
def test_time_based_should_return_correct_format_month(self):
113131
result = ResultBuilder.time_based(
114132
DateOperation.MONTH,

0 commit comments

Comments
 (0)