feat: add analytics metric granularity query
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
use serde_json::Value;
|
||||
use shared_kernel::{offset_datetime_to_unix_micros, parse_rfc3339};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::domain::*;
|
||||
use crate::errors::RuntimeProfileFieldError;
|
||||
@@ -502,6 +503,81 @@ pub fn build_runtime_tracking_daily_stat_id(
|
||||
)
|
||||
}
|
||||
|
||||
pub fn aggregate_runtime_tracking_daily_stats(
|
||||
stats: Vec<RuntimeAnalyticsDailyStatSnapshot>,
|
||||
event_key: &str,
|
||||
scope_kind: RuntimeTrackingScopeKind,
|
||||
scope_id: &str,
|
||||
granularity: AnalyticsGranularity,
|
||||
) -> Vec<AnalyticsBucketMetric> {
|
||||
let mut buckets: BTreeMap<(String, i64, i64), u64> = BTreeMap::new();
|
||||
let event_key = event_key.trim();
|
||||
let scope_id = scope_id.trim();
|
||||
|
||||
for stat in stats {
|
||||
if stat.event_key.trim() != event_key
|
||||
|| stat.scope_kind != scope_kind
|
||||
|| stat.scope_id.trim() != scope_id
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let dimension = build_analytics_date_dimension_from_date_key(stat.day_key);
|
||||
let (bucket_key, bucket_start_date_key, bucket_end_date_key) =
|
||||
analytics_bucket_for_dimension(&dimension, granularity);
|
||||
*buckets
|
||||
.entry((bucket_key, bucket_start_date_key, bucket_end_date_key))
|
||||
.or_insert(0) += u64::from(stat.count);
|
||||
}
|
||||
|
||||
buckets
|
||||
.into_iter()
|
||||
.map(
|
||||
|((bucket_key, bucket_start_date_key, bucket_end_date_key), value)| {
|
||||
AnalyticsBucketMetric {
|
||||
bucket_key,
|
||||
bucket_start_date_key,
|
||||
bucket_end_date_key,
|
||||
value,
|
||||
}
|
||||
},
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn analytics_bucket_for_dimension(
|
||||
dimension: &AnalyticsDateDimensionSnapshot,
|
||||
granularity: AnalyticsGranularity,
|
||||
) -> (String, i64, i64) {
|
||||
match granularity {
|
||||
AnalyticsGranularity::Day => (
|
||||
dimension.calendar_date.clone(),
|
||||
dimension.date_key,
|
||||
dimension.date_key,
|
||||
),
|
||||
AnalyticsGranularity::Week => (
|
||||
dimension.iso_week_key.to_string(),
|
||||
dimension.week_start_date_key,
|
||||
dimension.week_end_date_key,
|
||||
),
|
||||
AnalyticsGranularity::Month => (
|
||||
dimension.month_key.to_string(),
|
||||
dimension.month_start_date_key,
|
||||
dimension.month_end_date_key,
|
||||
),
|
||||
AnalyticsGranularity::Quarter => (
|
||||
dimension.quarter_key.to_string(),
|
||||
dimension.quarter_start_date_key,
|
||||
dimension.quarter_end_date_key,
|
||||
),
|
||||
AnalyticsGranularity::Year => (
|
||||
dimension.year_key.to_string(),
|
||||
dimension.year_start_date_key,
|
||||
dimension.year_end_date_key,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_runtime_profile_task_config_record(
|
||||
snapshot: RuntimeProfileTaskConfigSnapshot,
|
||||
) -> RuntimeProfileTaskConfigRecord {
|
||||
|
||||
Reference in New Issue
Block a user