pub mod influxdb;
use influxdb::InfluxDBClient;
use std::collections::HashMap;
#[derive(Debug)]
pub enum ClientTypes {
InfluxDB,
}
macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::HashMap::new();
$( map.insert($key, $val); )*
map
}}
}
pub struct ToMetrics {
backend_url: String,
client_type: ClientTypes,
bucket: String,
static_metric_name: String,
static_metric_metadata: HashMap<String, serde_json::Value>,
username: Option<String>,
password: Option<String>,
verbose: Option<bool>,
client: InfluxDBClient, }
impl ToMetrics {
pub async fn new(
backend_url: String,
client_type: ClientTypes,
bucket: String,
static_metric_name: String, ) -> Self {
if true {
println!("[ToMetrics] Initializing \"{bucket}|{static_metric_name}\" streaming to {:?} via \"{backend_url}\"", client_type);
}
let formatted_backend_url = backend_url;
let client = match client_type {
ClientTypes::InfluxDB => {
InfluxDBClient::new(formatted_backend_url.clone(), bucket.clone())
}
};
client.init().await;
ToMetrics {
backend_url: formatted_backend_url,
client_type: client_type,
bucket: bucket,
static_metric_name: static_metric_name,
static_metric_metadata: HashMap::<String, serde_json::Value>::new(),
username: None,
password: None,
verbose: None,
client: client,
}
}
pub async fn send(
&self,
data: HashMap<String, serde_json::Value>,
metadata: HashMap<String, serde_json::Value>,
) -> bool {
let timestamp_override_ns = chrono::offset::Utc::now().timestamp_nanos() as usize;
let mut all_metadata = self.static_metric_metadata.clone();
all_metadata.extend(metadata.into_iter());
self.client.send(self.static_metric_name.clone(), timestamp_override_ns, data, all_metadata).await
}
}
#[tokio::test]
async fn push_metric_test() {
assert_eq!(2 + 2, 4);
let tom = ToMetrics::new(
"http://influx_hostname:8086".to_string(),
ClientTypes::InfluxDB,
"testdb".to_string(),
"test_metric".to_string()).await;
assert!(tom.send(
hashmap!["vala".to_string()=>serde_json::json!(1)],
hashmap!["tag1".to_string()=>serde_json::json!("asdf")],
).await);
}