mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-02-23 10:48:32 +01:00
Simplify the name generation
This commit is contained in:
parent
11a11fc870
commit
4a058a080e
@ -6,6 +6,7 @@ use std::sync::{Arc, RwLock};
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
|
use itertools::Itertools;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
@ -22,27 +23,12 @@ pub struct Progress {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct InnerProgress {
|
struct InnerProgress {
|
||||||
/// The hierarchy of progress steps.
|
/// The hierarchy of steps.
|
||||||
steps: Vec<(TypeId, Box<dyn Step>, Instant)>,
|
steps: Vec<(TypeId, Box<dyn Step>, Instant)>,
|
||||||
/// The durations associated to the top level steps (*first*).
|
/// The durations associated to each steps.
|
||||||
durations: Vec<(String, Duration)>,
|
durations: Vec<(String, Duration)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name_from_steps<'a, I>(steps: I) -> String
|
|
||||||
where
|
|
||||||
I: Iterator<Item = &'a Box<dyn Step>> + ExactSizeIterator,
|
|
||||||
{
|
|
||||||
let len = steps.len();
|
|
||||||
let mut name = String::new();
|
|
||||||
for (i, step) in steps.into_iter().enumerate() {
|
|
||||||
name.push_str(&step.name());
|
|
||||||
if i + 1 < len {
|
|
||||||
name.push_str(" > ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
name
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Progress {
|
impl Progress {
|
||||||
pub fn update_progress<P: Step>(&self, sub_progress: P) {
|
pub fn update_progress<P: Step>(&self, sub_progress: P) {
|
||||||
let mut inner = self.steps.write().unwrap();
|
let mut inner = self.steps.write().unwrap();
|
||||||
@ -51,10 +37,7 @@ impl Progress {
|
|||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let step_type = TypeId::of::<P>();
|
let step_type = TypeId::of::<P>();
|
||||||
if let Some(idx) = steps.iter().position(|(id, _, _)| *id == step_type) {
|
if let Some(idx) = steps.iter().position(|(id, _, _)| *id == step_type) {
|
||||||
for (i, (_, _, started_at)) in steps[idx..].iter().enumerate() {
|
push_steps_durations(steps, durations, now, idx);
|
||||||
let full_name = name_from_steps(steps.iter().take(idx + i + 1).map(|(_, s, _)| s));
|
|
||||||
durations.push((full_name, now.duration_since(*started_at)));
|
|
||||||
}
|
|
||||||
steps.truncate(idx);
|
steps.truncate(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,15 +72,25 @@ impl Progress {
|
|||||||
let InnerProgress { steps, durations, .. } = &mut *inner;
|
let InnerProgress { steps, durations, .. } = &mut *inner;
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
for (i, (_, _, started_at)) in steps.iter().enumerate() {
|
push_steps_durations(steps, durations, now, 0);
|
||||||
let full_name = name_from_steps(steps.iter().take(i + 1).map(|(_, s, _)| s));
|
|
||||||
durations.push((full_name, now.duration_since(*started_at)));
|
|
||||||
}
|
|
||||||
|
|
||||||
durations.drain(..).map(|(name, duration)| (name, format!("{duration:.2?}"))).collect()
|
durations.drain(..).map(|(name, duration)| (name, format!("{duration:.2?}"))).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate the names associated with the durations and push them.
|
||||||
|
fn push_steps_durations(
|
||||||
|
steps: &[(TypeId, Box<dyn Step>, Instant)],
|
||||||
|
durations: &mut Vec<(String, Duration)>,
|
||||||
|
now: Instant,
|
||||||
|
idx: usize,
|
||||||
|
) {
|
||||||
|
for (i, (_, _, started_at)) in steps.iter().skip(idx).enumerate() {
|
||||||
|
let full_name = steps.iter().take(idx + i + 1).map(|(_, s, _)| s.name()).join(" > ");
|
||||||
|
durations.push((full_name, now.duration_since(*started_at)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// This trait lets you use the AtomicSubStep defined right below.
|
/// This trait lets you use the AtomicSubStep defined right below.
|
||||||
/// The name must be a const that never changed but that can't be enforced by the type system because it make the trait non object-safe.
|
/// The name must be a const that never changed but that can't be enforced by the type system because it make the trait non object-safe.
|
||||||
/// By forcing the Default trait + the &'static str we make it harder to miss-use the trait.
|
/// By forcing the Default trait + the &'static str we make it harder to miss-use the trait.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user