rs_postgres_stat2otel/
multi.rs

1//! Many sets of metrics items / single request(Label required)
2//!
3//! The query may return more than one row.
4
5use crate::{
6    evt::Event,
7    gauge::{Gauge, RawGauge},
8    label::Label,
9};
10
11/// Multi gets gauge items from many rows(Label required).
12pub struct Multi {
13    query: String,
14    label: Vec<Label>,
15    gauge: Vec<Gauge>,
16}
17
18impl Multi {
19    /// Gets the query string to get metrics rows.
20    pub fn as_query(&self) -> &str {
21        self.query.as_str()
22    }
23
24    /// Gets labels.
25    pub fn as_label(&self) -> &[Label] {
26        &self.label
27    }
28
29    /// Gets gauges.
30    pub fn as_gauge(&self) -> &[Gauge] {
31        &self.gauge
32    }
33}
34
35impl TryFrom<RawMulti> for Multi {
36    type Error = Event;
37    fn try_from(r: RawMulti) -> Result<Self, Self::Error> {
38        let query: String = r.query;
39        let label: Vec<_> = r.label;
40
41        let gauge: Vec<Gauge> = r.gauge.into_iter().try_fold(vec![], |mut v, r| {
42            let g: Gauge = Gauge::try_from(r)?;
43            v.push(g);
44            Ok(v)
45        })?;
46
47        Ok(Self {
48            query,
49            label,
50            gauge,
51        })
52    }
53}
54
55/// Raw(serialized) Multi.
56#[derive(serde::Deserialize)]
57pub struct RawMulti {
58    query: String,
59    label: Vec<Label>,
60    gauge: Vec<RawGauge>,
61}