feat(web): the website and API in ping-collector; Matrix gets the ntfy transitions
CI / Format & lint (push) Successful in 26s
CI / Test suite (push) Successful in 1m9s
Docs / Validate documentation (push) Successful in 25s
Rust / fmt, clippy, test, build (push) Successful in 11m46s

Phase 3 of docs/RUST_REWRITE.md: rust/collector/src/web (axum) serves what
the FastAPI app (src/web_api.py) served, so one process collects, forecasts,
notifies and serves. With WEB_LISTEN=0.0.0.0:8000 it runs inside
`ping-collector run`; `ping-collector serve` runs it alone.

Same routes, parameters, JSON fields and field order, status codes,
{"detail": ...} errors and pydantic-style 422 lists, Cache-Control, gzip
(>500 B), CORS, Umami API tracking, the dashboard and static files, and the
stale-while-revalidate response caches (now with byte budgets: a history
response can be 10 MB). /docs is the same Swagger UI (+ /redoc); its OpenAPI
document is built from the route table, whose per-route parameter
declarations also validate the requests, so docs and behaviour cannot drift.
Python output conventions are kept: isoformat(), round(), numpy's pairwise
sums (catchment means), Python's summation order (skill metrics).

scripts/api_parity.py compares the two servers route by route on one
database (53 cases incl. errors, admin and static files; query values vary
per run so FastAPI's caches answer fresh). Locally: 53 identical; the
station CRUD writes byte-identical stations.json files; the skill/trend
logic matches on 9 station/horizon pairs with synthetic model versions.
Intended differences: /health counts the ThaiWater telemetry of the same
gauges while RID lags (as the notifier); /scraping/status adds the collector
version and last forecast run; an old stored forecast is served marked
X-Data-Stale instead of recomputed; /sensors/thaiwater is cached a minute;
.geojson is served as application/geo+json. Load (local, 16 clients): the
dashboard's routes 1375 vs 552 req/s, uncached history 714 vs 53 req/s;
25 MB resident when serving only.

Matrix (MATRIX_*): the notifier sends the room the transitions ntfy gets
(per station, outlook, feed; not ntfy's per-topic summaries), each once,
with its own matrix: keys in notification_state, seeded from ntfy's on first
use so nothing already announced repeats. Checked against a fake homeserver.
It replaces the `run.py --alert-check` cron job (repeated alerts every 15
min), which install_collector.sh now comments out (crontab backup kept).

install_collector.sh: --check-api (ping-collector serve on :8001 next to
FastAPI, api_parity.py), --cutover-api (refuses on a difference; sets
WEB_LISTEN, stops and disables water-monitor.service), --rollback-api;
--rollback-forecast and --rollback bring the Python app back as server.
ping-collector.service: MemoryMax 512M. Config::from_vars for tests; route
tests against a throwaway schema.
This commit is contained in:
grabowski committed 2026-10-18 16:08:06 +07:00
1 parent 1d1c000922
commit 779728e38b
32 files changed
+5692 -160

No files matched your search

+95 -16
View File
@@ -16,11 +16,17 @@ use ping_forecast::ModelStore;
use ping_forecast::rain::RainSeries;
use serde_json::json;
use sqlx::PgPool;
use std::sync::{Arc, RwLock};
pub struct Collector {
pub cfg: Config,
pub pool: PgPool,
pub stations: Stations,
/// Shared with the website (the station CRUD API edits it).
pub stations: Arc<RwLock<Stations>>,
/// The last status written, shared with /scraping/status.
pub status: Arc<RwLock<serde_json::Value>>,
/// POST /scrape/trigger: poll now.
pub trigger: Arc<tokio::sync::Notify>,
pub rid: RidClient,
hii: HiiClient,
dams: DamsClient,
@@ -61,10 +67,36 @@ impl Collector {
http,
cfg,
pool,
stations,
stations: Arc::new(RwLock::new(stations)),
status: Arc::new(RwLock::new(json!({"is_running": false}))),
trigger: Arc::new(tokio::sync::Notify::new()),
})
}
/// The station mapping as of now (the API may edit it between cycles).
pub fn stations(&self) -> Stations {
self.stations.read().unwrap().clone()
}
/// The website/API state over this collector's pool, stations and status.
pub fn web_state(&self, in_process: bool) -> crate::web::Shared {
crate::web::AppState::new(
self.cfg.clone(),
self.pool.clone(),
self.http.clone(),
self.stations.clone(),
in_process.then(|| self.status.clone()),
in_process.then(|| self.trigger.clone()),
)
}
/// `ping-collector serve`: the website and API only; /scraping/status
/// reads the status file the collector service writes.
pub async fn serve(&self, listen: String) -> Result<()> {
db::ensure_schema(&self.pool).await?;
crate::web::serve(self.web_state(false), listen, shutdown_signal()).await
}
/// HII rain + water level, RID dams (today and yesterday), Open-Meteo.
/// Each source is failure-isolated.
pub async fn collect_hourly(&self, now: NaiveDateTime) {
@@ -107,9 +139,6 @@ impl Collector {
}
fn write_status(&self, stats: &Stats, running: bool) {
let Some(path) = &self.cfg.status_file else {
return;
};
let body = json!({
"is_running": running,
"total_runs": stats.total_runs,
@@ -129,6 +158,10 @@ impl Collector {
"notifications_sent": f.notified,
})),
});
*self.status.write().unwrap() = body.clone();
let Some(path) = &self.cfg.status_file else {
return;
};
let tmp = path.with_extension("json.tmp");
let result =
std::fs::write(&tmp, body.to_string()).and_then(|_| std::fs::rename(&tmp, path));
@@ -144,14 +177,40 @@ impl Collector {
db::ensure_schema(&self.pool).await?;
let shutdown = shutdown_signal();
tokio::pin!(shutdown);
// the website, when WEB_LISTEN is set: stopped with the collector
let (stop_web, web_stopped) = tokio::sync::watch::channel(false);
let web = self.cfg.web.listen.clone().map(|listen| {
let state = self.web_state(true);
let mut rx = web_stopped.clone();
tokio::spawn(async move {
let stop = async move {
let _ = rx.wait_for(|stop| *stop).await;
};
if let Err(e) = crate::web::serve(state, listen, stop).await {
tracing::error!("website stopped: {e:#}");
}
})
});
let result = self.collect_loop(&mut shutdown).await;
let _ = stop_web.send(true);
if let Some(handle) = web {
let _ = tokio::time::timeout(std::time::Duration::from_secs(10), handle).await;
}
result
}
async fn collect_loop(
&self,
shutdown: &mut std::pin::Pin<&mut impl std::future::Future<Output = ()>>,
) -> Result<()> {
if self.cfg.gap_fill_days_on_start > 0 {
let days = Some(self.cfg.gap_fill_days_on_start);
let stations = self.stations();
tokio::select! {
r = rid::fill_gaps(&self.rid, &self.pool, &self.stations, days, bangkok_now()) => {
r = rid::fill_gaps(&self.rid, &self.pool, &stations, days, bangkok_now()) => {
if let Err(e) = r { tracing::error!("startup gap fill failed: {e:#}") }
}
_ = &mut shutdown => return Ok(()),
_ = &mut *shutdown => return Ok(()),
}
}
@@ -168,7 +227,7 @@ impl Collector {
let start = bangkok_now();
self.write_status(&stats, true);
let outcome: CycleOutcome =
rid::run_cycle(&self.rid, &self.pool, &self.stations, start).await;
rid::run_cycle(&self.rid, &self.pool, &self.stations(), start).await;
stats.total_runs += 1;
stats.last_run = Some(start);
if outcome.current {
@@ -217,7 +276,8 @@ impl Collector {
.max(std::time::Duration::from_secs(30));
tokio::select! {
_ = tokio::time::sleep(wait) => {}
_ = &mut shutdown => {
_ = self.trigger.notified() => tracing::info!("collection triggered via the API"),
_ = &mut *shutdown => {
tracing::info!("shutdown signal received");
return Ok(());
}
@@ -231,7 +291,7 @@ impl Collector {
let now = bangkok_now();
if !dry_run {
db::ensure_schema(&self.pool).await?;
let o = rid::run_cycle(&self.rid, &self.pool, &self.stations, now).await;
let o = rid::run_cycle(&self.rid, &self.pool, &self.stations(), now).await;
println!(
"RID: reports {:?}, {} fetched, {} saved, latest {:?}, current hour in: {}{}",
o.dates,
@@ -348,7 +408,9 @@ impl Collector {
);
}
let Some(notifier) = forecasting::notifier(&self.cfg, self.http.clone()) else {
bail!("ntfy is off: NTFY_SERVER is not set");
bail!(
"notifications are off: neither NTFY_SERVER nor MATRIX_ACCESS_TOKEN/MATRIX_ROOM_ID is set"
);
};
let mut store = ModelStore::new(&self.cfg.models_dir);
let models = store.refresh()?;
@@ -370,15 +432,32 @@ impl Collector {
db::ensure_schema(&self.pool).await?;
if dry_run {
let states = db::notification_states(&self.pool).await?;
let plan =
notify::evaluate(&readings, &snap.forecasts, &states, &notifier.settings, now);
let mut plan = vec![];
if notifier.ntfy.is_some() {
plan.extend(
notify::evaluate(&readings, &snap.forecasts, &states, &notifier.settings, now)
.into_iter()
.map(|p| (p.notification.topic.clone(), p)),
);
}
if notifier.matrix.is_some() {
let mstates = notifier
.matrix_states(&self.pool, &states, now, false)
.await?;
plan.extend(
notifier
.matrix_plan(&readings, &snap.forecasts, &mstates, now)
.into_iter()
.map(|p| ("matrix".to_string(), p)),
);
}
if plan.is_empty() {
println!("no transitions: nothing would be published");
}
for p in plan {
for (to, p) in plan {
println!(
"would publish {}: {} -- {}",
p.notification.topic, p.notification.title, p.notification.message
"would publish {to}: {} -- {}",
p.notification.title, p.notification.message
);
}
} else {