|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +use chrono::{DateTime, Utc}; |
| 19 | +use model::site_explorer::SiteExplorerLastRun; |
| 20 | +use sqlx::{FromRow, PgConnection}; |
| 21 | + |
| 22 | +use crate::db_read::DbReader; |
| 23 | +use crate::{DatabaseError, DatabaseResult}; |
| 24 | + |
| 25 | +const LAST_RUN_ID: i16 = 1; |
| 26 | + |
| 27 | +#[derive(Debug, FromRow)] |
| 28 | +struct DbSiteExplorerLastRun { |
| 29 | + started_at: DateTime<Utc>, |
| 30 | + finished_at: DateTime<Utc>, |
| 31 | + success: bool, |
| 32 | + error: Option<String>, |
| 33 | + failure_category: Option<String>, |
| 34 | + endpoint_explorations: i64, |
| 35 | + endpoint_explorations_success: i64, |
| 36 | + endpoint_explorations_failed: i64, |
| 37 | + last_successful_finished_at: Option<DateTime<Utc>>, |
| 38 | + last_failed_finished_at: Option<DateTime<Utc>>, |
| 39 | +} |
| 40 | + |
| 41 | +impl From<DbSiteExplorerLastRun> for SiteExplorerLastRun { |
| 42 | + fn from(run: DbSiteExplorerLastRun) -> Self { |
| 43 | + Self { |
| 44 | + started_at: run.started_at, |
| 45 | + finished_at: run.finished_at, |
| 46 | + success: run.success, |
| 47 | + error: run.error, |
| 48 | + failure_category: run.failure_category, |
| 49 | + endpoint_explorations: run.endpoint_explorations, |
| 50 | + endpoint_explorations_success: run.endpoint_explorations_success, |
| 51 | + endpoint_explorations_failed: run.endpoint_explorations_failed, |
| 52 | + last_successful_finished_at: run.last_successful_finished_at, |
| 53 | + last_failed_finished_at: run.last_failed_finished_at, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Fetches metadata for the latest site explorer run. |
| 59 | +pub async fn fetch(db: impl DbReader<'_>) -> DatabaseResult<Option<SiteExplorerLastRun>> { |
| 60 | + let query = "SELECT started_at, finished_at, success, error, failure_category, endpoint_explorations, endpoint_explorations_success, endpoint_explorations_failed, last_successful_finished_at, last_failed_finished_at |
| 61 | + FROM site_explorer_run_status |
| 62 | + WHERE id = $1"; |
| 63 | + |
| 64 | + sqlx::query_as::<_, DbSiteExplorerLastRun>(query) |
| 65 | + .bind(LAST_RUN_ID) |
| 66 | + .fetch_optional(db) |
| 67 | + .await |
| 68 | + .map(|run| run.map(Into::into)) |
| 69 | + .map_err(|e| DatabaseError::query(query, e)) |
| 70 | +} |
| 71 | + |
| 72 | +/// Replaces metadata for the latest site explorer run. |
| 73 | +pub async fn upsert(txn: &mut PgConnection, last_run: &SiteExplorerLastRun) -> DatabaseResult<()> { |
| 74 | + let query = "INSERT INTO site_explorer_run_status ( |
| 75 | + id, |
| 76 | + started_at, |
| 77 | + finished_at, |
| 78 | + success, |
| 79 | + error, |
| 80 | + failure_category, |
| 81 | + endpoint_explorations, |
| 82 | + endpoint_explorations_success, |
| 83 | + endpoint_explorations_failed, |
| 84 | + last_successful_finished_at, |
| 85 | + last_failed_finished_at |
| 86 | +) |
| 87 | +VALUES ( |
| 88 | + $1, $2, $3, $4, $5, $6, $7, $8, $9, |
| 89 | + CASE WHEN $4 THEN $3 ELSE $10 END, |
| 90 | + CASE WHEN NOT $4 THEN $3 ELSE $11 END |
| 91 | +) |
| 92 | +ON CONFLICT (id) DO UPDATE SET |
| 93 | + started_at = EXCLUDED.started_at, |
| 94 | + finished_at = EXCLUDED.finished_at, |
| 95 | + success = EXCLUDED.success, |
| 96 | + error = EXCLUDED.error, |
| 97 | + failure_category = EXCLUDED.failure_category, |
| 98 | + endpoint_explorations = EXCLUDED.endpoint_explorations, |
| 99 | + endpoint_explorations_success = EXCLUDED.endpoint_explorations_success, |
| 100 | + endpoint_explorations_failed = EXCLUDED.endpoint_explorations_failed, |
| 101 | + last_successful_finished_at = CASE |
| 102 | + WHEN EXCLUDED.success THEN EXCLUDED.finished_at |
| 103 | + ELSE COALESCE( |
| 104 | + EXCLUDED.last_successful_finished_at, |
| 105 | + site_explorer_run_status.last_successful_finished_at |
| 106 | + ) |
| 107 | + END, |
| 108 | + last_failed_finished_at = CASE |
| 109 | + WHEN NOT EXCLUDED.success THEN EXCLUDED.finished_at |
| 110 | + ELSE COALESCE( |
| 111 | + EXCLUDED.last_failed_finished_at, |
| 112 | + site_explorer_run_status.last_failed_finished_at |
| 113 | + ) |
| 114 | + END"; |
| 115 | + |
| 116 | + sqlx::query(query) |
| 117 | + .bind(LAST_RUN_ID) |
| 118 | + .bind(last_run.started_at) |
| 119 | + .bind(last_run.finished_at) |
| 120 | + .bind(last_run.success) |
| 121 | + .bind(&last_run.error) |
| 122 | + .bind(&last_run.failure_category) |
| 123 | + .bind(last_run.endpoint_explorations) |
| 124 | + .bind(last_run.endpoint_explorations_success) |
| 125 | + .bind(last_run.endpoint_explorations_failed) |
| 126 | + .bind(last_run.last_successful_finished_at) |
| 127 | + .bind(last_run.last_failed_finished_at) |
| 128 | + .execute(txn) |
| 129 | + .await |
| 130 | + .map_err(|e| DatabaseError::query(query, e))?; |
| 131 | + |
| 132 | + Ok(()) |
| 133 | +} |
0 commit comments