Use localtime for active session + don't split into days

This commit is contained in:
Thomas Avé 2024-11-08 23:58:40 +01:00
parent 19cb4626e4
commit 312488c1a6
1 changed files with 2 additions and 10 deletions

View File

@ -133,18 +133,13 @@ async fn get_since(pool: &deadpool_diesel::sqlite::Pool, start: Option<NaiveDate
} }
fn format_duration(total_seconds: i64) -> String { fn format_duration(total_seconds: i64) -> String {
let days = total_seconds / (24 * 3600); let hours = total_seconds / 3600;
let hours = (total_seconds % (24 * 3600)) / 3600;
let minutes = (total_seconds % 3600) / 60; let minutes = (total_seconds % 3600) / 60;
let seconds = total_seconds % 60;
let mut parts = Vec::new(); let mut parts = Vec::new();
if days > 0 { parts.push(format!("{}d", days)); }
if hours > 0 { parts.push(format!("{}h", hours)); } if hours > 0 { parts.push(format!("{}h", hours)); }
if minutes > 0 { parts.push(format!("{}m", minutes)); } if minutes > 0 { parts.push(format!("{}m", minutes)); }
if seconds > 0 || parts.is_empty() { parts.push(format!("{}s", seconds)); }
parts.join(" ") parts.join(" ")
} }
@ -155,7 +150,7 @@ async fn get_metrics(State(pool): State<deadpool_diesel::sqlite::Pool>, seconds:
let active_sessions = conn.interact(|conn| work_periods::table.filter(work_periods::end_time.is_null()).select(( let active_sessions = conn.interact(|conn| work_periods::table.filter(work_periods::end_time.is_null()).select((
work_periods::start_time, work_periods::start_time,
sql::<sql_types::Timestamp>("COALESCE(end_time, datetime('now'))") sql::<sql_types::Timestamp>("COALESCE(end_time, datetime('now', 'localtime'))")
)).load::<(NaiveDateTime, NaiveDateTime)>(conn)).await.map_err(internal_error)?.map_err(internal_error)?; )).load::<(NaiveDateTime, NaiveDateTime)>(conn)).await.map_err(internal_error)?.map_err(internal_error)?;
let mut today = current_time.date_naive(); let mut today = current_time.date_naive();
@ -228,7 +223,6 @@ async fn start_tracking(State(pool): State<deadpool_diesel::sqlite::Pool>, Json(
async fn add_period(State(pool): State<deadpool_diesel::sqlite::Pool>, Json(payload): Json<NewPeriod>) -> Result<(StatusCode, Json<WorkPeriod>), (StatusCode, Json<Error>)> { async fn add_period(State(pool): State<deadpool_diesel::sqlite::Pool>, Json(payload): Json<NewPeriod>) -> Result<(StatusCode, Json<WorkPeriod>), (StatusCode, Json<Error>)> {
let conn = pool.get().await.map_err(internal_error)?; let conn = pool.get().await.map_err(internal_error)?;
// insert your application logic here
let res = conn let res = conn
.interact(move |conn| { .interact(move |conn| {
diesel::insert_into(work_periods::table) diesel::insert_into(work_periods::table)
@ -240,8 +234,6 @@ async fn add_period(State(pool): State<deadpool_diesel::sqlite::Pool>, Json(payl
.map_err(internal_error)? .map_err(internal_error)?
.map_err(internal_error)?; .map_err(internal_error)?;
// this will be converted into a JSON response
// with a status code of `201 Created`
Ok((StatusCode::CREATED, Json(res))) Ok((StatusCode::CREATED, Json(res)))
} }