Use localtime for active session + don't split into days
This commit is contained in:
parent
19cb4626e4
commit
312488c1a6
12
src/main.rs
12
src/main.rs
|
@ -133,18 +133,13 @@ async fn get_since(pool: &deadpool_diesel::sqlite::Pool, start: Option<NaiveDate
|
|||
}
|
||||
|
||||
fn format_duration(total_seconds: i64) -> String {
|
||||
let days = total_seconds / (24 * 3600);
|
||||
let hours = (total_seconds % (24 * 3600)) / 3600;
|
||||
let hours = total_seconds / 3600;
|
||||
let minutes = (total_seconds % 3600) / 60;
|
||||
let seconds = total_seconds % 60;
|
||||
|
||||
let mut parts = Vec::new();
|
||||
|
||||
if days > 0 { parts.push(format!("{}d", days)); }
|
||||
if hours > 0 { parts.push(format!("{}h", hours)); }
|
||||
if minutes > 0 { parts.push(format!("{}m", minutes)); }
|
||||
if seconds > 0 || parts.is_empty() { parts.push(format!("{}s", seconds)); }
|
||||
|
||||
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((
|
||||
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)?;
|
||||
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>)> {
|
||||
let conn = pool.get().await.map_err(internal_error)?;
|
||||
// insert your application logic here
|
||||
let res = conn
|
||||
.interact(move |conn| {
|
||||
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)?;
|
||||
|
||||
// this will be converted into a JSON response
|
||||
// with a status code of `201 Created`
|
||||
Ok((StatusCode::CREATED, Json(res)))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue