From 698e55c891c431d7c036a946e4519456b5f66c33 Mon Sep 17 00:00:00 2001 From: Celeo Date: Wed, 3 Jul 2024 19:04:34 -0700 Subject: [PATCH] Move that to a util file --- src/endpoints/homepage.rs | 46 +++------------------------------ src/utils/mod.rs | 1 + src/utils/vatsim.rs | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 src/utils/vatsim.rs diff --git a/src/endpoints/homepage.rs b/src/endpoints/homepage.rs index cdb9bb8..742f82a 100644 --- a/src/endpoints/homepage.rs +++ b/src/endpoints/homepage.rs @@ -3,16 +3,15 @@ use crate::{ shared::{AppError, AppState, CacheEntry, UserInfo, SESSION_USER_INFO_KEY}, utils::{ - flashed_messages, get_controller_cids_and_names, parse_metar, parse_vatsim_timestamp, - position_in_facility_airspace, GENERAL_HTTP_CLIENT, + flashed_messages, parse_metar, vatsim::get_online_facility_controllers, GENERAL_HTTP_CLIENT, }, }; use anyhow::{anyhow, Result}; use axum::{extract::State, response::Html, routing::get, Router}; -use log::{error, warn}; +use log::warn; use minijinja::{context, Environment}; use serde::Serialize; -use std::{collections::HashMap, sync::Arc, time::Instant}; +use std::{sync::Arc, time::Instant}; use tower_sessions::Session; use vatsim_utils::live_api::Vatsim; @@ -32,14 +31,6 @@ async fn page_home( async fn snippet_online_controllers( State(state): State>, ) -> Result, AppError> { - #[derive(Serialize)] - struct OnlineController { - cid: u64, - callsign: String, - name: String, - online_for: String, - } - // cache this endpoint's returned data for 60 seconds let cache_key = "ONLINE_CONTROLLERS"; if let Some(cached) = state.cache.get(&cache_key) { @@ -50,36 +41,7 @@ async fn snippet_online_controllers( state.cache.invalidate(&cache_key); } - let cid_name_map = match get_controller_cids_and_names(&state.db).await { - Ok(map) => map, - Err(e) => { - error!("Error generating controller CID -> name map: {e}"); - HashMap::new() - } - }; - - let now = chrono::Utc::now(); - let data = Vatsim::new().await?.get_v3_data().await?; - let online: Vec<_> = data - .controllers - .iter() - .filter(|controller| position_in_facility_airspace(&state.config, &controller.callsign)) - .map(|controller| { - let logon = parse_vatsim_timestamp(&controller.logon_time) - .expect("Could not parse VATSIM timestamp"); - let seconds = (now - logon).num_seconds() as u32; - OnlineController { - cid: controller.cid, - callsign: controller.callsign.clone(), - name: cid_name_map - .get(&controller.cid) - .map(|s| format!("{} {}", s.0, s.1)) - .unwrap_or(String::from("?")), - online_for: format!("{}h{}m", seconds / 3600, (seconds / 60) % 60), - } - }) - .collect(); - + let online = get_online_facility_controllers(&state.db, &state.config).await?; let template = state .templates .get_template("homepage/online_controllers")?; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 71217ed..50f7556 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -14,6 +14,7 @@ use std::collections::HashMap; pub mod auth; pub mod flashed_messages; +pub mod vatsim; pub mod vatusa; // I don't know what this is, but there's a SUP in ZDV that has this rating. diff --git a/src/utils/vatsim.rs b/src/utils/vatsim.rs new file mode 100644 index 0000000..9d103de --- /dev/null +++ b/src/utils/vatsim.rs @@ -0,0 +1,54 @@ +use super::{parse_vatsim_timestamp, position_in_facility_airspace}; +use crate::{shared::Config, utils::get_controller_cids_and_names}; +use anyhow::Result; +use log::error; +use serde::Serialize; +use sqlx::SqlitePool; +use std::collections::HashMap; +use vatsim_utils::live_api::Vatsim; + +#[derive(Debug, Serialize)] +pub struct OnlineController { + pub cid: u64, + pub callsign: String, + pub name: String, + pub online_for: String, +} + +/// Get facility controllers currently online. +pub async fn get_online_facility_controllers( + db: &SqlitePool, + config: &Config, +) -> Result> { + let cid_name_map = match get_controller_cids_and_names(db).await { + Ok(map) => map, + Err(e) => { + error!("Error generating controller CID -> name map: {e}"); + HashMap::new() + } + }; + + let now = chrono::Utc::now(); + let data = Vatsim::new().await?.get_v3_data().await?; + let online: Vec<_> = data + .controllers + .iter() + .filter(|controller| position_in_facility_airspace(config, &controller.callsign)) + .map(|controller| { + let logon = parse_vatsim_timestamp(&controller.logon_time) + .expect("Could not parse VATSIM timestamp"); + let seconds = (now - logon).num_seconds() as u32; + OnlineController { + cid: controller.cid, + callsign: controller.callsign.clone(), + name: cid_name_map + .get(&controller.cid) + .map(|s| format!("{} {}", s.0, s.1)) + .unwrap_or(String::from("?")), + online_for: format!("{}h{}m", seconds / 3600, (seconds / 60) % 60), + } + }) + .collect(); + + Ok(online) +}