Skip to content

Commit cb6681d

Browse files
committed
Refactor llmalert job to simplify metric_alert assignment and enhance anomaly explanation formatting
1 parent 2dcd486 commit cb6681d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

anomstack/jobs/llmalert.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ def llmalert(context, df: pd.DataFrame) -> pd.DataFrame:
157157
)
158158
df_metric["metric_timestamp"] = pd.to_datetime(df_metric["metric_timestamp"], format="%Y-%m-%d %H:%M:%S")
159159

160-
# if there are detected anomalies set metric_alert to 1 if it is not already 1
161-
if not df_metric["metric_alert"].any():
162-
df_metric["metric_alert"] = df_metric["anomaly_timestamp"].notnull().astype(int)
160+
# if there are detected anomalies set metric_alert to 1
161+
df_metric["metric_alert"] = df_metric["anomaly_timestamp"].notnull().astype(int)
163162

164163
num_anomalies_recent = df_metric["metric_alert"].tail(llmalert_recent_n).sum()
165164

@@ -175,9 +174,10 @@ def llmalert(context, df: pd.DataFrame) -> pd.DataFrame:
175174
anomaly_explanations = df_metric[
176175
df_metric["anomaly_timestamp"].notnull()
177176
][["anomaly_timestamp", "anomaly_explanation"]].apply(
178-
lambda x: f"{x[0]}: {x[1]}", axis=1
179-
).tail(llmalert_recent_n).sort_values(ascending=False)
180-
anomaly_explanations = "\n- ".join(anomaly_explanations)
177+
lambda x: f"- {x[0]}: {x[1]}", axis=1
178+
).sort_values(ascending=False)
179+
anomaly_explanations = anomaly_explanations.head(llmalert_recent_n)
180+
anomaly_explanations = "\n".join(anomaly_explanations)
181181
metric_timestamp_max = df_metric["metric_timestamp"].max()
182182
alert_title = (
183183
f"🤖 LLM says [{metric_name}] looks anomalous "

0 commit comments

Comments
 (0)