implementace Ekonomiky
This commit is contained in:
@@ -15,6 +15,7 @@ import httpx
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from app.db_json import record_to_dict
|
||||
from app.deps import set_pg_pool
|
||||
from app.routers.economics import router as economics_router
|
||||
from app.routers.ev import router as ev_router
|
||||
from app.routers.full_status import router as full_status_router
|
||||
from app.routers.plan import router as plan_router
|
||||
@@ -423,6 +424,63 @@ async def lifespan(app: FastAPI):
|
||||
id="forecast_refresh_2h",
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
async def scheduled_daily_economics_notification() -> None:
|
||||
from services.notification_service import notify_daily_economics
|
||||
|
||||
async with app.state.pg_pool.acquire() as conn:
|
||||
sites = await conn.fetch("SELECT id, code FROM ems.site WHERE active = true")
|
||||
for site in sites:
|
||||
site_id = int(site["id"])
|
||||
site_code = site["code"]
|
||||
try:
|
||||
row = await conn.fetchrow(
|
||||
"""
|
||||
SELECT import_kwh, export_kwh,
|
||||
import_cost_czk, export_revenue_czk,
|
||||
green_bonus_czk, total_balance_czk,
|
||||
planned_balance_czk
|
||||
FROM ems.vw_economics_daily
|
||||
WHERE site_id = $1
|
||||
AND day_local = (
|
||||
CURRENT_DATE AT TIME ZONE 'Europe/Prague' - INTERVAL '1 day'
|
||||
)::date
|
||||
""",
|
||||
site_id,
|
||||
)
|
||||
if row is None:
|
||||
continue
|
||||
yesterday = (
|
||||
datetime.now(ZoneInfo("Europe/Prague"))
|
||||
- timedelta(days=1)
|
||||
).strftime("%Y-%m-%d")
|
||||
await notify_daily_economics(
|
||||
site_code=site_code,
|
||||
day=yesterday,
|
||||
import_kwh=float(row["import_kwh"] or 0),
|
||||
import_cost=float(row["import_cost_czk"] or 0),
|
||||
export_kwh=float(row["export_kwh"] or 0),
|
||||
export_revenue=float(row["export_revenue_czk"] or 0),
|
||||
green_bonus=float(row["green_bonus_czk"] or 0),
|
||||
total_balance=float(row["total_balance_czk"] or 0),
|
||||
planned_balance=float(row["planned_balance_czk"])
|
||||
if row["planned_balance_czk"] is not None
|
||||
else None,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"scheduled_daily_economics_notification site=%s failed",
|
||||
site_id,
|
||||
)
|
||||
|
||||
scheduler.add_job(
|
||||
scheduled_daily_economics_notification,
|
||||
"cron",
|
||||
hour=7,
|
||||
minute=0,
|
||||
id="daily_economics_notification",
|
||||
replace_existing=True,
|
||||
)
|
||||
scheduler.start()
|
||||
|
||||
telemetry_task = asyncio.create_task(run_telemetry_loop_wrapper(app.state.pg_pool))
|
||||
@@ -454,6 +512,7 @@ app = FastAPI(title="EMS Platform", lifespan=lifespan)
|
||||
app.include_router(plan_router, prefix="/api/v1")
|
||||
app.include_router(ev_router, prefix="/api/v1")
|
||||
app.include_router(full_status_router, prefix="/api/v1")
|
||||
app.include_router(economics_router, prefix="/api/v1")
|
||||
|
||||
sites_router = APIRouter(prefix="/api/v1/sites", tags=["sites"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user