aranet-store 0.1.13

Local data persistence for Aranet sensor readings
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Query builders for readings and history.
//!
//! This module provides fluent query builders for filtering and paginating
//! stored sensor data. Both [`ReadingQuery`] and [`HistoryQuery`] follow
//! the builder pattern for ergonomic query construction.
//!
//! # Example
//!
//! ```
//! use aranet_store::{Store, ReadingQuery, HistoryQuery};
//! use time::{OffsetDateTime, Duration};
//!
//! let store = Store::open_in_memory()?;
//! let yesterday = OffsetDateTime::now_utc() - Duration::hours(24);
//!
//! // Query recent readings with pagination
//! let query = ReadingQuery::new()
//!     .device("Aranet4 17C3C")
//!     .since(yesterday)
//!     .limit(50)
//!     .offset(0);
//!
//! let readings = store.query_readings(&query)?;
//!
//! // Query all history for export
//! let history_query = HistoryQuery::new()
//!     .device("Aranet4 17C3C")
//!     .oldest_first();
//!
//! let history = store.query_history(&history_query)?;
//! # Ok::<(), aranet_store::Error>(())
//! ```

use time::OffsetDateTime;

/// Maximum allowed limit for queries to prevent DoS via large result sets.
/// This caps LIMIT values to prevent memory exhaustion attacks.
pub const MAX_QUERY_LIMIT: u32 = 1_000_000;

/// Fluent query builder for current readings.
///
/// Use this to construct queries for [`Store::query_readings`](crate::Store::query_readings).
/// All filter methods are optional and can be chained in any order.
///
/// By default, queries return results ordered by `captured_at` descending
/// (newest first).
///
/// # Example
///
/// ```
/// use aranet_store::ReadingQuery;
/// use time::{OffsetDateTime, Duration};
///
/// let now = OffsetDateTime::now_utc();
///
/// // Query last hour's readings for a device
/// let query = ReadingQuery::new()
///     .device("Aranet4 17C3C")
///     .since(now - Duration::hours(1))
///     .limit(100);
///
/// // Query with pagination
/// let page_2 = ReadingQuery::new()
///     .device("Aranet4 17C3C")
///     .limit(50)
///     .offset(50);
///
/// // Query oldest first (chronological order)
/// let chronological = ReadingQuery::new()
///     .device("Aranet4 17C3C")
///     .oldest_first();
/// ```
#[derive(Debug, Default, Clone)]
pub struct ReadingQuery {
    /// Filter by device ID.
    pub device_id: Option<String>,
    /// Filter readings after this time.
    pub since: Option<OffsetDateTime>,
    /// Filter readings before this time.
    pub until: Option<OffsetDateTime>,
    /// Maximum number of results.
    pub limit: Option<u32>,
    /// Offset for pagination.
    pub offset: Option<u32>,
    /// Order by captured_at descending (newest first).
    pub newest_first: bool,
}

impl ReadingQuery {
    /// Create a new query with default settings.
    ///
    /// Default behavior:
    /// - No device filter (all devices)
    /// - No time range filter
    /// - No limit (all matching records)
    /// - Ordered by newest first
    pub fn new() -> Self {
        Self {
            newest_first: true,
            ..Default::default()
        }
    }

    /// Filter by device ID.
    ///
    /// Only include readings from the specified device.
    pub fn device(mut self, device_id: &str) -> Self {
        self.device_id = Some(device_id.to_string());
        self
    }

    /// Filter to readings captured at or after this time.
    ///
    /// Useful for querying "last N hours" or "since last sync".
    pub fn since(mut self, time: OffsetDateTime) -> Self {
        self.since = Some(time);
        self
    }

    /// Filter to readings captured at or before this time.
    ///
    /// Use with `since()` to query a specific time range.
    pub fn until(mut self, time: OffsetDateTime) -> Self {
        self.until = Some(time);
        self
    }

    /// Limit the maximum number of results returned.
    ///
    /// Use with `offset()` for pagination. Values are capped at [`MAX_QUERY_LIMIT`]
    /// to prevent resource exhaustion.
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit.min(MAX_QUERY_LIMIT));
        self
    }

    /// Skip the first N results.
    ///
    /// Use with `limit()` for pagination. For example, to get page 2
    /// with 50 items per page: `.limit(50).offset(50)`.
    pub fn offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Order results by oldest first (ascending by `captured_at`).
    ///
    /// By default, queries return newest first. Use this for chronological
    /// ordering, useful when exporting or processing data sequentially.
    pub fn oldest_first(mut self) -> Self {
        self.newest_first = false;
        self
    }

    /// Build the SQL WHERE clause and parameters.
    pub(crate) fn build_where(&self) -> (String, Vec<Box<dyn rusqlite::ToSql>>) {
        let mut conditions = Vec::new();
        let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

        if let Some(ref device_id) = self.device_id {
            conditions.push("device_id = ?");
            params.push(Box::new(device_id.clone()));
        }

        if let Some(since) = self.since {
            conditions.push("captured_at >= ?");
            params.push(Box::new(since.unix_timestamp()));
        }

        if let Some(until) = self.until {
            conditions.push("captured_at <= ?");
            params.push(Box::new(until.unix_timestamp()));
        }

        let where_clause = if conditions.is_empty() {
            String::new()
        } else {
            format!("WHERE {}", conditions.join(" AND "))
        };

        (where_clause, params)
    }

    /// Build the full SQL query.
    pub(crate) fn build_sql(&self) -> String {
        let (where_clause, _) = self.build_where();
        let order = if self.newest_first { "DESC" } else { "ASC" };

        let mut sql = format!(
            "SELECT id, device_id, captured_at, co2, temperature, pressure, humidity, \
             battery, status, radon, radiation_rate, radiation_total \
             FROM readings {} ORDER BY captured_at {}",
            where_clause, order
        );

        if let Some(limit) = self.limit {
            sql.push_str(&format!(" LIMIT {}", limit));
        }

        if let Some(offset) = self.offset {
            sql.push_str(&format!(" OFFSET {}", offset));
        }

        sql
    }
}

/// Fluent query builder for history records.
///
/// Use this to construct queries for [`Store::query_history`](crate::Store::query_history),
/// [`Store::history_stats`](crate::Store::history_stats), and export methods.
/// All filter methods are optional and can be chained in any order.
///
/// By default, queries return results ordered by `timestamp` descending
/// (newest first).
///
/// # Example
///
/// ```
/// use aranet_store::HistoryQuery;
/// use time::{OffsetDateTime, Duration};
///
/// let now = OffsetDateTime::now_utc();
///
/// // Query last week's history
/// let query = HistoryQuery::new()
///     .device("Aranet4 17C3C")
///     .since(now - Duration::days(7));
///
/// // Query specific date range for export
/// let export_query = HistoryQuery::new()
///     .device("Aranet4 17C3C")
///     .since(now - Duration::days(30))
///     .until(now - Duration::days(7))
///     .oldest_first();
/// ```
#[derive(Debug, Default, Clone)]
pub struct HistoryQuery {
    /// Filter by device ID (optional).
    pub device_id: Option<String>,
    /// Include only records at or after this time (optional).
    pub since: Option<OffsetDateTime>,
    /// Include only records at or before this time (optional).
    pub until: Option<OffsetDateTime>,
    /// Maximum number of results to return (optional).
    pub limit: Option<u32>,
    /// Number of results to skip for pagination (optional).
    pub offset: Option<u32>,
    /// If true, order by timestamp descending (newest first). Default: true.
    pub newest_first: bool,
}

impl HistoryQuery {
    /// Create a new query with default settings.
    ///
    /// Default behavior:
    /// - No device filter (all devices)
    /// - No time range filter
    /// - No limit (all matching records)
    /// - Ordered by newest first
    pub fn new() -> Self {
        Self {
            newest_first: true,
            ..Default::default()
        }
    }

    /// Filter by device ID.
    ///
    /// Only include history records from the specified device.
    pub fn device(mut self, device_id: &str) -> Self {
        self.device_id = Some(device_id.to_string());
        self
    }

    /// Filter to records at or after this time.
    ///
    /// Useful for querying "last N days" or data after a specific point.
    pub fn since(mut self, time: OffsetDateTime) -> Self {
        self.since = Some(time);
        self
    }

    /// Filter to records at or before this time.
    ///
    /// Use with `since()` to query a specific time range.
    pub fn until(mut self, time: OffsetDateTime) -> Self {
        self.until = Some(time);
        self
    }

    /// Limit the maximum number of results returned.
    ///
    /// Use with `offset()` for pagination. Values are capped at [`MAX_QUERY_LIMIT`]
    /// to prevent resource exhaustion.
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit.min(MAX_QUERY_LIMIT));
        self
    }

    /// Skip the first N results.
    ///
    /// Use with `limit()` for pagination. For example, to get page 3
    /// with 100 items per page: `.limit(100).offset(200)`.
    pub fn offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Order results by oldest first (ascending by `timestamp`).
    ///
    /// By default, queries return newest first. Use this for chronological
    /// ordering, which is useful for CSV export or time-series analysis.
    pub fn oldest_first(mut self) -> Self {
        self.newest_first = false;
        self
    }
}

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

    // ==================== ReadingQuery Tests ====================

    #[test]
    fn test_reading_query_new_defaults() {
        let query = ReadingQuery::new();
        assert!(query.device_id.is_none());
        assert!(query.since.is_none());
        assert!(query.until.is_none());
        assert!(query.limit.is_none());
        assert!(query.offset.is_none());
        assert!(query.newest_first);
    }

    #[test]
    fn test_reading_query_default_is_different_from_new() {
        let default_query = ReadingQuery::default();
        let new_query = ReadingQuery::new();

        // Default doesn't set newest_first, but new() does
        assert!(!default_query.newest_first);
        assert!(new_query.newest_first);
    }

    #[test]
    fn test_reading_query_device_filter() {
        let query = ReadingQuery::new().device("test-device-123");
        assert_eq!(query.device_id, Some("test-device-123".to_string()));
    }

    #[test]
    fn test_reading_query_since_filter() {
        let time = datetime!(2024-01-15 10:30:00 UTC);
        let query = ReadingQuery::new().since(time);
        assert_eq!(query.since, Some(time));
    }

    #[test]
    fn test_reading_query_until_filter() {
        let time = datetime!(2024-01-15 18:30:00 UTC);
        let query = ReadingQuery::new().until(time);
        assert_eq!(query.until, Some(time));
    }

    #[test]
    fn test_reading_query_limit() {
        let query = ReadingQuery::new().limit(100);
        assert_eq!(query.limit, Some(100));
    }

    #[test]
    fn test_reading_query_offset() {
        let query = ReadingQuery::new().offset(50);
        assert_eq!(query.offset, Some(50));
    }

    #[test]
    fn test_reading_query_oldest_first() {
        let query = ReadingQuery::new().oldest_first();
        assert!(!query.newest_first);
    }

    #[test]
    fn test_reading_query_chaining() {
        let since = datetime!(2024-01-01 00:00:00 UTC);
        let until = datetime!(2024-12-31 23:59:59 UTC);

        let query = ReadingQuery::new()
            .device("my-device")
            .since(since)
            .until(until)
            .limit(10)
            .offset(5)
            .oldest_first();

        assert_eq!(query.device_id, Some("my-device".to_string()));
        assert_eq!(query.since, Some(since));
        assert_eq!(query.until, Some(until));
        assert_eq!(query.limit, Some(10));
        assert_eq!(query.offset, Some(5));
        assert!(!query.newest_first);
    }

    #[test]
    fn test_reading_query_build_where_empty() {
        let query = ReadingQuery::new();
        let (where_clause, params) = query.build_where();
        assert_eq!(where_clause, "");
        assert!(params.is_empty());
    }

    #[test]
    fn test_reading_query_build_where_device_only() {
        let query = ReadingQuery::new().device("test-device");
        let (where_clause, params) = query.build_where();
        assert_eq!(where_clause, "WHERE device_id = ?");
        assert_eq!(params.len(), 1);
    }

    #[test]
    fn test_reading_query_build_where_time_range() {
        let since = datetime!(2024-01-01 00:00:00 UTC);
        let until = datetime!(2024-12-31 23:59:59 UTC);

        let query = ReadingQuery::new().since(since).until(until);
        let (where_clause, params) = query.build_where();

        assert_eq!(where_clause, "WHERE captured_at >= ? AND captured_at <= ?");
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn test_reading_query_build_where_all_filters() {
        let since = datetime!(2024-01-01 00:00:00 UTC);
        let until = datetime!(2024-12-31 23:59:59 UTC);

        let query = ReadingQuery::new()
            .device("device-1")
            .since(since)
            .until(until);
        let (where_clause, params) = query.build_where();

        assert!(where_clause.contains("device_id = ?"));
        assert!(where_clause.contains("captured_at >= ?"));
        assert!(where_clause.contains("captured_at <= ?"));
        assert_eq!(params.len(), 3);
    }

    #[test]
    fn test_reading_query_build_sql_basic() {
        let query = ReadingQuery::new();
        let sql = query.build_sql();

        assert!(sql.contains("SELECT"));
        assert!(sql.contains("FROM readings"));
        assert!(sql.contains("ORDER BY captured_at DESC"));
        assert!(!sql.contains("WHERE"));
        assert!(!sql.contains("LIMIT"));
        assert!(!sql.contains("OFFSET"));
    }

    #[test]
    fn test_reading_query_build_sql_with_limit() {
        let query = ReadingQuery::new().limit(50);
        let sql = query.build_sql();

        assert!(sql.contains("LIMIT 50"));
    }

    #[test]
    fn test_reading_query_build_sql_with_offset() {
        let query = ReadingQuery::new().offset(25);
        let sql = query.build_sql();

        assert!(sql.contains("OFFSET 25"));
    }

    #[test]
    fn test_reading_query_build_sql_oldest_first() {
        let query = ReadingQuery::new().oldest_first();
        let sql = query.build_sql();

        assert!(sql.contains("ORDER BY captured_at ASC"));
    }

    #[test]
    fn test_reading_query_build_sql_complete() {
        let since = datetime!(2024-06-01 00:00:00 UTC);
        let query = ReadingQuery::new()
            .device("my-sensor")
            .since(since)
            .limit(100)
            .offset(10)
            .oldest_first();

        let sql = query.build_sql();

        assert!(sql.contains("WHERE"));
        assert!(sql.contains("device_id = ?"));
        assert!(sql.contains("captured_at >= ?"));
        assert!(sql.contains("ORDER BY captured_at ASC"));
        assert!(sql.contains("LIMIT 100"));
        assert!(sql.contains("OFFSET 10"));
    }

    #[test]
    fn test_reading_query_build_sql_selects_all_columns() {
        let query = ReadingQuery::new();
        let sql = query.build_sql();

        assert!(sql.contains("id"));
        assert!(sql.contains("device_id"));
        assert!(sql.contains("captured_at"));
        assert!(sql.contains("co2"));
        assert!(sql.contains("temperature"));
        assert!(sql.contains("pressure"));
        assert!(sql.contains("humidity"));
        assert!(sql.contains("battery"));
        assert!(sql.contains("status"));
        assert!(sql.contains("radon"));
        assert!(sql.contains("radiation_rate"));
        assert!(sql.contains("radiation_total"));
    }

    // ==================== HistoryQuery Tests ====================

    #[test]
    fn test_history_query_new_defaults() {
        let query = HistoryQuery::new();
        assert!(query.device_id.is_none());
        assert!(query.since.is_none());
        assert!(query.until.is_none());
        assert!(query.limit.is_none());
        assert!(query.offset.is_none());
        assert!(query.newest_first);
    }

    #[test]
    fn test_history_query_default_is_different_from_new() {
        let default_query = HistoryQuery::default();
        let new_query = HistoryQuery::new();

        // Default doesn't set newest_first, but new() does
        assert!(!default_query.newest_first);
        assert!(new_query.newest_first);
    }

    #[test]
    fn test_history_query_device_filter() {
        let query = HistoryQuery::new().device("aranet4-abc123");
        assert_eq!(query.device_id, Some("aranet4-abc123".to_string()));
    }

    #[test]
    fn test_history_query_since_filter() {
        let time = datetime!(2024-03-15 08:00:00 UTC);
        let query = HistoryQuery::new().since(time);
        assert_eq!(query.since, Some(time));
    }

    #[test]
    fn test_history_query_until_filter() {
        let time = datetime!(2024-03-15 20:00:00 UTC);
        let query = HistoryQuery::new().until(time);
        assert_eq!(query.until, Some(time));
    }

    #[test]
    fn test_history_query_limit() {
        let query = HistoryQuery::new().limit(500);
        assert_eq!(query.limit, Some(500));
    }

    #[test]
    fn test_history_query_offset() {
        let query = HistoryQuery::new().offset(200);
        assert_eq!(query.offset, Some(200));
    }

    #[test]
    fn test_history_query_oldest_first() {
        let query = HistoryQuery::new().oldest_first();
        assert!(!query.newest_first);
    }

    #[test]
    fn test_history_query_chaining() {
        let since = datetime!(2024-01-01 00:00:00 UTC);
        let until = datetime!(2024-06-30 23:59:59 UTC);

        let query = HistoryQuery::new()
            .device("sensor-xyz")
            .since(since)
            .until(until)
            .limit(1000)
            .offset(100)
            .oldest_first();

        assert_eq!(query.device_id, Some("sensor-xyz".to_string()));
        assert_eq!(query.since, Some(since));
        assert_eq!(query.until, Some(until));
        assert_eq!(query.limit, Some(1000));
        assert_eq!(query.offset, Some(100));
        assert!(!query.newest_first);
    }

    #[test]
    fn test_history_query_clone() {
        let query = HistoryQuery::new().device("device-1").limit(50);
        let cloned = query.clone();

        assert_eq!(cloned.device_id, query.device_id);
        assert_eq!(cloned.limit, query.limit);
    }

    #[test]
    fn test_reading_query_clone() {
        let query = ReadingQuery::new().device("device-1").limit(50);
        let cloned = query.clone();

        assert_eq!(cloned.device_id, query.device_id);
        assert_eq!(cloned.limit, query.limit);
    }

    #[test]
    fn test_reading_query_debug() {
        let query = ReadingQuery::new().device("test");
        let debug_str = format!("{:?}", query);
        assert!(debug_str.contains("ReadingQuery"));
        assert!(debug_str.contains("test"));
    }

    #[test]
    fn test_history_query_debug() {
        let query = HistoryQuery::new().device("test");
        let debug_str = format!("{:?}", query);
        assert!(debug_str.contains("HistoryQuery"));
        assert!(debug_str.contains("test"));
    }

    #[test]
    fn test_reading_query_limit_zero() {
        let query = ReadingQuery::new().limit(0);
        let sql = query.build_sql();
        assert!(sql.contains("LIMIT 0"));
    }

    #[test]
    fn test_reading_query_large_pagination() {
        // Limit is clamped to MAX_QUERY_LIMIT, but offset is not
        let query = ReadingQuery::new().limit(u32::MAX).offset(u32::MAX);
        let sql = query.build_sql();
        assert!(sql.contains(&format!("LIMIT {}", MAX_QUERY_LIMIT)));
        assert!(sql.contains(&format!("OFFSET {}", u32::MAX)));
    }
}