veloq-core 0.4.0

Shared envelope, ProfileSource trait, and sort/time helpers for the VeloQ profile-query CLI.
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
//! Time literal parsing for CLI args.
//!
//! Accepted single durations: `42ns`, `100us`, `1500ms`, `1.2s`.
//! Accepted ranges: `<dur>-<dur>` (e.g. `1.2s-1.5s`).
//!
//! Ranges are interpreted as **offsets from trace start**. Callers add
//! the trace's `start_ns` before issuing SQL.

use std::num::ParseFloatError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum TimeParseError {
    #[error("empty time literal")]
    Empty,
    #[error("unknown time unit in `{0}` (expected ns/us/ms/s)")]
    UnknownUnit(String),
    #[error("invalid number in `{lit}`: {source}")]
    BadNumber {
        lit: String,
        #[source]
        source: ParseFloatError,
    },
    #[error("range must be `start-end`, got `{0}`")]
    BadRange(String),
    #[error("range end ({end} ns) must be greater than start ({start} ns)")]
    EmptyRange { start: i64, end: i64 },
    #[error("time literal `{0}` overflows i64 nanoseconds")]
    Overflow(String),
}

/// Parse a single duration literal into nanoseconds.
///
/// Examples: `"42ns"` → 42, `"1.2s"` → 1_200_000_000.
pub fn parse_duration_ns(s: &str) -> Result<i64, TimeParseError> {
    let s = s.trim();
    if s.is_empty() {
        return Err(TimeParseError::Empty);
    }

    // The number part is the leading run of digits / decimal point,
    // plus optionally a single leading sign. A `-` / `+` after the
    // first character is a unit-side glyph at best and a malformed
    // input at worst — let f64::parse reject it. (Previously we
    // silently consumed inline signs into the number portion, which
    // let `1-2ms` mis-split into ("1-2", "ms").)
    let mut split = 0usize;
    for (i, c) in s.char_indices() {
        let in_number = c.is_ascii_digit() || c == '.' || ((c == '-' || c == '+') && i == 0);
        if in_number {
            split = i + c.len_utf8();
        } else {
            break;
        }
    }
    let (num_part, unit_part) = s.split_at(split);
    let unit = unit_part.trim().to_ascii_lowercase();

    let value: f64 = num_part
        .parse()
        .map_err(|e: ParseFloatError| TimeParseError::BadNumber {
            lit: s.to_string(),
            source: e,
        })?;
    if !value.is_finite() {
        return Err(TimeParseError::Overflow(s.to_string()));
    }

    let multiplier_ns: f64 = match unit.as_str() {
        "ns" | "" => 1.0,
        "us" | "µs" => 1_000.0,
        "ms" => 1_000_000.0,
        "s" => 1_000_000_000.0,
        _ => return Err(TimeParseError::UnknownUnit(unit_part.to_string())),
    };

    // Explicit overflow check: `as i64` would silently saturate at
    // i64::MAX (or 0 for NaN, already caught above), producing nonsense
    // windows. Reject anything outside the representable range.
    let ns = value * multiplier_ns;
    if !ns.is_finite() || ns > i64::MAX as f64 || ns < i64::MIN as f64 {
        return Err(TimeParseError::Overflow(s.to_string()));
    }
    Ok(ns as i64)
}

/// Format a nanosecond duration for compact human-facing labels.
pub fn format_duration_ns(ns: i64) -> String {
    let abs = ns.saturating_abs();
    if abs < 1_000 {
        format!("{ns} ns")
    } else if abs < 1_000_000 {
        format!("{} us", trim_decimal(ns as f64 / 1_000.0))
    } else if abs < 1_000_000_000 {
        format!("{} ms", trim_decimal(ns as f64 / 1_000_000.0))
    } else {
        format!("{} s", trim_decimal(ns as f64 / 1_000_000_000.0))
    }
}

fn trim_decimal(value: f64) -> String {
    let mut out = format!("{value:.3}");
    if out.contains('.') {
        while out.ends_with('0') {
            out.pop();
        }
        if out.ends_with('.') {
            out.pop();
        }
    }
    out
}

/// Endpoint of a `TimeWindow`. Two anchors:
/// - `Relative(ns)` — offset from the trace's primary origin (default
///   for bare literals like `1.2s`).
/// - `Absolute(ns)` — absolute ns (literal prefixed with `@`, e.g.
///   `@-185s`). Useful when the agent has read raw timestamps from
///   `summary`'s `per_table` and wants to address them directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimePoint {
    Relative(i64),
    Absolute(i64),
}

impl TimePoint {
    pub fn resolve(&self, origin_ns: i64) -> i64 {
        match *self {
            TimePoint::Relative(off) => origin_ns + off,
            TimePoint::Absolute(abs) => abs,
        }
    }

    pub fn parse(s: &str) -> Result<Self, TimeParseError> {
        let s = s.trim();
        if let Some(rest) = s.strip_prefix('@') {
            Ok(TimePoint::Absolute(parse_duration_ns(rest)?))
        } else {
            Ok(TimePoint::Relative(parse_duration_ns(s)?))
        }
    }
}

/// Half-open time window. Endpoints are anchored as `TimePoint`s and
/// resolved to absolute ns at query time.
#[derive(Debug, Clone, Copy)]
pub struct TimeWindow {
    pub start: TimePoint,
    pub end: TimePoint,
}

impl TimeWindow {
    /// Parse a `start-end` literal. Each endpoint independently honours
    /// the `@` prefix, so you can mix anchors (e.g. `@0-1s` = from
    /// absolute 0 to 1s after primary origin) if you really need to.
    pub fn parse(s: &str) -> Result<Self, TimeParseError> {
        let s = s.trim();
        // Find the dash that separates the two endpoints. Skip a
        // possible leading `@` and a possible numeric sign so a literal
        // like `@-185s-@-180s` still parses.
        let mut search_start = 0usize;
        let bytes = s.as_bytes();
        if bytes.first() == Some(&b'@') {
            search_start = 1;
        }
        if matches!(bytes.get(search_start), Some(b'-' | b'+')) {
            search_start += 1;
        }
        let dash = s[search_start..]
            .find('-')
            .map(|i| i + search_start)
            .ok_or_else(|| TimeParseError::BadRange(s.to_string()))?;
        let (lhs, rhs_with_dash) = s.split_at(dash);
        let rhs = &rhs_with_dash[1..];

        let start = TimePoint::parse(lhs)?;
        let end = TimePoint::parse(rhs)?;
        Ok(Self { start, end })
    }

    /// Resolve to absolute ns by anchoring `Relative` endpoints against
    /// `primary_origin_ns`. Returns `(start_ns, end_ns)`.
    pub fn absolute(&self, primary_origin_ns: i64) -> Result<(i64, i64), TimeParseError> {
        let s = self.start.resolve(primary_origin_ns);
        let e = self.end.resolve(primary_origin_ns);
        if e <= s {
            return Err(TimeParseError::EmptyRange { start: s, end: e });
        }
        Ok((s, e))
    }
}

/// Duration-on-an-event predicate, parsed from a CLI string like
/// `">1ms"` or `"100us-1ms"`. Used by `search --duration`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DurationFilter {
    /// `> threshold_ns`
    Gt(i64),
    /// `>= threshold_ns`
    Gte(i64),
    /// `< threshold_ns`
    Lt(i64),
    /// `<= threshold_ns`
    Lte(i64),
    /// `min_ns ..= max_ns`
    Range { min_ns: i64, max_ns: i64 },
}

impl DurationFilter {
    /// Render as a SQL fragment in terms of `duration_col`. The fragment
    /// is parameterised (uses `?`); call `.sql_params()` to get the bind
    /// list in the matching order.
    pub fn sql(&self, duration_col: &str) -> String {
        match self {
            DurationFilter::Gt(_) => format!("{duration_col} > ?"),
            DurationFilter::Gte(_) => format!("{duration_col} >= ?"),
            DurationFilter::Lt(_) => format!("{duration_col} < ?"),
            DurationFilter::Lte(_) => format!("{duration_col} <= ?"),
            DurationFilter::Range { .. } => {
                format!("{duration_col} BETWEEN ? AND ?")
            }
        }
    }

    pub fn sql_params(&self) -> Vec<i64> {
        match *self {
            DurationFilter::Gt(n) => vec![n],
            DurationFilter::Gte(n) => vec![n],
            DurationFilter::Lt(n) => vec![n],
            DurationFilter::Lte(n) => vec![n],
            DurationFilter::Range { min_ns, max_ns } => vec![min_ns, max_ns],
        }
    }

    pub fn matches(&self, duration_ns: i64) -> bool {
        match *self {
            DurationFilter::Gt(ns) => duration_ns > ns,
            DurationFilter::Gte(ns) => duration_ns >= ns,
            DurationFilter::Lt(ns) => duration_ns < ns,
            DurationFilter::Lte(ns) => duration_ns <= ns,
            DurationFilter::Range { min_ns, max_ns } => {
                duration_ns >= min_ns && duration_ns <= max_ns
            }
        }
    }

    pub fn parse(s: &str) -> Result<Self, TimeParseError> {
        let s = s.trim();
        if s.is_empty() {
            return Err(TimeParseError::Empty);
        }

        // Range form: `<dur>-<dur>` (and not starting with `>` or `<`).
        // Skip past the first codepoint when searching for the separator
        // dash — the first char could be a leading numeric sign and
        // won't itself be the separator. Char-aware indexing keeps
        // multibyte input (e.g. `µs`) from byte-slicing into the middle
        // of a codepoint and panicking before the JSON error envelope.
        if !s.starts_with('<') && !s.starts_with('>') {
            let after_first = s.char_indices().nth(1).map(|(i, _)| i).unwrap_or(s.len());
            if let Some(dash) = s[after_first..].find('-').map(|i| i + after_first) {
                let (lhs, rhs) = s.split_at(dash);
                let rhs = &rhs[1..]; // skip the dash itself (ASCII, 1 byte)
                let min_ns = parse_duration_ns(lhs)?;
                let max_ns = parse_duration_ns(rhs)?;
                if max_ns < min_ns {
                    return Err(TimeParseError::EmptyRange {
                        start: min_ns,
                        end: max_ns,
                    });
                }
                return Ok(DurationFilter::Range { min_ns, max_ns });
            }
            // Bare literal with no operator is ambiguous (equal? at least?).
            // Force the user to be explicit.
            return Err(TimeParseError::BadRange(format!(
                "{s} — duration filter needs a comparator (`>`, `>=`, `<`, `<=`) or a range (`min-max`)"
            )));
        }

        // Comparator form. Local typed enum drops the stringly-typed
        // `op` round-trip and makes the final `match` exhaustive without
        // an unreachable arm.
        enum Op {
            Gt,
            Ge,
            Lt,
            Le,
        }
        let (op, rest) = if let Some(rest) = s.strip_prefix(">=") {
            (Op::Ge, rest)
        } else if let Some(rest) = s.strip_prefix("<=") {
            (Op::Le, rest)
        } else if let Some(rest) = s.strip_prefix('>') {
            (Op::Gt, rest)
        } else if let Some(rest) = s.strip_prefix('<') {
            (Op::Lt, rest)
        } else {
            return Err(TimeParseError::BadRange(s.to_string()));
        };
        let n = parse_duration_ns(rest.trim())?;
        Ok(match op {
            Op::Gt => DurationFilter::Gt(n),
            Op::Ge => DurationFilter::Gte(n),
            Op::Lt => DurationFilter::Lt(n),
            Op::Le => DurationFilter::Lte(n),
        })
    }
}

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

    #[test]
    fn durations() -> anyhow::Result<()> {
        assert_eq!(parse_duration_ns("42ns")?, 42);
        assert_eq!(parse_duration_ns("100us")?, 100_000);
        assert_eq!(parse_duration_ns("1.5ms")?, 1_500_000);
        assert_eq!(parse_duration_ns("1.2s")?, 1_200_000_000);
        Ok(())
    }

    #[test]
    fn duration_labels_are_compact() {
        assert_eq!(format_duration_ns(0), "0 ns");
        assert_eq!(format_duration_ns(42), "42 ns");
        assert_eq!(format_duration_ns(1_500), "1.5 us");
        assert_eq!(format_duration_ns(12_345_678), "12.346 ms");
        assert_eq!(format_duration_ns(1_200_000_000), "1.2 s");
        assert_eq!(format_duration_ns(-1_500), "-1.5 us");
        assert_eq!(format_duration_ns(-12_345_678), "-12.346 ms");
        assert_eq!(format_duration_ns(-1_200_000_000), "-1.2 s");
        assert_eq!(format_duration_ns(i64::MIN), "-9223372036.855 s");
    }

    #[test]
    fn ranges() -> anyhow::Result<()> {
        let w = TimeWindow::parse("1.2s-1.5s")?;
        assert_eq!(w.start, TimePoint::Relative(1_200_000_000));
        assert_eq!(w.end, TimePoint::Relative(1_500_000_000));
        let (a, b) = w.absolute(186_198_374)?;
        assert_eq!(a, 1_386_198_374);
        assert_eq!(b, 1_686_198_374);
        Ok(())
    }

    #[test]
    fn absolute_endpoints() -> anyhow::Result<()> {
        let w = TimeWindow::parse("@-185s-@-180s")?;
        assert_eq!(w.start, TimePoint::Absolute(-185_000_000_000));
        assert_eq!(w.end, TimePoint::Absolute(-180_000_000_000));
        // origin is ignored for absolute endpoints
        let (a, b) = w.absolute(123)?;
        assert_eq!((a, b), (-185_000_000_000, -180_000_000_000));
        Ok(())
    }

    #[test]
    fn mixed_anchors() -> anyhow::Result<()> {
        let w = TimeWindow::parse("@-1s-2s")?;
        assert_eq!(w.start, TimePoint::Absolute(-1_000_000_000));
        assert_eq!(w.end, TimePoint::Relative(2_000_000_000));
        let (a, b) = w.absolute(5_000_000_000)?;
        assert_eq!((a, b), (-1_000_000_000, 7_000_000_000));
        Ok(())
    }

    #[test]
    fn bad_unit() {
        assert!(matches!(
            parse_duration_ns("5min"),
            Err(TimeParseError::UnknownUnit(_))
        ));
    }

    #[test]
    fn overflow_rejected() {
        // `9999999999999s` * 1e9 ns/s > i64::MAX
        assert!(matches!(
            parse_duration_ns("9999999999999s"),
            Err(TimeParseError::Overflow(_))
        ));
        // Scientific notation isn't supported by the lexer (the number
        // part stops at `e`), so this lands as UnknownUnit rather than
        // Overflow — fine either way, just not a number we'd silently
        // saturate.
        assert!(parse_duration_ns("1e30s").is_err());
    }

    #[test]
    fn inline_signs_rejected() {
        // Earlier impl silently split `1-2ms` as ("1-2", "ms"); now `-`
        // mid-literal stays with the unit half and forces BadNumber/Unit.
        assert!(parse_duration_ns("1-2ms").is_err());
    }

    #[test]
    fn multibyte_input_doesnt_panic() {
        // Regression: `µ` is two bytes, so the old byte-indexed `s[1..]`
        // lookup in `DurationFilter::parse` would split into the middle
        // of the codepoint and panic. Now it should fall through to a
        // structured error.
        assert!(DurationFilter::parse("µs").is_err());
        assert!(DurationFilter::parse("µ-1ms").is_err());
    }

    #[test]
    fn empty_range_rejected_at_resolve() -> anyhow::Result<()> {
        // The parser is structural — it builds endpoints without
        // touching the trace origin. The empty/inverted check moved to
        // `.absolute()` so absolute endpoints (which only get a true
        // ordering once resolved) are checked uniformly.
        let w = TimeWindow::parse("1s-1s")?;
        assert!(matches!(
            w.absolute(0),
            Err(TimeParseError::EmptyRange { .. })
        ));
        Ok(())
    }

    #[test]
    fn duration_filter_comparators() -> anyhow::Result<()> {
        assert_eq!(
            DurationFilter::parse(">1ms")?,
            DurationFilter::Gt(1_000_000)
        );
        assert_eq!(
            DurationFilter::parse(">=100us")?,
            DurationFilter::Gte(100_000)
        );
        assert_eq!(
            DurationFilter::parse("<1s")?,
            DurationFilter::Lt(1_000_000_000)
        );
        assert_eq!(DurationFilter::parse("<=42ns")?, DurationFilter::Lte(42));
        Ok(())
    }

    #[test]
    fn duration_filter_range() -> anyhow::Result<()> {
        assert_eq!(
            DurationFilter::parse("100us-1ms")?,
            DurationFilter::Range {
                min_ns: 100_000,
                max_ns: 1_000_000
            }
        );
        Ok(())
    }

    #[test]
    fn duration_filter_matches_typed_values() {
        assert!(DurationFilter::Gt(10).matches(11));
        assert!(!DurationFilter::Gt(10).matches(10));
        assert!(DurationFilter::Gte(10).matches(10));
        assert!(DurationFilter::Lt(10).matches(9));
        assert!(!DurationFilter::Lt(10).matches(10));
        assert!(DurationFilter::Lte(10).matches(10));
        assert!(
            DurationFilter::Range {
                min_ns: 10,
                max_ns: 20
            }
            .matches(15)
        );
    }

    #[test]
    fn duration_filter_bare_literal_rejected() {
        assert!(DurationFilter::parse("1ms").is_err());
    }
}