Skip to content

Commit

Permalink
Summary lines ready, still need to show them periodically in wicket
Browse files Browse the repository at this point in the history
Created using spr 1.3.4
  • Loading branch information
sunshowers committed Oct 31, 2023
2 parents 79fbd61 + a6e1869 commit fa101e2
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 54 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions update-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ schemars = { workspace = true, features = ["uuid1"] }
slog.workspace = true
swrite.workspace = true
tokio = { workspace = true, features = ["macros", "sync", "time", "rt-multi-thread"] }
unicode-width.workspace = true
uuid.workspace = true
omicron-workspace-hack.workspace = true

Expand Down
87 changes: 78 additions & 9 deletions update-engine/examples/update-engine-basic/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use owo_colors::OwoColorize;
use tokio::{sync::mpsc, task::JoinHandle};
use update_engine::{
display::{LineDisplay, LineDisplayStyles},
display::{GroupDisplay, LineDisplay, LineDisplayStyles},
events::ProgressCounter,
};

Expand All @@ -34,14 +34,18 @@ pub(crate) fn make_displayer(
) -> (JoinHandle<Result<()>>, mpsc::Sender<Event>) {
let (sender, receiver) = mpsc::channel(512);
let log = log.clone();
let join_handle = match display_style {
DisplayStyle::ProgressBar => tokio::task::spawn(async move {
display_progress_bar(&log, receiver).await
}),
DisplayStyle::Line => tokio::task::spawn(async move {
display_line(&log, receiver, prefix).await
}),
};
let join_handle =
match display_style {
DisplayStyle::ProgressBar => tokio::task::spawn(async move {
display_progress_bar(&log, receiver).await
}),
DisplayStyle::Line => tokio::task::spawn(async move {
display_line(&log, receiver, prefix).await
}),
DisplayStyle::Group => tokio::task::spawn(async move {
display_group(&log, receiver).await
}),
};

(join_handle, sender)
}
Expand Down Expand Up @@ -71,6 +75,71 @@ async fn display_line(
Ok(())
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
enum GroupDisplayKey {
Example,
Other,
}

async fn display_group(
log: &slog::Logger,
mut receiver: mpsc::Receiver<Event>,
) -> Result<()> {
slog::info!(log, "setting up display");

let mut display = GroupDisplay::new(
[
(GroupDisplayKey::Example, "example"),
(GroupDisplayKey::Other, "other"),
],
std::io::stdout(),
);
// For now, always colorize. TODO: figure out whether colorization should be
// done based on always/auto/never etc.
if supports_color::on(supports_color::Stream::Stdout).is_some() {
display.set_styles(LineDisplayStyles::colorized());
}

display.set_progress_interval(Duration::from_millis(50));

let mut example_buffer = EventBuffer::default();
let mut example_buffer_last_seen = None;
let mut other_buffer = EventBuffer::default();
let mut other_buffer_last_seen = None;

let mut interval = tokio::time::interval(Duration::from_secs(2));
interval.tick().await;

loop {
tokio::select! {
_ = interval.tick() => {
// Print out status lines every 2 seconds.
display.write_stats("Status")?;
}
event = receiver.recv() => {
let Some(event) = event else { break };
example_buffer.add_event(event.clone());
other_buffer.add_event(event);

display.add_event_report(
&GroupDisplayKey::Example,
example_buffer.generate_report_since(&mut example_buffer_last_seen),
)?;
display.add_event_report(
&GroupDisplayKey::Other,
other_buffer.generate_report_since(&mut other_buffer_last_seen),
)?;
display.write_events()?;
}
}
}

// Print status at the end.
display.write_stats("Summary")?;

Ok(())
}

async fn display_progress_bar(
log: &slog::Logger,
mut receiver: mpsc::Receiver<Event>,
Expand Down
3 changes: 3 additions & 0 deletions update-engine/examples/update-engine-basic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl App {
let display_style = match self.display_style {
DisplayStyleOpt::ProgressBar => DisplayStyle::ProgressBar,
DisplayStyleOpt::Line => DisplayStyle::Line,
DisplayStyleOpt::Group => DisplayStyle::Group,
DisplayStyleOpt::Auto => {
if std::io::stdout().is_terminal() {
DisplayStyle::ProgressBar
Expand Down Expand Up @@ -123,6 +124,7 @@ impl App {
enum DisplayStyleOpt {
ProgressBar,
Line,
Group,
#[default]
Auto,
}
Expand All @@ -131,6 +133,7 @@ enum DisplayStyleOpt {
enum DisplayStyle {
ProgressBar,
Line,
Group,
}

/// Context shared across steps. This forms the lifetime "'a" defined by the
Expand Down
26 changes: 13 additions & 13 deletions update-engine/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,23 @@ impl<S: StepSpec> EventBuffer<S> {
///
/// This report can be serialized and sent over the wire.
pub fn generate_report(&self) -> EventReport<S> {
self.generate_report_since(None)
self.generate_report_since(&mut None)
}

/// Generates an [`EventReport`] for this buffer, updating `last_seen` to a
/// new value for incremental report generation.
///
/// This report can be serialized and sent over the wire.
pub fn generate_report_since(
&self,
mut last_seen: Option<usize>,
last_seen: &mut Option<usize>,
) -> EventReport<S> {
// Gather step events across all keys.
let mut step_events = Vec::new();
let mut progress_events = Vec::new();
for (_, step_data) in self.steps().as_slice() {
step_events
.extend(step_data.step_events_since_impl(last_seen).cloned());
.extend(step_data.step_events_since_impl(*last_seen).cloned());
progress_events
.extend(step_data.step_status.progress_event().cloned());
}
Expand All @@ -162,14 +166,14 @@ impl<S: StepSpec> EventBuffer<S> {
if let Some(last) = step_events.last() {
// Only update last_seen if there are new step events (otherwise it
// stays the same).
last_seen = Some(last.event_index);
*last_seen = Some(last.event_index);
}

EventReport {
step_events,
progress_events,
root_execution_id: self.root_execution_id(),
last_seen,
last_seen: *last_seen,
}
}

Expand Down Expand Up @@ -1864,7 +1868,7 @@ mod tests {
for (i, event) in self.generated_events.iter().enumerate() {
for time in 0..times {
(event_fn)(&mut buffer, event);
let report = buffer.generate_report_since(last_seen);
let report = buffer.generate_report_since(&mut last_seen);
let is_last_event = i == self.generated_events.len() - 1;
self.assert_general_properties(
&buffer,
Expand All @@ -1879,7 +1883,6 @@ mod tests {
})
.unwrap();
reported_step_events.extend(report.step_events);
last_seen = report.last_seen;

// Ensure that the last root index was updated for this
// event's corresponding steps, but not for any others.
Expand All @@ -1895,7 +1898,8 @@ mod tests {

// Call last_seen without feeding a new event in to ensure that
// a report with no step events is produced.
let report = buffer.generate_report_since(last_seen);
let mut last_seen_2 = last_seen;
let report = buffer.generate_report_since(&mut last_seen_2);
ensure!(
report.step_events.is_empty(),
"{description}, at index {i} (time {time}),\
Expand Down Expand Up @@ -1974,16 +1978,12 @@ mod tests {
for (i, event) in self.generated_events.iter().enumerate() {
let event_added = (event_fn)(&mut buffer, event);

let report = match last_seen_opt {
let report = match &mut last_seen_opt {
Some(last_seen) => buffer.generate_report_since(last_seen),
None => buffer.generate_report(),
};

let is_last_event = i == self.generated_events.len() - 1;
if let Some(last_seen) = &mut last_seen_opt {
*last_seen = report.last_seen;
}

self.assert_general_properties(&buffer, &report, is_last_event)
.with_context(|| {
format!(
Expand Down
3 changes: 1 addition & 2 deletions update-engine/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ impl NestedEventBuffer {
report: EventReport<S>,
) -> EventReport<NestedSpec> {
self.buffer.add_event_report(report.into_generic());
let ret = self.buffer.generate_report_since(self.last_seen);
self.last_seen = ret.last_seen;
let ret = self.buffer.generate_report_since(&mut self.last_seen);
ret
}
}
Expand Down
Loading

0 comments on commit fa101e2

Please sign in to comment.