diff --git a/Cargo.lock b/Cargo.lock index e75f604..0055b96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -690,7 +690,7 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "subbeat" -version = "0.0.3" +version = "0.0.4" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 48eb782..e6e138b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subbeat" -version = "0.0.3" +version = "0.0.4" edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/cli.rs b/src/cli.rs index d6d96e5..b1543cf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,19 +1,14 @@ use clap::{App, Arg, SubCommand}; +use subbeat::types::QueryConfig; pub struct CLI { - pub url: String, - pub key: String, - pub datasource_url: String, - pub query: String, - pub from: u64, - pub to: u64, - pub step: u64, + pub query_config: QueryConfig, } impl CLI { pub fn new() -> CLI { let matches = App::new("subbeat") - .version("0.0.2") + .version("0.0.4") .about("Timeseries toolkit") .subcommand( SubCommand::with_name("grafana") @@ -63,6 +58,40 @@ impl CLI { .index(7), ), ) + .subcommand( + SubCommand::with_name("prometheus") + .about("Use prometheus API as datasource") + .arg( + Arg::with_name("PROM_URL") + .help("URL to your Grafana instance") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("query") + .help("your query to datasource") + .required(true) + .index(2), + ) + .arg( + Arg::with_name("from") + .help("timestamp") + .required(true) + .index(3), + ) + .arg( + Arg::with_name("to") + .help("timestampt") + .required(true) + .index(4), + ) + .arg( + Arg::with_name("step") + .help("aggregation step") + .required(true) + .index(5), + ), + ) .get_matches(); if let Some(matches) = matches.subcommand_matches("grafana") { @@ -74,24 +103,39 @@ impl CLI { let to = matches.value_of("to").unwrap().parse().unwrap(); let step = matches.value_of("step").unwrap().parse().unwrap(); return CLI { - url: url.to_owned(), - key: key.to_owned(), - datasource_url: datasource_url.to_owned(), - query: query.to_owned(), - from, - to, - step, + query_config: QueryConfig { + datasource_type: subbeat::types::DatasourceType::Grafana, + url: url.to_owned(), + key: key.to_owned(), + datasource_url: datasource_url.to_owned(), + query: query.to_owned(), + from, + to, + step, + }, }; - } else { + }; + + if let Some(matches) = matches.subcommand_matches("prometheus") { + let url = matches.value_of("PROM_URL").unwrap(); + let query = matches.value_of("query").unwrap(); + let from = matches.value_of("from").unwrap().parse().unwrap(); + let to = matches.value_of("to").unwrap().parse().unwrap(); + let step = matches.value_of("step").unwrap().parse().unwrap(); return CLI { - url: "url.to_owned()".to_string(), - key: "key.to_owned()".to_string(), - datasource_url: "datasource_url.to_owned()".to_string(), - query: "query.to_owned()".to_string(), - from: 0, - to: 0, - step: 0, + query_config: QueryConfig { + datasource_type: subbeat::types::DatasourceType::Grafana, + url: url.to_owned(), + key: "key".to_owned(), + datasource_url: "datasource_url".to_owned(), + query: query.to_owned(), + from, + to, + step, + }, }; - } + }; + + panic!("Unknown datasource"); } } diff --git a/src/datasources.rs b/src/datasources.rs index c5e6c31..85443df 100644 --- a/src/datasources.rs +++ b/src/datasources.rs @@ -1 +1 @@ -pub mod grafana; \ No newline at end of file +pub mod grafana; diff --git a/src/datasources/grafana.rs b/src/datasources/grafana.rs index 273cc72..299be63 100644 --- a/src/datasources/grafana.rs +++ b/src/datasources/grafana.rs @@ -9,8 +9,6 @@ mod prometheus; use serde_json; - - pub struct Grafana { url: String, api_key: String, diff --git a/src/main.rs b/src/main.rs index e172aa5..884d8ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,22 @@ mod types; async fn main() -> types::Result<()> { let cli = cli::CLI::new(); - let gs = grafana::Grafana::new(cli.url.to_string(), cli.key.to_string()); + let gs = grafana::Grafana::new( + cli.query_config.url.to_string(), + cli.query_config.key.to_string(), + ); // gs.test_connection().await?; // gs.get_datasources().await?; // "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1" let r = gs - .extract_metrics(&cli.datasource_url, &cli.query, cli.from, cli.to, cli.step) + .extract_metrics( + &cli.query_config.datasource_url, + &cli.query_config.query, + cli.query_config.from, + cli.query_config.to, + cli.query_config.step, + ) .await?; let key = r.data.keys().nth(0).unwrap(); diff --git a/src/types.rs b/src/types.rs index 45f2b58..488f116 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,3 +3,19 @@ use anyhow; // A simple type alias so as to DRY. pub type Result = anyhow::Result; + +pub enum DatasourceType { + Grafana, + Prometheus, +} + +pub struct QueryConfig { + pub datasource_type: DatasourceType, + pub url: String, + pub key: String, + pub datasource_url: String, + pub query: String, + pub from: u64, + pub to: u64, + pub step: u64, +}