MTeguri commited on
Commit
32c8275
·
1 Parent(s): 520eb26

Enhance timezone handling in run_troubleshooting function: Import pytz for JST conversion and update timestamp filtering to ensure UTC consistency.

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -88,6 +88,8 @@ import gradio as gr
88
  import pandas as pd
89
  import supabase
90
  import datetime # Import datetime here as it's used in run_troubleshooting
 
 
91
 
92
  # Assuming the data loading and check_thresholds function from the previous cell are available
93
 
@@ -95,21 +97,17 @@ import datetime # Import datetime here as it's used in run_troubleshooting
95
  def run_troubleshooting():
96
  try:
97
  # Get current time and calculate the time 24 hours ago
98
- #current_time = datetime.datetime.now(datetime.timezone.utc)
99
- #time_24_hours_ago = current_time - datetime.timedelta(hours=24)
100
- # 現在のUTC時刻
101
- current_time = datetime.datetime.now(datetime.timezone.utc)
102
 
103
  # 24時間前のUTC
104
- time_24_hours_ago = current_time - datetime.timedelta(hours=24)
105
 
106
  # Filter sensor data for the last 24 hours
107
  # Use the globally available sensor_df and filter it each time the function is called
108
  global sensor_df
109
- #recent_sensor_df = sensor_df[sensor_df['datetime'] < time_24_hours_ago].copy()
110
  recent_sensor_df = sensor_df[
111
- (sensor_df['datetime'] >= time_24_hours_ago) &
112
- (sensor_df['datetime'] <= current_time)
113
  ].copy()
114
 
115
  # Ensure other dataframes are accessible (they are loaded globally once)
@@ -140,6 +138,12 @@ def run_troubleshooting():
140
  # 結果データフレームの作成
141
  result_df = pd.DataFrame(result_list)
142
 
 
 
 
 
 
 
143
  # If no alerts, return "異常ありません"
144
  if result_df.empty:
145
  return "過去24時間 異常ありません"
 
88
  import pandas as pd
89
  import supabase
90
  import datetime # Import datetime here as it's used in run_troubleshooting
91
+ import pytz # Import pytz for timezone conversion
92
+
93
 
94
  # Assuming the data loading and check_thresholds function from the previous cell are available
95
 
 
97
  def run_troubleshooting():
98
  try:
99
  # Get current time and calculate the time 24 hours ago
100
+ current_time_utc = datetime.datetime.now(datetime.timezone.utc)
 
 
 
101
 
102
  # 24時間前のUTC
103
+ time_24_hours_ago_utc = current_time_utc - datetime.timedelta(hours=24)
104
 
105
  # Filter sensor data for the last 24 hours
106
  # Use the globally available sensor_df and filter it each time the function is called
107
  global sensor_df
 
108
  recent_sensor_df = sensor_df[
109
+ (sensor_df['datetime'] >= time_24_hours_ago_utc) &
110
+ (sensor_df['datetime'] <= current_time_utc)
111
  ].copy()
112
 
113
  # Ensure other dataframes are accessible (they are loaded globally once)
 
138
  # 結果データフレームの作成
139
  result_df = pd.DataFrame(result_list)
140
 
141
+ # Convert timestamp to JST 時間変換
142
+ if not result_df.empty and 'timestamp' in result_df.columns:
143
+ JST = pytz.timezone('Asia/Tokyo')
144
+ result_df['timestamp'] = result_df['timestamp'].dt.tz_convert(JST)
145
+
146
+
147
  # If no alerts, return "異常ありません"
148
  if result_df.empty:
149
  return "過去24時間 異常ありません"