Browse Source

prom begin

main
Alexey Velikiy 3 years ago
parent
commit
e07b060e0f
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 92
      src/cli.rs
  4. 2
      src/datasources.rs
  5. 2
      src/datasources/grafana.rs
  6. 13
      src/main.rs
  7. 16
      src/types.rs

2
Cargo.lock generated

@ -690,7 +690,7 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "subbeat"
version = "0.0.3"
version = "0.0.4"
dependencies = [
"anyhow",
"async-trait",

2
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"

92
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");
}
}

2
src/datasources.rs

@ -1 +1 @@
pub mod grafana;
pub mod grafana;

2
src/datasources/grafana.rs

@ -9,8 +9,6 @@ mod prometheus;
use serde_json;
pub struct Grafana {
url: String,
api_key: String,

13
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();

16
src/types.rs

@ -3,3 +3,19 @@ use anyhow;
// A simple type alias so as to DRY.
pub type Result<T> = anyhow::Result<T>;
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,
}

Loading…
Cancel
Save