//! Admin: the error log of the analyses, answers and prompt test runs (every //! failed attempt, written by the worker), newest first. Entries can be //! deleted one by one or all that match a filter. use axum::Json; use axum::extract::{Path, Query, State}; use axum::http::StatusCode; use axum_extra::extract::WithRejection; use serde::Deserialize; use serde_json::{Value, json}; use time::OffsetDateTime; use uuid::Uuid; use super::St; use crate::auth::AdminUser; use crate::error::{ApiError, ApiResult}; use crate::views::rfc3339; #[derive(Deserialize)] #[serde(deny_unknown_fields)] pub struct ErrorQuery { limit: Option, offset: Option, /// `analysis`, `question` or `preview`. source: Option, /// `retry`, `fallback` or `failed`. outcome: Option, } type Row = (i64, OffsetDateTime, String, Uuid, String, String, i32, String, String, Option, Option); pub async fn list( State(state): St, _admin: AdminUser, WithRejection(Query(query), _): WithRejection, ApiError>, ) -> ApiResult> { let limit = query.limit.unwrap_or(100).clamp(1, 500); let offset = query.offset.unwrap_or(0).max(0); // Whose text it was: the requester of the analysis or the asker. let rows: Vec = sqlx::query_as( "SELECT e.id, e.created_at, e.source, e.item_id, e.kind, e.model, e.attempt, e.outcome, e.error, e.seconds, \ u.email \ FROM generation_errors e \ LEFT JOIN analyses a ON e.source = 'analysis' AND a.id = e.item_id \ LEFT JOIN questions q ON e.source = 'question' AND q.id = e.item_id \ LEFT JOIN prompt_previews p ON e.source = 'preview' AND p.id = e.item_id \ LEFT JOIN users u ON u.id = coalesce(a.requested_by, q.asked_by, p.created_by) \ WHERE ($1::text IS NULL OR e.source = $1) AND ($2::text IS NULL OR e.outcome = $2) \ ORDER BY e.id DESC LIMIT $3 OFFSET $4", ) .bind(&query.source) .bind(&query.outcome) .bind(limit) .bind(offset) .fetch_all(&state.db) .await?; let (total, last_day): (i64, i64) = sqlx::query_as( "SELECT count(*), count(*) FILTER (WHERE created_at > now() - interval '1 day') FROM generation_errors \ WHERE ($1::text IS NULL OR source = $1) AND ($2::text IS NULL OR outcome = $2)", ) .bind(&query.source) .bind(&query.outcome) .fetch_one(&state.db) .await?; let errors: Vec = rows .into_iter() .map(|(id, at, source, item, kind, model, attempt, outcome, error, seconds, email)| { json!({ "id": id, "created_at": rfc3339(at), "source": source, "item_id": item, "kind": kind, "model": model, "attempt": attempt, "outcome": outcome, "error": error, "seconds": seconds, "user": email, }) }) .collect(); Ok(Json(json!({ "errors": errors, "total": total, "last_day": last_day, "limit": limit, "offset": offset }))) } #[derive(Deserialize)] #[serde(deny_unknown_fields)] pub struct DeleteQuery { source: Option, outcome: Option, } /// Deletes every entry matching the filter (none given: all of them). pub async fn delete_matching( State(state): St, AdminUser(admin): AdminUser, WithRejection(Query(query), _): WithRejection, ApiError>, ) -> ApiResult> { let deleted = sqlx::query( "DELETE FROM generation_errors WHERE ($1::text IS NULL OR source = $1) AND ($2::text IS NULL OR outcome = $2)", ) .bind(&query.source) .bind(&query.outcome) .execute(&state.db) .await? .rows_affected(); tracing::info!(admin = %admin.id(), deleted, "error log entries deleted"); Ok(Json(json!({ "deleted": deleted }))) } /// Deletes one entry. pub async fn delete_one( State(state): St, _admin: AdminUser, WithRejection(Path(id), _): WithRejection, ApiError>, ) -> ApiResult { let result = sqlx::query("DELETE FROM generation_errors WHERE id = $1").bind(id).execute(&state.db).await?; if result.rows_affected() == 0 { return Err(ApiError::NotFound("error")); } Ok(StatusCode::NO_CONTENT) }