Adapt binnedjson case
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::nodes::require_test_hosts_running;
|
||||
use crate::nodes::{require_sls_test_host_running, require_test_hosts_running};
|
||||
use chrono::{DateTime, Utc};
|
||||
use err::Error;
|
||||
use http::StatusCode;
|
||||
@@ -6,6 +6,7 @@ use hyper::Body;
|
||||
use netpod::query::{BinnedQuery, CacheUsage};
|
||||
use netpod::{log::*, AppendToUrl};
|
||||
use netpod::{AggKind, Channel, Cluster, NanoRange, APP_JSON};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
@@ -72,6 +73,28 @@ async fn get_binned_json_2_inner() -> Result<(), Error> {
|
||||
.await
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_sls_archive_1() -> Result<(), Error> {
|
||||
let fut = async move {
|
||||
let rh = require_sls_test_host_running()?;
|
||||
let cluster = &rh.cluster;
|
||||
let channel = Channel {
|
||||
backend: "sls-archive".into(),
|
||||
name: "ABOMA-CH-6G:U-DCLINK".into(),
|
||||
};
|
||||
let begstr = "2021-10-20T22:00:00Z";
|
||||
let endstr = "2021-11-12T00:00:00Z";
|
||||
let res = get_binned_json_common_res(channel, begstr, endstr, 10, AggKind::TimeWeightedScalar, cluster).await?;
|
||||
assert_eq!(res.finalised_range, true);
|
||||
assert_eq!(res.ts_anchor, 1634688000);
|
||||
assert!((res.avgs[3].unwrap() - 1.01470947265625).abs() < 1e-4);
|
||||
assert!((res.avgs[4].unwrap() - 24.06792449951172).abs() < 1e-4);
|
||||
assert!((res.avgs[11].unwrap() - 0.00274658203125).abs() < 1e-4);
|
||||
Ok(())
|
||||
};
|
||||
taskrun::run(fut)
|
||||
}
|
||||
|
||||
async fn get_binned_json_common(
|
||||
channel_name: &str,
|
||||
beg_date: &str,
|
||||
@@ -149,3 +172,62 @@ async fn get_binned_json_common(
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct BinnedResponse {
|
||||
#[serde(rename = "tsAnchor")]
|
||||
ts_anchor: u64,
|
||||
#[serde(rename = "tsMs")]
|
||||
ts_ms: Vec<u64>,
|
||||
#[serde(rename = "tsNs")]
|
||||
ts_ns: Vec<u64>,
|
||||
mins: Vec<Option<f64>>,
|
||||
maxs: Vec<Option<f64>>,
|
||||
avgs: Vec<Option<f64>>,
|
||||
counts: Vec<u64>,
|
||||
#[serde(rename = "finalisedRange", default = "bool_false")]
|
||||
finalised_range: bool,
|
||||
}
|
||||
|
||||
fn bool_false() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
async fn get_binned_json_common_res(
|
||||
channel: Channel,
|
||||
beg_date: &str,
|
||||
end_date: &str,
|
||||
bin_count: u32,
|
||||
agg_kind: AggKind,
|
||||
cluster: &Cluster,
|
||||
) -> Result<BinnedResponse, Error> {
|
||||
let t1 = Utc::now();
|
||||
let node0 = &cluster.nodes[0];
|
||||
let beg_date: DateTime<Utc> = beg_date.parse()?;
|
||||
let end_date: DateTime<Utc> = end_date.parse()?;
|
||||
let range = NanoRange::from_date_time(beg_date, end_date);
|
||||
let mut query = BinnedQuery::new(channel, range, bin_count, agg_kind);
|
||||
query.set_timeout(Duration::from_millis(15000));
|
||||
query.set_cache_usage(CacheUsage::Ignore);
|
||||
let mut url = Url::parse(&format!("http://{}:{}/api/4/binned", node0.host, node0.port))?;
|
||||
query.append_to_url(&mut url);
|
||||
let url = url;
|
||||
debug!("get_binned_json_common get {}", url);
|
||||
let req = hyper::Request::builder()
|
||||
.method(http::Method::GET)
|
||||
.uri(url.to_string())
|
||||
.header(http::header::ACCEPT, APP_JSON)
|
||||
.body(Body::empty())?;
|
||||
let client = hyper::Client::new();
|
||||
let res = client.request(req).await?;
|
||||
if res.status() != StatusCode::OK {
|
||||
error!("get_binned_json_common client response {:?}", res);
|
||||
}
|
||||
let res = hyper::body::to_bytes(res.into_body()).await?;
|
||||
let t2 = chrono::Utc::now();
|
||||
let _ms = t2.signed_duration_since(t1).num_milliseconds() as u64;
|
||||
let res = String::from_utf8_lossy(&res).to_string();
|
||||
info!("GOT: {}", res);
|
||||
let res: BinnedResponse = serde_json::from_str(res.as_str())?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user