pub mod analysis;
mod html;
mod stats;
#[cfg(feature = "pdf")]
mod pdf;
#[cfg(feature = "pdf")]
mod svg;
pub use stats::Stats;
use log::info;
use gnss_qc::prelude::{Duration, Epoch, QcOutputItem, TimeScale};
use crate::{
engine::SessionContext,
error::Error,
sink::{Consumer, RunSummary},
workspace::Workspace,
};
use analysis::Analysis;
#[derive(Debug, Clone)]
pub struct Report {
pub title: String,
pub subtitle: String,
pub generated: String,
pub sections: Vec<Section>,
}
#[derive(Debug, Clone)]
pub struct Section {
pub id: String,
pub title: String,
pub blocks: Vec<Block>,
}
impl Section {
pub fn new(id: &str, title: &str) -> Self {
Self {
id: id
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect(),
title: title.to_string(),
blocks: Vec::new(),
}
}
pub fn push(&mut self, block: Block) {
self.blocks.push(block);
}
}
#[derive(Debug, Clone)]
pub enum Block {
Heading(String),
Text(String),
Notice(String),
KeyValue(Vec<(String, String)>),
Table(Table),
Chart(Chart),
}
#[derive(Debug, Clone, Default)]
pub struct Table {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
}
impl Table {
pub fn new(headers: &[&str]) -> Self {
Self {
headers: headers.iter().map(|h| h.to_string()).collect(),
rows: Vec::new(),
}
}
pub fn push(&mut self, row: Vec<String>) {
self.rows.push(row);
}
}
#[derive(Debug, Clone, Copy)]
pub enum XAxis {
Time(Epoch),
#[cfg_attr(not(any(feature = "navigation", feature = "ionex")), allow(dead_code))]
Linear,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ChartKind {
Lines,
Markers,
Map,
}
#[derive(Debug, Clone)]
pub struct Series {
pub name: String,
pub x: Vec<f64>,
pub y: Vec<f64>,
}
impl Series {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
x: Vec::new(),
y: Vec::new(),
}
}
pub fn push(&mut self, x: f64, y: f64) {
self.x.push(x);
self.y.push(y);
}
pub fn decimated(&self, max: usize) -> Series {
if self.x.len() <= max || max == 0 {
return self.clone();
}
let step = self.x.len().div_ceil(max);
Series {
name: self.name.clone(),
x: self.x.iter().step_by(step).copied().collect(),
y: self.y.iter().step_by(step).copied().collect(),
}
}
}
#[derive(Debug, Clone)]
pub struct Chart {
pub title: String,
pub x_label: String,
pub y_label: String,
pub x_axis: XAxis,
pub kind: ChartKind,
pub equal_axes: bool,
pub series: Vec<Series>,
}
impl Chart {
pub fn time(title: &str, y_label: &str, origin: Epoch) -> Self {
Self {
title: title.to_string(),
x_label: format!("{}", origin.time_scale),
y_label: y_label.to_string(),
x_axis: XAxis::Time(origin),
kind: ChartKind::Lines,
equal_axes: false,
series: Vec::new(),
}
}
#[cfg_attr(not(any(feature = "navigation", feature = "ionex")), allow(dead_code))]
pub fn xy(title: &str, x_label: &str, y_label: &str) -> Self {
Self {
title: title.to_string(),
x_label: x_label.to_string(),
y_label: y_label.to_string(),
x_axis: XAxis::Linear,
kind: ChartKind::Markers,
equal_axes: false,
series: Vec::new(),
}
}
pub fn with_kind(mut self, kind: ChartKind) -> Self {
self.kind = kind;
self
}
#[cfg_attr(not(feature = "navigation"), allow(dead_code))]
pub fn with_equal_axes(mut self) -> Self {
self.equal_axes = true;
self
}
pub fn push(&mut self, series: Series) {
if !series.x.is_empty() {
self.series.push(series);
}
}
pub fn is_empty(&self) -> bool {
self.series.is_empty()
}
}
pub fn elapsed_s(origin: Epoch, t: Epoch) -> f64 {
(t - origin).to_seconds()
}
pub fn time_tag(origin: Epoch, x_s: f64) -> String {
let t = origin + Duration::from_seconds(x_s);
let ts = if origin.time_scale == TimeScale::TAI {
TimeScale::UTC
} else {
origin.time_scale
};
let (y, m, d, hh, mm, ss, ns) = t.to_gregorian(ts);
format!(
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
y,
m,
d,
hh,
mm,
ss,
ns / 1_000_000
)
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Formats {
pub html: bool,
pub pdf: bool,
}
pub struct ReportSink {
workspace: Workspace,
context: SessionContext,
formats: Formats,
analyses: Vec<Box<dyn Analysis>>,
}
impl ReportSink {
pub fn new(workspace: &Workspace, context: &SessionContext, formats: Formats) -> Self {
Self {
workspace: workspace.clone(),
context: context.clone(),
formats,
analyses: analysis::for_command(context),
}
}
fn build(&mut self, run: &RunSummary) -> Report {
let mut sections = Vec::new();
for analysis in self.analyses.iter_mut() {
sections.extend(analysis.sections(&self.context, run));
}
sections.push(analysis::context::sections(&self.context, run));
let generated = Epoch::now()
.map(|now| {
let (y, m, d, hh, mm, _, _) = now.to_gregorian_utc();
format!("{:04}-{:02}-{:02} {:02}:{:02} UTC", y, m, d, hh, mm)
})
.unwrap_or_default();
Report {
title: self.context.title.clone(),
subtitle: format!("rinex-cli {}", self.context.command),
generated,
sections,
}
}
}
impl Consumer for ReportSink {
fn consume(&mut self, item: &QcOutputItem) -> Result<(), Error> {
for analysis in self.analyses.iter_mut() {
analysis.consume(item);
}
Ok(())
}
fn finish(&mut self, run: &RunSummary) -> Result<(), Error> {
let report = self.build(run);
if self.formats.html {
let name = "report.html";
let html = html::render(&report);
std::fs::write(self.workspace.path(name), html)?;
info!("{} generated", self.workspace.path(name).display());
}
if self.formats.pdf {
#[cfg(feature = "pdf")]
{
let name = "report.pdf";
let pdf = pdf::render(&report)?;
std::fs::write(self.workspace.path(name), pdf)?;
info!("{} generated", self.workspace.path(name).display());
}
#[cfg(not(feature = "pdf"))]
log::error!(
"PDF report not available: rinex-cli was built without the \"pdf\" feature"
);
}
Ok(())
}
}