subbeat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

use std::collections::HashMap;
use async_trait::async_trait;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use crate::{metric::{Metric, MetricResult}, types::{self, InfluxConfig}, utils::{self, normalize_url}};
use serde_qs as qs;
#[derive(Clone)]
pub struct Influx {
url: String,
org_id: String,
query: String,
token: String,
}
impl Influx {
pub fn new(cfg: &InfluxConfig) -> Influx {
Influx {
url: cfg.url.to_owned(),
org_id: cfg.org_id.to_owned(),
token: cfg.token.to_owned(),
query: cfg.query.to_owned(),
}
}
}
pub fn parse_result(value: Value) -> types::Result<MetricResult> {
Err(anyhow::format_err!("not implemented"))
}
#[async_trait]
impl Metric for Influx {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let url = format!(
"{}/api/v2/query?orgId={}",
normalize_url(self.url.to_owned()),
self.org_id
);
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), format!("Token {}", self.token).to_owned());
let (_status_code, value) = utils::post_with_headers(&url, headers).await?;
return parse_result(value);
}
}
// curl -XPOST "localhost:8086/api/v2/query?orgID=5abe4759f7360f1c" -sS \
// -H 'Accept:application/csv' \
// -H 'Authorization: Token sCAB2MVo8TJxhUH8UDJZIeCPwf-cykBtO0jhr207qCQSZ9d43JXObCYK_uAml2BL26JBYFauz95yIeC51kxQog==' \
// -H 'Content-type:application/vnd.flux' \
// -d 'from(bucket:"main-backet")
// |> range(start:-5m)
// |> filter(fn:(r) => r._measurement == "cpu")'