Skip to content

Commit 88fb563

Browse files
committed
Refactor index route in dashboard to improve function naming, enhance documentation with detailed docstrings, and streamline data retrieval logic.
1 parent 6a6d28e commit 88fb563

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

dashboard/routes/index.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,30 @@
77
88
"""
99

10-
from fasthtml.common import *
11-
from monsterui.all import *
12-
from fasthtml.svg import *
10+
from fasthtml.common import Div, H2, P, A, Script, Title, Safe, Request
11+
from monsterui.all import UkIcon, ButtonT, TextPresets, Card, Button, DivLAligned
1312
import pandas as pd
1413
import logging
14+
1515
from dashboard.app import app, rt
1616
from dashboard.components import create_batch_card
1717
from dashboard.batch_stats import calculate_batch_stats
1818
from dashboard.data import get_data
1919
from dashboard.constants import DEFAULT_LAST_N
2020

2121

22-
log = logging.getLogger("anomstack")
22+
log = logging.getLogger("anomstack_dashboard")
23+
2324

25+
def get_batch_data(batch_name: str) -> pd.DataFrame:
26+
"""Get batch data, either from cache or by fetching.
2427
25-
def _get_batch_data(batch_name: str) -> pd.DataFrame:
26-
"""Get batch data, either from cache or by fetching."""
28+
Args:
29+
batch_name (str): The name of the batch to get data for.
30+
31+
Returns:
32+
pd.DataFrame: The batch data.
33+
"""
2734
if batch_name not in app.state.df_cache:
2835
try:
2936
df = get_data(app.state.specs_enabled[batch_name],
@@ -38,11 +45,15 @@ def _get_batch_data(batch_name: str) -> pd.DataFrame:
3845
return app.state.df_cache[batch_name]
3946

4047

41-
def _get_sorted_batch_stats() -> tuple:
42-
"""Calculate and sort batch statistics."""
48+
def get_sorted_batch_stats() -> tuple:
49+
"""Calculate and sort batch statistics.
50+
51+
Returns:
52+
tuple: A tuple containing the batch statistics and the sorted batch names.
53+
"""
4354
batch_stats = {}
4455
for batch_name in app.state.metric_batches:
45-
df = _get_batch_data(batch_name)
56+
df = get_batch_data(batch_name)
4657
batch_stats[batch_name] = calculate_batch_stats(df, batch_name)
4758

4859
filtered_batches = {
@@ -62,8 +73,16 @@ def _get_sorted_batch_stats() -> tuple:
6273
return batch_stats, sorted_batch_names
6374

6475

65-
def _create_main_content(batch_stats: dict, sorted_batch_names: list) -> Div:
66-
"""Create the main dashboard content."""
76+
def create_main_content(batch_stats: dict, sorted_batch_names: list) -> Div:
77+
"""Create the main dashboard content.
78+
79+
Args:
80+
batch_stats (dict): The batch statistics.
81+
sorted_batch_names (list): The sorted batch names.
82+
83+
Returns:
84+
Div: The main dashboard content.
85+
"""
6786
return Div(
6887
Card(
6988
DivLAligned(
@@ -119,8 +138,15 @@ def _create_main_content(batch_stats: dict, sorted_batch_names: list) -> Div:
119138

120139

121140
@rt("/refresh-all")
122-
def post(request: Request):
123-
"""Refresh all batch data."""
141+
def post(request: Request) -> list:
142+
"""Refresh all batch data.
143+
144+
Args:
145+
request (Request): The request object.
146+
147+
Returns:
148+
list: The index route.
149+
"""
124150
try:
125151
app.state.df_cache.clear()
126152
app.state.stats_cache.clear()
@@ -142,8 +168,8 @@ def index(request: Request):
142168
}}
143169
""")
144170

145-
batch_stats, sorted_batch_names = _get_sorted_batch_stats()
146-
main_content = _create_main_content(batch_stats, sorted_batch_names)
171+
batch_stats, sorted_batch_names = get_sorted_batch_stats()
172+
main_content = create_main_content(batch_stats, sorted_batch_names)
147173

148174
if is_htmx:
149175
return main_content

0 commit comments

Comments
 (0)