|
1 | | -use anyhow::Result; |
2 | | -use core::panic; |
3 | | -use serde::{Deserialize, Serialize}; |
4 | | -use serde_yaml::Value; |
5 | | -use std::collections::HashMap; |
6 | | -use std::fs::File; |
7 | | -use std::io::BufReader; |
8 | | -use std::ops::Index; |
9 | | - |
10 | | -use crate::data_model::aggregation_config::{AggregationConfig, AggregationIdInfo}; |
11 | | -use crate::data_model::enums::QueryLanguage; |
12 | | -use crate::data_model::inference_config::{InferenceConfig, SchemaConfig}; |
13 | | -use sketch_db_common::capability_matching::find_compatible_aggregation as common_find_compatible; |
14 | | -use sketch_db_common::query_requirements::QueryRequirements; |
15 | | - |
16 | | -#[derive(Debug, Clone, Serialize, Deserialize)] |
17 | | -pub struct StreamingConfig { |
18 | | - pub aggregation_configs: HashMap<u64, AggregationConfig>, |
19 | | -} |
20 | | - |
21 | | -impl StreamingConfig { |
22 | | - pub fn new(aggregation_configs: HashMap<u64, AggregationConfig>) -> Self { |
23 | | - Self { |
24 | | - aggregation_configs, |
25 | | - } |
26 | | - } |
27 | | - |
28 | | - pub fn get_aggregation_config(&self, aggregation_id: u64) -> Option<&AggregationConfig> { |
29 | | - self.aggregation_configs.get(&aggregation_id) |
30 | | - } |
31 | | - |
32 | | - pub fn get_all_aggregation_configs(&self) -> &HashMap<u64, AggregationConfig> { |
33 | | - &self.aggregation_configs |
34 | | - } |
35 | | - |
36 | | - pub fn contains(&self, aggregation_id: u64) -> bool { |
37 | | - self.aggregation_configs.contains_key(&aggregation_id) |
38 | | - } |
39 | | - |
40 | | - pub fn from_yaml_file(yaml_file: &str) -> Result<Self> { |
41 | | - let file = File::open(yaml_file)?; |
42 | | - let reader = BufReader::new(file); |
43 | | - let data: Value = serde_yaml::from_reader(reader)?; |
44 | | - |
45 | | - Self::from_yaml_data(&data, None) |
46 | | - } |
47 | | - |
48 | | - pub fn from_yaml_data( |
49 | | - data: &Value, |
50 | | - inference_config: Option<&InferenceConfig>, |
51 | | - ) -> Result<Self> { |
52 | | - let mut retention_map: HashMap<u64, u64> = HashMap::new(); |
53 | | - let mut read_count_threshold_map: HashMap<u64, u64> = HashMap::new(); |
54 | | - |
55 | | - if let Some(inference_config) = inference_config { |
56 | | - for query_config in &inference_config.query_configs { |
57 | | - for aggregation in &query_config.aggregations { |
58 | | - let aggregation_id = aggregation.aggregation_id; |
59 | | - if let Some(num_aggregates) = aggregation.num_aggregates_to_retain { |
60 | | - // OLD: Keep last value only (for backwards compatibility) |
61 | | - retention_map.insert(aggregation_id, num_aggregates); |
62 | | - |
63 | | - // NEW: Sum up num_aggregates_to_retain across all queries |
64 | | - *read_count_threshold_map.entry(aggregation_id).or_insert(0) += |
65 | | - num_aggregates; |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - // Derive query_language from inference_config schema |
72 | | - let query_language = inference_config |
73 | | - .map(|ic| match &ic.schema { |
74 | | - SchemaConfig::PromQL(_) => QueryLanguage::promql, |
75 | | - SchemaConfig::SQL(_) => QueryLanguage::sql, |
76 | | - SchemaConfig::ElasticQueryDSL => QueryLanguage::elastic_querydsl, |
77 | | - SchemaConfig::ElasticSQL(_) => QueryLanguage::elastic_sql, |
78 | | - }) |
79 | | - .unwrap_or(QueryLanguage::promql); // Default to promql if no inference_config |
80 | | - |
81 | | - let mut aggregation_configs: HashMap<u64, AggregationConfig> = HashMap::new(); |
82 | | - |
83 | | - if let Some(aggregations) = data.get("aggregations").and_then(|v| v.as_sequence()) { |
84 | | - for aggregation_data in aggregations { |
85 | | - if let Some(aggregation_id) = aggregation_data.get("aggregationId") { |
86 | | - let aggregation_id_u64 = aggregation_id.as_u64().or_else(|| panic!()).unwrap(); |
87 | | - let num_aggregates_to_retain = retention_map.get(&aggregation_id_u64); |
88 | | - let read_count_threshold = read_count_threshold_map.get(&aggregation_id_u64); |
89 | | - let config = AggregationConfig::from_yaml_data( |
90 | | - aggregation_data, |
91 | | - num_aggregates_to_retain.copied(), |
92 | | - read_count_threshold.copied(), |
93 | | - query_language, |
94 | | - )?; |
95 | | - aggregation_configs.insert(aggregation_id_u64, config); |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - Ok(Self::new(aggregation_configs)) |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -impl StreamingConfig { |
105 | | - /// Find a compatible aggregation for the given requirements using capability-based matching. |
106 | | - /// Delegates to `sketch_db_common::find_compatible_aggregation`. |
107 | | - pub fn find_compatible_aggregation( |
108 | | - &self, |
109 | | - requirements: &QueryRequirements, |
110 | | - ) -> Option<AggregationIdInfo> { |
111 | | - common_find_compatible(&self.aggregation_configs, requirements) |
112 | | - } |
113 | | -} |
114 | | - |
115 | | -impl Index<u64> for StreamingConfig { |
116 | | - type Output = AggregationConfig; |
117 | | - |
118 | | - fn index(&self, aggregation_id: u64) -> &Self::Output { |
119 | | - &self.aggregation_configs[&aggregation_id] |
120 | | - } |
121 | | -} |
122 | | - |
123 | | -impl Default for StreamingConfig { |
124 | | - fn default() -> Self { |
125 | | - Self::new(HashMap::new()) |
126 | | - } |
127 | | -} |
| 1 | +pub use sketch_db_common::streaming_config::*; |
0 commit comments