From 3f17c452978a66615fe769c26238eb22f7dd6a86 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sat, 27 Jul 2024 18:32:40 +0100 Subject: [PATCH] Allow zero time_entries --- src/main.rs | 10 +++++++--- src/toggl/mod.rs | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b94f0c..35e0d68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,13 +53,14 @@ impl Worker { } async fn update(&mut self, default_look_back: TimeDelta) -> Result<(), AppError> { let result = sqlx::query!("select max(updated_at) as last_updated_at from time_entries") - .fetch_one(&mut self.db) + .fetch_optional(&mut self.db) .await .expect("Could not fetch max updated_at from time_entries"); let existing_ids = self.get_ids().await?; - let fetch_since = result.last_updated_at + let fetch_since = result + .and_then(|record| record.last_updated_at) .unwrap_or_else(|| Utc::now() - default_look_back); let time_entries = self.toggl_api @@ -291,6 +292,9 @@ async fn main() { dotenv::dotenv() .expect("Failed to load .env file"); + // Init tracing + tracing_subscriber::fmt::init(); + let api = TogglApi::new( sensitive::API_TOKEN, sensitive::WORKSPACE_ID, @@ -304,7 +308,7 @@ async fn main() { toggl_api: api, }; - worker.update(TimeDelta::days(7)) + worker.update(TimeDelta::days(90)) .await .unwrap(); } diff --git a/src/toggl/mod.rs b/src/toggl/mod.rs index 44f5f9d..9d4d78d 100644 --- a/src/toggl/mod.rs +++ b/src/toggl/mod.rs @@ -30,6 +30,8 @@ impl ReqwestRateLimiter { #[async_trait] impl reqwest_ratelimit::RateLimiter for ReqwestRateLimiter { async fn acquire_permit(&self) { + // We don't need to introduce jitter here as that is handled by the retry_request + // middleware. self.rate_limiter.until_ready().await; } } @@ -74,6 +76,7 @@ impl TogglApi { } /// Get the workspaces that a user is a part of + #[tracing::instrument(skip(self))] async fn get_users_workspaces( &self ) -> Result, TogglError> { @@ -90,6 +93,7 @@ impl TogglApi { } /// Get a specific workspace by its ID + #[tracing::instrument(skip(self))] pub async fn get_workspace(&self, id: u64) -> Result { let url = format!( "{base_url}/workspaces/{id}", @@ -104,6 +108,7 @@ impl TogglApi { /// Fetches all time entries for this user from Toggl that have been modified since the given /// date. + #[tracing::instrument(skip(self))] pub async fn get_time_entries_for_user_modified_since(&self, since: DateTime) -> Result, TogglError> { let url = format!( "{base_url}/me/time_entries?since={since}", @@ -118,6 +123,7 @@ impl TogglApi { /// Fetches all time entries for this user from Toggl that have a start time between the given /// start and end times. + #[tracing::instrument(skip(self))] pub async fn get_time_user_entries_between(&self, start: DateTime, until: DateTime) -> Result, TogglError> { let url = format!( "{base_url}/me/time_entries", @@ -133,6 +139,7 @@ impl TogglApi { .send().await?).await } + #[tracing::instrument(skip(self))] pub async fn get_current_time_entry(&self) -> Result, TogglError> { let url = format!( "{base_url}/me/time_entries/current", @@ -146,6 +153,7 @@ impl TogglApi { .json().await?) } + #[tracing::instrument(skip(self))] pub async fn get_projects(&self) -> Result, TogglError> { let url = format!( "{base_url}/workspaces/{workspace_id}/projects", @@ -158,6 +166,7 @@ impl TogglApi { .send().await?).await } + #[tracing::instrument(skip(self))] pub async fn get_clients(&self) -> Result, TogglError> { let url = format!( "{base_url}/workspaces/{workspace_id}/clients", @@ -183,6 +192,7 @@ impl TogglApi { Ok(result) } + #[tracing::instrument(skip(self))] pub async fn get_tags(&self) -> Result, TogglError> { let url = format!( "{base_url}/workspaces/{workspace_id}/tags", @@ -201,6 +211,7 @@ impl TogglApi { filters } + #[tracing::instrument(skip(self))] pub async fn search(&self, filters: types::TogglReportFilters) -> Result, TogglError> { let url = format!( "{base_url}/workspace/{workspace_id}/search/time_entries",