Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
"""Planification de la collecte automatique des métriques."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
from app.models.database import async_session_maker
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
METRICS_COLLECTION_JOB_ID = "metrics_collect_all"
|
|
|
|
|
|
def interval_to_seconds(interval: Optional[str]) -> Optional[int]:
|
|
if not interval or interval == "off":
|
|
return None
|
|
|
|
mapping = {
|
|
"5min": 5 * 60,
|
|
"15min": 15 * 60,
|
|
"30min": 30 * 60,
|
|
"1h": 60 * 60,
|
|
"6h": 6 * 60 * 60,
|
|
"12h": 12 * 60 * 60,
|
|
"24h": 24 * 60 * 60,
|
|
}
|
|
|
|
return mapping.get(interval)
|
|
|
|
|
|
def apply_interval(scheduler: AsyncIOScheduler, interval: str) -> None:
|
|
seconds = interval_to_seconds(interval)
|
|
|
|
existing = scheduler.get_job(METRICS_COLLECTION_JOB_ID)
|
|
if seconds is None:
|
|
if existing:
|
|
scheduler.remove_job(METRICS_COLLECTION_JOB_ID)
|
|
logger.info("[METRICS_SCHEDULER] Job removed (interval=off)")
|
|
return
|
|
|
|
from app.routes.builtin_playbooks import collect_all_metrics_job
|
|
|
|
scheduler.add_job(
|
|
collect_all_metrics_job,
|
|
trigger="interval",
|
|
seconds=seconds,
|
|
id=METRICS_COLLECTION_JOB_ID,
|
|
name="Metrics Collection",
|
|
replace_existing=True,
|
|
)
|
|
logger.info(f"[METRICS_SCHEDULER] Job scheduled: interval={interval} ({seconds}s)")
|
|
|
|
|
|
def apply_interval_using_global_scheduler(interval: str) -> None:
|
|
from app.services import scheduler_service
|
|
|
|
apply_interval(scheduler_service.scheduler, interval)
|
|
|
|
|
|
async def load_and_apply_from_db() -> str:
|
|
from app.crud.app_setting import AppSettingRepository
|
|
from app.services import scheduler_service
|
|
|
|
interval = "off"
|
|
async with async_session_maker() as session:
|
|
repo = AppSettingRepository(session)
|
|
row = await repo.get("metrics_collection_interval")
|
|
if row and row.value:
|
|
interval = row.value
|
|
|
|
logger.info(f"[METRICS_SCHEDULER] Loading from DB: interval={interval}")
|
|
apply_interval(scheduler_service.scheduler, interval)
|
|
return interval
|