From bbe52332cdc8adef28f5eba177bd8599e6168f50 Mon Sep 17 00:00:00 2001 From: George Burton <8233643+gecBurton@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:22:42 +0100 Subject: [PATCH] feature/graph-time-scales (#1115) * added tomescales to graph * fixed display name * wip * bug fix --- .../redbox_core/dash_apps/report_app.py | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/django_app/redbox_app/redbox_core/dash_apps/report_app.py b/django_app/redbox_app/redbox_core/dash_apps/report_app.py index f1f004b9b..526fc326e 100644 --- a/django_app/redbox_app/redbox_core/dash_apps/report_app.py +++ b/django_app/redbox_app/redbox_core/dash_apps/report_app.py @@ -2,7 +2,7 @@ from dash import dcc, html from dash.dependencies import Input, Output from django.db.models import Count, Sum -from django.db.models.functions import TruncDate +from django.db.models.functions import TruncDay, TruncHour, TruncWeek from plotly.graph_objects import Figure # ----------------------------------# @@ -38,6 +38,16 @@ app.layout = html.Div( [ dcc.Graph(id="line-chart"), + html.P("Select time-scale"), + dcc.Dropdown( + id="time-scale", + options=[ + {"label": "weekly", "value": "week"}, + {"label": "daily", "value": "day"}, + {"label": "hourly", "value": "hour"}, + ], + value="day", + ), html.P("Select metric"), dcc.Dropdown( id="metric", @@ -53,7 +63,7 @@ id="breakdown", options=[ {"label": "route", "value": "route"}, - {"label": "model", "value": "chat__chat_backend"}, + {"label": "model", "value": "chat__chat_backend__name"}, {"label": "none", "value": None}, ], value="route", @@ -66,27 +76,36 @@ # ----------# -@app.callback(Output("line-chart", "figure"), [Input("metric", "value"), Input("breakdown", "value")]) -def update_graph(metric: str, breakdown: str | None, **kwargs) -> Figure: # noqa: ARG001 +@app.callback( + Output("line-chart", "figure"), + [ + Input("time-scale", "value"), + Input("metric", "value"), + Input("breakdown", "value"), + ], +) +def update_graph(scale: str, metric: str, breakdown: str | None, **kwargs) -> Figure: # noqa: ARG001 """A standard plotly callback. Note **kwargs must be used for compatibility across both Dash and DjangoDash. """ breakdown_args = [breakdown] if breakdown else [] + scale_func = {"week": TruncWeek, "day": TruncDay, "hour": TruncHour}[scale] + queryset = ( - models.ChatMessage.objects.annotate(day=TruncDate("created_at")) - .values("day", *breakdown_args) + models.ChatMessage.objects.annotate(time=scale_func("created_at")) + .values("time", *breakdown_args) .annotate( message_count=Count("id", distinct=True), unique_users=Count("chat__user", distinct=True), token_count=Sum("chatmessagetokenuse__token_count"), ) - .order_by("day") - .values("day", "message_count", "unique_users", "token_count", *breakdown_args) + .order_by("time") + .values("time", "message_count", "unique_users", "token_count", *breakdown_args) ) breakdown_colours = {"color": breakdown} if breakdown else {} - return px.bar(queryset, x="day", y=metric, title="use per day", **breakdown_colours) + return px.bar(queryset, x="time", y=metric, title="use per day", **breakdown_colours) if __name__ == "__main__":