You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/google/adk/tools/bigquery/query_tool.py
+10-5Lines changed: 10 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1136,7 +1136,7 @@ def detect_anomalies(
1136
1136
history_data: str,
1137
1137
times_series_timestamp_col: str,
1138
1138
times_series_data_col: str,
1139
-
horizon: Optional[int] =10,
1139
+
horizon: Optional[int] =1000,
1140
1140
target_data: Optional[str] =None,
1141
1141
times_series_id_cols: Optional[list[str]] =None,
1142
1142
anomaly_prob_threshold: Optional[float] =0.95,
@@ -1158,7 +1158,7 @@ def detect_anomalies(
1158
1158
times_series_data_col (str): The name of the column containing the
1159
1159
numerical values to be forecasted and anomaly detected.
1160
1160
horizon (int, optional): The number of time steps to forecast into the
1161
-
future. Defaults to 10.
1161
+
future. Defaults to 1000.
1162
1162
target_data (str, optional): The table id of the BigQuery table containing
1163
1163
the target time series data or a query statement that select the target
1164
1164
data.
@@ -1301,9 +1301,14 @@ def detect_anomalies(
1301
1301
OPTIONS ({options_str})
1302
1302
AS {history_data_source}
1303
1303
"""
1304
+
order_by_id_cols= (
1305
+
", ".join(colforcolintimes_series_id_cols) +", "
1306
+
iftimes_series_id_cols
1307
+
else""
1308
+
)
1304
1309
1305
1310
anomaly_detection_query=f"""
1306
-
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold))
1311
+
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold)) ORDER BY {order_by_id_cols}{times_series_timestamp_col}
target_data_source=f"SELECT * FROM `{target_data}`"
1320
+
target_data_source=f"(SELECT * FROM `{target_data}`)"
1316
1321
1317
1322
anomaly_detection_query=f"""
1318
-
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold), {target_data_source})
1323
+
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold), {target_data_source}) ORDER BY {order_by_id_cols}{times_series_timestamp_col}
1319
1324
"""
1320
1325
1321
1326
# Create a session and run the create model query.
SELECT * FROM ML.DETECT_ANOMALIES(MODEL detect_anomalies_model_test_uuid, STRUCT(0.8 AS anomaly_prob_threshold), (SELECT * FROM `test-dataset.target-table`))
1558
+
SELECT * FROM ML.DETECT_ANOMALIES(MODEL detect_anomalies_model_test_uuid, STRUCT(0.8 AS anomaly_prob_threshold), (SELECT * FROM `test-dataset.target-table`)) ORDER BY dim1, dim2, ts_timestamp
1559
+
"""
1560
+
1561
+
assertmock_execute_sql.call_count==2
1562
+
mock_execute_sql.assert_any_call(
1563
+
project_id="test-project",
1564
+
query=expected_create_model_query,
1565
+
credentials=mock_credentials,
1566
+
settings=mock_settings,
1567
+
tool_context=mock_tool_context,
1568
+
caller_id="detect_anomalies",
1569
+
)
1570
+
mock_execute_sql.assert_any_call(
1571
+
project_id="test-project",
1572
+
query=expected_anomaly_detection_query,
1573
+
credentials=mock_credentials,
1574
+
settings=mock_settings,
1575
+
tool_context=mock_tool_context,
1576
+
caller_id="detect_anomalies",
1577
+
)
1578
+
1579
+
1580
+
# detect_anomalies calls execute_sql twice. We need to test that
1581
+
# the queries are properly constructed and call execute_sql with the correct
SELECT * FROM ML.DETECT_ANOMALIES(MODEL detect_anomalies_model_test_uuid, STRUCT(0.95 AS anomaly_prob_threshold), (SELECT * FROM `test-dataset.target-table`)) ORDER BY ts_timestamp
0 commit comments