[ENG-635] Job time estimation (#842)

* fix extension case sensitivity

* job time estimation wip

* update schema

* use `DateTime` to handle estimated completion time

* use a date with `dayjs`

* remove old migrations

* update migrations

* remove dead code

* Quick tweaks

* unused import

---------

Co-authored-by: brxken128 <77554505+brxken128@users.noreply.github.com>
Co-authored-by: ameer2468 <33054370+ameer2468@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2023-05-23 22:21:52 +00:00
1 parent da2e78dc61
commit ea46e7736a
9 files changed
+157 -106

No files matched your search

+7 -2
View File
@@ -331,6 +331,7 @@ pub struct JobReport {
pub completed_task_count: i32,
pub message: String,
pub estimated_completion: DateTime<Utc>,
// pub percentage_complete: f64,
}
@@ -364,8 +365,8 @@ impl From<job::Data> for JobReport {
.map(|errors_str| errors_str.split("\n\n").map(str::to_string).collect())
.unwrap_or_default(),
created_at: Some(data.date_created.into()),
started_at: data.date_started.map(|d| d.into()),
completed_at: data.date_completed.map(|d| d.into()),
started_at: data.date_started.map(DateTime::into),
completed_at: data.date_completed.map(DateTime::into),
parent_id: data
.parent_id
.map(|id| Uuid::from_slice(&id).expect("corrupted database")),
@@ -373,6 +374,9 @@ impl From<job::Data> for JobReport {
task_count: data.task_count,
completed_task_count: data.completed_task_count,
message: String::new(),
estimated_completion: data
.date_estimated_completion
.map_or(Utc::now(), DateTime::into),
}
}
}
@@ -395,6 +399,7 @@ impl JobReport {
parent_id: None,
completed_task_count: 0,
message: String::new(),
estimated_completion: Utc::now(),
}
}
+21 -1
View File
@@ -1,7 +1,7 @@
use crate::invalidate_query;
use crate::job::{DynJob, JobError, JobManager, JobReportUpdate, JobStatus};
use crate::library::Library;
use chrono::Utc;
use chrono::{DateTime, Utc};
use std::{sync::Arc, time::Duration};
use tokio::sync::oneshot;
use tokio::{
@@ -67,6 +67,7 @@ pub struct Worker {
report: JobReport,
worker_events_tx: UnboundedSender<WorkerEvent>,
worker_events_rx: Option<UnboundedReceiver<WorkerEvent>>,
start_time: Option<DateTime<Utc>>,
}
impl Worker {
@@ -78,6 +79,7 @@ impl Worker {
report,
worker_events_tx,
worker_events_rx: Some(worker_events_rx),
start_time: None,
}
}
@@ -111,6 +113,8 @@ impl Worker {
worker.report.started_at = Some(Utc::now());
}
worker.start_time = Some(Utc::now());
// If the report doesn't have a created_at date, it's a new report
if worker.report.created_at.is_none() {
worker.report.create(&library).await?;
@@ -217,6 +221,22 @@ impl Worker {
}
}
}
// Calculate elapsed time
if let Some(start_time) = worker.start_time {
let elapsed = Utc::now() - start_time;
// Calculate remaining time
let task_count = worker.report.task_count as usize;
let completed_task_count = worker.report.completed_task_count as usize;
let remaining_task_count = task_count.saturating_sub(completed_task_count);
let remaining_time_per_task = elapsed / (completed_task_count + 1) as i32; // Adding 1 to avoid division by zero
let remaining_time = remaining_time_per_task * remaining_task_count as i32;
// Update the report with estimated remaining time
worker.report.estimated_completion = Utc::now()
.checked_add_signed(remaining_time)
.unwrap_or(Utc::now());
}
invalidate_query!(library, "jobs.getRunning");
}