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

21
src/datasources.rs

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

11
src/datasources/grafana.rs

@ -1,4 +1,5 @@
use crate::metric::Metric;
use crate::types::GrafanaConfig;
use crate::{metric::MetricResult, types};
use async_trait::async_trait;
@ -18,12 +19,12 @@ pub struct Grafana {
}
impl Grafana {
pub fn new(url: String, api_key: String, datasource_url: String, query: String) -> Grafana {
pub fn new(config: &GrafanaConfig) -> Grafana {
Grafana {
api_key,
url,
datasource_url,
query,
api_key: config.api_key.to_owned(),
url: config.url.to_owned(),
datasource_url: config.datasource_url.to_owned(),
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_json::Value;
use crate::{
metric::{Metric, MetricResult},
types,
utils::{self, normalize_url},
};
use crate::{metric::{Metric, MetricResult}, types::{self, PrometheusConfig}, utils::{self, normalize_url}};
use serde_qs as qs;
@ -26,10 +22,10 @@ struct Query {
}
impl Prometheus {
pub fn new(url: &String, query: &String) -> Prometheus {
pub fn new(cfg: &PrometheusConfig) -> Prometheus {
Prometheus {
url: url.to_owned(),
query: query.to_owned(),
url: cfg.url.to_owned(),
query: cfg.query.to_owned(),
}
}
}

2
src/main.rs

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

28
src/types.rs

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

Loading…
Cancel
Save