Skip to content

Commit

Permalink
Move that to a util file
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Jul 4, 2024
1 parent 7e012dd commit 698e55c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
46 changes: 4 additions & 42 deletions src/endpoints/homepage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,14 +31,6 @@ async fn page_home(
async fn snippet_online_controllers(
State(state): State<Arc<AppState>>,
) -> Result<Html<String>, 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) {
Expand All @@ -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")?;
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 54 additions & 0 deletions src/utils/vatsim.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<OnlineController>> {
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)
}

0 comments on commit 698e55c

Please sign in to comment.