This commit is contained in:
Mateusz Gruszczyński
2026-08-23 23:32:26 +02:00
parent b23578a0c8
commit f569b7db14
42 changed files with 597 additions and 4104 deletions
+21
View File
@@ -55,6 +55,7 @@ pub fn router(state: AppState) -> Router {
.route("/api/automations", get(list_automations).post(create_automation))
.route("/api/automations/:id", get(get_automation).put(update_automation).delete(delete_automation))
.route("/api/readings", get(readings))
.route("/api/history", get(history))
.route("/api/events", get(events))
.route("/api/settings", get(get_settings).put(update_settings))
.route("/api/access-tokens", get(list_access_tokens).post(create_access_token))
@@ -730,6 +731,26 @@ async fn readings(State(state): State<AppState>, Query(query): Query<ReadingsQue
Ok(Json(json!({"readings": values})))
}
#[derive(Debug, Deserialize)]
struct HistoryQuery { zone_id: Option<String>, hours: Option<i64>, limit: Option<u32> }
async fn history(State(state): State<AppState>, Query(query): Query<HistoryQuery>) -> Result<Json<Value>, AppError> {
let hours = query.hours.unwrap_or(24).clamp(1, 24 * 31);
let zone_id = query.zone_id.as_deref().filter(|value| !value.is_empty() && *value != "all");
if let Some(zone_id) = zone_id {
if state.db.get_zone(zone_id)?.is_none() {
return Err(AppError::NotFound(format!("zone {zone_id}")));
}
}
let bucket_seconds = match hours {
1..=6 => 30,
7..=24 => 120,
25..=168 => 600,
_ => 1800,
};
let values = state.db.list_zone_history(zone_id, Utc::now() - ChronoDuration::hours(hours), bucket_seconds, query.limit.unwrap_or(12_000))?;
Ok(Json(json!({"readings": values, "bucket_seconds": bucket_seconds})))
}
#[derive(Debug, Deserialize)]
struct EventsQuery { limit: Option<u32> }
async fn events(State(state): State<AppState>, Query(query): Query<EventsQuery>) -> Result<Json<Value>, AppError> {