use std::time::Instant;
use shep_client::RequestError;
use shep_core::protocol::{Lamb, ProcessInfo, Response};
use shep_core::status::ProcStatus;
use crate::lookout::app::{App, LambWalk, Msg, Sent};
use super::flock::with_selection;
use super::render::render_all;
pub fn sheep_with_lambs() -> ProcessInfo {
ProcessInfo::builder(9, "gateway", ProcStatus::Online)
.pid(Some(48_301))
.lambs(Some(vec![
Lamb::new(48_302, "node"),
Lamb::new(48_303, "sh"),
]))
.build()
}
pub fn with_lamb_reading(walk: LambWalk) -> App {
with_lamb_reading_for(9, walk)
}
pub fn with_lamb_reading_for(id: u32, walk: LambWalk) -> App {
let mut app = with_selection(sheep_with_lambs());
app.update(Msg::Replied {
sent: Sent::Lambs { id },
result: reply_for(id, &walk),
});
app
}
pub fn app_with_lamb_reading_at(walk: LambWalk) -> (App, Instant) {
let t0 = Instant::now();
let mut app = with_selection(sheep_with_lambs());
app.update(Msg::Tick { now: t0 });
app.update(Msg::Replied {
sent: Sent::Lambs { id: 9 },
result: reply_for(9, &walk),
});
(app, t0)
}
fn reply_for(id: u32, walk: &LambWalk) -> Result<Response, RequestError> {
let lambs = match walk {
LambWalk::Failed => return Err(RequestError::Closed),
LambWalk::NotWalked => None,
LambWalk::Walked(lambs) => Some(lambs.clone()),
};
Ok(Response::Described(vec![
ProcessInfo::builder(id, "gateway", ProcStatus::Online)
.pid(Some(48_301))
.lambs(lambs)
.build(),
]))
}
pub fn lamb_line_of(app: &App) -> String {
render_all(&crate::lookout::view::detail::detail_lines(app, 200))
.lines()
.find(|line| line.starts_with("lambs "))
.map(str::to_string)
.expect("the pane has a lamb line")
}