MTeguri commited on
Commit
855f499
·
1 Parent(s): 1634454

Refactor datetime handling in app.py: Ensure UTC conversion for sensor timestamps and adjust filtering logic for recent sensor data.

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -27,8 +27,7 @@ sensor_df = pd.DataFrame(sensor_data.data)
27
  troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
28
 
29
  # Convert 'datetime' column to datetime objects (初回のみ実行)
30
- sensor_df['datetime'] = pd.to_datetime(sensor_df['datetime'])
31
-
32
 
33
  # 閾値チェック関数の定義
34
  def check_thresholds(sensor_df_filtered, threshold_df): # Renamed parameter to clarify it's the filtered data
@@ -96,13 +95,22 @@ import datetime # Import datetime here as it's used in run_troubleshooting
96
  def run_troubleshooting():
97
  try:
98
  # Get current time and calculate the time 24 hours ago
99
- current_time = datetime.datetime.now(datetime.timezone.utc)
100
- time_24_hours_ago = current_time - datetime.timedelta(hours=24)
 
 
 
 
 
101
 
102
  # Filter sensor data for the last 24 hours
103
  # Use the globally available sensor_df and filter it each time the function is called
104
  global sensor_df
105
- recent_sensor_df = sensor_df[sensor_df['datetime'] >= time_24_hours_ago].copy()
 
 
 
 
106
 
107
  # Ensure other dataframes are accessible (they are loaded globally once)
108
  global threshold_df
 
27
  troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
28
 
29
  # Convert 'datetime' column to datetime objects (初回のみ実行)
30
+ sensor_df['datetime'] = pd.to_datetime(sensor_df['datetime'], utc=True)
 
31
 
32
  # 閾値チェック関数の定義
33
  def check_thresholds(sensor_df_filtered, threshold_df): # Renamed parameter to clarify it's the filtered data
 
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_utc = datetime.datetime.now(datetime.timezone.utc)
102
+
103
+ # 24時間前のUTC
104
+ time_24_hours_ago_utc = current_time_utc - 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)
116
  global threshold_df