MTeguri commited on
Commit
be7e905
·
1 Parent(s): cd0375c

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
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -145,33 +145,27 @@ def check_thresholds(sensor_df_filtered, threshold_df):
145
 
146
 
147
  # ② run_troubleshooting 内の空チェックとタイムゾーン担保
148
- def run_troubleshooting():
 
149
  try:
150
  current_time_utc = datetime.datetime.now(datetime.timezone.utc)
151
- time_24_hours_ago_utc = current_time_utc - datetime.timedelta(hours=24)
 
 
152
 
153
  global sensor_df, threshold_df, troubleshooting_df
154
 
 
155
  recent_sensor_df = sensor_df[
156
- (sensor_df['datetime'] >= time_24_hours_ago_utc) &
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 "過去24時間 異常ありません(アラート0件)"
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=None, # No direct input needed as it uses existing dataframes
241
  outputs="text",
242
  title="Troubleshooting Information",
243
- description="Displays troubleshooting information based on sensor and threshold data."
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インターフェースの起動