From 004def8fbbf22c6ebe41657ef91e6f9b54e65c27 Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Sat, 10 Dec 2022 22:01:27 -0500 Subject: [PATCH] update health to be more helpful --- Cargo.lock | 27 ++++++++++++- services/api/Cargo.toml | 7 ++-- services/api/src/health.rs | 63 ++++++++++++++++++++++++++++++- services/api/src/html/index.html | 18 ++++++--- services/api/src/html/ingest.html | 19 ++++++++++ services/api/src/main.rs | 5 +++ services/websocket/src/main.rs | 11 ++++-- 7 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 services/api/src/html/ingest.html diff --git a/Cargo.lock b/Cargo.lock index 321c5d9..7935f7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,7 @@ dependencies = [ "async-graphql", "async-graphql-axum", "axum", + "chrono", "lazy_static", "reqwest", "serde", @@ -76,6 +77,7 @@ dependencies = [ "async-trait", "base64", "bytes", + "chrono", "fast_chemail", "fnv", "futures-util", @@ -329,8 +331,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" dependencies = [ "iana-time-zone", + "js-sys", "num-integer", "num-traits", + "time", + "wasm-bindgen", "winapi", ] @@ -744,7 +749,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -1141,7 +1146,7 @@ checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.42.0", ] @@ -1843,6 +1848,7 @@ dependencies = [ "bitflags", "byteorder", "bytes", + "chrono", "crc", "crossbeam-queue", "dirs", @@ -2038,6 +2044,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2419,6 +2436,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/services/api/Cargo.toml b/services/api/Cargo.toml index 08997d5..178d5a7 100644 --- a/services/api/Cargo.toml +++ b/services/api/Cargo.toml @@ -8,11 +8,12 @@ edition = "2021" [dependencies] serde_json = "1.0.89" serde = "1.0.149" -async-graphql = { version = "5.0.3" } +async-graphql = { version = "5.0.3", features = ["chrono"] } async-graphql-axum = "5.0.3" axum = "0.6.1" -sqlx = { version = "0.6.2", features = [ "runtime-tokio-native-tls", "postgres" ] } +sqlx = { version = "0.6.2", features = [ "runtime-tokio-native-tls", "postgres", "chrono" ] } tokio = { version = "1.23.0", features = [ "full" ] } tower-http = { version = "0.3.5", features = ["cors"] } lazy_static = "1.4.0" -reqwest = "0.11.13" \ No newline at end of file +reqwest = "0.11.13" +chrono = "0.4.23" \ No newline at end of file diff --git a/services/api/src/health.rs b/services/api/src/health.rs index 84eca16..66a4c8c 100644 --- a/services/api/src/health.rs +++ b/services/api/src/health.rs @@ -1,5 +1,7 @@ -use async_graphql::{Context, Enum, Object}; +use crate::utils::ID_TO_WORLD; +use async_graphql::{Context, Enum, Object, SimpleObject}; use axum::{http::StatusCode, response::IntoResponse, Extension, Json}; +use chrono::{DateTime, Utc}; use sqlx::{query, Pool, Postgres, Row}; pub async fn get_health(Extension(pool): Extension>) -> impl IntoResponse { @@ -53,6 +55,37 @@ enum UpDown { pub struct Health {} +impl Health { + async fn most_recent_event_time<'ctx>( + &self, + ctx: &Context<'ctx>, + world_id: i32, + ) -> (UpDown, Option>) { + let pool = ctx.data::>().unwrap(); + + let events_resp = + query("SELECT time FROM analytics WHERE world_id = $1 ORDER BY time DESC LIMIT 1") + .bind(world_id) + .fetch_one(pool) + .await; + + match events_resp { + Ok(row) => { + let last_event: DateTime = row.get(0); + + if last_event < Utc::now() - chrono::Duration::minutes(5) { + return (UpDown::Down, None); + } else { + return (UpDown::Up, Some(last_event)); + } + } + Err(_) => { + return (UpDown::Down, None); + } + } + } +} + /// Reports on the health of Saerro Listening Post #[Object] impl Health { @@ -104,6 +137,34 @@ impl Health { .map(|_| UpDown::Up) .unwrap_or(UpDown::Down) } + + /// Shows a disclaimer for the worlds check + async fn worlds_disclaimer(&self) -> String { + "This is a best-effort check. A world reports `DOWN` when it doesn't have new events for 5 minutes. It could be broken, it could be the reality of the game state.".to_string() + } + + /// Checks if a world has had any events for the last 5 minutes + async fn worlds<'ctx>(&self, ctx: &Context<'ctx>) -> Vec { + let mut worlds = Vec::new(); + for (id, name) in ID_TO_WORLD.iter() { + let (status, last_event) = self.most_recent_event_time(ctx, *id).await; + worlds.push(WorldUpDown { + id: *id, + name: name.to_string(), + status, + last_event, + }); + } + worlds + } +} + +#[derive(SimpleObject)] +struct WorldUpDown { + id: i32, + name: String, + status: UpDown, + last_event: Option>, } #[derive(Default)] diff --git a/services/api/src/html/index.html b/services/api/src/html/index.html index 0677a47..70e807e 100644 --- a/services/api/src/html/index.html +++ b/services/api/src/html/index.html @@ -48,7 +48,7 @@
  • Current system status (

    For help, please contact us in #api-dev on the PlanetSide 2 Discord.

    - [github] [pstop] + [ingest stats] [github] [pstop]