diff --git a/src/console/api.rs b/src/console/api.rs index 9d6fde6b..c29655c8 100644 --- a/src/console/api.rs +++ b/src/console/api.rs @@ -314,7 +314,8 @@ pub fn console_api_config_v2(config: &mut web::ServiceConfig) { ) .service( web::resource("/metrics/timeline") - .route(web::get().to(v2::metrics_api::query_metrics_timeline)), + .route(web::get().to(v2::metrics_api::query_metrics_timeline)) + .route(web::post().to(v2::metrics_api::query_metrics_timeline_json)), ), ); } diff --git a/src/console/model/metrics_model.rs b/src/console/model/metrics_model.rs index c11bd31a..08313ea1 100644 --- a/src/console/model/metrics_model.rs +++ b/src/console/model/metrics_model.rs @@ -8,19 +8,25 @@ pub struct TimelineQueryRequest { // LEAST | MINUTE pub timeline_group_name: Option, //pub query_all_key: bool, - pub keys: Option, + // 用逗号分割 + pub string_key: Option, + // string_key与keys只取其中一个 + pub keys: Option>, pub node_id: Option, } impl From for TimelineQueryParam { fn from(value: TimelineQueryRequest) -> Self { - let keys = value - .keys - .unwrap_or_default() - .split(",") - .filter(|e| !e.is_empty()) - .map(|e| e.to_string()) - .collect(); + let mut keys = value.keys.unwrap_or_default(); + if keys.is_empty() { + keys = value + .string_key + .unwrap_or_default() + .split(",") + .filter(|e| !e.is_empty()) + .map(|e| e.to_string()) + .collect(); + } Self { start_time: value.start_time.unwrap_or_default(), timeline_group_name: value.timeline_group_name.unwrap_or_default(), diff --git a/src/console/v2/metrics_api.rs b/src/console/v2/metrics_api.rs index bcfddb8d..4b391fac 100644 --- a/src/console/v2/metrics_api.rs +++ b/src/console/v2/metrics_api.rs @@ -24,6 +24,20 @@ pub async fn query_metrics_timeline( } } +pub async fn query_metrics_timeline_json( + app: Data>, + web::Json(req): web::Json, +) -> actix_web::Result { + let param: TimelineQueryParam = req.into(); + match do_query_metrics_timeline(app, param).await { + Ok(v) => Ok(v), + Err(err) => Ok(HttpResponse::Ok().json(ApiResult::<()>::error( + "SYSTEM_ERROR".to_owned(), + Some(err.to_string()), + ))), + } +} + async fn do_query_metrics_timeline( app: Data>, param: TimelineQueryParam, diff --git a/src/metrics/core.rs b/src/metrics/core.rs index e25be429..04dfa608 100644 --- a/src/metrics/core.rs +++ b/src/metrics/core.rs @@ -314,6 +314,8 @@ impl Inject for MetricsManager { if let Some(sys_config) = sys_config { self.metrics_enable = sys_config.metrics_enable; self.collect_interval = sys_config.metrics_collect_interval_second; + self.metrics_timeline_manager + .set_least_interval(self.collect_interval); self.log_interval = sys_config.metrics_log_interval_second; if self.metrics_enable { log::info!("metrics enable! log_interval: {}s", self.log_interval); diff --git a/src/metrics/timeline/core.rs b/src/metrics/timeline/core.rs index 3125441b..3d29f3c7 100644 --- a/src/metrics/timeline/core.rs +++ b/src/metrics/timeline/core.rs @@ -10,14 +10,16 @@ pub struct TimelineGroup { timelines: LinkedList, pub(crate) limit_count: usize, pub(crate) last_time: u64, + pub(crate) interval_second: u64, } impl TimelineGroup { - pub fn new(limit_count: usize) -> Self { + pub fn new(limit_count: usize, interval: u64) -> Self { Self { timelines: LinkedList::new(), limit_count, last_time: 0, + interval_second: interval, } } @@ -98,6 +100,7 @@ impl TimelineGroup { last_time, from_node_id: 0, time_index, + interval_second: self.interval_second, gauge_data, summery_data, } @@ -139,11 +142,15 @@ pub struct MetricsTimelineManager { impl MetricsTimelineManager { pub fn new() -> Self { Self { - minute_timeline_group: TimelineGroup::new(360), - least_timeline_group: TimelineGroup::new(360), + minute_timeline_group: TimelineGroup::new(360, 60), + least_timeline_group: TimelineGroup::new(360, 15), } } + pub fn set_least_interval(&mut self, least_interval: u64) { + self.least_timeline_group.interval_second = least_interval; + } + pub fn add_minute_record(&mut self, snapshot: MetricsSnapshot) { self.minute_timeline_group.add_record(snapshot); } diff --git a/src/metrics/timeline/model.rs b/src/metrics/timeline/model.rs index 8d0266c0..6902a7a4 100644 --- a/src/metrics/timeline/model.rs +++ b/src/metrics/timeline/model.rs @@ -158,6 +158,7 @@ pub struct TimelineQueryResponse { pub last_time: u64, pub from_node_id: u64, pub time_index: Vec, + pub interval_second: u64, pub gauge_data: HashMap>, pub summery_data: HashMap, }