Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
*: Update according to reviews
Browse files Browse the repository at this point in the history
Changes the use of term "metrics", deduplicates match pattern and
uses maplit::hashmap! macro.
  • Loading branch information
Allen Bai committed Sep 19, 2019
1 parent c1ae43c commit 1d92353
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ liboverdrop = "^0.0.2"
log = "^0.4.6"
serde = { version = "^1.0.91", features = ["derive"] }
toml = "^0.5.1"
maplit = "^1.0"

[package.metadata.release]
sign-commit = true
Expand Down
36 changes: 16 additions & 20 deletions src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::config::inputs;
use failure::{Fallible, ResultExt};
use serde::Serialize;
use std::collections::HashMap;
use maplit;

/// Kernel arguments location
static KERNEL_ARGS_FILE: &str = "/proc/cmdline";
Expand All @@ -22,9 +23,8 @@ impl Identity {
pub(crate) fn new(cfg: &inputs::CollectingInput) -> Fallible<Self> {
let collecting_level = &cfg.level;
let id = match collecting_level.as_str() {
"minimal" => Self::try_default("minimal").context("failed to build minimal (default) identity")?,
"full" => Self::try_default("full").context("failed to build full identity")?,
&_ => Self::try_default("minimal").context("failed to build minimal (default) identity")?,
level @ "minimal" | level @ "full" => Self::try_default(level).context(format!("failed to build '{}' identity", level))?,
&_ => Self::try_default("minimal").context("failed to build 'minimal' identity")?,
};

Ok(id)
Expand All @@ -35,33 +35,29 @@ impl Identity {
let platform = platform::read_id(KERNEL_ARGS_FILE)?;

let id = match level {
"minimal" => Self {
level: String::from("minimal"),
"minimal" | "full" => Self {
level: level.to_string(),
platform,
},
"full" => Self {
level: String::from("full"),
platform
},
&_ => Self {
level: String::from("minimal"),
level: "minimal".to_string(),
platform,
},
};

Ok(id)
}

/// Getter for collected metrics, returned as a HashMap
pub fn get_metrics(&self) -> HashMap<String, String> {
let mut vars = HashMap::new();

vars.insert("level".to_string(), self.level.clone());
vars.insert("platform".to_string(), self.platform.clone());
/// Getter for collected data, returned as a HashMap
pub fn get_data(&self) -> HashMap<String, String> {
let vars = maplit::hashmap!{
"level".to_string() => self.level.clone(),
"platform".to_string() => self.platform.clone(),
};

// Insert data specific to different levels
match self.level.as_str() {
"minimal" => (),
"full" => (),
"minimal" | "full" => (),
&_ => (),
};

Expand Down Expand Up @@ -94,7 +90,7 @@ mod tests {
#[test]
fn test_minimal() {
let id = Identity::mock_default("minimal");
let vars = id.get_metrics();
let vars = id.get_data();

assert!(vars.contains_key("level"));
assert!(vars.contains_key("platform"));
Expand All @@ -103,7 +99,7 @@ mod tests {
#[test]
fn test_full() {
let id = Identity::mock_default("full");
let vars = id.get_metrics();
let vars = id.get_data();

assert!(vars.contains_key("level"));
assert!(vars.contains_key("platform"));
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn check_config(config: &inputs::ConfigInput) -> failure::Fallible<()> {
Ok(())
}

fn send_metrics(id: &identity::Identity) -> failure::Fallible<()> {
// TODO: Send metrics to remote endpoint
for (key, value) in id.get_metrics() {
fn send_data(id: &identity::Identity) -> failure::Fallible<()> {
// TODO: Send data to remote endpoint
for (key, value) in id.get_data() {
println!("{}: {}", key, value);
}

Expand Down Expand Up @@ -68,10 +68,10 @@ fn main() -> failure::Fallible<()> {

check_config(&config)?;

// Collect the metrics
// Collect the data
let id = identity::Identity::new(&config.collecting)?;
// Send to the remote endpoint
send_metrics(&id)?;
send_data(&id)?;

Ok(())
}

0 comments on commit 1d92353

Please sign in to comment.