Skip to content

Commit 454677e

Browse files
committed
Add ensure_timestamp parameter to get_data function for improved timestamp handling
1 parent 4bb1055 commit 454677e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

dashboard/data.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from anomstack.sql.read import read_sql
88

99

10-
def get_data(spec: dict, max_n: int = 30) -> pd.DataFrame:
10+
def get_data(spec: dict, max_n: int = 30, ensure_timestamp: bool = False) -> pd.DataFrame:
1111
"""
1212
Get data from the database for a given spec and max_n.
1313
@@ -25,5 +25,9 @@ def get_data(spec: dict, max_n: int = 30) -> pd.DataFrame:
2525
)
2626
db = spec["db"]
2727
df = read_sql(sql, db=db)
28+
29+
if ensure_timestamp:
30+
df["metric_timestamp"] = pd.to_datetime(df["metric_timestamp"], errors="coerce")
31+
df = df.sort_values("metric_timestamp")
2832

2933
return df

dashboard/routes.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def index(request: Request):
6565
try:
6666

6767
df = get_data(
68-
app.state.specs_enabled[batch_name], max_n=DEFAULT_ALERT_MAX_N
68+
app.state.specs_enabled[batch_name],
69+
max_n=DEFAULT_ALERT_MAX_N,
70+
ensure_timestamp=True
6971
)
7072

7173
except Exception as e:
@@ -179,7 +181,9 @@ def get_batch_view(batch_name: str, session, initial_load: int = DEFAULT_LOAD_N_
179181
# First, ensure we have the data and stats calculated
180182
if batch_name not in app.state.df_cache:
181183
app.state.df_cache[batch_name] = get_data(
182-
app.state.specs_enabled[batch_name], max_n=DEFAULT_ALERT_MAX_N
184+
app.state.specs_enabled[batch_name],
185+
max_n=DEFAULT_ALERT_MAX_N,
186+
ensure_timestamp=True
183187
)
184188
app.state.calculate_metric_stats(batch_name)
185189
elif batch_name not in app.state.stats_cache:
@@ -376,7 +380,9 @@ def post(batch_name: str, alert_max_n: int = DEFAULT_ALERT_MAX_N, session=None):
376380
app.state.clear_batch_cache(batch_name)
377381

378382
app.state.df_cache[batch_name] = get_data(
379-
app.state.specs_enabled[batch_name], max_n=alert_max_n
383+
app.state.specs_enabled[batch_name],
384+
max_n=alert_max_n,
385+
ensure_timestamp=True
380386
)
381387
app.state.calculate_metric_stats(batch_name)
382388

0 commit comments

Comments
 (0)