Skip to content

Commit

Permalink
feature/graph-time-scales (#1115)
Browse files Browse the repository at this point in the history
* added tomescales to graph

* fixed display name

* wip

* bug fix
  • Loading branch information
gecBurton authored Oct 22, 2024
1 parent 348465e commit bbe5233
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions django_app/redbox_app/redbox_core/dash_apps/report_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# ----------------------------------#
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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__":
Expand Down

0 comments on commit bbe5233

Please sign in to comment.