Skip to content

Commit

Permalink
Faster activity sync WIP 2/?
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Oct 18, 2024
1 parent 7ef7937 commit 4091079
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
20 changes: 11 additions & 9 deletions vzdv-tasks/src/activity.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -105,19 +105,23 @@ async fn update_single_activity(
db: &Pool<Sqlite>,
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) {
continue;
}
counter += session.minutes_on_callsign.parse::<f32>().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}"))?;
Expand All @@ -138,19 +142,17 @@ pub async fn update_online_controller_activity(config: &Config, db: &Pool<Sqlite
let vatsim = vatsim_utils::live_api::Vatsim::new().await?;
vatsim.get_v3_data().await?.controllers
};
let start_of_month = chrono::Utc::now()
.checked_sub_months(Months::new(1))
.unwrap()
.format("%Y-%m-%d")
.to_string();
let start_of_month = chrono::Utc::now().format("%Y-%m-01").to_string();

for controller in online_controllers {
let cid = controller.cid;
if !on_roster_cids.contains(&cid) {
continue;
}
debug!("Spot-updating activity for {cid}");
if let Err(e) = update_single_activity(config, db, &start_of_month, cid).await {
if let Err(e) =
update_single_activity(config, db, &start_of_month, cid, &controller.logon_time).await
{
error!("Error spot-updating CID {cid}: {e}")
}
// wait a second to be nice to the VATSIM API
Expand Down
11 changes: 5 additions & 6 deletions vzdv-tasks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async fn main() {
let roster_handle = {
let db = db.clone();
tokio::spawn(async move {
debug!("Waiting 10 seconds before starting roster sync");
time::sleep(Duration::from_secs(10)).await;
debug!("Waiting 15 seconds before starting roster sync");
time::sleep(Duration::from_secs(15)).await;
loop {
info!("Querying roster");
match roster::update_roster(&db).await {
Expand All @@ -59,10 +59,9 @@ async fn main() {
let config = config.clone();
let db = db.clone();
tokio::spawn(async move {
debug!("Waiting 60 seconds before starting activity sync");
time::sleep(Duration::from_secs(60)).await;
// FIXME to 0
for index in 1u64.. {
debug!("Waiting 30 seconds before starting activity sync");
time::sleep(Duration::from_secs(30)).await;
for index in 0u64.. {
/*
* Update everyone on a 6 hour schedule (15 minutes * 24 ticks = 6 hours).
* This update makes sure that everyone's data is accurate.
Expand Down

0 comments on commit 4091079

Please sign in to comment.