solorpower_crawler/scripts_archive/update_current_kw.py
2026-03-30 13:01:18 +09:00

96 lines
2.9 KiB
Python

"""
2월 데이터의 current_kw 업데이트
과거 데이터의 경우 current_kw = today_kwh (시간별 발전량)로 설정
"""
import sys
import os
from datetime import datetime, timedelta
from dotenv import load_dotenv
load_dotenv()
if sys.platform.startswith('win'):
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
from database import get_supabase_client
def update_current_kw(plant_id, plant_name):
"""2월 데이터의 current_kw를 today_kwh로 업데이트"""
print(f"\n{'='*70}")
print(f"🔧 [{plant_name}] current_kw 업데이트 중...")
print(f"{'='*70}")
client = get_supabase_client()
if not client:
return
# 2월 1일부터 27일까지
start = datetime(2026, 2, 1)
end = datetime(2026, 2, 27)
total_updated = 0
current = start
while current <= end:
date_str = current.strftime("%Y-%m-%d")
# 해당 날짜의 모든 시간별 데이터 가져오기
result = client.table("solar_logs") \
.select("id, current_kw, today_kwh") \
.eq("plant_id", plant_id) \
.gte("created_at", f"{date_str}T00:00:00") \
.lt("created_at", f"{(current + timedelta(days=1)).strftime('%Y-%m-%d')}T00:00:00") \
.execute()
if not result.data:
current += timedelta(days=1)
continue
# current_kw가 0이고 today_kwh가 0이 아닌 레코드만 업데이트
updated_count = 0
for record in result.data:
if record['current_kw'] == 0 and record['today_kwh'] != 0:
try:
# current_kw를 today_kwh로 업데이트
client.table("solar_logs") \
.update({"current_kw": record['today_kwh']}) \
.eq("id", record['id']) \
.execute()
updated_count += 1
except Exception as e:
print(f" ⚠️ 업데이트 실패 (id: {record['id']}): {e}")
if updated_count > 0:
print(f"{date_str}: {updated_count}건 업데이트")
total_updated += updated_count
current += timedelta(days=1)
print(f"\n✅ [{plant_name}] 총 {total_updated}건 업데이트 완료")
def main():
plants = [
('kremc-05', '5호기'),
('nrems-09', '9호기')
]
print("\n" + "="*70)
print("🔧 2월 데이터 current_kw 업데이트 시작")
print("="*70)
for plant_id, plant_name in plants:
update_current_kw(plant_id, plant_name)
print("\n" + "="*70)
print("🎉 current_kw 업데이트 완료!")
print("="*70 + "\n")
if __name__ == "__main__":
main()