Allow zero time_entries

This commit is contained in:
Joshua Coles 2024-07-27 18:32:40 +01:00
parent a72acbc42f
commit 3f17c45297
2 changed files with 18 additions and 3 deletions

View File

@ -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();
}

View File

@ -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<Vec<types::Workspace>, 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<types::Workspace, TogglError> {
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<Utc>) -> Result<Vec<types::TimeEntry>, 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<Utc>, until: DateTime<Utc>) -> Result<Vec<types::TimeEntry>, 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<Option<types::TimeEntry>, 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<Vec<types::Project>, 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<Vec<types::TrackingClient>, 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<Vec<types::Tag>, 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<Vec<types::ReportEntry>, TogglError> {
let url = format!(
"{base_url}/workspace/{workspace_id}/search/time_entries",