- The built-in Hungarian system prompts and the user message frame are written in Hungarian - Admin page Modellek: the model of each analysis kind, chosen from the OpenRouter list (structured-output models only) with prices and a cost estimate per analysis - Admin: regenerate any analysis in place with the current prompt and model (no charge) - Fix: a failed forecast analysis refunds a forecast unit, not a natal one Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
116 lines
4.9 KiB
Rust
116 lines
4.9 KiB
Rust
//! `backend recalculate`: after a change in the calculation, recalculates
|
|
//! every stored chart, comparison and forecast with the horoscope service,
|
|
//! then deletes all analyses and queues them again: a short summary and a
|
|
//! detailed analysis for every chart, one analysis for every comparison and
|
|
//! every forecast. Nobody is charged; each analysis is requested in the name
|
|
//! of the owner, in the owner's language.
|
|
|
|
use anyhow::Context;
|
|
use serde_json::Value;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use super::analyses::{Target, queue};
|
|
use super::charts::{CHART_COLUMNS, ChartRow};
|
|
use crate::AppState;
|
|
|
|
async fn all_charts(db: &PgPool) -> Result<Vec<ChartRow>, sqlx::Error> {
|
|
sqlx::query_as(&format!("SELECT {CHART_COLUMNS} FROM charts ORDER BY created_at")).fetch_all(db).await
|
|
}
|
|
|
|
async fn locale(db: &PgPool, user: Uuid) -> Result<String, sqlx::Error> {
|
|
sqlx::query_scalar("SELECT locale FROM users WHERE id = $1").bind(user).fetch_one(db).await
|
|
}
|
|
|
|
pub async fn run(state: &AppState) -> anyhow::Result<()> {
|
|
let db = &state.db;
|
|
|
|
let charts = all_charts(db).await?;
|
|
for chart in &charts {
|
|
let (_, result, hash) = super::charts::calculate(state, chart.birth_data.clone())
|
|
.await
|
|
.map_err(|e| anyhow::anyhow!("chart {}: {e:?}", chart.id))?;
|
|
sqlx::query("UPDATE charts SET result = $1, chart_hash = $2 WHERE id = $3")
|
|
.bind(&result)
|
|
.bind(&hash)
|
|
.bind(chart.id)
|
|
.execute(db)
|
|
.await?;
|
|
}
|
|
tracing::info!(count = charts.len(), "charts recalculated");
|
|
let charts = all_charts(db).await?;
|
|
let chart = |id: Uuid| charts.iter().find(|c| c.id == id).context("missing chart");
|
|
|
|
let synastries: Vec<(Uuid, Uuid, Uuid, Uuid)> =
|
|
sqlx::query_as("SELECT id, owner_id, chart1_id, chart2_id FROM synastries ORDER BY created_at")
|
|
.fetch_all(db)
|
|
.await?;
|
|
let mut synastry_hashes = Vec::new();
|
|
for &(id, owner, chart1, chart2) in &synastries {
|
|
let (result, hash) = super::synastries::calculate(state, chart(chart1)?, chart(chart2)?)
|
|
.await
|
|
.map_err(|e| anyhow::anyhow!("synastry {id}: {e:?}"))?;
|
|
sqlx::query("UPDATE synastries SET result = $1, synastry_hash = $2 WHERE id = $3")
|
|
.bind(&result)
|
|
.bind(&hash)
|
|
.bind(id)
|
|
.execute(db)
|
|
.await?;
|
|
synastry_hashes.push((id, owner, hash));
|
|
}
|
|
tracing::info!(count = synastries.len(), "comparisons recalculated");
|
|
|
|
let forecasts: Vec<(Uuid, Uuid, i32, Option<Value>, String)> =
|
|
sqlx::query_as("SELECT id, chart_id, year, location, location_key FROM forecasts ORDER BY created_at")
|
|
.fetch_all(db)
|
|
.await?;
|
|
let mut forecast_hashes = Vec::new();
|
|
for (id, chart_id, year, location, location_key) in &forecasts {
|
|
let c = chart(*chart_id)?;
|
|
let (result, hash) = super::forecasts::calculate(state, c, *year, location.as_ref(), location_key)
|
|
.await
|
|
.map_err(|e| anyhow::anyhow!("forecast {id}: {e:?}"))?;
|
|
let updated =
|
|
sqlx::query("UPDATE forecasts SET chart_hash = $1, result = $2, forecast_hash = $3 WHERE id = $4")
|
|
.bind(&c.chart_hash)
|
|
.bind(&result)
|
|
.bind(&hash)
|
|
.bind(id)
|
|
.execute(db)
|
|
.await;
|
|
match updated {
|
|
Ok(_) => forecast_hashes.push((*id, c.owner_id, hash)),
|
|
// The same year and place from an earlier version of the birth data: a duplicate now.
|
|
Err(sqlx::Error::Database(e)) if e.is_unique_violation() => {
|
|
sqlx::query("DELETE FROM forecasts WHERE id = $1").bind(id).execute(db).await?;
|
|
tracing::warn!(%id, "duplicate forecast removed");
|
|
}
|
|
Err(e) => return Err(e.into()),
|
|
}
|
|
}
|
|
tracing::info!(count = forecast_hashes.len(), "forecasts recalculated");
|
|
|
|
let deleted = sqlx::query("DELETE FROM analyses").execute(db).await?.rows_affected();
|
|
let mut queued = 0;
|
|
for c in &charts {
|
|
let language = locale(db, c.owner_id).await?;
|
|
let summary = Target::Summary { chart_id: c.id, hash: c.chart_hash.clone() };
|
|
queue(state, c.owner_id, &summary, &language, None).await?;
|
|
let natal = Target::Natal { chart_id: c.id, hash: c.chart_hash.clone() };
|
|
queue(state, c.owner_id, &natal, &language, None).await?;
|
|
queued += 2;
|
|
}
|
|
for (synastry_id, owner, hash) in synastry_hashes {
|
|
let target = Target::Synastry { synastry_id, hash };
|
|
queue(state, owner, &target, &locale(db, owner).await?, None).await?;
|
|
queued += 1;
|
|
}
|
|
for (forecast_id, owner, hash) in forecast_hashes {
|
|
let target = Target::Forecast { forecast_id, hash };
|
|
queue(state, owner, &target, &locale(db, owner).await?, None).await?;
|
|
queued += 1;
|
|
}
|
|
tracing::info!(deleted, queued, "analyses queued again");
|
|
Ok(())
|
|
}
|