Browse Source

qurieng continue

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
5f70beb1bb
  1. 2
      client/.env.development
  2. 2
      client/.env.production
  3. 3
      server/src/api.rs
  4. 46
      server/src/api/metric.rs
  5. 10
      server/src/services/metric_service.rs

2
client/.env.development

@ -1 +1 @@
VUE_APP_API_URL="http://localhost:8000/api/"
VUE_APP_API_URL="http://localhost:8000/api/"

2
client/.env.production

@ -1 +1 @@
VUE_APP_API_URL="/api/"
VUE_APP_API_URL="/api/"

3
server/src/api.rs

@ -66,12 +66,13 @@ impl API<'_> {
message: "ok".to_owned(),
})
});
let metrics = metric::get_route(self.metric_service.clone());
let login = auth::get_route(self.user_service.clone());
let public = warp::fs::dir("public");
println!("Start server on 8000 port");
// TODO: move it to "server"
let routes = login.or(options).or(public).or(not_found);
let routes = login.or(metrics).or(options).or(public).or(not_found);
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
}
}

46
server/src/api/metric.rs

@ -1,10 +1,13 @@
use hastic::services::{metric_service, user_service};
use subbeat::metric::Metric;
use warp::filters::method::get;
use warp::http::HeaderValue;
use warp::hyper::server::conn::Http;
use warp::hyper::{Body, StatusCode};
use warp::reject::Reject;
use warp::{http::Response, Filter};
use warp::{Rejection, Reply};
use warp::{reject, Rejection, Reply};
use serde::Serialize;
@ -12,19 +15,48 @@ use crate::api::{self, API};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::convert::Infallible;
use std::fmt::Debug;
use std::sync::Arc;
#[derive(Serialize)]
struct QueryResponse {}
struct QueryResponse {
message: String,
}
#[derive(Debug)]
struct BadQuery;
impl Reject for BadQuery {}
async fn query(
p: HashMap<String, String>,
ms: Arc<RwLock<metric_service::MetricService>>,
) -> Result<impl warp::Reply, warp::Rejection> {
if !p.contains_key("from") || !p.contains_key("to") || !p.contains_key("step") {
return Err(warp::reject::custom(BadQuery));
}
let from = p.get("from").unwrap().parse::<u64>().unwrap();
let to = p.get("to").unwrap().parse::<u64>().unwrap();
let step = p.get("step").unwrap().parse::<u64>().unwrap();
let prom = ms.read().get_prom();
drop(ms);
let res = prom.query(from, to, step).await;
// let pm = subbeat::datasources::prometheus::Prometheus::new(&"http://".to_owned(), &"asd".to_owned());
// let r = pm.query(from, to, step).await;
Ok(API::json(&QueryResponse {
message: "hello".to_string(),
}))
}
pub fn get_route(
metric_service: Arc<RwLock<user_service::UserService>>,
metric_service: Arc<RwLock<metric_service::MetricService>>,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
return warp::path!("api" / "metric")
.and(get())
.and(warp::query::<HashMap<String, String>>())
.map(move |p: HashMap<String, String>| {
let qr = QueryResponse {};
return api::API::json(&qr);
});
.and(warp::any().map(move || metric_service.clone()))
.and_then(query);
}

10
server/src/services/metric_service.rs

@ -1,13 +1,19 @@
use subbeat::datasources::prometheus::Prometheus;
pub struct MetricService {
prom: Prometheus,
url: String,
query: String,
}
impl MetricService {
pub fn new(url: &str, query: &str) -> MetricService {
MetricService {
prom: Prometheus::new(&url.to_string(), &query.to_string()),
url: url.to_string(),
query: url.to_string(),
}
}
pub fn get_prom(&self) -> Prometheus {
Prometheus::new(&self.url.to_string(), &self.query.to_string())
}
}

Loading…
Cancel
Save