Skip to content

Commit

Permalink
refactor: move source enum to central
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Jul 26, 2024
1 parent cf53198 commit 488844b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 89 deletions.
88 changes: 1 addition & 87 deletions meter-core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,97 +34,11 @@ impl ReadItem {
}
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
#[repr(u8)]
#[non_exhaustive]
pub enum TrafficSource {
#[default]
Other = 0u8,

Prometheus = 1u8,
Influx = 2u8,
HTTP = 3u8,
MySQL = 4u8,
Postgres = 5u8,
GRPC = 6u8,
OTLP = 7u8,
}

impl From<u8> for TrafficSource {
fn from(value: u8) -> Self {
match value {
1 => TrafficSource::Prometheus,
2 => TrafficSource::Influx,
3 => TrafficSource::HTTP,
4 => TrafficSource::MySQL,
5 => TrafficSource::Postgres,
6 => TrafficSource::GRPC,
7 => TrafficSource::OTLP,

_ => TrafficSource::Other,
}
}
}

impl From<u32> for TrafficSource {
fn from(value: u32) -> Self {
match value {
1 => TrafficSource::Prometheus,
2 => TrafficSource::Influx,
3 => TrafficSource::HTTP,
4 => TrafficSource::MySQL,
5 => TrafficSource::Postgres,
6 => TrafficSource::GRPC,
7 => TrafficSource::OTLP,

_ => TrafficSource::Other,
}
}
}

#[derive(Debug)]
#[non_exhaustive]
pub struct MeterRecord {
pub catalog: String,
pub schema: String,
pub value: u64,
pub source: TrafficSource,
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_traffic_source_roundtrip() {
assert_eq!(
TrafficSource::Prometheus,
TrafficSource::from(TrafficSource::Prometheus as u8)
);
assert_eq!(
TrafficSource::Influx,
TrafficSource::from(TrafficSource::Influx as u8)
);
assert_eq!(
TrafficSource::MySQL,
TrafficSource::from(TrafficSource::MySQL as u8)
);
assert_eq!(
TrafficSource::Postgres,
TrafficSource::from(TrafficSource::Postgres as u8)
);
assert_eq!(
TrafficSource::GRPC,
TrafficSource::from(TrafficSource::GRPC as u8)
);
assert_eq!(
TrafficSource::OTLP,
TrafficSource::from(TrafficSource::OTLP as u8)
);
assert_eq!(
TrafficSource::Other,
TrafficSource::from(TrafficSource::Other as u8)
);
assert_eq!(TrafficSource::Other, TrafficSource::from(100u8));
}
pub source: u8,
}
2 changes: 1 addition & 1 deletion meter-macros/src/read_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ macro_rules! read_meter {
/// read_meter!("greptime", "public", ReadItem {
/// cpu_time: cpu_time_ns,
/// table_scan: table_scan_bytes,
/// }, TrafficSource::Other);
/// }, 0);
/// ```
#[cfg(not(feature = "noop"))]
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion meter-macros/src/write_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! write_meter {
/// let registry = global_registry();
/// registry.register_calculator(Arc::new(MockInsertCalculator));
///
/// write_meter!("greptime", "public", MockInsert, TrafficSource::Other);
/// write_meter!("greptime", "public", MockInsert, 0);
/// ```
#[cfg(not(feature = "noop"))]
#[macro_export]
Expand Down

0 comments on commit 488844b

Please sign in to comment.