Initial commit

This commit is contained in:
2024-02-07 10:29:24 +01:00
commit 46dc6ffb71
6 changed files with 2164 additions and 0 deletions

116
src/main.rs Normal file
View File

@@ -0,0 +1,116 @@
use chrono::{DateTime, Utc, Timelike, Days};
use influxdb::{Client, Timestamp, WriteQuery};
use influxdb::InfluxDbWriteable;
use clap::Parser;
use handlebars::Handlebars;
use std::collections::HashMap;
use std::fs::File;
#[derive(InfluxDbWriteable)]
struct InfluxData {
time: DateTime<Utc>,
value: f64,
#[influxdb(tag)] system: String,
#[influxdb(tag)] metric: String,
#[influxdb(tag)] unit: String,
}
#[derive(Parser)]
#[command(author, version, about = "Foo", long_about = None)]
struct CliOptions {
#[arg(short, long, action)]
file: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli_options = CliOptions::parse();
let config_data: serde_json::Value = serde_json::from_reader(File::open(cli_options.file).unwrap())
.expect("file should be proper JSON");
let mut data = HashMap::new();
for (key, value) in config_data["vars"].as_object().unwrap() {
data.insert(key, value.as_str().unwrap().to_string());
}
let mut today: DateTime<Utc> = Utc::now();
today = today.with_hour(0).unwrap().with_minute(0).unwrap().with_second(0).unwrap().with_nanosecond(0).unwrap();
let tomorrow = today.checked_add_days(Days::new(1)).unwrap();
let day_start = "day_start".to_string();
data.insert(&day_start, today.format("%Y-%m-%d %H:%M:%S").to_string());
let day_end = "day_end".to_string();
data.insert(&day_end, tomorrow.format("%Y-%m-%d %H:%M:%S").to_string());
let dt = Utc::now();
let mut write_queries: Vec<WriteQuery> = Vec::new();
if let Some(configs) = config_data["configs"].as_array() {
for config in configs {
let mut handlebars = Handlebars::new();
handlebars
.register_template_string("url", config["url"].as_str().unwrap())
.unwrap();
let url = handlebars.render("url", &data).unwrap();
println!("URL: {}", url);
let response_json: serde_json::Value = reqwest::get(url)
.await?
.json()
.await?;
if let Some(values) = config["values"].as_array() {
for value in values {
if let Some(path) = value["path"].as_array() {
let mut sth = &response_json;
for segment in path {
if sth.is_array() {
let segment = segment.as_i64().unwrap();
match segment >= 0 {
false => {
sth = &sth[(sth.as_array().unwrap().len() as i64 + segment) as usize];
}
true => {
sth = &sth[segment as usize];
}
};
} else {
let segment = segment.as_str().unwrap();
sth = &sth[segment];
}
}
let res_value = sth.as_f64().unwrap_or(0.0);
let system = value["tags"]["system"].as_str().unwrap().to_string();
let metric = value["tags"]["metric"].as_str().unwrap().to_string();
let unit = value["tags"]["unit"].as_str().unwrap().to_string();
let measurement = value["measurement"].as_str().unwrap().to_string();
write_queries.push(
InfluxData {
time: Timestamp::from(dt).into(),
value: res_value,
system,
metric,
unit,
}.into_query(measurement),
);
}
}
}
}
}
let influx_conf = &config_data["influx"];
let url = influx_conf["url"].as_str().unwrap().to_string();
let database = influx_conf["database"].as_str().unwrap().to_string();
let username = influx_conf["username"].as_str().unwrap().to_string();
let password = influx_conf["password"].as_str().unwrap().to_string();
let client = Client::new(url, database)
.with_auth(username, password);
let write_result = client
.query(write_queries)
.await;
assert!(write_result.is_ok(), "Write result was not okay");
Ok(())
}