aranet-cli 0.2.0

Command-line interface for Aranet environmental sensors
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
//! History command implementation.

use std::path::PathBuf;
use std::time::Duration;

use anyhow::{Context, Result, bail};
use aranet_core::HistoryOptions;
use aranet_store::{HistoryQuery, Store};
use time::OffsetDateTime;

use crate::cli::OutputFormat;
use crate::format::{FormatOptions, format_history_csv, format_history_json, format_history_text};
use crate::style;
use crate::util::{require_device_interactive, write_output};

/// Options for querying history from the cache.
struct CacheQueryOptions<'a> {
    device: Option<String>,
    count: u32,
    since_dt: Option<OffsetDateTime>,
    until_dt: Option<OffsetDateTime>,
    format: OutputFormat,
    output: Option<&'a PathBuf>,
    quiet: bool,
    opts: &'a FormatOptions,
}

/// Parse a date/time string in various formats:
/// - RFC3339: "2024-01-15T10:30:00Z"
/// - YYYY-MM-DD: "2024-01-15"
/// - Relative: "today", "yesterday", "7d", "24h", "1w"
fn parse_datetime(s: &str) -> Result<OffsetDateTime> {
    let s_lower = s.to_lowercase();
    let now = OffsetDateTime::now_utc();

    // Handle relative date keywords
    match s_lower.as_str() {
        "now" => return Ok(now),
        "today" => {
            let today = now.date();
            return Ok(today.with_hms(0, 0, 0).expect("valid time").assume_utc());
        }
        "yesterday" => {
            let yesterday = now.date() - time::Duration::days(1);
            return Ok(yesterday
                .with_hms(0, 0, 0)
                .expect("valid time")
                .assume_utc());
        }
        _ => {}
    }

    // Handle relative duration patterns: "7d", "24h", "1w", "30m"
    if let Some(duration) = parse_relative_duration(&s_lower) {
        return Ok(now - duration);
    }

    // Try RFC3339 first (e.g., "2024-01-15T10:30:00Z")
    if let Ok(dt) = OffsetDateTime::parse(s, &time::format_description::well_known::Rfc3339) {
        return Ok(dt);
    }

    // Try YYYY-MM-DD format (treat as start of day in UTC)
    let format =
        time::format_description::parse("[year]-[month]-[day]").expect("valid format description");
    if let Ok(date) = time::Date::parse(s, &format) {
        return Ok(date.with_hms(0, 0, 0).expect("valid time").assume_utc());
    }

    bail!(
        "Invalid date format '{}'. Use RFC3339 (2024-01-15T10:30:00Z), YYYY-MM-DD, \
         or relative (today, yesterday, 7d, 24h, 1w)",
        s
    )
}

/// Parse relative duration strings like "7d", "24h", "1w", "30m"
fn parse_relative_duration(s: &str) -> Option<time::Duration> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }

    // Find where the number ends and the unit begins
    let (num_str, unit) = s.split_at(s.chars().take_while(|c| c.is_ascii_digit()).count());

    let num: i64 = num_str.parse().ok()?;
    if num <= 0 {
        return None;
    }

    match unit.trim() {
        "m" | "min" | "mins" | "minute" | "minutes" => Some(time::Duration::minutes(num)),
        "h" | "hr" | "hrs" | "hour" | "hours" => Some(time::Duration::hours(num)),
        "d" | "day" | "days" => Some(time::Duration::days(num)),
        "w" | "wk" | "wks" | "week" | "weeks" => Some(time::Duration::weeks(num)),
        _ => None,
    }
}

/// Arguments for the history command.
pub struct HistoryArgs<'a> {
    pub device: Option<String>,
    pub count: u32,
    pub since: Option<String>,
    pub until: Option<String>,
    pub timeout: Duration,
    pub format: OutputFormat,
    pub output: Option<&'a PathBuf>,
    pub quiet: bool,
    pub opts: &'a FormatOptions,
    pub cache: bool,
}

pub async fn cmd_history(args: HistoryArgs<'_>) -> Result<()> {
    let HistoryArgs {
        device,
        count,
        since,
        until,
        timeout,
        format,
        output,
        quiet,
        opts,
        cache,
    } = args;

    // Parse date filters upfront to fail fast
    let since_dt = since.as_ref().map(|s| parse_datetime(s)).transpose()?;
    let until_dt = until.as_ref().map(|s| parse_datetime(s)).transpose()?;

    // If --cache flag is set, read from local database instead of device
    if cache {
        return cmd_history_from_cache(CacheQueryOptions {
            device,
            count,
            since_dt,
            until_dt,
            format,
            output,
            quiet,
            opts,
        });
    }

    let identifier = require_device_interactive(device).await?;

    // Set up progress bar for text output
    let show_progress = !quiet && matches!(format, OutputFormat::Text);

    // Connect to device (with its own spinner if show_progress is true)
    let device =
        crate::util::connect_device_with_progress(&identifier, timeout, show_progress).await?;

    // Create progress bar for download phase
    let pb = if show_progress {
        let pb = style::download_progress_bar();
        pb.set_message("Downloading history...");
        Some(pb)
    } else {
        None
    };

    // Clone progress bar for callback (ProgressBar uses Arc internally)
    let pb_for_callback = pb.clone();

    let history_options = if let Some(pb_callback) = pb_for_callback {
        HistoryOptions::default().with_progress(move |progress| {
            let percent = (progress.overall_progress * 100.0) as u64;
            pb_callback.set_position(percent);
            pb_callback.set_message(format!(
                "Downloading {:?} ({}/{})",
                progress.current_param, progress.param_index, progress.total_params
            ));
        })
    } else {
        HistoryOptions::default()
    };

    let device_id = device.address().to_string();
    let history_result = device
        .download_history_with_options(history_options)
        .await
        .context("Failed to download history");

    crate::util::disconnect_device(&device).await;
    let history = history_result?;

    if let Some(pb) = pb {
        pb.finish_with_message("Download complete");
    }

    // Save history to store (unified data architecture)
    crate::util::save_history_to_store(&device_id, &history);

    // Apply date filters
    let history: Vec<_> = history
        .into_iter()
        .filter(|r| {
            if let Some(since) = since_dt
                && r.timestamp < since
            {
                return false;
            }
            if let Some(until) = until_dt
                && r.timestamp > until
            {
                return false;
            }
            true
        })
        .collect();

    // Reverse to show newest first (device sends oldest first)
    let mut history = history;
    history.reverse();

    // Apply count limit if specified (0 means all)
    let history: Vec<_> = if count > 0 {
        history.into_iter().take(count as usize).collect()
    } else {
        history
    };

    if !quiet && matches!(format, OutputFormat::Text) {
        eprintln!("Downloaded {} records.", history.len());
    }

    let content = match format {
        OutputFormat::Json => format_history_json(&history, opts)?,
        OutputFormat::Text => format_history_text(&history, opts),
        OutputFormat::Csv => format_history_csv(&history, opts),
    };

    write_output(output, &content)?;
    Ok(())
}

/// Read history from local cache instead of connecting to the device.
fn cmd_history_from_cache(options: CacheQueryOptions<'_>) -> Result<()> {
    let CacheQueryOptions {
        device,
        count,
        since_dt,
        until_dt,
        format,
        output,
        quiet,
        opts,
    } = options;

    let store = Store::open_default().context("Failed to open database")?;

    // For cache mode, we need a device identifier
    let device_id = match device {
        Some(id) => id,
        None => {
            // Try to find a default device or list available devices
            let devices = store.list_devices()?;
            if devices.is_empty() {
                bail!("No devices in cache. Run 'aranet sync' first to cache device data.");
            }
            if devices.len() == 1 {
                devices[0].id.clone()
            } else {
                eprintln!("Multiple devices in cache. Please specify one with --device:");
                for d in &devices {
                    let name = d.name.as_deref().unwrap_or("(unnamed)");
                    eprintln!("  {} - {}", d.id, name);
                }
                bail!("Device required when multiple devices are cached");
            }
        }
    };

    // Build query
    let mut query = HistoryQuery::new().device(&device_id);

    if count > 0 {
        query = query.limit(count);
    }

    if let Some(since) = since_dt {
        query = query.since(since);
    }

    if let Some(until) = until_dt {
        query = query.until(until);
    }

    let records = store.query_history(&query)?;

    if records.is_empty() {
        if !quiet {
            eprintln!(
                "No history records found for {}. Run 'aranet sync' to cache device history.",
                device_id
            );
        }
        return Ok(());
    }

    // Convert to HistoryRecord for formatting
    let history: Vec<_> = records.iter().map(|r| r.to_history()).collect();

    if !quiet && matches!(format, OutputFormat::Text) {
        eprintln!("Retrieved {} records from cache.", history.len());
    }

    let content = match format {
        OutputFormat::Json => format_history_json(&history, opts)?,
        OutputFormat::Text => format_history_text(&history, opts),
        OutputFormat::Csv => format_history_csv(&history, opts),
    };

    write_output(output, &content)?;
    Ok(())
}

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

    // ========================================================================
    // parse_relative_duration tests
    // ========================================================================

    #[test]
    fn test_parse_relative_duration_empty() {
        assert!(parse_relative_duration("").is_none());
        assert!(parse_relative_duration("   ").is_none());
    }

    #[test]
    fn test_parse_relative_duration_minutes() {
        assert_eq!(
            parse_relative_duration("30m"),
            Some(time::Duration::minutes(30))
        );
        assert_eq!(
            parse_relative_duration("30min"),
            Some(time::Duration::minutes(30))
        );
        assert_eq!(
            parse_relative_duration("30mins"),
            Some(time::Duration::minutes(30))
        );
        assert_eq!(
            parse_relative_duration("1minute"),
            Some(time::Duration::minutes(1))
        );
        assert_eq!(
            parse_relative_duration("5minutes"),
            Some(time::Duration::minutes(5))
        );
    }

    #[test]
    fn test_parse_relative_duration_hours() {
        assert_eq!(
            parse_relative_duration("24h"),
            Some(time::Duration::hours(24))
        );
        assert_eq!(
            parse_relative_duration("1hr"),
            Some(time::Duration::hours(1))
        );
        assert_eq!(
            parse_relative_duration("2hrs"),
            Some(time::Duration::hours(2))
        );
        assert_eq!(
            parse_relative_duration("1hour"),
            Some(time::Duration::hours(1))
        );
        assert_eq!(
            parse_relative_duration("12hours"),
            Some(time::Duration::hours(12))
        );
    }

    #[test]
    fn test_parse_relative_duration_days() {
        assert_eq!(parse_relative_duration("7d"), Some(time::Duration::days(7)));
        assert_eq!(
            parse_relative_duration("1day"),
            Some(time::Duration::days(1))
        );
        assert_eq!(
            parse_relative_duration("30days"),
            Some(time::Duration::days(30))
        );
    }

    #[test]
    fn test_parse_relative_duration_weeks() {
        assert_eq!(
            parse_relative_duration("1w"),
            Some(time::Duration::weeks(1))
        );
        assert_eq!(
            parse_relative_duration("2wk"),
            Some(time::Duration::weeks(2))
        );
        assert_eq!(
            parse_relative_duration("4wks"),
            Some(time::Duration::weeks(4))
        );
        assert_eq!(
            parse_relative_duration("1week"),
            Some(time::Duration::weeks(1))
        );
        assert_eq!(
            parse_relative_duration("2weeks"),
            Some(time::Duration::weeks(2))
        );
    }

    #[test]
    fn test_parse_relative_duration_invalid() {
        // Invalid unit
        assert!(parse_relative_duration("7x").is_none());
        assert!(parse_relative_duration("7y").is_none());
        assert!(parse_relative_duration("7s").is_none()); // seconds not supported

        // No number
        assert!(parse_relative_duration("d").is_none());
        assert!(parse_relative_duration("days").is_none());

        // Zero or negative not allowed
        assert!(parse_relative_duration("0d").is_none());
        assert!(parse_relative_duration("-1d").is_none());
    }

    // ========================================================================
    // parse_datetime tests
    // ========================================================================

    #[test]
    fn test_parse_datetime_now() {
        let before = OffsetDateTime::now_utc();
        let result = parse_datetime("now").unwrap();
        let after = OffsetDateTime::now_utc();

        assert!(result >= before);
        assert!(result <= after);
    }

    #[test]
    fn test_parse_datetime_now_case_insensitive() {
        // Should work with any case
        assert!(parse_datetime("NOW").is_ok());
        assert!(parse_datetime("Now").is_ok());
    }

    #[test]
    fn test_parse_datetime_today() {
        let result = parse_datetime("today").unwrap();
        let now = OffsetDateTime::now_utc();

        assert_eq!(result.date(), now.date());
        assert_eq!(result.hour(), 0);
        assert_eq!(result.minute(), 0);
        assert_eq!(result.second(), 0);
    }

    #[test]
    fn test_parse_datetime_yesterday() {
        let result = parse_datetime("yesterday").unwrap();
        let now = OffsetDateTime::now_utc();
        let expected_date = now.date() - time::Duration::days(1);

        assert_eq!(result.date(), expected_date);
        assert_eq!(result.hour(), 0);
        assert_eq!(result.minute(), 0);
        assert_eq!(result.second(), 0);
    }

    #[test]
    fn test_parse_datetime_rfc3339() {
        let result = parse_datetime("2024-01-15T10:30:00Z").unwrap();

        assert_eq!(result.year(), 2024);
        assert_eq!(result.month(), time::Month::January);
        assert_eq!(result.day(), 15);
        assert_eq!(result.hour(), 10);
        assert_eq!(result.minute(), 30);
        assert_eq!(result.second(), 0);
    }

    #[test]
    fn test_parse_datetime_date_only() {
        let result = parse_datetime("2024-01-15").unwrap();

        assert_eq!(result.year(), 2024);
        assert_eq!(result.month(), time::Month::January);
        assert_eq!(result.day(), 15);
        // Date-only should be start of day
        assert_eq!(result.hour(), 0);
        assert_eq!(result.minute(), 0);
        assert_eq!(result.second(), 0);
    }

    #[test]
    fn test_parse_datetime_relative_days() {
        let before = OffsetDateTime::now_utc();
        let result = parse_datetime("7d").unwrap();
        let after = OffsetDateTime::now_utc();

        // Result should be approximately 7 days ago
        let expected_min = before - time::Duration::days(7);
        let expected_max = after - time::Duration::days(7);

        assert!(result >= expected_min);
        assert!(result <= expected_max);
    }

    #[test]
    fn test_parse_datetime_relative_hours() {
        let before = OffsetDateTime::now_utc();
        let result = parse_datetime("24h").unwrap();
        let after = OffsetDateTime::now_utc();

        let expected_min = before - time::Duration::hours(24);
        let expected_max = after - time::Duration::hours(24);

        assert!(result >= expected_min);
        assert!(result <= expected_max);
    }

    #[test]
    fn test_parse_datetime_invalid() {
        assert!(parse_datetime("invalid").is_err());
        assert!(parse_datetime("2024/01/15").is_err()); // Wrong format
        assert!(parse_datetime("").is_err());
        assert!(parse_datetime("not-a-date").is_err());
    }

    #[test]
    fn test_parse_datetime_error_message() {
        let result = parse_datetime("invalid");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("Invalid date format"));
        assert!(err.to_string().contains("invalid"));
    }
}