rs_postgres_stat2otel/
single.rs

1//! Single set of metrics items / single request
2//!
3//! A query must return one row.
4
5use crate::{
6    evt::Event,
7    gauge::{Gauge, RawGauge},
8    label::Label,
9};
10
11/// Single gets gauge items from single row.
12pub struct Single {
13    query: String,
14    label: Option<Vec<Label>>,
15    gauge: Vec<Gauge>,
16}
17
18impl TryFrom<RawSingle> for Single {
19    type Error = Event;
20    fn try_from(r: RawSingle) -> Result<Self, Self::Error> {
21        let query: String = r.query;
22        let label: Option<_> = r.label;
23
24        let gauge: Vec<Gauge> = r.gauge.into_iter().try_fold(vec![], |mut v, raw| {
25            let g: Gauge = Gauge::try_from(raw)?;
26            v.push(g);
27            Ok(v)
28        })?;
29        Ok(Self {
30            query,
31            label,
32            gauge,
33        })
34    }
35}
36
37/// Raw(serialized) Single.
38#[derive(serde::Deserialize)]
39pub struct RawSingle {
40    query: String,
41    label: Option<Vec<Label>>,
42    gauge: Vec<RawGauge>,
43}
44
45impl Single {
46    /// Gets the query string to get a metrics row.
47    pub fn as_query(&self) -> &str {
48        self.query.as_str()
49    }
50
51    /// Takes all labels if exists.
52    pub fn take_label(&mut self) -> Option<Vec<Label>> {
53        self.label.take()
54    }
55
56    /// Gets all labels(can be empty).
57    pub fn as_label(&self) -> &[Label] {
58        match &self.label {
59            None => &[],
60            Some(l) => l,
61        }
62    }
63
64    /// Gets all gauge items.
65    pub fn as_gauge(&self) -> &[Gauge] {
66        &self.gauge
67    }
68}