# ========================================== # sync_plants.py - 발전소 정보 동기화 # ========================================== # config.py의 발전소 정보를 Supabase plants 테이블에 Upsert from datetime import datetime try: from dotenv import load_dotenv load_dotenv() except ImportError: pass from config import get_all_plants from database import get_supabase_client def sync_plants(): """ 로컬 config.py의 발전소 정보를 Supabase plants 테이블에 동기화 """ print(f"\n🔄 [발전소 동기화] 시작... ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})") print("-" * 60) client = get_supabase_client() if not client: print("❌ Supabase 연결 실패") return False plants = get_all_plants() # 중복 제거 (is_split인 1,2호기는 별도 처리) unique_plants = {} for plant in plants: plant_id = plant.get('id', '') is_split = plant.get('options', {}).get('is_split', False) if is_split: # 1, 2호기 분리 (용량 N빵) total_capacity = plant.get('capacity_kw', 100.0) unit_capacity = total_capacity / 2 start_date = plant.get('start_date', '') unique_plants['nrems-01'] = { 'id': 'nrems-01', 'name': f"{plant.get('company_name', '')} 1호기", 'type': plant.get('type', ''), 'capacity': unit_capacity, 'constructed_at': start_date, 'company_id': 1 } unique_plants['nrems-02'] = { 'id': 'nrems-02', 'name': f"{plant.get('company_name', '')} 2호기", 'type': plant.get('type', ''), 'capacity': unit_capacity, 'constructed_at': start_date, 'company_id': 1 } elif plant_id: unique_plants[plant_id] = { 'id': plant_id, 'name': f"{plant.get('company_name', '')} {plant.get('name', '')}", 'type': plant.get('type', ''), 'capacity': plant.get('capacity_kw', 0.0), 'constructed_at': plant.get('start_date', ''), 'company_id': 1 } success_count = 0 for plant_id, plant_data in unique_plants.items(): try: result = client.table("plants").upsert( plant_data, on_conflict="id" ).execute() print(f" ✅ {plant_data['name']} (용량: {plant_data['capacity']} kW)") success_count += 1 except Exception as e: print(f" ❌ {plant_id} 실패: {e}") print("-" * 60) print(f"✅ 동기화 완료: {success_count}/{len(unique_plants)}개") return True if __name__ == "__main__": sync_plants()