Browse Source

datasourceConfig

main
Alexey Velikiy 3 years ago
parent
commit
20eb2999b6
  1. 16
      src/cli.rs
  2. 21
      src/datasources.rs
  3. 11
      src/datasources/grafana.rs
  4. 12
      src/datasources/prometheus.rs
  5. 2
      src/main.rs
  6. 28
      src/types.rs

16
src/cli.rs

@ -1,5 +1,5 @@
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, SubCommand};
use subbeat::types::QueryConfig; use subbeat::{datasources::grafana::Grafana, types::{DatasourceConfig, GrafanaConfig, PrometheusConfig, QueryConfig}};
pub struct CLI { pub struct CLI {
pub query_config: QueryConfig, pub query_config: QueryConfig,
@ -104,11 +104,12 @@ impl CLI {
let step = matches.value_of("step").unwrap().parse().unwrap(); let step = matches.value_of("step").unwrap().parse().unwrap();
return CLI { return CLI {
query_config: QueryConfig { query_config: QueryConfig {
datasource_type: subbeat::types::DatasourceType::Grafana, datasource_config: DatasourceConfig::Grafana(GrafanaConfig {
url: url.to_owned(), url: url.to_owned(),
key: key.to_owned(), api_key: key.to_owned(),
datasource_url: datasource_url.to_owned(), datasource_url: datasource_url.to_owned(),
query: query.to_owned(), query: query.to_owned()
}),
from, from,
to, to,
step, step,
@ -124,11 +125,10 @@ impl CLI {
let step = matches.value_of("step").unwrap().parse().unwrap(); let step = matches.value_of("step").unwrap().parse().unwrap();
return CLI { return CLI {
query_config: QueryConfig { query_config: QueryConfig {
datasource_type: subbeat::types::DatasourceType::Prometheus, datasource_config: DatasourceConfig::Prometheus(PrometheusConfig {
url: url.to_owned(), url: url.to_owned(),
key: "key".to_owned(), query: query.to_owned()
datasource_url: "datasource_url".to_owned(), }),
query: query.to_owned(),
from, from,
to, to,
step, step,

21
src/datasources.rs

@ -1,22 +1,11 @@
use crate::{ use crate::{metric::Metric, types::{DatasourceConfig}};
metric::Metric,
types::{DatasourceType, QueryConfig},
};
pub mod grafana; pub mod grafana;
pub mod prometheus; pub mod prometheus;
pub fn resolve(query_config: &QueryConfig) -> Box<dyn Metric + Sync> { pub fn resolve(config: &DatasourceConfig) -> Box<dyn Metric + Sync> {
if query_config.datasource_type == DatasourceType::Grafana { match config {
let gs = grafana::Grafana::new( DatasourceConfig::Grafana(cfg) => Box::new(grafana::Grafana::new(cfg)),
query_config.url.to_string(), DatasourceConfig::Prometheus(cfg) => Box::new(prometheus::Prometheus::new(cfg))
query_config.key.to_string(),
query_config.datasource_url.to_string(),
query_config.query.to_string(),
);
return Box::new(gs);
} else {
let pm = prometheus::Prometheus::new(&query_config.url, &query_config.query);
return Box::new(pm);
} }
} }

11
src/datasources/grafana.rs

@ -1,4 +1,5 @@
use crate::metric::Metric; use crate::metric::Metric;
use crate::types::GrafanaConfig;
use crate::{metric::MetricResult, types}; use crate::{metric::MetricResult, types};
use async_trait::async_trait; use async_trait::async_trait;
@ -18,12 +19,12 @@ pub struct Grafana {
} }
impl Grafana { impl Grafana {
pub fn new(url: String, api_key: String, datasource_url: String, query: String) -> Grafana { pub fn new(config: &GrafanaConfig) -> Grafana {
Grafana { Grafana {
api_key, api_key: config.api_key.to_owned(),
url, url: config.url.to_owned(),
datasource_url, datasource_url: config.datasource_url.to_owned(),
query, query: config.query.to_owned(),
} }
} }

12
src/datasources/prometheus.rs

@ -3,11 +3,7 @@ use async_trait::async_trait;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use crate::{ use crate::{metric::{Metric, MetricResult}, types::{self, PrometheusConfig}, utils::{self, normalize_url}};
metric::{Metric, MetricResult},
types,
utils::{self, normalize_url},
};
use serde_qs as qs; use serde_qs as qs;
@ -26,10 +22,10 @@ struct Query {
} }
impl Prometheus { impl Prometheus {
pub fn new(url: &String, query: &String) -> Prometheus { pub fn new(cfg: &PrometheusConfig) -> Prometheus {
Prometheus { Prometheus {
url: url.to_owned(), url: cfg.url.to_owned(),
query: query.to_owned(), query: cfg.query.to_owned(),
} }
} }
} }

2
src/main.rs

@ -9,7 +9,7 @@ mod types;
async fn main() -> types::Result<()> { async fn main() -> types::Result<()> {
let cli = cli::CLI::new(); let cli = cli::CLI::new();
let ds = resolve(&cli.query_config); let ds = resolve(&cli.query_config.datasource_config);
let r = ds let r = ds
.query( .query(

28
src/types.rs

@ -4,19 +4,31 @@ use anyhow;
pub type Result<T> = anyhow::Result<T>; pub type Result<T> = anyhow::Result<T>;
#[derive(PartialEq)]
pub enum DatasourceType { pub struct PrometheusConfig {
Grafana, pub url: String,
Prometheus, pub query: String,
} }
pub struct QueryConfig {
pub datasource_type: DatasourceType, pub struct GrafanaConfig {
pub url: String, pub url: String,
pub key: String, pub api_key: String,
pub datasource_url: String, pub datasource_url: String,
pub query: String, pub query: String,
}
pub enum DatasourceConfig {
Grafana(GrafanaConfig),
Prometheus(PrometheusConfig)
}
pub struct QueryConfig {
pub datasource_config: DatasourceConfig,
pub from: u64, pub from: u64,
pub to: u64, pub to: u64,
pub step: u64, pub step: u64
} }

Loading…
Cancel
Save