mod event;
mod notice;
mod tokens;
use std::path::Path;
use std::time::Instant;
use crate::finish::Finished;
use crate::network::{self, Job};
use crate::out::errln;
use crate::switches::TELEMETRY;
use crate::{endpoints, settings};
pub fn prepare(
command: &'static str,
finished: &Finished,
started: Instant,
root: &Path,
) -> Option<Job> {
let duration = started.elapsed();
let state = TELEMETRY.state();
if !state.on {
return None;
}
let address = endpoints::collector();
let mut kept = settings::read(TELEMETRY.file);
let (installation, drawn) = tokens::installation(&mut kept);
let unseen = !kept["noticeShown"].as_bool().unwrap_or(false);
if unseen {
errln!("{}", notice::text(&address).trim_end());
kept["noticeShown"] = serde_json::json!(true);
}
if drawn || unseen {
let _ = settings::write(TELEMETRY.file, &kept);
}
let (finished, root) = (finished.clone(), root.to_owned());
Some(Box::new(move |left| {
let started = Instant::now();
let checkout = tokens::checkout(&root);
let built = event::build(
command,
&finished,
duration,
&installation,
checkout.as_ref(),
state.internal,
);
if let Some(event) = built {
let body = serde_json::to_vec(&event).expect("a JSON value serializes");
let _ = network::post_json(&address, &body, left.saturating_sub(started.elapsed()));
}
}))
}