sql first refactor
This commit is contained in:
@@ -11,6 +11,7 @@ from zoneinfo import ZoneInfo
|
||||
import httpx
|
||||
|
||||
from app.config import get_settings
|
||||
from app.db_json import fetch_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -119,18 +120,14 @@ async def _apply_ote_json_to_db(conn, payload: dict) -> int:
|
||||
|
||||
async def count_ote_slots_prague_day(conn, target_day: date) -> int:
|
||||
"""Počet řádků OTE_CZ pro kalendářní den v Europe/Prague (plný den 92/96/100)."""
|
||||
return int(
|
||||
await conn.fetchval(
|
||||
"""
|
||||
SELECT COUNT(*)::int
|
||||
FROM ems.market_interval_price
|
||||
WHERE market_source = 'OTE_CZ'
|
||||
AND (interval_start AT TIME ZONE 'Europe/Prague')::date = $1::date
|
||||
""",
|
||||
target_day,
|
||||
)
|
||||
or 0
|
||||
stats = await fetch_json(
|
||||
conn,
|
||||
"select ems.fn_ote_day_slot_stats_prague($1::date)",
|
||||
target_day,
|
||||
)
|
||||
if not isinstance(stats, dict):
|
||||
stats = json.loads(stats)
|
||||
return int(stats.get("count") or 0)
|
||||
|
||||
|
||||
async def import_ote_prices_for_day(
|
||||
@@ -147,18 +144,15 @@ async def import_ote_prices_for_day(
|
||||
return -1, day_str, 0.0, fetch_error or "fetch_failed"
|
||||
try:
|
||||
n = await _apply_ote_json_to_db(conn, payload)
|
||||
first_price = await conn.fetchval(
|
||||
"""
|
||||
SELECT buy_raw_price_czk_kwh
|
||||
FROM ems.market_interval_price
|
||||
WHERE market_source = 'OTE_CZ'
|
||||
AND (interval_start AT TIME ZONE 'Europe/Prague')::date = $1::date
|
||||
ORDER BY interval_start
|
||||
LIMIT 1
|
||||
""",
|
||||
stats_after = await fetch_json(
|
||||
conn,
|
||||
"select ems.fn_ote_day_slot_stats_prague($1::date)",
|
||||
target_day,
|
||||
)
|
||||
n_imported = await count_ote_slots_prague_day(conn, target_day)
|
||||
if not isinstance(stats_after, dict):
|
||||
stats_after = json.loads(stats_after)
|
||||
first_price = stats_after.get("first_price")
|
||||
n_imported = int(stats_after.get("count") or 0)
|
||||
if not ote_prague_day_slots_look_complete(n_imported):
|
||||
logger.warning(
|
||||
"OTE: %s slotů pro %s (plný den = jedna z %s; jinak neúplná data)",
|
||||
@@ -248,7 +242,7 @@ async def import_ote_prices(
|
||||
"""
|
||||
if site_id is not None:
|
||||
row = await db.fetchrow(
|
||||
"SELECT timezone FROM ems.site WHERE id = $1", site_id
|
||||
"select timezone from ems.vw_site_directory where id = $1", site_id
|
||||
)
|
||||
if row is None:
|
||||
logger.error("OTE import: site id=%s nenalezen", site_id)
|
||||
@@ -290,26 +284,15 @@ async def import_ote_prices(
|
||||
|
||||
try:
|
||||
n = await _apply_ote_json_to_db(db, payload)
|
||||
first_price = await db.fetchval(
|
||||
"""
|
||||
SELECT buy_raw_price_czk_kwh
|
||||
FROM ems.market_interval_price
|
||||
WHERE market_source = 'OTE_CZ'
|
||||
AND interval_start::date = $1::date
|
||||
ORDER BY interval_start
|
||||
LIMIT 1
|
||||
""",
|
||||
target_day,
|
||||
)
|
||||
n_imported = await db.fetchval(
|
||||
"""
|
||||
SELECT COUNT(*)::int
|
||||
FROM ems.market_interval_price
|
||||
WHERE market_source = 'OTE_CZ'
|
||||
AND interval_start::date = $1::date
|
||||
""",
|
||||
stats_after = await fetch_json(
|
||||
db,
|
||||
"select ems.fn_ote_day_slot_stats_prague($1::date)",
|
||||
target_day,
|
||||
)
|
||||
if not isinstance(stats_after, dict):
|
||||
stats_after = json.loads(stats_after)
|
||||
first_price = stats_after.get("first_price")
|
||||
n_imported = int(stats_after.get("count") or 0)
|
||||
incomplete = not ote_prague_day_slots_look_complete(n_imported or 0)
|
||||
if incomplete:
|
||||
now_p = datetime.now(ZoneInfo("Europe/Prague"))
|
||||
|
||||
Reference in New Issue
Block a user