Refactor poll to use the time_entry based approach from now on
This commit is contained in:
parent
889859dbae
commit
62657f2bdd
54
src/poll.rs
54
src/poll.rs
@ -1,14 +1,21 @@
|
|||||||
use crate::toggl_api::TogglApiClient;
|
use crate::entity::prelude::TimeEntry;
|
||||||
use crate::entity::{client, project};
|
use crate::entity::{client, project};
|
||||||
use crate::toggl_api::types::{Project, Client, TogglReportQuery};
|
use crate::toggl_api::types::{Client, Project, TogglReportQuery};
|
||||||
use sea_orm::{DatabaseConnection, EntityTrait, QuerySelect};
|
use crate::toggl_api::TogglApiClient;
|
||||||
use tracing::instrument;
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use crate::utils::day_exclusivity_condition;
|
use crate::utils::day_exclusivity_condition;
|
||||||
|
use chrono::{DateTime, FixedOffset};
|
||||||
|
use sea_orm::DatabaseBackend::Postgres;
|
||||||
|
use sea_orm::{
|
||||||
|
ConnectionTrait, DatabaseConnection, EntityOrSelect, EntityTrait, QueryOrder, QuerySelect,
|
||||||
|
Statement,
|
||||||
|
};
|
||||||
|
use std::ops::Sub;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
#[tracing::instrument(skip(client, db))]
|
#[tracing::instrument(skip(client, db))]
|
||||||
pub async fn poll_job(client: TogglApiClient, db: DatabaseConnection, poll_period: u64) {
|
pub async fn poll_job(client: TogglApiClient, db: DatabaseConnection, poll_period: u64) {
|
||||||
// Every 2h, poll the Toggl API for new time entries for today to cache them in the database
|
// Periodically poll the Toggl API for new time entries for today to cache them in the database
|
||||||
let period = tokio::time::Duration::from_secs(poll_period);
|
let period = tokio::time::Duration::from_secs(poll_period);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -35,18 +42,22 @@ pub async fn perform_poll(
|
|||||||
client: &TogglApiClient,
|
client: &TogglApiClient,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> utils::Result<usize> {
|
) -> utils::Result<usize> {
|
||||||
let now = chrono::Utc::now();
|
let since = db
|
||||||
let today_string = now
|
.query_one(Statement::from_string(
|
||||||
.date_naive()
|
Postgres,
|
||||||
.format("%Y-%m-%d")
|
r#"select server_updated_at from time_entry order by server_updated_at desc limit 1"#,
|
||||||
.to_string();
|
))
|
||||||
|
.await?
|
||||||
|
.map(|row| row.try_get_by::<DateTime<FixedOffset>, _>("server_updated_at"))
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(
|
||||||
|
chrono::Utc::now()
|
||||||
|
.sub(chrono::Duration::days(1))
|
||||||
|
.fixed_offset(),
|
||||||
|
);
|
||||||
|
|
||||||
let report = client
|
let time_entries = client
|
||||||
.full_report(&TogglReportQuery {
|
.fetch_time_entries_modified_since(since.to_utc())
|
||||||
start_date: Some(today_string.clone()),
|
|
||||||
end_date: Some(today_string.clone()),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let existing_project_ids = project::Entity::find()
|
let existing_project_ids = project::Entity::find()
|
||||||
@ -56,7 +67,7 @@ pub async fn perform_poll(
|
|||||||
.all(db)
|
.all(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let new_projects = report
|
let new_projects = time_entries
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|entry| entry.project_id)
|
.filter_map(|entry| entry.project_id)
|
||||||
.any(|project_id| !existing_project_ids.contains(&(project_id as i64)));
|
.any(|project_id| !existing_project_ids.contains(&(project_id as i64)));
|
||||||
@ -77,12 +88,13 @@ pub async fn perform_poll(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::routes::cache_report(
|
crate::sync_service::cache_report(
|
||||||
&db,
|
&db,
|
||||||
&report,
|
&report,
|
||||||
Some(
|
Some(day_exclusivity_condition(
|
||||||
day_exclusivity_condition(now.date_naive(), now.date_naive()),
|
now.date_naive(),
|
||||||
),
|
now.date_naive(),
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(report.len())
|
Ok(report.len())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user