Skip to main content

hyprstream_metrics_api/storage/
view.rs

1use crate::aggregation::{AggregateFunction, GroupBy, TimeWindow};
2use arrow::datatypes::{DataType, Field, Fields, Schema, TimeUnit};
3use serde::{Deserialize, Serialize};
4use std::collections::HashSet;
5use std::sync::Arc;
6use std::time::SystemTime;
7
8/// Serializable schema representation
9#[derive(Debug, Clone, Serialize, Deserialize)]
10struct SerializableSchema {
11    fields: Vec<SerializableField>,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15struct SerializableField {
16    name: String,
17    data_type: String,
18    nullable: bool,
19}
20
21impl From<&Schema> for SerializableSchema {
22    fn from(schema: &Schema) -> Self {
23        SerializableSchema {
24            fields: schema
25                .fields()
26                .iter()
27                .map(|f| SerializableField {
28                    name: f.name().clone(),
29                    data_type: format!("{:?}", f.data_type()),
30                    nullable: f.is_nullable(),
31                })
32                .collect(),
33        }
34    }
35}
36
37impl From<SerializableSchema> for Schema {
38    fn from(schema: SerializableSchema) -> Self {
39        let fields: Fields = schema
40            .fields
41            .into_iter()
42            .map(|f| {
43                Field::new(
44                    &f.name,
45                    // Parse data type from string representation
46                    match f.data_type.as_str() {
47                        "Int64" => DataType::Int64,
48                        "Float64" => DataType::Float64,
49                        "Timestamp(Nanosecond, None)" => {
50                            DataType::Timestamp(TimeUnit::Nanosecond, None)
51                        }
52                        // "Utf8" and all other types default to string
53                        _ => DataType::Utf8,
54                    },
55                    f.nullable,
56                )
57            })
58            .collect::<Vec<Field>>()
59            .into();
60
61        Schema::new(fields)
62    }
63}
64
65/// Specification for a view aggregation
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct AggregationSpec {
68    pub column: String,
69    pub function: AggregateFunction,
70}
71
72/// Complete definition of a view
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct ViewDefinition {
75    pub source_table: String,
76    pub columns: Vec<String>,
77    pub aggregations: Vec<AggregationSpec>,
78    pub group_by: Option<GroupBy>,
79    pub window: Option<TimeWindow>,
80    pub dependencies: HashSet<String>,
81    #[serde(
82        serialize_with = "serialize_schema",
83        deserialize_with = "deserialize_schema"
84    )]
85    pub schema: Arc<Schema>,
86}
87
88fn serialize_schema<S>(schema: &Arc<Schema>, serializer: S) -> Result<S::Ok, S::Error>
89where
90    S: serde::Serializer,
91{
92    SerializableSchema::from(schema.as_ref()).serialize(serializer)
93}
94
95fn deserialize_schema<'de, D>(deserializer: D) -> Result<Arc<Schema>, D::Error>
96where
97    D: serde::Deserializer<'de>,
98{
99    let serializable = SerializableSchema::deserialize(deserializer)?;
100    Ok(Arc::new(Schema::from(serializable)))
101}
102
103/// Metadata for a view stored in the backend
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct ViewMetadata {
106    pub name: String,
107    pub definition: ViewDefinition,
108    pub created_at: SystemTime,
109}
110
111impl ViewDefinition {
112    /// Create a new view definition
113    pub fn new(
114        source_table: String,
115        columns: Vec<String>,
116        aggregations: Vec<AggregationSpec>,
117        group_by: Option<GroupBy>,
118        window: Option<TimeWindow>,
119        schema: Arc<Schema>,
120    ) -> Self {
121        // Calculate dependencies from source table and any referenced columns
122        let mut dependencies = HashSet::new();
123        dependencies.insert(source_table.clone());
124
125        Self {
126            source_table,
127            columns,
128            aggregations,
129            group_by,
130            window,
131            dependencies,
132            schema,
133        }
134    }
135
136    /// Build the SQL definition for this view
137    pub fn to_sql(&self) -> String {
138        let mut sql = String::from("SELECT ");
139
140        // Add columns
141        let mut first = true;
142        for col in &self.columns {
143            if !first {
144                sql.push_str(", ");
145            }
146            sql.push_str(col);
147            first = false;
148        }
149
150        // Add aggregations
151        for agg in &self.aggregations {
152            if !first {
153                sql.push_str(", ");
154            }
155            sql.push_str(&format!(
156                "{}({}) as {}_{}",
157                agg.function,
158                agg.column,
159                agg.function.to_string().to_lowercase(),
160                agg.column
161            ));
162            first = false;
163        }
164
165        // Add FROM clause
166        sql.push_str(&format!(" FROM {}", self.source_table));
167
168        // Add GROUP BY if present
169        if let Some(group_by) = &self.group_by {
170            if !group_by.columns.is_empty() {
171                sql.push_str(" GROUP BY ");
172                sql.push_str(&group_by.columns.join(", "));
173            }
174        }
175
176        sql
177    }
178}
179
180impl ViewMetadata {
181    /// Create new view metadata
182    pub fn new(name: String, definition: ViewDefinition) -> Self {
183        Self {
184            name,
185            definition,
186            created_at: SystemTime::now(),
187        }
188    }
189}