Some checks are pending
CI / Crawler (Python ${{ matrix.python-version }}) (3.10) (push) Waiting to run
CI / Crawler (Python ${{ matrix.python-version }}) (3.11) (push) Waiting to run
CI / API (Python 3.11) (push) Waiting to run
CI / Database migration (push) Waiting to run
CI / App web build (Node 20) (push) Waiting to run
80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""원본 사이트의 특정 일별 값을 DB 저장 없이 확인하는 운영 점검 도구."""
|
|
|
|
import argparse
|
|
import importlib
|
|
import os
|
|
import sys
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
CRAWLER_DIR = os.path.dirname(CURRENT_DIR)
|
|
sys.path.insert(0, CRAWLER_DIR)
|
|
load_dotenv(os.path.join(CRAWLER_DIR, ".env"))
|
|
|
|
from config import get_all_plants
|
|
|
|
|
|
def build_plant_map():
|
|
plants = {}
|
|
for plant in get_all_plants():
|
|
if plant.get("options", {}).get("is_split"):
|
|
for site_id, split_index in (("nrems-01", 1), ("nrems-02", 2)):
|
|
split_plant = plant.copy()
|
|
split_plant["id"] = site_id
|
|
split_plant["options"] = plant["options"].copy()
|
|
split_plant["options"]["split_index"] = split_index
|
|
plants[site_id] = split_plant
|
|
else:
|
|
plants[plant["id"]] = plant
|
|
return plants
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"targets",
|
|
nargs="+",
|
|
metavar="PLANT_ID=YYYY-MM-DD",
|
|
help="확인할 발전소 ID와 날짜",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
plants = build_plant_map()
|
|
failed = False
|
|
|
|
for target in args.targets:
|
|
site_id, separator, date = target.partition("=")
|
|
if not separator or site_id not in plants:
|
|
print(f"INVALID {target}")
|
|
failed = True
|
|
continue
|
|
|
|
plant = plants[site_id]
|
|
module = importlib.import_module(f"crawlers.{plant['type']}")
|
|
data = module.fetch_history_daily(plant, date, date)
|
|
|
|
if data is None:
|
|
print(f"ERROR {site_id} {date}: request or parse failure")
|
|
failed = True
|
|
continue
|
|
|
|
matched = [row for row in data if row.get("plant_id") == site_id]
|
|
if not matched:
|
|
print(f"NO_DATA {site_id} {date}")
|
|
continue
|
|
|
|
for row in matched:
|
|
print(
|
|
f"OK {site_id} {row.get('date')}: "
|
|
f"{float(row.get('generation_kwh', 0)):.2f} kWh"
|
|
)
|
|
|
|
raise SystemExit(1 if failed else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|