Browse Source

query url params

main
Alexey Velikiy 3 years ago
parent
commit
c3a1c4877a
  1. 11
      src/grafana_service.rs
  2. 9
      src/grafana_service/prometheus.rs
  3. 3
      src/main.rs
  4. 2
      src/metric.rs

11
src/grafana_service.rs

@ -34,15 +34,14 @@ impl GrafanaService {
Ok(())
}
pub async fn extract_metrics(&self, panel_url: &str) -> types::Result<MetricResult> {
pub async fn extract_metrics(&self, datasource_url: &str, query: &str, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let pm = prometheus::Prometheus::new(
self,
"/api/datasources/proxy/1/api/v1/query_range",
"rate(go_memstats_alloc_bytes_total[5m])",
15,
datasource_url,
query
);
let r = pm.query(1634672070, 1634672970).await?;
// TODO: split big query to chunks
let r = pm.query(from, to, step).await?;
Ok(r)
}

9
src/grafana_service/prometheus.rs

@ -15,16 +15,15 @@ use super::GrafanaService;
pub struct Prometheus<'a> {
url: String,
query: String,
step: u32,
grafana_service: &'a GrafanaService,
}
#[derive(Deserialize, Serialize)]
struct Query {
query: String,
step: u32,
start: u64,
end: u64,
step: u64
}
impl<'a> Prometheus<'a> {
@ -32,13 +31,11 @@ impl<'a> Prometheus<'a> {
grafana_service: &'a GrafanaService,
url: &str,
query: &str,
step: u32,
) -> Prometheus<'a> {
Prometheus {
url: url.to_owned(),
grafana_service,
query: query.to_string(),
step,
}
}
}
@ -78,10 +75,10 @@ fn parse_result(value: Value) -> types::Result<MetricResult> {
#[async_trait]
impl Metric for Prometheus<'_> {
async fn query(&self, from: u64, to: u64) -> types::Result<MetricResult> {
async fn query(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let q = Query {
query: self.query.to_owned(),
step: self.step,
step: step,
start: from,
end: to,
};

3
src/main.rs

@ -29,7 +29,8 @@ async fn main() -> types::Result<()> {
// gs.test_connection().await?;
// gs.get_datasources().await?;
let r = gs.extract_metrics("http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1")
// "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1"
let r = gs.extract_metrics("/api/datasources/proxy/1/api/v1/query_range", "rate(go_memstats_alloc_bytes_total[5m])", 1634672070, 1634672970, 15)
.await?;
let key = r.keys().nth(0).unwrap();

2
src/metric.rs

@ -30,5 +30,5 @@ pub type MetricResult = HashMap<String, Vec<(u64, f64)>>;
#[async_trait]
pub trait Metric {
async fn query(&self, from: u64, to: u64) -> types::Result<MetricResult>;
async fn query(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult>;
}

Loading…
Cancel
Save