Spaces:
Sleeping
Sleeping
Update supabase_utils.py
Browse files- supabase_utils.py +50 -0
supabase_utils.py
CHANGED
|
@@ -98,6 +98,56 @@ def get_harmonic_predictions(station_id, start_time, end_time):
|
|
| 98 |
traceback.print_exc()
|
| 99 |
return []
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def save_predictions_to_supabase(station_id, prediction_results):
|
| 102 |
"""예측 결과를 Supabase에 저장"""
|
| 103 |
supabase = get_supabase_client()
|
|
|
|
| 98 |
traceback.print_exc()
|
| 99 |
return []
|
| 100 |
|
| 101 |
+
def get_tide_predictions(station_id, start_time, end_time):
|
| 102 |
+
"""사용자가 입력한 KST 시간을 UTC로 변환하여 조석 예측 데이터 조회"""
|
| 103 |
+
supabase = get_supabase_client()
|
| 104 |
+
if not supabase:
|
| 105 |
+
print("Supabase 클라이언트를 생성할 수 없습니다.")
|
| 106 |
+
return []
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
kst = pytz.timezone('Asia/Seoul')
|
| 110 |
+
|
| 111 |
+
# 안전한 datetime 변환
|
| 112 |
+
if isinstance(start_time, str):
|
| 113 |
+
start_time = pd.to_datetime(start_time)
|
| 114 |
+
if isinstance(end_time, str):
|
| 115 |
+
end_time = pd.to_datetime(end_time)
|
| 116 |
+
|
| 117 |
+
# pandas Timestamp를 datetime으로 변환
|
| 118 |
+
if hasattr(start_time, 'to_pydatetime'):
|
| 119 |
+
start_time = start_time.to_pydatetime()
|
| 120 |
+
if hasattr(end_time, 'to_pydatetime'):
|
| 121 |
+
end_time = end_time.to_pydatetime()
|
| 122 |
+
|
| 123 |
+
# KST 시간대가 없으면 추가
|
| 124 |
+
if start_time.tzinfo is None:
|
| 125 |
+
start_time = kst.localize(start_time)
|
| 126 |
+
if end_time.tzinfo is None:
|
| 127 |
+
end_time = kst.localize(end_time)
|
| 128 |
+
|
| 129 |
+
# UTC로 변환
|
| 130 |
+
start_utc = start_time.astimezone(pytz.UTC)
|
| 131 |
+
end_utc = end_time.astimezone(pytz.UTC)
|
| 132 |
+
|
| 133 |
+
start_str = start_utc.strftime('%Y-%m-%dT%H:%M:%S+00:00')
|
| 134 |
+
end_str = end_utc.strftime('%Y-%m-%dT%H:%M:%S+00:00')
|
| 135 |
+
|
| 136 |
+
result = supabase.table('tide_predictions')\
|
| 137 |
+
.select('*')\
|
| 138 |
+
.eq('station_id', station_id)\
|
| 139 |
+
.gte('predicted_at', start_str)\
|
| 140 |
+
.lte('predicted_at', end_str)\
|
| 141 |
+
.order('predicted_at')\
|
| 142 |
+
.limit(1000)\
|
| 143 |
+
.execute()
|
| 144 |
+
|
| 145 |
+
return result.data if result.data else []
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"조석 예측값 조회 오류: {e}")
|
| 148 |
+
traceback.print_exc()
|
| 149 |
+
return []
|
| 150 |
+
|
| 151 |
def save_predictions_to_supabase(station_id, prediction_results):
|
| 152 |
"""예측 결과를 Supabase에 저장"""
|
| 153 |
supabase = get_supabase_client()
|