feat(collector): forecast and ntfy in ping-collector; ThaiWater keeps alerts current at night
CI / Format & lint (push) Successful in 22s
CI / Test suite (push) Successful in 59s
Docs / Validate documentation (push) Successful in 25s
Rust / fmt, clippy, test, build (push) Successful in 10m2s
Security / Dependency vulnerabilities (push) Successful in 2m11s
Security / Static analysis (push) Successful in 22s
Security / License report (push) Successful in 1m37s

Phase 2b/2c of docs/RUST_REWRITE.md. With FORECAST_IN_COLLECTOR=true (shared
.env) ping-collector, after each cycle that brought newer RID or HII readings
and at least hourly: loads the last 336 h (RID + HII mirror fill), fetches the
rain forecast, runs every exported model (rust/forecast), stores
forecast_history and forecast_current, and sends the ntfy notifications.
The Python API then serves forecast_current as /forecast (computing in-process
only when it is older than FORECAST_MAX_AGE_SECONDS) and neither forecasts nor
notifies.

ntfy (collector/src/notify.rs): a port of src/notify.py with the same topics,
texts, hysteresis, capacity guard and notification_state keys, checked word
for word on 751 messages generated by Python (testdata/gen_notify_cases.py).
The change: for stations whose ThaiWater twin is the same sensor, a reading
newer than RID's drives the level alerts (marked as ThaiWater telemetry), and
the feed counts as stale only when neither source has a reading for 3 h. RID
alone lagging is reported once after RID_STALE_ALERT_HOURS (18). This ends the
nightly "gauge feed stale" / "recovered" pairs caused by RID's late reports.

Commands: `forecast [--json] [--save] [--now] [--rain-file]`,
`notify --dry-run`. scripts/forecast_parity.py compares Python and the
collector on the live database (same moment, same rain); locally 48/48 rows
identical. install_collector.sh: models export + that check on every install,
--cutover-forecast (refuses on a failed check) and --rollback-forecast.

Trees are packed into 24-byte nodes: 70 MB peak with all 15 models (was 140).
Forecast values are written to NUMERIC as shortest round-trip text, so they
round as psycopg2's do. DB test schemas get a counter (parallel tests collided
on Windows' coarse clock).
This commit is contained in:
grabowski committed 2026-10-18 14:43:39 +07:00
1 parent 5c4f464383
commit 6ae67dec63
27 files changed
+2656 -118

No files matched your search

+139 -2
View File
@@ -3,13 +3,17 @@
use crate::config::{Config, USER_AGENT};
use crate::dams::DamsClient;
use crate::db;
use crate::forecasting::{self, Forecaster, RunInfo};
use crate::hii::HiiClient;
use crate::notify;
use crate::rain::OpenMeteoClient;
use crate::rid::{self, CycleOutcome, RidClient};
use crate::stations::Stations;
use crate::util::{bangkok_now, floor_hour};
use anyhow::Result;
use anyhow::{Result, bail};
use chrono::{Duration, NaiveDateTime};
use ping_forecast::ModelStore;
use ping_forecast::rain::RainSeries;
use serde_json::json;
use sqlx::PgPool;
@@ -21,6 +25,7 @@ pub struct Collector {
hii: HiiClient,
dams: DamsClient,
rain: OpenMeteoClient,
http: reqwest::Client,
}
#[derive(Default)]
@@ -33,6 +38,7 @@ struct Stats {
feed_current: Option<bool>,
latest_reading: Option<NaiveDateTime>,
readings_saved: usize,
forecast: Option<RunInfo>,
}
fn iso(t: Option<NaiveDateTime>) -> serde_json::Value {
@@ -51,7 +57,8 @@ impl Collector {
rid: RidClient::new(http.clone(), cfg.rid_api_url.clone()),
hii: HiiClient::new(http.clone(), cfg.hii_api_base.clone()),
dams: DamsClient::new(http.clone(), cfg.dams_url.clone()),
rain: OpenMeteoClient::new(http, cfg.openmeteo_url.clone()),
rain: OpenMeteoClient::new(http.clone(), cfg.openmeteo_url.clone()),
http,
cfg,
pool,
stations,
@@ -114,6 +121,13 @@ impl Collector {
"latest_reading": iso(stats.latest_reading),
"readings_saved": stats.readings_saved,
"collector": concat!("ping-collector ", env!("CARGO_PKG_VERSION")),
"forecast": stats.forecast.as_ref().map(|f| json!({
"last_run": iso(Some(f.at)),
"rows": f.rows,
"model_rows": f.model_rows,
"models": f.models,
"notifications_sent": f.notified,
})),
});
let tmp = path.with_extension("json.tmp");
let result =
@@ -143,6 +157,13 @@ impl Collector {
let mut stats = Stats::default();
let mut last_hourly_slot: Option<NaiveDateTime> = None;
let mut forecaster = self.cfg.forecast_enabled.then(|| {
tracing::info!(
"FORECAST_IN_COLLECTOR: forecasting from {} and notifying here",
self.cfg.models_dir.display()
);
Forecaster::new(&self.cfg, self.http.clone())
});
loop {
let start = bangkok_now();
self.write_status(&stats, true);
@@ -166,6 +187,11 @@ impl Collector {
last_hourly_slot = Some(slot);
self.collect_hourly(bangkok_now()).await;
}
if let Some(f) = forecaster.as_mut()
&& let Some(info) = f.maybe_run(&self.pool, &self.rain, bangkok_now()).await
{
stats.forecast = Some(info);
}
let now = bangkok_now();
let next = rid::next_poll_time(
@@ -256,6 +282,117 @@ impl Collector {
}
}
impl Collector {
/// `ping-collector forecast`: compute from the database now (or at
/// `now`), print a table or the /forecast JSON, store only with `save`.
pub async fn forecast(
&self,
json_out: bool,
save: bool,
now: Option<NaiveDateTime>,
rain: Option<RainSeries>,
) -> Result<()> {
let mut store = ModelStore::new(&self.cfg.models_dir);
let models = store.refresh()?;
if models.is_empty() {
bail!(
"no exported models in {} (run `python -m src.ml.export`)",
self.cfg.models_dir.display()
);
}
let now = now.unwrap_or_else(bangkok_now);
let snap = forecasting::compute(&self.pool, &self.rain, &models, now, rain).await?;
if json_out {
println!("{}", serde_json::to_string(&snap.forecasts)?);
} else {
println!(
"{:<6} {:>3} {:<9} {:<16} {:>7} {:>7} {:>9} {:>8}",
"code", "h", "source", "as_of", "level", "peak", "p_warning", "p_danger"
);
for f in &snap.forecasts {
println!(
"{:<6} {:>3} {:<9} {:<16} {:>7.2} {:>7.2} {:>9.3} {:>8.3}",
f.station_code,
f.horizon_hours,
f.source,
f.as_of.format("%Y-%m-%d %H:%M"),
f.current_level,
f.predicted_max_level,
f.p_warning,
f.p_danger
);
}
eprintln!(
"{} models from {}; {}",
models.len(),
self.cfg.models_dir.display(),
forecasting::summarize(&snap)
);
}
if save {
db::ensure_schema(&self.pool).await?;
let n = db::save_forecast_history(&self.pool, &snap.forecasts).await?;
db::save_forecast_current(&self.pool, now, &snap.forecasts).await?;
eprintln!("stored: {n} forecast_history rows, forecast_current");
}
Ok(())
}
/// `ping-collector notify`: what would be (or, without `dry_run`, is)
/// published right now against the stored notification state.
pub async fn notify(&self, dry_run: bool) -> Result<()> {
if !dry_run && !self.cfg.forecast_enabled {
bail!(
"the Python app notifies while FORECAST_IN_COLLECTOR is not true; \
use --dry-run"
);
}
let Some(notifier) = forecasting::notifier(&self.cfg, self.http.clone()) else {
bail!("ntfy is off: NTFY_SERVER is not set");
};
let mut store = ModelStore::new(&self.cfg.models_dir);
let models = store.refresh()?;
let now = bangkok_now();
let snap = forecasting::compute(&self.pool, &self.rain, &models, now, None).await?;
let readings =
notify::readings_with_hii(&db::latest_rid_readings(&self.pool).await?, &snap.fills);
for r in &readings {
println!(
"{:<6} {} {:>7} {:?}{}",
r.station_code,
r.timestamp.format("%Y-%m-%d %H:%M"),
r.water_level.map_or("-".into(), |l| format!("{l:.2}")),
r.source,
r.discharge_percent
.map_or(String::new(), |p| format!(" {p:.0}%"))
);
}
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);
if plan.is_empty() {
println!("no transitions: nothing would be published");
}
for p in plan {
println!(
"would publish {}: {} -- {}",
p.notification.topic, p.notification.title, p.notification.message
);
}
} else {
for n in notifier
.run(&self.pool, &readings, &snap.forecasts, now)
.await?
{
println!("published {}: {}", n.topic, n.title);
}
}
Ok(())
}
}
async fn shutdown_signal() {
#[cfg(unix)]
{