Alexey Velikiy
3 years ago
7 changed files with 59 additions and 55 deletions
@ -1,50 +1,3 @@ |
|||||||
use anyhow; |
|
||||||
use hastic::services::metric_service::MetricService; |
|
||||||
use hastic::services::segments_service::Segment; |
|
||||||
use hastic::{config::Config, services::segments_service::SegmentType}; |
|
||||||
|
|
||||||
use subbeat::metric::{Metric, MetricResult}; |
|
||||||
|
|
||||||
struct AnalyticUnit { |
struct AnalyticUnit { |
||||||
metric_service: MetricService, |
// TODO: fields
|
||||||
} |
|
||||||
|
|
||||||
impl AnalyticUnit { |
|
||||||
fn new(config: &Config) -> AnalyticUnit { |
|
||||||
AnalyticUnit { |
|
||||||
metric_service: MetricService::new(&config.prom_url, &config.query), |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
pub async fn get_detections(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> { |
|
||||||
let prom = self.metric_service.get_prom(); |
|
||||||
let mr = prom.query(from, to, 10).await?; |
|
||||||
|
|
||||||
let key = mr.data.keys().nth(0).unwrap(); |
|
||||||
let ts = &mr.data[key]; |
|
||||||
|
|
||||||
let mut result = Vec::<Segment>::new(); |
|
||||||
let mut from: Option<u64> = None; |
|
||||||
for (t, v) in ts { |
|
||||||
if *v > 100.0 { |
|
||||||
if from.is_some() { |
|
||||||
continue; |
|
||||||
} else { |
|
||||||
from = Some(*t); |
|
||||||
} |
|
||||||
} else { |
|
||||||
if from.is_some() { |
|
||||||
result.push(Segment { |
|
||||||
id: None, |
|
||||||
from: from.unwrap(), |
|
||||||
to: *t, |
|
||||||
segment_type: SegmentType::Detection, |
|
||||||
}); |
|
||||||
from = None; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
Ok(result) |
|
||||||
} |
|
||||||
} |
} |
||||||
|
@ -1,3 +1,4 @@ |
|||||||
pub mod metric_service; |
pub mod metric_service; |
||||||
pub mod segments_service; |
pub mod segments_service; |
||||||
pub mod user_service; |
pub mod user_service; |
||||||
|
pub mod analytic_service; |
||||||
|
@ -0,0 +1,53 @@ |
|||||||
|
use crate::config::Config; |
||||||
|
|
||||||
|
use super::{metric_service::MetricService, segments_service::{Segment, SegmentType}}; |
||||||
|
|
||||||
|
use subbeat::metric::Metric; |
||||||
|
|
||||||
|
use anyhow; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct AnalyticService { |
||||||
|
metric_service: MetricService, |
||||||
|
} |
||||||
|
|
||||||
|
impl AnalyticService { |
||||||
|
fn new(config: &Config) -> AnalyticService { |
||||||
|
AnalyticService { |
||||||
|
metric_service: MetricService::new(&config.prom_url, &config.query), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub async fn get_detections(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> { |
||||||
|
let prom = self.metric_service.get_prom(); |
||||||
|
let mr = prom.query(from, to, 10).await?; |
||||||
|
|
||||||
|
let key = mr.data.keys().nth(0).unwrap(); |
||||||
|
let ts = &mr.data[key]; |
||||||
|
|
||||||
|
let mut result = Vec::<Segment>::new(); |
||||||
|
let mut from: Option<u64> = None; |
||||||
|
for (t, v) in ts { |
||||||
|
if *v > 100.0 { |
||||||
|
if from.is_some() { |
||||||
|
continue; |
||||||
|
} else { |
||||||
|
from = Some(*t); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if from.is_some() { |
||||||
|
result.push(Segment { |
||||||
|
id: None, |
||||||
|
from: from.unwrap(), |
||||||
|
to: *t, |
||||||
|
segment_type: SegmentType::Detection, |
||||||
|
}); |
||||||
|
from = None; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Ok(result) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue