sonda 1.4.0

CLI for Sonda — synthetic telemetry generator for testing observability pipelines
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
//! YAML scenario generation from detected patterns.
//!
//! Converts pattern detection results into valid sonda scenario YAML that
//! uses operational vocabulary aliases (`steady`, `spike_event`, `leak`,
//! `saturation`, `flap`) or base generators (`sawtooth`, `step`) where
//! appropriate.
//!
//! The generated YAML is designed to be loadable by `sonda run --scenario`.

use std::collections::HashMap;

use super::csv_reader::ColumnMeta;
use super::pattern::Pattern;
use crate::yaml_helpers::{format_float, format_rate, needs_quoting, ParamValue};

/// A single scenario derived from one CSV column.
#[derive(Debug)]
pub struct ScenarioSpec {
    /// Metric name for this scenario.
    pub name: String,
    /// Generator type name (operational alias or base generator).
    pub generator_type: String,
    /// Generator parameters as key-value pairs.
    pub generator_params: Vec<(String, ParamValue)>,
    /// Static labels for this scenario.
    pub labels: HashMap<String, String>,
}

/// Convert a pattern and column metadata into a scenario specification.
///
/// The `rate` parameter is needed to convert point-based durations into
/// time-based durations for generator parameters. The `duration` string
/// (e.g., `"60s"`, `"5m"`) is forwarded to patterns like `Climb` that
/// map to the `leak` alias, where `time_to_ceiling` must be >= the
/// scenario duration to pass sonda-core validation.
pub fn pattern_to_spec(
    pattern: &Pattern,
    meta: &ColumnMeta,
    rate: f64,
    duration: &str,
) -> ScenarioSpec {
    let name = meta
        .metric_name
        .clone()
        .unwrap_or_else(|| format!("column_{}", meta.index));

    let labels = meta.labels.clone();

    let (generator_type, generator_params) = match pattern {
        Pattern::Steady { center, amplitude } => {
            // 60s is a reasonable default period for steady oscillation:
            // it matches common scrape intervals (Prometheus default 15-60s)
            // and produces visually smooth waves at typical emission rates.
            // The pattern detector does not estimate period from the data
            // because a steady signal's frequency is not its defining trait.
            let period = "60s".to_string();
            let params = vec![
                ("center".to_string(), ParamValue::Float(*center)),
                ("amplitude".to_string(), ParamValue::Float(*amplitude)),
                ("period".to_string(), ParamValue::String(period)),
            ];
            ("steady".to_string(), params)
        }
        Pattern::Spike {
            baseline,
            spike_height,
            spike_duration_points,
            spike_interval_points,
        } => {
            let spike_dur_secs = points_to_duration(*spike_duration_points, rate);
            let spike_int_secs = points_to_duration(*spike_interval_points, rate);
            let params = vec![
                ("baseline".to_string(), ParamValue::Float(*baseline)),
                ("spike_height".to_string(), ParamValue::Float(*spike_height)),
                (
                    "spike_duration".to_string(),
                    ParamValue::String(format_duration(spike_dur_secs)),
                ),
                (
                    "spike_interval".to_string(),
                    ParamValue::String(format_duration(spike_int_secs)),
                ),
            ];
            ("spike_event".to_string(), params)
        }
        Pattern::Climb { baseline, ceiling } => {
            // Use "leak" alias: one-way ramp to ceiling. The leak alias
            // desugaring in sonda-core validates that time_to_ceiling >=
            // scenario duration, so we set it to match the scenario
            // duration exactly (the ramp fills the entire run).
            let params = vec![
                ("baseline".to_string(), ParamValue::Float(*baseline)),
                ("ceiling".to_string(), ParamValue::Float(*ceiling)),
                (
                    "time_to_ceiling".to_string(),
                    ParamValue::String(duration.to_string()),
                ),
            ];
            ("leak".to_string(), params)
        }
        Pattern::Sawtooth {
            min,
            max,
            period_points,
        } => {
            let period_secs = points_to_duration(*period_points, rate);
            let params = vec![
                ("min".to_string(), ParamValue::Float(*min)),
                ("max".to_string(), ParamValue::Float(*max)),
                ("period_secs".to_string(), ParamValue::Float(period_secs)),
            ];
            ("sawtooth".to_string(), params)
        }
        Pattern::Flap {
            up_value,
            down_value,
            up_duration_points,
            down_duration_points,
        } => {
            let up_dur_secs = points_to_duration(*up_duration_points, rate);
            let down_dur_secs = points_to_duration(*down_duration_points, rate);
            let params = vec![
                ("up_value".to_string(), ParamValue::Float(*up_value)),
                ("down_value".to_string(), ParamValue::Float(*down_value)),
                (
                    "up_duration".to_string(),
                    ParamValue::String(format_duration(up_dur_secs)),
                ),
                (
                    "down_duration".to_string(),
                    ParamValue::String(format_duration(down_dur_secs)),
                ),
            ];
            ("flap".to_string(), params)
        }
        Pattern::Step { start, step_size } => {
            let params = vec![
                ("start".to_string(), ParamValue::Float(*start)),
                ("step_size".to_string(), ParamValue::Float(*step_size)),
            ];
            ("step".to_string(), params)
        }
    };

    ScenarioSpec {
        name,
        generator_type,
        generator_params,
        labels,
    }
}

/// Convert data points to a duration in seconds, given the emission rate.
fn points_to_duration(points: usize, rate: f64) -> f64 {
    if rate <= 0.0 {
        return points as f64;
    }
    points as f64 / rate
}

/// Format a duration in seconds as a human-readable string.
///
/// Uses the smallest whole unit that expresses the value without fractions:
/// - "Xs" for whole seconds
/// - "Xm" for values that divide evenly into minutes
/// - Falls back to "X.Ys" for fractional seconds
fn format_duration(secs: f64) -> String {
    if secs <= 0.0 {
        return "1s".to_string();
    }

    let rounded = (secs * 10.0).round() / 10.0;

    if rounded >= 60.0 && (rounded % 60.0).abs() < 0.01 {
        format!("{}m", (rounded / 60.0).round() as u64)
    } else if (rounded - rounded.round()).abs() < 0.01 {
        format!("{}s", rounded.round() as u64)
    } else {
        format!("{rounded:.1}s")
    }
}

/// Render a complete scenario YAML string from a list of scenario specs.
///
/// Always produces a v2 scenario file (`version: 2`) with a `defaults:`
/// block carrying the shared rate/duration/encoder/sink plus a
/// `scenarios:` list where each entry is a `metrics` signal. v1 shapes
/// are never emitted — the unified loader only accepts v2.
pub fn render_yaml(specs: &[ScenarioSpec], rate: f64, duration: &str) -> String {
    if specs.is_empty() {
        return String::new();
    }

    let mut out = String::with_capacity(specs.len() * 512 + 256);
    out.push_str("version: 2\n");
    out.push('\n');
    out.push_str("defaults:\n");
    out.push_str(&format!("  rate: {}\n", format_rate(rate)));
    out.push_str(&format!("  duration: {duration}\n"));
    out.push_str("  encoder:\n");
    out.push_str("    type: prometheus_text\n");
    out.push_str("  sink:\n");
    out.push_str("    type: stdout\n");
    out.push('\n');
    out.push_str("scenarios:\n");

    for spec in specs {
        out.push_str(&format!("  - id: {}\n", spec.name));
        out.push_str("    signal_type: metrics\n");
        out.push_str(&format!("    name: {}\n", spec.name));

        render_generator(&mut out, spec, 4);

        if !spec.labels.is_empty() {
            render_labels(&mut out, &spec.labels, 4);
        }

        out.push('\n');
    }

    out
}

/// Render the generator block for a scenario spec.
fn render_generator(out: &mut String, spec: &ScenarioSpec, indent: usize) {
    let pad = " ".repeat(indent);
    out.push_str(&format!("{pad}generator:\n"));
    out.push_str(&format!("{pad}  type: {}\n", spec.generator_type));
    for (key, value) in &spec.generator_params {
        match value {
            ParamValue::Float(v) => {
                out.push_str(&format!("{pad}  {key}: {}\n", format_float(*v)));
            }
            ParamValue::String(s) => {
                out.push_str(&format!("{pad}  {key}: \"{s}\"\n"));
            }
        }
    }
}

/// Render the labels block.
fn render_labels(out: &mut String, labels: &HashMap<String, String>, indent: usize) {
    let pad = " ".repeat(indent);
    out.push_str(&format!("{pad}labels:\n"));
    // Sort labels for deterministic output.
    let mut sorted: Vec<_> = labels.iter().collect();
    sorted.sort_by_key(|(k, _)| *k);
    for (key, value) in sorted {
        // Quote values that might be misinterpreted by YAML parsers.
        if needs_quoting(value) {
            out.push_str(&format!("{pad}  {key}: \"{value}\"\n"));
        } else {
            out.push_str(&format!("{pad}  {key}: {value}\n"));
        }
    }
}

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

    // -----------------------------------------------------------------------
    // format_duration
    // -----------------------------------------------------------------------

    #[test]
    fn format_duration_whole_seconds() {
        assert_eq!(format_duration(30.0), "30s");
    }

    #[test]
    fn format_duration_minutes() {
        assert_eq!(format_duration(120.0), "2m");
    }

    #[test]
    fn format_duration_fractional_seconds() {
        assert_eq!(format_duration(1.5), "1.5s");
    }

    #[test]
    fn format_duration_zero_returns_one_second() {
        assert_eq!(format_duration(0.0), "1s");
    }

    // -----------------------------------------------------------------------
    // pattern_to_spec: steady
    // -----------------------------------------------------------------------

    #[test]
    fn steady_pattern_to_spec_uses_alias() {
        let pattern = Pattern::Steady {
            center: 50.0,
            amplitude: 10.0,
        };
        let meta = ColumnMeta {
            index: 1,
            metric_name: Some("cpu_usage".to_string()),
            labels: HashMap::new(),
        };
        let spec = pattern_to_spec(&pattern, &meta, 1.0, "60s");
        assert_eq!(spec.name, "cpu_usage");
        assert_eq!(spec.generator_type, "steady");
    }

    #[test]
    fn spike_pattern_to_spec_uses_alias() {
        let pattern = Pattern::Spike {
            baseline: 10.0,
            spike_height: 90.0,
            spike_duration_points: 5,
            spike_interval_points: 30,
        };
        let meta = ColumnMeta {
            index: 1,
            metric_name: Some("error_rate".to_string()),
            labels: HashMap::new(),
        };
        let spec = pattern_to_spec(&pattern, &meta, 1.0, "60s");
        assert_eq!(spec.generator_type, "spike_event");
    }

    #[test]
    fn climb_pattern_to_spec_uses_leak_alias() {
        let pattern = Pattern::Climb {
            baseline: 0.0,
            ceiling: 100.0,
        };
        let meta = ColumnMeta {
            index: 1,
            metric_name: Some("mem_usage".to_string()),
            labels: HashMap::new(),
        };
        let spec = pattern_to_spec(&pattern, &meta, 1.0, "60s");
        assert_eq!(spec.generator_type, "leak");
    }

    #[test]
    fn climb_pattern_sets_time_to_ceiling_from_duration() {
        let pattern = Pattern::Climb {
            baseline: 10.0,
            ceiling: 90.0,
        };
        let meta = ColumnMeta {
            index: 1,
            metric_name: Some("mem_leak".to_string()),
            labels: HashMap::new(),
        };
        let spec = pattern_to_spec(&pattern, &meta, 1.0, "5m");
        let ttc = spec
            .generator_params
            .iter()
            .find(|(k, _)| k == "time_to_ceiling");
        assert!(ttc.is_some(), "must include time_to_ceiling param");
        match &ttc.unwrap().1 {
            ParamValue::String(s) => assert_eq!(s, "5m"),
            other => panic!("expected String, got {other:?}"),
        }
    }

    #[test]
    fn flap_pattern_to_spec_uses_alias() {
        let pattern = Pattern::Flap {
            up_value: 1.0,
            down_value: 0.0,
            up_duration_points: 10,
            down_duration_points: 5,
        };
        let meta = ColumnMeta {
            index: 1,
            metric_name: Some("link_state".to_string()),
            labels: HashMap::new(),
        };
        let spec = pattern_to_spec(&pattern, &meta, 1.0, "60s");
        assert_eq!(spec.generator_type, "flap");
    }

    // -----------------------------------------------------------------------
    // render_yaml: single scenario
    // -----------------------------------------------------------------------

    #[test]
    fn render_single_scenario_produces_valid_v2_yaml() {
        let spec = ScenarioSpec {
            name: "cpu_usage".to_string(),
            generator_type: "steady".to_string(),
            generator_params: vec![
                ("center".to_string(), ParamValue::Float(50.0)),
                ("amplitude".to_string(), ParamValue::Float(10.0)),
                ("period".to_string(), ParamValue::String("60s".to_string())),
            ],
            labels: HashMap::new(),
        };
        let yaml = render_yaml(&[spec], 1.0, "60s");
        assert!(
            yaml.starts_with("version: 2\n"),
            "v2 output must begin with `version: 2`, got: {yaml}"
        );
        assert!(yaml.contains("defaults:"));
        assert!(yaml.contains("scenarios:"));
        assert!(yaml.contains("id: cpu_usage"));
        assert!(yaml.contains("name: cpu_usage"));
        assert!(yaml.contains("rate: 1"));
        assert!(yaml.contains("type: steady"));
        assert!(yaml.contains("center: 50.0"));
        assert!(yaml.contains("type: prometheus_text"));
        assert!(yaml.contains("type: stdout"));
    }

    // -----------------------------------------------------------------------
    // render_yaml: multi scenario
    // -----------------------------------------------------------------------

    #[test]
    fn render_multi_scenario_has_v2_header_and_defaults() {
        let specs = vec![
            ScenarioSpec {
                name: "cpu".to_string(),
                generator_type: "steady".to_string(),
                generator_params: vec![("center".to_string(), ParamValue::Float(50.0))],
                labels: HashMap::new(),
            },
            ScenarioSpec {
                name: "mem".to_string(),
                generator_type: "steady".to_string(),
                generator_params: vec![("center".to_string(), ParamValue::Float(80.0))],
                labels: HashMap::new(),
            },
        ];
        let yaml = render_yaml(&specs, 1.0, "60s");
        assert!(yaml.starts_with("version: 2\n"));
        assert!(yaml.contains("defaults:"));
        assert!(yaml.contains("scenarios:"));
        assert!(yaml.contains("signal_type: metrics"));
        assert!(yaml.contains("id: cpu"));
        assert!(yaml.contains("id: mem"));
        assert!(yaml.contains("name: cpu"));
        assert!(yaml.contains("name: mem"));
    }

    // -----------------------------------------------------------------------
    // render_yaml: labels preserved
    // -----------------------------------------------------------------------

    #[test]
    fn render_yaml_preserves_labels() {
        let mut labels = HashMap::new();
        labels.insert("instance".to_string(), "web-01".to_string());
        labels.insert("job".to_string(), "node_exporter".to_string());
        let spec = ScenarioSpec {
            name: "up".to_string(),
            generator_type: "flap".to_string(),
            generator_params: vec![
                ("up_value".to_string(), ParamValue::Float(1.0)),
                ("down_value".to_string(), ParamValue::Float(0.0)),
            ],
            labels,
        };
        let yaml = render_yaml(&[spec], 1.0, "60s");
        assert!(yaml.contains("instance: web-01"));
        assert!(yaml.contains("job: node_exporter"));
    }

    // -----------------------------------------------------------------------
    // render_yaml: empty specs
    // -----------------------------------------------------------------------

    #[test]
    fn render_yaml_empty_returns_empty() {
        assert_eq!(render_yaml(&[], 1.0, "60s"), "");
    }

    // -----------------------------------------------------------------------
    // points_to_duration
    // -----------------------------------------------------------------------

    #[test]
    fn points_to_duration_basic() {
        assert_eq!(points_to_duration(10, 1.0), 10.0);
        assert_eq!(points_to_duration(10, 2.0), 5.0);
        assert_eq!(points_to_duration(30, 0.5), 60.0);
    }

    // -----------------------------------------------------------------------
    // Determinism: same input produces same output
    // -----------------------------------------------------------------------

    #[test]
    fn render_yaml_is_deterministic() {
        let make_spec = || ScenarioSpec {
            name: "test".to_string(),
            generator_type: "steady".to_string(),
            generator_params: vec![("center".to_string(), ParamValue::Float(50.0))],
            labels: HashMap::new(),
        };
        let a = render_yaml(&[make_spec()], 1.0, "60s");
        let b = render_yaml(&[make_spec()], 1.0, "60s");
        assert_eq!(a, b);
    }
}