coralogix-cli 0.1.13

Coralogix CLI - the observability backbone for AI agents and engineering teams.
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
use serde_json::json;

use super::*;

// ── helpers ───────────────────────────────────────────────────────────────────

/// Build a tagged JSON row that instant_samples_to_toon_rows expects.
fn make_instant_row(metric: &[(&str, &str)], value: &str, profile: Option<&str>) -> Value {
    let metric_obj: serde_json::Map<String, Value> = metric
        .iter()
        .map(|(k, v)| (k.to_string(), json!(v)))
        .collect();
    let mut row = json!({
        "metric": metric_obj,
        "value": [json!(1_700_000_000u64), json!(value)],
    });
    if let Some(p) = profile {
        if let Value::Object(ref mut m) = row {
            m.insert("profile".to_string(), json!(p));
        }
    }
    row
}

// ── instant_samples_to_toon_rows (multi-profile) ──────────────────────────────

#[test]
fn empty_input_returns_empty_vec() {
    let rows = instant_samples_to_toon_rows(&[], true);
    assert!(rows.is_empty());
}

#[test]
fn single_sample_single_label_with_profile() {
    let sample = make_instant_row(&[("job", "prometheus")], "1", Some("default"));
    let rows = instant_samples_to_toon_rows(&[sample], true);

    assert_eq!(rows.len(), 1);
    let obj = rows[0].as_object().unwrap();
    assert_eq!(obj["job"], json!("prometheus"));
    assert_eq!(obj["value"], json!("1"));
    assert_eq!(obj["profile"], json!("default"));
    assert!(!obj.contains_key("timestamp"));
}

#[test]
fn single_sample_multiple_labels_with_profile() {
    let sample = make_instant_row(
        &[("job", "node"), ("instance", "host:9100")],
        "42",
        Some("p1"),
    );
    let rows = instant_samples_to_toon_rows(&[sample], true);

    let obj = rows[0].as_object().unwrap();
    // Metric label keys come first sorted alphabetically, then "profile", then "value".
    let keys: Vec<&str> = obj.keys().map(String::as_str).collect();
    assert_eq!(keys, vec!["instance", "job", "profile", "value"]);
    assert_eq!(obj["instance"], json!("host:9100"));
    assert_eq!(obj["job"], json!("node"));
    assert_eq!(obj["value"], json!("42"));
}

#[test]
fn multiple_samples_same_labels_produce_uniform_rows_with_profile() {
    let samples = vec![
        make_instant_row(&[("job", "a"), ("instance", "h1")], "1", Some("default")),
        make_instant_row(&[("job", "b"), ("instance", "h2")], "2", Some("default")),
    ];
    let rows = instant_samples_to_toon_rows(&samples, true);

    assert_eq!(rows.len(), 2);
    for row in &rows {
        let keys: Vec<&str> = row
            .as_object()
            .unwrap()
            .keys()
            .map(String::as_str)
            .collect();
        // instance, job, profile, value
        assert_eq!(keys.len(), 4);
    }
    assert_eq!(rows[0].as_object().unwrap()["value"], json!("1"));
    assert_eq!(rows[1].as_object().unwrap()["value"], json!("2"));
}

#[test]
fn multiple_samples_differing_labels_padded_with_empty_string_with_profile() {
    // One sample has an extra label "pod" that the other lacks.
    let samples = vec![
        make_instant_row(&[("job", "a")], "1", Some("default")),
        make_instant_row(&[("job", "b"), ("pod", "p1")], "2", Some("default")),
    ];
    let rows = instant_samples_to_toon_rows(&samples, true);

    assert_eq!(rows.len(), 2);

    let r0 = rows[0].as_object().unwrap();
    let r1 = rows[1].as_object().unwrap();

    // Both rows must carry the full key set: job, pod (metric labels sorted), then profile, value.
    assert_eq!(
        r0.keys().map(String::as_str).collect::<Vec<_>>(),
        vec!["job", "pod", "profile", "value"]
    );
    assert_eq!(
        r1.keys().map(String::as_str).collect::<Vec<_>>(),
        vec!["job", "pod", "profile", "value"]
    );

    // Missing label is filled with an empty string so toon can render a table.
    assert_eq!(r0["pod"], json!(""));
    assert_eq!(r1["pod"], json!("p1"));
}

#[test]
fn label_keys_are_sorted_alphabetically_before_profile_and_value() {
    let sample = make_instant_row(
        &[("zzz", "z"), ("aaa", "a"), ("mmm", "m")],
        "0",
        Some("default"),
    );
    let rows = instant_samples_to_toon_rows(&[sample], true);

    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    // Metric label keys sorted first, then "profile", then "value" last.
    assert_eq!(keys, vec!["aaa", "mmm", "zzz", "profile", "value"]);
}

#[test]
fn value_column_is_always_last_with_profile() {
    let sample = make_instant_row(&[("a", "1"), ("z", "2")], "99", Some("default"));
    let rows = instant_samples_to_toon_rows(&[sample], true);
    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    assert_eq!(keys.last(), Some(&"value"));
}

#[test]
fn multi_profile_samples_carry_distinct_profile_values() {
    let samples = vec![
        make_instant_row(&[("job", "api")], "1", Some("prod")),
        make_instant_row(&[("job", "api")], "2", Some("staging")),
    ];
    let rows = instant_samples_to_toon_rows(&samples, true);

    assert_eq!(rows[0].as_object().unwrap()["profile"], json!("prod"));
    assert_eq!(rows[1].as_object().unwrap()["profile"], json!("staging"));
}

// ── instant_samples_to_toon_rows (single-profile, no profile column) ──────────

#[test]
fn single_profile_sample_omits_profile_column() {
    let sample = make_instant_row(&[("job", "prometheus")], "1", None);
    let rows = instant_samples_to_toon_rows(&[sample], false);

    assert_eq!(rows.len(), 1);
    let obj = rows[0].as_object().unwrap();
    assert_eq!(obj["job"], json!("prometheus"));
    assert_eq!(obj["value"], json!("1"));
    assert!(!obj.contains_key("profile"));
}

#[test]
fn single_profile_multiple_labels_omits_profile_column() {
    let sample = make_instant_row(&[("job", "node"), ("instance", "host:9100")], "42", None);
    let rows = instant_samples_to_toon_rows(&[sample], false);

    let obj = rows[0].as_object().unwrap();
    // Metric label keys sorted alphabetically, then "value". No "profile".
    let keys: Vec<&str> = obj.keys().map(String::as_str).collect();
    assert_eq!(keys, vec!["instance", "job", "value"]);
    assert!(!obj.contains_key("profile"));
}

#[test]
fn single_profile_value_column_is_always_last() {
    let sample = make_instant_row(&[("a", "1"), ("z", "2")], "99", None);
    let rows = instant_samples_to_toon_rows(&[sample], false);
    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    assert_eq!(keys.last(), Some(&"value"));
    assert!(!keys.contains(&"profile"));
}

// ── range_samples_to_toon_rows ────────────────────────────────────────────────

/// Build a tagged JSON row that range_samples_to_toon_rows expects.
fn make_range_row(metric: &[(&str, &str)], values: &[(u64, &str)], profile: Option<&str>) -> Value {
    let metric_obj: serde_json::Map<String, Value> = metric
        .iter()
        .map(|(k, v)| (k.to_string(), json!(v)))
        .collect();
    let values_arr: Vec<Value> = values.iter().map(|(ts, val)| json!([ts, val])).collect();
    let mut row = json!({
        "metric": metric_obj,
        "values": values_arr,
    });
    if let Some(p) = profile {
        if let Value::Object(ref mut m) = row {
            m.insert("profile".to_string(), json!(p));
        }
    }
    row
}

#[test]
fn range_empty_input_returns_empty_vec() {
    let rows = range_samples_to_toon_rows(&[], false);
    assert!(rows.is_empty());
}

#[test]
fn range_single_series_single_point() {
    // 1 series, 1 point → 1 row; timestamp becomes a column header containing the value.
    let sample = make_range_row(&[("job", "api")], &[(1_719_014_400, "42")], None);
    let rows = range_samples_to_toon_rows(&[sample], false);

    assert_eq!(rows.len(), 1);
    let obj = rows[0].as_object().unwrap();
    assert_eq!(obj["job"], json!("api"));
    // The timestamp column key is the ISO string.
    assert_eq!(obj["2024-06-22T00:00:00Z"], json!("42"));
    // No separate "timestamp" or "value" columns.
    assert!(!obj.contains_key("timestamp"));
    assert!(!obj.contains_key("value"));
}

#[test]
fn range_single_series_multiple_points() {
    // 1 series, 3 points → 1 row with 3 timestamp columns.
    let sample = make_range_row(
        &[("job", "api")],
        &[
            (1_719_014_400, "1"),
            (1_719_014_460, "2"),
            (1_719_014_520, "3"),
        ],
        None,
    );
    let rows = range_samples_to_toon_rows(&[sample], false);

    assert_eq!(rows.len(), 1);
    let obj = rows[0].as_object().unwrap();
    assert_eq!(obj["job"], json!("api"));
    assert_eq!(obj["2024-06-22T00:00:00Z"], json!("1"));
    assert_eq!(obj["2024-06-22T00:01:00Z"], json!("2"));
    assert_eq!(obj["2024-06-22T00:02:00Z"], json!("3"));
}

#[test]
fn range_multiple_series_same_timestamps() {
    // 2 series sharing the same timestamps → 2 rows with shared timestamp columns.
    let samples = vec![
        make_range_row(
            &[("job", "a")],
            &[(1_719_014_400, "10"), (1_719_014_460, "20")],
            None,
        ),
        make_range_row(
            &[("job", "b")],
            &[(1_719_014_400, "30"), (1_719_014_460, "40")],
            None,
        ),
    ];
    let rows = range_samples_to_toon_rows(&samples, false);

    assert_eq!(rows.len(), 2);
    let r0 = rows[0].as_object().unwrap();
    let r1 = rows[1].as_object().unwrap();
    assert_eq!(r0["job"], json!("a"));
    assert_eq!(r0["2024-06-22T00:00:00Z"], json!("10"));
    assert_eq!(r0["2024-06-22T00:01:00Z"], json!("20"));
    assert_eq!(r1["job"], json!("b"));
    assert_eq!(r1["2024-06-22T00:00:00Z"], json!("30"));
    assert_eq!(r1["2024-06-22T00:01:00Z"], json!("40"));
}

#[test]
fn range_multiple_series_differing_labels_padded() {
    let samples = vec![
        make_range_row(&[("job", "a")], &[(1_719_014_400, "1")], None),
        make_range_row(
            &[("job", "b"), ("pod", "p1")],
            &[(1_719_014_400, "2")],
            None,
        ),
    ];
    let rows = range_samples_to_toon_rows(&samples, false);

    assert_eq!(rows.len(), 2);
    // Missing label padded with empty string.
    assert_eq!(rows[0].as_object().unwrap()["pod"], json!(""));
    assert_eq!(rows[1].as_object().unwrap()["pod"], json!("p1"));
}

#[test]
fn range_sparse_timestamps_padded() {
    // Series 0 has T1+T2; Series 1 has only T1 → T2 padded with "" for series 1.
    let samples = vec![
        make_range_row(
            &[("job", "a")],
            &[(1_719_014_400, "1"), (1_719_014_460, "2")],
            None,
        ),
        make_range_row(&[("job", "b")], &[(1_719_014_400, "3")], None),
    ];
    let rows = range_samples_to_toon_rows(&samples, false);

    assert_eq!(rows.len(), 2);
    // Series b is missing the second timestamp.
    assert_eq!(
        rows[1].as_object().unwrap()["2024-06-22T00:01:00Z"],
        json!("")
    );
}

#[test]
fn range_label_keys_sorted_alphabetically() {
    let sample = make_range_row(
        &[("zzz", "z"), ("aaa", "a"), ("mmm", "m")],
        &[(1_719_014_400, "0")],
        None,
    );
    let rows = range_samples_to_toon_rows(&[sample], false);
    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    // Sorted label keys first, then the timestamp column.
    assert_eq!(keys, vec!["aaa", "mmm", "zzz", "2024-06-22T00:00:00Z"]);
}

#[test]
fn range_timestamp_is_iso8601() {
    // 1719014400 == 2024-06-22T00:00:00Z — verifies epoch→ISO conversion.
    let sample = make_range_row(&[("job", "x")], &[(1_719_014_400, "1")], None);
    let rows = range_samples_to_toon_rows(&[sample], false);
    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    assert!(keys.contains(&"2024-06-22T00:00:00Z"));
}

#[test]
fn range_with_profile_column() {
    let sample = make_range_row(&[("job", "api")], &[(1_719_014_400, "1")], Some("prod"));
    let rows = range_samples_to_toon_rows(&[sample], true);
    let obj = rows[0].as_object().unwrap();
    assert_eq!(obj["profile"], json!("prod"));
    // profile appears between label keys and timestamp columns.
    let keys: Vec<&str> = obj.keys().map(String::as_str).collect();
    assert_eq!(keys, vec!["job", "profile", "2024-06-22T00:00:00Z"]);
}

#[test]
fn range_without_profile_column() {
    let sample = make_range_row(&[("job", "api")], &[(1_719_014_400, "1")], None);
    let rows = range_samples_to_toon_rows(&[sample], false);
    let obj = rows[0].as_object().unwrap();
    assert!(!obj.contains_key("profile"));
}

#[test]
fn range_column_order() {
    // Full column order: sorted labels → profile → timestamps.
    let sample = make_range_row(
        &[("job", "api"), ("instance", "h1")],
        &[(1_719_014_400, "1"), (1_719_014_460, "2")],
        Some("prod"),
    );
    let rows = range_samples_to_toon_rows(&[sample], true);
    let keys: Vec<&str> = rows[0]
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    assert_eq!(
        keys,
        vec![
            "instance",
            "job",
            "profile",
            "2024-06-22T00:00:00Z",
            "2024-06-22T00:01:00Z"
        ]
    );
}