scirs2_io/database/
timeseries.rs1use crate::error::Result;
4use scirs2_core::ndarray::Array2;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct TimePoint {
9 pub timestamp: i64,
10 pub value: f64,
11 pub metadata: Option<serde_json::Value>,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct TimeSeries {
16 pub name: String,
17 pub points: Vec<TimePoint>,
18 pub tags: std::collections::HashMap<String, String>,
19}
20
21impl TimeSeries {
22 pub fn new(name: impl Into<String>) -> Self {
23 Self {
24 name: name.into(),
25 points: Vec::new(),
26 tags: std::collections::HashMap::new(),
27 }
28 }
29
30 pub fn add_point(&mut self, timestamp: i64, value: f64) {
31 self.points.push(TimePoint {
32 timestamp,
33 value,
34 metadata: None,
35 });
36 }
37
38 pub fn to_array(&self) -> Result<Array2<f64>> {
39 let data: Vec<f64> = self
40 .points
41 .iter()
42 .flat_map(|p| vec![p.timestamp as f64, p.value])
43 .collect();
44
45 Array2::from_shape_vec((self.points.len(), 2), data)
46 .map_err(|e| crate::error::IoError::Other(e.to_string()))
47 }
48}