From 4091079889fe18275162c16b031f8dda96285eab Mon Sep 17 00:00:00 2001 From: Celeo Date: Fri, 18 Oct 2024 14:59:53 -0700 Subject: [PATCH] Faster activity sync WIP 2/? --- vzdv-tasks/src/activity.rs | 20 +++++++++++--------- vzdv-tasks/src/main.rs | 11 +++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/vzdv-tasks/src/activity.rs b/vzdv-tasks/src/activity.rs index 9fae26b..e45682a 100644 --- a/vzdv-tasks/src/activity.rs +++ b/vzdv-tasks/src/activity.rs @@ -1,7 +1,7 @@ //! Update activity from VATSIM. use anyhow::{Context, Result}; -use chrono::Months; +use chrono::{DateTime, Months, Utc}; use log::{debug, error}; use sqlx::{Pool, Row, Sqlite}; use std::{collections::HashMap, time::Duration}; @@ -105,8 +105,9 @@ async fn update_single_activity( db: &Pool, start_of_month: &str, cid: u64, + logon_time: &str, ) -> Result<()> { - let sessions = rest_api::get_atc_sessions(cid, None, None, None, Some(start_of_month)).await?; + let sessions = rest_api::get_atc_sessions(cid, None, None, Some(start_of_month), None).await?; let mut counter = 0.0; for session in sessions.results { if !position_in_facility_airspace(config, &session.callsign) { @@ -114,10 +115,13 @@ async fn update_single_activity( } counter += session.minutes_on_callsign.parse::().unwrap() * 60.0; } + let logon_time = DateTime::parse_from_rfc3339(logon_time)?.timestamp(); + let online_seconds = Utc::now().timestamp() - logon_time; + let minutes = ((counter + online_seconds as f32) / 60.0).round() as u32; sqlx::query(sql::UPDATE_ACTIVITY) .bind(cid as u32) .bind(start_of_month[0..7].to_string()) - .bind(counter) + .bind(minutes) .execute(db) .await .with_context(|| format!("Updating CID {cid}"))?; @@ -138,11 +142,7 @@ pub async fn update_online_controller_activity(config: &Config, db: &Pool