use std::collections::BTreeMap;
use std::fmt;
use serde_json::Value;
use crate::contract::{Body, Contract, Resource};
use crate::frame::{self, Applied, FieldState, Frame, Layout, Slot, Unknown};
use crate::packed::{Field, FieldKind};
use crate::wire::{Seq, Stamp};
use super::{
Declaration, Measure, MetricKind, Pair, RecordTime, SERIES, Sampled, Semantic, is_unit, pair,
};
pub const INLINE: &[&str] = &["kind", "unit", "semantic"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldSeries {
pub field: String,
pub index: usize,
pub kind: MetricKind,
pub unit: Option<String>,
pub semantic: Option<Semantic>,
pub mirror: Option<String>,
pub width: Option<u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Reason {
SeriesField,
SeriesValue,
KindValue,
UnitValue,
SemanticValue,
MirrorMissing,
MirrorInvalid,
KindMissing,
NotACarrier,
}
impl Reason {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::SeriesValue => "series_value",
Self::SeriesField => "series_field",
Self::KindValue => "kind_value",
Self::UnitValue => "unit_value",
Self::SemanticValue => "semantic_value",
Self::KindMissing => "kind_missing",
Self::MirrorMissing => "mirror_missing",
Self::MirrorInvalid => "mirror_invalid",
Self::NotACarrier => "not_a_carrier",
}
}
#[must_use]
pub const fn says(self) -> &'static str {
match self {
Self::SeriesValue => {
"telemetry.series is not a table of fields, each a mirror or {kind, unit, semantic}"
}
Self::SeriesField => "the entry names no field of the frame's record type",
Self::KindValue => "kind is not one of the six kinds",
Self::UnitValue => "unit is not 1 to 64 printable ASCII characters (0x21 to 0x7E)",
Self::SemanticValue => "semantic is not one of the eight semantics",
Self::KindMissing => "nothing gives the series a kind",
Self::MirrorMissing => {
"the mirror names no single non-frame metric resource of the interface with no parameter"
}
Self::MirrorInvalid => "the mirrored resource's own declaration is invalid",
Self::NotACarrier => "the field's type cannot carry the series' kind",
}
}
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Problem {
pub entry: Option<String>,
pub reason: Reason,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Series {
Declared(Vec<FieldSeries>),
Invalid(Vec<Problem>),
Ignored,
NotThisProfiles,
}
impl Series {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Declared(_) => "declared",
Self::Invalid(_) => "invalid",
Self::Ignored => "ignored",
Self::NotThisProfiles => "not_this_profiles",
}
}
#[must_use]
pub fn declared(&self) -> Option<&[FieldSeries]> {
match self {
Self::Declared(s) => Some(s),
_ => None,
}
}
}
#[must_use]
pub fn carries(field: &Field, kind: MetricKind) -> bool {
matches!(
(kind, &field.kind),
(MetricKind::Counter, FieldKind::Counter)
| (
MetricKind::Gauge,
FieldKind::Int { .. } | FieldKind::Fixed { .. }
)
| (MetricKind::Boolean, FieldKind::Bool)
| (MetricKind::Text, FieldKind::Enum { .. })
)
}
struct Given {
kind: Option<MetricKind>,
unit: Option<String>,
semantic: Option<Semantic>,
mirror: Option<String>,
}
fn entry(value: &Value, mirror: &dyn Fn(&str) -> Option<Declaration>) -> (Vec<Reason>, Given) {
let mut why = Vec::new();
let mut given = Given {
kind: None,
unit: None,
semantic: None,
mirror: None,
};
match value {
Value::Object(m) => {
if m.keys().any(|k| !INLINE.contains(&k.as_str())) {
why.push(Reason::SeriesValue);
}
let kind = m
.get("kind")
.map(|v| v.as_str().and_then(MetricKind::parse));
let unit = m
.get("unit")
.map(|v| v.as_str().filter(|u| is_unit(u)).map(str::to_owned));
let semantic = m
.get("semantic")
.map(|v| v.as_str().and_then(Semantic::parse));
for (wrong, r) in [
(matches!(kind, Some(None)), Reason::KindValue),
(matches!(unit, Some(None)), Reason::UnitValue),
(matches!(semantic, Some(None)), Reason::SemanticValue),
] {
if wrong {
why.push(r);
}
}
if kind.is_none() {
why.push(Reason::KindMissing);
}
given.kind = kind.flatten();
given.unit = unit.flatten();
given.semantic = semantic.flatten();
}
Value::String(name) => {
given.mirror = Some(name.clone());
match mirror(name) {
None | Some(Declaration::NotThisProfiles | Declaration::Ignored) => {
why.push(Reason::MirrorMissing);
}
Some(Declaration::Invalid(_)) => why.push(Reason::MirrorInvalid),
Some(Declaration::Declared(d)) => match d.kind {
None => why.push(Reason::KindMissing),
Some(k) => {
given.kind = Some(k);
given.unit = d.unit;
given.semantic = d.semantic;
}
},
}
}
_ => why.push(Reason::SeriesValue),
}
(why, given)
}
#[must_use]
pub fn series(
fields: &[Field],
annotations: &BTreeMap<String, Value>,
mirror: &dyn Fn(&str) -> Option<Declaration>,
) -> Series {
let Some(table) = annotations.get(SERIES) else {
let v1 = [super::KIND, super::UNIT, super::BUCKETS, super::SEMANTIC];
return if v1.iter().any(|k| annotations.contains_key(*k)) {
Series::Ignored
} else {
Series::NotThisProfiles
};
};
let whole = |reason| {
Series::Invalid(vec![Problem {
entry: None,
reason,
}])
};
let Some(table) = table.as_object().filter(|t| !t.is_empty()) else {
return whole(Reason::SeriesValue);
};
let mut entries: Vec<(&String, &Value)> = table.iter().collect();
entries.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
let mut problems = Vec::new();
let mut out = Vec::new();
for (name, value) in entries {
let at = fields.iter().position(|f| &f.name == name);
let (mut why, given) = entry(value, mirror);
if at.is_none() {
why.insert(0, Reason::SeriesField);
}
if let (Some(i), Some(kind)) = (at, given.kind)
&& !carries(&fields[i], kind)
{
why.push(Reason::NotACarrier);
}
if why.is_empty() {
let i = at.expect("an entry with no reason names a field");
out.push(FieldSeries {
field: name.clone(),
index: i,
kind: given.kind.expect("an entry with no reason has a kind"),
unit: given.unit,
semantic: given.semantic,
mirror: given.mirror,
width: matches!(fields[i].kind, FieldKind::Counter).then_some(fields[i].width),
});
}
problems.extend(why.into_iter().map(|reason| Problem {
entry: Some(name.clone()),
reason,
}));
}
if !problems.is_empty() {
return Series::Invalid(problems);
}
out.sort_by_key(|s| s.index);
Series::Declared(out)
}
#[must_use]
pub fn mirror_in(c: &Contract, name: &str) -> Option<Declaration> {
let mut named = c.resources.iter().filter(|r| r.template.as_str() == name);
let r = named.next()?;
if named.next().is_some() || !r.params.is_empty() {
return None;
}
if let Body::Data(d) = &r.body
&& d.frame.is_some()
{
return None;
}
Some(super::declaration_of(c, r))
}
#[must_use]
pub fn series_of(c: &Contract, r: &Resource) -> Option<Series> {
let layout = Layout::of_resource(c, r).ok().flatten()?;
Some(series(layout.fields(), &r.annotations, &|name| {
mirror_in(c, name)
}))
}
#[derive(Debug, Clone, PartialEq)]
pub struct FieldValue {
pub measure: Measure,
pub json: Value,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FieldReading {
Sample(FieldValue),
Held { value: FieldValue, within: bool },
Absent,
OwnerError,
BeyondRange,
Unnamed,
Unknown(Unknown),
}
impl FieldReading {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Sample(_) => "sample",
Self::Held { .. } => "held",
Self::Absent => "absent",
Self::OwnerError => "owner_error",
Self::BeyondRange => "beyond_range",
Self::Unnamed => "unnamed",
Self::Unknown(_) => "unknown",
}
}
#[must_use]
pub const fn sample(&self) -> Option<&FieldValue> {
match self {
Self::Sample(v) => Some(v),
_ => None,
}
}
#[must_use]
pub const fn value(&self) -> Option<&FieldValue> {
match self {
Self::Sample(v) | Self::Held { value: v, .. } => Some(v),
_ => None,
}
}
}
fn value_of(s: &FieldSeries, field: &Field, code: u64) -> Option<FieldValue> {
let json = field.value(code, None).ok()?;
let measure = match s.kind {
MetricKind::Counter => Measure::Counter {
value: code,
width: field.width,
},
MetricKind::Gauge => Measure::Gauge(match &json {
Value::String(d) => d.parse::<f64>().ok()?,
v => v.as_i64()? as f64,
}),
MetricKind::Boolean => Measure::Boolean(json.as_bool()?),
MetricKind::Text => Measure::Text(json.as_str()?.to_owned()),
MetricKind::Histogram | MetricKind::Binary => return None,
};
Some(FieldValue { measure, json })
}
#[must_use]
pub fn reading_of(s: &FieldSeries, field: &Field, slot: Slot, state: FieldState) -> FieldReading {
let (code, within) = match state {
FieldState::Unknown(why) => return FieldReading::Unknown(why),
FieldState::Absent => return FieldReading::Absent,
FieldState::Known(c) => (c, false),
FieldState::Within(c) => (c, true),
};
match field.reserved_name(code) {
Some("error") => return FieldReading::OwnerError,
Some("out_of_range") => return FieldReading::BeyondRange,
Some(_) => return FieldReading::Absent,
None => {}
}
let Some(value) = value_of(s, field, code) else {
return FieldReading::Unnamed;
};
if matches!(slot, Slot::Code(c) if c == code) && !within {
FieldReading::Sample(value)
} else {
FieldReading::Held { value, within }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Position {
pub frame: u64,
pub record: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SeriesRead {
pub series: usize,
pub reading: FieldReading,
pub pair: Option<(Position, Pair)>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RecordSeries {
pub seq: Seq,
pub time: RecordTime,
pub fields: Vec<SeriesRead>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Folded {
pub applied: Applied,
pub records: Vec<RecordSeries>,
}
#[derive(Debug, Clone)]
struct Last {
measure: Measure,
epoch: String,
at: Position,
time: RecordTime,
arrival_ns: i128,
}
impl Last {
fn sampled(&self) -> Sampled<'_> {
Sampled {
value: &self.measure,
epoch: Some(&self.epoch),
timestamp_ms: None,
stamp_ns: None,
arrival_ns: self.arrival_ns,
record: Some(self.time),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Fold {
reader: frame::Reader,
frames: u64,
epochs: u64,
last: BTreeMap<usize, Last>,
}
impl Fold {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn reader(&self) -> &frame::Reader {
&self.reader
}
pub fn refuse(&mut self, stamp: Stamp) {
self.reader.refuse(stamp);
}
pub fn apply(
&mut self,
layout: &Layout,
series: &[FieldSeries],
stamp: Stamp,
frame: &Frame,
arrival_ns: i128,
) -> Folded {
let applied = self.reader.apply(layout, stamp, frame);
if applied.follows.resets() && !matches!(applied.follows, frame::Follows::Gap { .. }) {
self.epochs += 1;
}
let epoch = format!("{}/{}", stamp.run.hex(), self.epochs);
let n = self.frames;
self.frames += 1;
let fields = layout.fields();
let mut records = Vec::with_capacity(frame.records.len());
for (i, (rec, read)) in frame.records.iter().zip(&applied.records).enumerate() {
let time = RecordTime {
frame: n,
when: read.when,
after_ms: read.after_ms,
at_ms: read.at_ms,
};
let at = Position {
frame: n,
record: i,
};
let mut out = Vec::with_capacity(series.len());
for (k, s) in series.iter().enumerate() {
let (Some(field), Some(slot), Some(state)) = (
fields.get(s.index),
rec.slots.get(s.index),
read.fields.get(s.index),
) else {
continue;
};
let reading = reading_of(s, field, *slot, *state);
let pair = match (&reading, s.kind) {
(FieldReading::Sample(v), MetricKind::Counter) => {
let now = Last {
measure: v.measure.clone(),
epoch: epoch.clone(),
at,
time,
arrival_ns,
};
let judged = self.last.get(&s.index).map(|before| {
(
before.at,
pair(&before.sampled(), &now.sampled(), s.unit.as_deref(), None),
)
});
self.last.insert(s.index, now);
judged
}
_ => None,
};
out.push(SeriesRead {
series: k,
reading,
pair,
});
}
records.push(RecordSeries {
seq: read.seq,
time,
fields: out,
});
}
Folded { applied, records }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::telemetry::{Continuity, NoRate, TimeBasis};
use serde_json::json;
fn link() -> Vec<Field> {
let doc = json!({"types": {"Link": {"fields": [
{"name": "dropped", "type": "counter", "width": 8, "optional": true},
{"name": "queue_depth", "type": "int", "minimum": 0, "maximum": 1022,
"reserved": ["error", "out_of_range"], "optional": true},
{"name": "mode", "type": "enum", "values": ["idle", "tx"], "escape": true},
{"name": "at", "type": "time"},
]}}});
crate::packed::layout_in(&doc, "Link")
.unwrap()
.fields()
.cloned()
.collect()
}
fn ann(v: Value) -> BTreeMap<String, Value> {
BTreeMap::from([(SERIES.to_owned(), v)])
}
#[test]
fn an_entry_is_checked_against_its_field_and_its_mirror() {
let none = |_: &str| None;
let s = series(
&link(),
&ann(json!({"dropped": {"kind": "counter", "unit": "1"}, "mode": {"kind": "text"}})),
&none,
);
let d = s.declared().expect("declared");
assert_eq!(d[0].width, Some(8));
assert_eq!(d[1].kind, MetricKind::Text);
let s = series(
&link(),
&ann(json!({"at": {"kind": "gauge"}, "nope": "x/y", "queue_depth": 3})),
&none,
);
let Series::Invalid(p) = s else {
panic!("{s:?}")
};
let got: Vec<_> = p
.iter()
.map(|p| (p.entry.as_deref().unwrap(), p.reason.as_str()))
.collect();
assert_eq!(
got,
[
("at", "not_a_carrier"),
("nope", "series_field"),
("nope", "mirror_missing"),
("queue_depth", "series_value"),
]
);
assert_eq!(
series(&link(), &BTreeMap::new(), &none),
Series::NotThisProfiles
);
assert_eq!(series(&link(), &ann(json!({})), &none).as_str(), "invalid");
}
#[test]
fn only_an_included_value_is_a_sample() {
let f = link();
let s = FieldSeries {
field: "queue_depth".into(),
index: 1,
kind: MetricKind::Gauge,
unit: None,
semantic: None,
mirror: None,
width: None,
};
let r = |slot, state| reading_of(&s, &f[1], slot, state).as_str();
assert_eq!(r(Slot::Code(4), FieldState::Known(4)), "sample");
assert_eq!(r(Slot::Omitted, FieldState::Known(4)), "held");
assert_eq!(r(Slot::Omitted, FieldState::Within(4)), "held");
let top = (1u64 << f[1].width) - 1;
assert_eq!(r(Slot::Code(top), FieldState::Known(top)), "owner_error");
assert_eq!(
r(Slot::Code(top - 1), FieldState::Known(top - 1)),
"beyond_range"
);
assert_eq!(r(Slot::Absent, FieldState::Absent), "absent");
}
#[test]
fn a_narrow_counter_reads_in_its_serial_window() {
let at = |frame, after_ms| RecordTime {
frame,
when: frame::When::Measured,
after_ms,
at_ms: Some(1_000 * frame + after_ms),
};
let s = |v, t| Sampled {
value: v,
epoch: Some("r"),
timestamp_ms: None,
stamp_ns: None,
arrival_ns: 0,
record: Some(t),
};
let c = |value| Measure::Counter { value, width: 8 };
let (a, b, z) = (c(250), c(4), c(0));
let p = pair(&s(&a, at(0, 0)), &s(&b, at(1, 0)), None, None);
assert_eq!(p.continuity, Continuity::SameEpoch);
let r = p.rate.unwrap();
assert_eq!((r.per_s, r.basis), (10.0, Some(TimeBasis::Measured)));
let p = pair(&s(&b, at(0, 0)), &s(&z, at(1, 0)), None, None);
assert_eq!(p.continuity, Continuity::Unobservable);
assert_eq!(p.rate, Err(NoRate::WrapOrDecrease));
assert_eq!(p.unobservable(), Some("wrap_or_decrease"));
}
}