Read internal event data frames also through http

This commit is contained in:
Dominik Werder
2023-08-17 14:36:04 +02:00
parent 92e58291f3
commit 954cff05f0
19 changed files with 460 additions and 146 deletions
+1
View File
@@ -1,5 +1,6 @@
pub mod binned;
pub mod databuffer_tools;
pub mod eventdata;
pub mod events;
pub mod search;
pub mod status;
+89
View File
@@ -0,0 +1,89 @@
use crate::response;
use crate::response_err;
use crate::ReqCtx;
use err::thiserror;
use err::ThisError;
use err::ToPublicError;
use futures_util::TryStreamExt;
use http::Method;
use http::Request;
use http::Response;
use http::StatusCode;
use hyper::Body;
use netpod::log::*;
use netpod::NodeConfigCached;
use netpod::ServiceVersion;
#[derive(Debug, ThisError)]
pub enum EventDataError {
HttpBadMethod,
HttpBadAccept,
QueryParse,
#[error("Error({0})")]
Error(Box<dyn ToPublicError>),
InternalError,
}
impl ToPublicError for EventDataError {
fn to_public_error(&self) -> String {
match self {
EventDataError::HttpBadMethod => format!("{self}"),
EventDataError::HttpBadAccept => format!("{self}"),
EventDataError::QueryParse => format!("{self}"),
EventDataError::Error(e) => e.to_public_error(),
EventDataError::InternalError => format!("{self}"),
}
}
}
pub struct EventDataHandler {}
impl EventDataHandler {
pub fn handler(req: &Request<Body>) -> Option<Self> {
if req.uri().path().eq("/api/4/private/eventdata/frames") {
Some(Self {})
} else {
None
}
}
pub async fn handle(
&self,
req: Request<Body>,
_ctx: &ReqCtx,
ncc: &NodeConfigCached,
_service_version: &ServiceVersion,
) -> Result<Response<Body>, EventDataError> {
if req.method() != Method::POST {
Ok(response(StatusCode::NOT_ACCEPTABLE)
.body(Body::empty())
.map_err(|_| EventDataError::InternalError)?)
} else {
match Self::handle_req(req, ncc).await {
Ok(ret) => Ok(ret),
Err(e) => {
error!("{e}");
let res = response_err(StatusCode::NOT_ACCEPTABLE, e.to_public_error())
.map_err(|_| EventDataError::InternalError)?;
Ok(res)
}
}
}
}
async fn handle_req(req: Request<Body>, ncc: &NodeConfigCached) -> Result<Response<Body>, EventDataError> {
let (_head, body) = req.into_parts();
let frames =
nodenet::conn::events_get_input_frames(body.map_err(|e| err::Error::with_msg_no_trace(e.to_string())))
.await
.map_err(|_| EventDataError::InternalError)?;
let (evsubq,) = nodenet::conn::events_parse_input_query(frames).map_err(|_| EventDataError::QueryParse)?;
let stream = nodenet::conn::create_response_bytes_stream(evsubq, ncc)
.await
.map_err(|e| EventDataError::Error(Box::new(e)))?;
let ret = response(StatusCode::OK)
.body(Body::wrap_stream(stream))
.map_err(|_| EventDataError::InternalError)?;
Ok(ret)
}
}
+20 -22
View File
@@ -132,14 +132,9 @@ pub async fn host(node_config: NodeConfigCached, service_version: ServiceVersion
if let Some(bind) = node_config.node.prometheus_api_bind {
tokio::spawn(prometheus::host(bind));
}
let _update_task = if node_config.node_config.cluster.run_map_pulse_task {
Some(UpdateTask::new(node_config.clone()))
} else {
None
};
let rawjh = taskrun::spawn(events_service(node_config.clone()));
// let rawjh = taskrun::spawn(events_service(node_config.clone()));
use std::str::FromStr;
let addr = SocketAddr::from_str(&format!("{}:{}", node_config.node.listen, node_config.node.port))?;
let addr = SocketAddr::from_str(&format!("{}:{}", node_config.node.listen(), node_config.node.port))?;
let make_service = make_service_fn({
move |conn: &AddrStream| {
debug!("new connection from {:?}", conn.remote_addr());
@@ -147,20 +142,19 @@ pub async fn host(node_config: NodeConfigCached, service_version: ServiceVersion
let addr = conn.remote_addr();
let service_version = service_version.clone();
async move {
Ok::<_, Error>(service_fn({
move |req| {
// TODO send to logstash
info!(
"http-request {:?} - {:?} - {:?} - {:?}",
addr,
req.method(),
req.uri(),
req.headers()
);
let f = http_service(req, node_config.clone(), service_version.clone());
Cont { f: Box::pin(f) }
}
}))
let ret = service_fn(move |req| {
// TODO send to logstash
info!(
"http-request {:?} - {:?} - {:?} - {:?}",
addr,
req.method(),
req.uri(),
req.headers()
);
let f = http_service(req, node_config.clone(), service_version.clone());
Cont { f: Box::pin(f) }
});
Ok::<_, Error>(ret)
}
}
});
@@ -168,7 +162,7 @@ pub async fn host(node_config: NodeConfigCached, service_version: ServiceVersion
.serve(make_service)
.await
.map(|e| RetrievalError::TextError(format!("{e:?}")))?;
rawjh.await??;
// rawjh.await??;
Ok(())
}
@@ -382,6 +376,10 @@ async fn http_service_inner(
} else {
Ok(response(StatusCode::METHOD_NOT_ALLOWED).body(Body::empty())?)
}
} else if let Some(h) = api4::eventdata::EventDataHandler::handler(&req) {
Ok(h.handle(req, ctx, &node_config, service_version)
.await
.map_err(|e| Error::with_msg_no_trace(e.to_string()))?)
} else if let Some(h) = api4::status::StatusNodesRecursive::handler(&req) {
Ok(h.handle(req, ctx, &node_config, service_version).await?)
} else if let Some(h) = StatusBoardAllHandler::handler(&req) {
+5 -3
View File
@@ -164,7 +164,6 @@ pub struct MapPulseHisto {
_counts: Vec<u64>,
}
const MAP_INDEX_FULL_URL_PREFIX: &'static str = "/api/1/map/index/full/";
const _MAP_INDEX_FAST_URL_PREFIX: &'static str = "/api/1/map/index/fast/";
const MAP_PULSE_HISTO_URL_PREFIX: &'static str = "/api/1/map/pulse/histo/";
const MAP_PULSE_URL_PREFIX: &'static str = "/api/1/map/pulse/";
@@ -490,7 +489,7 @@ pub struct IndexFullHttpFunction {}
impl IndexFullHttpFunction {
pub fn handler(req: &Request<Body>) -> Option<Self> {
if req.uri().path().starts_with(MAP_INDEX_FULL_URL_PREFIX) {
if req.uri().path().eq("/api/1/map/index/full") {
Some(Self {})
} else {
None
@@ -724,7 +723,10 @@ impl Future for UpdateTask {
}
impl UpdateTask {
pub fn new(node_config: NodeConfigCached) -> UpdateTaskGuard {
/// Returns a guard which must be kept alive as long as the service should run.
/// Should instead of this use a system-timer and call the rest api.
#[allow(unused)]
fn new(node_config: NodeConfigCached) -> UpdateTaskGuard {
let do_abort = Arc::new(AtomicUsize::default());
let task = Self {
do_abort: do_abort.clone(),