oxigdal-cli 0.1.7

Command-line interface for OxiGDAL geospatial operations
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
//! Performance profiler for geospatial operations.
//!
//! Provides `Profiler` (start/stop timing), `Operation` (file-dispatched actions),
//! and `execute_operation` (run N iterations and return durations) for the
//! `oxigdal profile` CLI subcommand.

use anyhow::{Context, Result};
use serde::Serialize;
use std::str::FromStr;
use std::time::{Duration, Instant};

// ─── Profiler ─────────────────────────────────────────────────────────────────

/// Accumulates wall-clock measurements for repeated operations.
///
/// Typical use:
/// ```rust,ignore
/// let mut p = Profiler::new("open");
/// p.start();
/// // ... do work ...
/// p.stop();
/// println!("{}", p.report());
/// ```
pub struct Profiler {
    name: String,
    measurements: Vec<Duration>,
    current_start: Option<Instant>,
}

impl Profiler {
    /// Create a new profiler with the given name.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            measurements: Vec::new(),
            current_start: None,
        }
    }

    /// Start a timing lap.
    ///
    /// Calling `start` when a lap is already running resets the start time.
    pub fn start(&mut self) {
        self.current_start = Some(Instant::now());
    }

    /// Stop the current timing lap and record the measurement.
    ///
    /// If `start` was never called, this is a no-op.
    pub fn stop(&mut self) {
        if let Some(start) = self.current_start.take() {
            self.measurements.push(start.elapsed());
        }
    }

    /// Returns a human-readable statistics table.
    ///
    /// Columns: count | min ms | mean ms | median ms | p95 ms | p99 ms | max ms
    #[must_use]
    pub fn report(&self) -> String {
        let n = self.measurements.len();
        if n == 0 {
            return format!("=== {} ===\nNo measurements recorded.\n", self.name);
        }

        let stats = compute_stats(&self.measurements);

        let header = format!("=== {} ({} iterations) ===", self.name, n);
        let row = format!(
            "count={n}  min={:.3}ms  mean={:.3}ms  median={:.3}ms  p95={:.3}ms  p99={:.3}ms  max={:.3}ms",
            stats.min_ms, stats.mean_ms, stats.median_ms, stats.p95_ms, stats.p99_ms, stats.max_ms,
        );
        format!("{header}\n{row}\n")
    }

    /// Serialise the statistics + raw measurements to a pretty-printed JSON string.
    ///
    /// # Errors
    ///
    /// Returns an error if JSON serialisation fails (should not happen in practice).
    pub fn export_json(&self) -> Result<String> {
        let n = self.measurements.len();
        let measurements_ms: Vec<f64> = self.measurements.iter().map(duration_to_ms).collect();

        let payload = if n == 0 {
            ProfilerJson {
                name: self.name.clone(),
                count: 0,
                min_ms: 0.0,
                mean_ms: 0.0,
                median_ms: 0.0,
                p95_ms: 0.0,
                p99_ms: 0.0,
                max_ms: 0.0,
                measurements_ms,
            }
        } else {
            let stats = compute_stats(&self.measurements);
            ProfilerJson {
                name: self.name.clone(),
                count: n,
                min_ms: stats.min_ms,
                mean_ms: stats.mean_ms,
                median_ms: stats.median_ms,
                p95_ms: stats.p95_ms,
                p99_ms: stats.p99_ms,
                max_ms: stats.max_ms,
                measurements_ms,
            }
        };

        serde_json::to_string_pretty(&payload)
            .context("Failed to serialise profiler report to JSON")
    }
}

// ─── JSON export shape ─────────────────────────────────────────────────────────

#[derive(Serialize)]
struct ProfilerJson {
    name: String,
    count: usize,
    min_ms: f64,
    mean_ms: f64,
    median_ms: f64,
    p95_ms: f64,
    p99_ms: f64,
    max_ms: f64,
    measurements_ms: Vec<f64>,
}

// ─── Internal statistics ───────────────────────────────────────────────────────

struct Stats {
    min_ms: f64,
    mean_ms: f64,
    median_ms: f64,
    p95_ms: f64,
    p99_ms: f64,
    max_ms: f64,
}

fn duration_to_ms(d: &Duration) -> f64 {
    d.as_secs_f64() * 1_000.0
}

fn compute_stats(measurements: &[Duration]) -> Stats {
    let n = measurements.len();
    debug_assert!(n > 0, "compute_stats called with empty slice");

    let mut sorted: Vec<f64> = measurements.iter().map(duration_to_ms).collect();
    sorted.sort_by(|a, b| a.total_cmp(b));

    let min_ms = sorted[0];
    let max_ms = sorted[n - 1];
    let mean_ms = sorted.iter().sum::<f64>() / n as f64;
    let median_ms = percentile_from_sorted(&sorted, 50.0);
    let p95_ms = percentile_from_sorted(&sorted, 95.0);
    let p99_ms = percentile_from_sorted(&sorted, 99.0);

    Stats {
        min_ms,
        mean_ms,
        median_ms,
        p95_ms,
        p99_ms,
        max_ms,
    }
}

/// Compute the p-th percentile from a pre-sorted slice using linear interpolation.
fn percentile_from_sorted(sorted: &[f64], p: f64) -> f64 {
    let n = sorted.len();
    if n == 1 {
        return sorted[0];
    }
    // rank in [0, n-1] via the "index = p/100 * (n-1)" formula
    let rank = p / 100.0 * (n - 1) as f64;
    let lower = rank.floor() as usize;
    let upper = rank.ceil() as usize;
    if lower == upper {
        sorted[lower]
    } else {
        let frac = rank - lower as f64;
        sorted[lower] * (1.0 - frac) + sorted[upper] * frac
    }
}

// ─── Operation ────────────────────────────────────────────────────────────────

/// A geospatial operation that the profiler can benchmark.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
    /// Open the dataset (parse headers / verify magic bytes / read metadata).
    Open,
    /// Read all features from a vector dataset.
    ReadFeatures,
    /// Read raster band data (band 0 at overview level 0).
    ReadBands,
    /// Compute basic statistics by reading all data.
    Stats,
}

impl Operation {
    /// Execute the operation once against `input`.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be opened or read.
    pub fn execute(&self, input: &str) -> Result<()> {
        let path = std::path::Path::new(input);
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_lowercase())
            .unwrap_or_default();

        match self {
            Self::Open => execute_open(input, &ext),
            Self::ReadFeatures => execute_read_features(input, &ext),
            Self::ReadBands => execute_read_bands(input, &ext),
            Self::Stats => execute_stats(input, &ext),
        }
    }
}

impl FromStr for Operation {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "open" | "open-dataset" | "opendataset" => Ok(Self::Open),
            "read-features" | "readfeatures" | "features" => Ok(Self::ReadFeatures),
            "read-bands" | "readbands" | "bands" => Ok(Self::ReadBands),
            "stats" | "compute-stats" | "computestats" => Ok(Self::Stats),
            other => anyhow::bail!(
                "Unknown operation: '{other}'. Valid options: open, read-features, read-bands, stats"
            ),
        }
    }
}

impl std::fmt::Display for Operation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            Self::Open => "open",
            Self::ReadFeatures => "read-features",
            Self::ReadBands => "read-bands",
            Self::Stats => "stats",
        };
        write!(f, "{name}")
    }
}

// ─── execute_operation ────────────────────────────────────────────────────────

/// Run `op` `iterations` times against `input` and return the per-iteration durations.
///
/// Superseded by `commands::profile::run_profiler` (which additionally feeds a
/// [`Profiler`] for report/JSON export), but kept as a lighter-weight public
/// helper for callers that only need raw per-iteration [`Duration`]s.
///
/// # Errors
///
/// Propagates any error that occurs during the first failing iteration.
#[allow(dead_code)]
pub fn execute_operation(op: &Operation, input: &str, iterations: usize) -> Result<Vec<Duration>> {
    let mut durations = Vec::with_capacity(iterations);
    for i in 0..iterations {
        let start = Instant::now();
        op.execute(input)
            .with_context(|| format!("Iteration {i} of operation '{op}' failed"))?;
        durations.push(start.elapsed());
    }
    Ok(durations)
}

// ─── Dispatch helpers ─────────────────────────────────────────────────────────

fn execute_open(input: &str, ext: &str) -> Result<()> {
    match ext {
        "tif" | "tiff" => {
            use oxigdal_core::io::FileDataSource;
            use oxigdal_geotiff::GeoTiffReader;
            let source = FileDataSource::open(input)
                .with_context(|| format!("Failed to open GeoTIFF: {input}"))?;
            let _reader = GeoTiffReader::open(source)
                .with_context(|| format!("Failed to parse GeoTIFF header: {input}"))?;
            Ok(())
        }
        "geojson" | "json" => {
            use oxigdal_geojson::GeoJsonReader;
            use std::fs::File;
            use std::io::BufReader;
            let file =
                File::open(input).with_context(|| format!("Failed to open GeoJSON: {input}"))?;
            let _reader = GeoJsonReader::new(BufReader::new(file));
            Ok(())
        }
        "fgb" => {
            use oxigdal_flatgeobuf::FlatGeobufReader;
            use std::fs::File;
            let file =
                File::open(input).with_context(|| format!("Failed to open FlatGeobuf: {input}"))?;
            let _reader = FlatGeobufReader::new(file)
                .with_context(|| format!("Failed to parse FlatGeobuf header: {input}"))?;
            Ok(())
        }
        other => anyhow::bail!(
            "Unsupported file extension for 'open' operation: '{other}'. \
             Supported: tif, tiff, geojson, json, fgb"
        ),
    }
}

fn execute_read_features(input: &str, ext: &str) -> Result<()> {
    match ext {
        "geojson" | "json" => {
            use oxigdal_geojson::GeoJsonReader;
            use std::fs::File;
            use std::io::BufReader;
            let file =
                File::open(input).with_context(|| format!("Failed to open GeoJSON: {input}"))?;
            let mut reader = GeoJsonReader::new(BufReader::new(file));
            let _fc = reader
                .read_feature_collection()
                .with_context(|| format!("Failed to read features from {input}"))?;
            Ok(())
        }
        "fgb" => {
            use oxigdal_flatgeobuf::FlatGeobufReader;
            use std::fs::File;
            let file =
                File::open(input).with_context(|| format!("Failed to open FlatGeobuf: {input}"))?;
            let mut reader = FlatGeobufReader::new(file)
                .with_context(|| format!("Failed to parse FlatGeobuf: {input}"))?;
            let mut iter = reader
                .features()
                .with_context(|| format!("Failed to iterate features from {input}"))?;
            while iter.next().is_some() {}
            Ok(())
        }
        other => anyhow::bail!(
            "Unsupported file extension for 'read-features' operation: '{other}'. \
             Supported: geojson, json, fgb"
        ),
    }
}

fn execute_read_bands(input: &str, ext: &str) -> Result<()> {
    match ext {
        "tif" | "tiff" => {
            use oxigdal_core::io::FileDataSource;
            use oxigdal_geotiff::GeoTiffReader;
            let source = FileDataSource::open(input)
                .with_context(|| format!("Failed to open GeoTIFF: {input}"))?;
            let reader = GeoTiffReader::open(source)
                .with_context(|| format!("Failed to parse GeoTIFF: {input}"))?;
            let _data = reader
                .read_band(0, 0)
                .with_context(|| format!("Failed to read band from {input}"))?;
            Ok(())
        }
        other => anyhow::bail!(
            "Unsupported file extension for 'read-bands' operation: '{other}'. \
             Supported: tif, tiff"
        ),
    }
}

fn execute_stats(input: &str, ext: &str) -> Result<()> {
    match ext {
        "tif" | "tiff" => execute_read_bands(input, ext),
        "geojson" | "json" | "fgb" => execute_read_features(input, ext),
        other => anyhow::bail!(
            "Unsupported file extension for 'stats' operation: '{other}'. \
             Supported: tif, tiff, geojson, json, fgb"
        ),
    }
}

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

    #[test]
    fn test_profiler_no_measurements() {
        let p = Profiler::new("test");
        let report = p.report();
        assert!(
            report.contains("No measurements"),
            "empty profiler should say no measurements"
        );
    }

    #[test]
    fn test_profiler_single_measurement() {
        let mut p = Profiler::new("single");
        p.start();
        std::thread::sleep(Duration::from_millis(5));
        p.stop();
        let report = p.report();
        assert!(report.contains("count=1"), "report should show count=1");
        assert!(
            report.contains("single"),
            "report should include profiler name"
        );
    }

    #[test]
    fn test_profiler_export_json_empty() {
        let p = Profiler::new("empty");
        let json = p.export_json().expect("export_json should not fail");
        let v: serde_json::Value = serde_json::from_str(&json).expect("valid JSON");
        assert_eq!(v["name"], "empty");
        assert_eq!(v["count"], 0);
        assert!(
            v["measurements_ms"]
                .as_array()
                .map(|a| a.is_empty())
                .unwrap_or(false),
            "measurements_ms should be empty"
        );
    }

    #[test]
    fn test_profiler_export_json_with_data() {
        let mut p = Profiler::new("json_test");
        for _ in 0..3 {
            p.start();
            std::thread::sleep(Duration::from_millis(2));
            p.stop();
        }
        let json = p.export_json().expect("export_json should not fail");
        let v: serde_json::Value = serde_json::from_str(&json).expect("valid JSON");
        assert_eq!(v["count"], 3);
        assert_eq!(
            v["measurements_ms"]
                .as_array()
                .map(|a| a.len())
                .unwrap_or(0),
            3
        );
        assert!(v["min_ms"].as_f64().unwrap_or(0.0) > 0.0);
        assert!(v["max_ms"].as_f64().unwrap_or(0.0) >= v["min_ms"].as_f64().unwrap_or(0.0));
    }

    #[test]
    fn test_operation_from_str_valid() {
        assert_eq!("open".parse::<Operation>().ok(), Some(Operation::Open));
        assert_eq!(
            "read-features".parse::<Operation>().ok(),
            Some(Operation::ReadFeatures)
        );
        assert_eq!(
            "read-bands".parse::<Operation>().ok(),
            Some(Operation::ReadBands)
        );
        assert_eq!("stats".parse::<Operation>().ok(), Some(Operation::Stats));
        // Aliases
        assert_eq!("OPEN".parse::<Operation>().ok(), Some(Operation::Open));
        assert_eq!(
            "features".parse::<Operation>().ok(),
            Some(Operation::ReadFeatures)
        );
    }

    #[test]
    fn test_operation_from_str_invalid() {
        let result = "unknown_op".parse::<Operation>();
        assert!(result.is_err(), "unknown operation should return Err");
        let err_msg = result
            .expect_err("parsing an unknown operation must fail")
            .to_string();
        assert!(
            err_msg.contains("Unknown operation"),
            "error should mention 'Unknown operation'"
        );
    }

    #[test]
    fn test_percentile_from_sorted_single() {
        let data = vec![42.0f64];
        assert!((percentile_from_sorted(&data, 50.0) - 42.0).abs() < 1e-10);
        assert!((percentile_from_sorted(&data, 95.0) - 42.0).abs() < 1e-10);
    }

    #[test]
    fn test_percentile_from_sorted_multiple() {
        let data: Vec<f64> = (1..=10).map(|x| x as f64).collect();
        let median = percentile_from_sorted(&data, 50.0);
        // median of [1..10] should be 5.5
        assert!(
            (median - 5.5).abs() < 1e-10,
            "median should be 5.5, got {median}"
        );
        let min = percentile_from_sorted(&data, 0.0);
        assert!((min - 1.0).abs() < 1e-10);
        let max = percentile_from_sorted(&data, 100.0);
        assert!((max - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_stop_without_start_is_noop() {
        let mut p = Profiler::new("noop");
        p.stop(); // should not panic
        assert_eq!(p.measurements.len(), 0);
    }
}