1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// redis-zset-ts/src/time_series.rs
//
// Copyright (c) 2024, Frank Pagliughi <fpagliughi@mindspring.com>
// All Rights Reserved
//
// Licensed under the MIT license:
//   <LICENSE or http://opensource.org/licenses/MIT>
// This file may not be copied, modified, or distributed except according
// to those terms.
//

//! Simple time-series database functionality using Redis Sorted 'Z' Sets.
//!

use crate::{as_timestamp, timestamp, Result};
use redis::{Client, Commands, Connection};
use rmp_serde as rmps;
use serde::{de::DeserializeOwned, Serialize};
use std::{
    marker::PhantomData,
    time::{Duration, SystemTime},
};

/////////////////////////////////////////////////////////////////////////////

/// A timestamped value
pub struct TimeValue<T> {
    /// The timestamp
    pub timestamp: f64,
    /// The value
    pub value: T,
}

impl<T> TimeValue<T> {
    /// Created a new value with the current time
    pub fn new(val: T) -> Self {
        Self::with_timestamp(timestamp(), val)
    }

    /// Creates a value with the specified timestamp
    pub fn with_timestamp(ts: f64, val: T) -> Self {
        Self {
            timestamp: ts,
            value: val,
        }
    }

    /// Converts the value into a tuple
    pub fn into_tuple(self) -> (f64, T) {
        (self.timestamp, self.value)
    }
}

impl<T> From<(f64, T)> for TimeValue<T> {
    /// Create a Time value from a tuple.
    fn from(v: (f64, T)) -> Self {
        Self {
            timestamp: v.0,
            value: v.1,
        }
    }
}

impl<T: Clone> Clone for TimeValue<T> {
    fn clone(&self) -> Self {
        Self {
            timestamp: self.timestamp,
            value: self.value.clone(),
        }
    }
}

impl<T: Copy> Copy for TimeValue<T> {}

/////////////////////////////////////////////////////////////////////////////

/// Connection to a Redis Time Series
pub struct TimeSeries<T> {
    /// The name/key of the Redis sorted set.
    key: String,
    /// The Redis client
    #[allow(dead_code)]
    cli: Client,
    /// The Redis connection
    conn: Connection,
    /// Placeholder for data type
    phantom: PhantomData<T>,
}

impl<T> TimeSeries<T> {
    /// Creates a new connection to the named time series.
    pub fn new(namespace: &str, name: &str) -> Result<Self> {
        Self::with_host("localhost", namespace, name)
    }

    /// Creates a new connection to the named time series on the specified
    /// host.
    pub fn with_host(host: &str, namespace: &str, name: &str) -> Result<Self> {
        Self::with_uri(&format!("redis://{}/", host), namespace, name)
    }

    /// Creates a new connection to the named time series on the host with
    /// the specified URI.
    pub fn with_uri(uri: &str, namespace: &str, name: &str) -> Result<Self> {
        let key = if namespace.is_empty() {
            name.into()
        }
        else {
            format!("{}:{}", namespace, name)
        };
        let cli = Client::open(uri)?;
        let conn = cli.get_connection()?;
        Ok(Self {
            key,
            cli,
            conn,
            phantom: PhantomData,
        })
    }

    /// Deletes all the values in the time series before the specified
    /// time stamp.
    pub fn purge(&mut self, ts: f64) -> Result<()> {
        let ts = format!("({}", ts);
        self.conn.zrembyscore(&self.key, "-inf", ts)?;
        Ok(())
    }

    /// Deletes all the values in the time series before the specified time.
    pub fn purge_time(&mut self, ts: SystemTime) -> Result<()> {
        self.purge(as_timestamp(ts))
    }

    /// Deletes all the values in the time series except points that fall in
    /// the most recent time specified by the duration.
    pub fn purge_duration(&mut self, dur: Duration) -> Result<()> {
        self.purge(timestamp() - dur.as_secs_f64())
    }

    /// Removes the entire timeseries from the Redis DB.
    pub fn delete(&mut self) -> Result<()> {
        self.conn.del(&self.key)?;
        Ok(())
    }
}

impl<T: Serialize> TimeSeries<T> {
    /// Adds a point to the time series.
    pub fn add(&mut self, ts: f64, val: T) -> Result<()> {
        let rval = rmps::encode::to_vec_named(&(ts, val))?;
        self.conn.zadd(&self.key, rval, ts)?;
        Ok(())
    }

    /// Adds a point to the time series, using the current time as the
    /// timestamp.
    pub fn add_now(&mut self, val: T) -> Result<()> {
        let ts = timestamp();
        self.add(ts, val)
    }

    /// Adds a point to the time series as a TimeValue.
    pub fn add_value<V>(&mut self, val: V) -> Result<()>
    where
        V: Into<TimeValue<T>>,
    {
        let val = val.into();
        self.add(val.timestamp, val.value)
    }

    /// Adds multiple points to the time series.
    pub fn add_multiple(&mut self, vals: &[TimeValue<T>]) -> Result<()> {
        let rvals: Vec<(f64, Vec<u8>)> = vals
            .iter()
            .map(|v| {
                let rval = rmps::encode::to_vec_named(&(v.timestamp, &v.value)).unwrap();
                (v.timestamp, rval)
            })
            .collect();

        self.conn.zadd_multiple(&self.key, &rvals)?;
        Ok(())
    }

    /// Adds multiple points to the time series from a slice of tuples.
    pub fn add_multiple_values(&mut self, vals: &[(f64, T)]) -> Result<()> {
        let rvals: Vec<(f64, Vec<u8>)> = vals
            .iter()
            .map(|(ts, v)| (*ts, rmps::encode::to_vec_named(&(ts, v)).unwrap()))
            .collect();

        self.conn.zadd_multiple(&self.key, &rvals)?;
        Ok(())
    }
}

impl<T: DeserializeOwned> TimeSeries<T> {
    /// Gets values from a time range
    /// The timestamps can be the usual floating point values, or anything
    /// that can be conbverted to a Redis argument, such as special strings
    /// like "-inf", "+inf", or "2.0)" to indicate open ranges.
    pub fn get_range_any<R, RR>(&mut self, ts1: R, ts2: RR) -> Result<Vec<TimeValue<T>>>
    where
        R: redis::ToRedisArgs,
        RR: redis::ToRedisArgs,
    {
        let v: Vec<Vec<u8>> = self.conn.zrangebyscore(&self.key, ts1, ts2)?;
        let vret = v
            .iter()
            .map(|buf| rmps::decode::from_slice(buf).unwrap())
            .map(|(timestamp, value)| TimeValue { timestamp, value })
            .collect();
        Ok(vret)
    }

    /// Gets values from a time range
    /// This gets the values from `ts1` up to, but not including, `ts2`.
    pub fn get_range(&mut self, ts1: f64, ts2: f64) -> Result<Vec<TimeValue<T>>> {
        let ts2 = format!("({}", ts2);
        self.get_range_any::<_, _>(ts1, ts2)
    }

    /// Gets values from a time range
    /// This gets the values from `ts1` up to, but not including, `ts2`.
    pub fn get_time_range(
        &mut self,
        ts1: SystemTime,
        ts2: SystemTime,
    ) -> Result<Vec<TimeValue<T>>> {
        self.get_range(as_timestamp(ts1), as_timestamp(ts2))
    }

    /// Gets values starting from the specified time up to the latest value.
    pub fn get_range_from(&mut self, ts: f64) -> Result<Vec<TimeValue<T>>> {
        self.get_range_any::<_, _>(ts, "+inf")
    }

    /// Gets all the values in the series.
    /// This should be used with caution if the series is large.
    pub fn get_all(&mut self) -> Result<Vec<TimeValue<T>>> {
        self.get_range_any::<_, _>("-inf", "+inf")
    }
}

/////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;

    const NAMESPACE: &str = "redis-zset-ts";

    #[test]
    fn test_time_value() {
        let tv = TimeValue::from((0.0, 42));
        assert_eq!(42, tv.value);

        let tv = tv.into_tuple();
        assert_eq!(42, tv.1);
    }

    #[test]
    fn test_conn() {
        let mut series = TimeSeries::<i32>::new(NAMESPACE, "conn").unwrap();
        let _ = series.delete();
    }

    #[test]
    fn test_add() {
        let mut series = TimeSeries::new(NAMESPACE, "add").unwrap();
        let _ = series.delete();
        series.add_now(&42).unwrap();
    }

    #[test]
    fn test_get() {
        let mut series = TimeSeries::new(NAMESPACE, "get").unwrap();
        let _ = series.delete();

        series.add(2.0, 42).unwrap();
        series.add(3.0, 99).unwrap();
        series.add_value((4.0, 13)).unwrap();

        let v = series.get_range(2.0, 3.0).unwrap();
        assert_eq!(1, v.len());
        assert_eq!(42, v[0].value);

        let v = series.get_range(2.0, 4.0).unwrap();
        assert_eq!(2, v.len());
        assert_eq!(42, v[0].value);
        assert_eq!(99, v[1].value);

        let v = series.get_range_from(3.0).unwrap();
        assert_eq!(2, v.len());
        assert_eq!(99, v[0].value);
        assert_eq!(13, v[1].value);

        let v = series.get_all().unwrap();
        assert_eq!(3, v.len());
        assert_eq!(42, v[0].value);
        assert_eq!(99, v[1].value);
        assert_eq!(13, v[2].value);
    }

    #[test]
    fn test_purge() {
        let mut series = TimeSeries::new(NAMESPACE, "purge").unwrap();
        let _ = series.delete();

        series.add(2.0, 42).unwrap();
        series.add(3.0, 99).unwrap();
        series.add_value((4.0, 13)).unwrap();

        series.purge(3.0).unwrap();

        let v = series.get_range(1.0, 5.0).unwrap();
        assert_eq!(2, v.len());
        assert_eq!(99, v[0].value);
        assert_eq!(13, v[1].value);
    }
}