Spaces:
Runtime error
Runtime error
Refactor run_troubleshooting function to allow user-defined time range for troubleshooting: Update time filtering logic, improve empty check response, and enhance Gradio interface with adjustable input for hours.
Browse files
app.py
CHANGED
|
@@ -145,33 +145,27 @@ def check_thresholds(sensor_df_filtered, threshold_df):
|
|
| 145 |
|
| 146 |
|
| 147 |
# ② run_troubleshooting 内の空チェックとタイムゾーン担保
|
| 148 |
-
|
|
|
|
| 149 |
try:
|
| 150 |
current_time_utc = datetime.datetime.now(datetime.timezone.utc)
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
|
| 153 |
global sensor_df, threshold_df, troubleshooting_df
|
| 154 |
|
|
|
|
| 155 |
recent_sensor_df = sensor_df[
|
| 156 |
-
(sensor_df['datetime'] >=
|
| 157 |
(sensor_df['datetime'] <= current_time_utc)
|
| 158 |
].copy()
|
| 159 |
|
|
|
|
| 160 |
alerts_df = check_thresholds(recent_sensor_df, threshold_df)
|
| 161 |
|
| 162 |
-
# まず空チェック(ここで 'timestamp' KeyError を根絶)
|
| 163 |
if alerts_df.empty:
|
| 164 |
-
return "過去
|
| 165 |
-
|
| 166 |
-
# 'timestamp' 列が存在するか念のため防御(列固定しているので基本 True)
|
| 167 |
-
if 'timestamp' not in alerts_df.columns:
|
| 168 |
-
return "過去24時間 異常ありません(アラート0件/timestamp列なし)"
|
| 169 |
-
|
| 170 |
-
# 型とTZを担保:tz-naive → UTC を付与
|
| 171 |
-
if not pd.api.types.is_datetime64_any_dtype(alerts_df['timestamp']):
|
| 172 |
-
alerts_df['timestamp'] = pd.to_datetime(alerts_df['timestamp'], errors='coerce', utc=True)
|
| 173 |
-
elif alerts_df['timestamp'].dt.tz is None:
|
| 174 |
-
alerts_df['timestamp'] = alerts_df['timestamp'].dt.tz_localize('UTC')
|
| 175 |
|
| 176 |
grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
|
| 177 |
multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
|
|
@@ -237,10 +231,10 @@ def run_troubleshooting():
|
|
| 237 |
# Gradioインターフェースの設定
|
| 238 |
iface = gr.Interface(
|
| 239 |
fn=run_troubleshooting,
|
| 240 |
-
inputs=
|
| 241 |
outputs="text",
|
| 242 |
title="Troubleshooting Information",
|
| 243 |
-
description="
|
| 244 |
)
|
| 245 |
|
| 246 |
# Gradioインターフェースの起動
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
# ② run_troubleshooting 内の空チェックとタイムゾーン担保
|
| 148 |
+
# トラブルシューティング実行関数
|
| 149 |
+
def run_troubleshooting(hours: int = 24): # ← デフォルト24時間
|
| 150 |
try:
|
| 151 |
current_time_utc = datetime.datetime.now(datetime.timezone.utc)
|
| 152 |
+
|
| 153 |
+
# ユーザー指定の時間分さかのぼる
|
| 154 |
+
time_start_utc = current_time_utc - datetime.timedelta(hours=hours)
|
| 155 |
|
| 156 |
global sensor_df, threshold_df, troubleshooting_df
|
| 157 |
|
| 158 |
+
# 指定範囲のセンサーデータを抽出
|
| 159 |
recent_sensor_df = sensor_df[
|
| 160 |
+
(sensor_df['datetime'] >= time_start_utc) &
|
| 161 |
(sensor_df['datetime'] <= current_time_utc)
|
| 162 |
].copy()
|
| 163 |
|
| 164 |
+
# 閾値チェック実行
|
| 165 |
alerts_df = check_thresholds(recent_sensor_df, threshold_df)
|
| 166 |
|
|
|
|
| 167 |
if alerts_df.empty:
|
| 168 |
+
return f"過去{hours}時間 異常ありません"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
|
| 171 |
multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
|
|
|
|
| 231 |
# Gradioインターフェースの設定
|
| 232 |
iface = gr.Interface(
|
| 233 |
fn=run_troubleshooting,
|
| 234 |
+
inputs=gr.Number(value=24, label="過去◯時間"), # ← デフォルトは24
|
| 235 |
outputs="text",
|
| 236 |
title="Troubleshooting Information",
|
| 237 |
+
description="指定した時間数のセンサーデータから閾値チェックを行います"
|
| 238 |
)
|
| 239 |
|
| 240 |
# Gradioインターフェースの起動
|