Skip to main content

socorro_cli/models/
crash_pings.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use serde::{Deserialize, Serialize};
6
7// --- API response types (struct-of-arrays with string deduplication) ---
8
9#[derive(Debug, Deserialize, Serialize)]
10pub struct IndexedStrings {
11    pub strings: Vec<String>,
12    pub values: Vec<u32>,
13}
14
15#[derive(Debug, Deserialize, Serialize)]
16pub struct NullableIndexedStrings {
17    pub strings: Vec<Option<String>>,
18    pub values: Vec<u32>,
19}
20
21impl IndexedStrings {
22    pub fn get(&self, i: usize) -> &str {
23        &self.strings[self.values[i] as usize]
24    }
25}
26
27impl NullableIndexedStrings {
28    pub fn get(&self, i: usize) -> Option<&str> {
29        self.strings[self.values[i] as usize].as_deref()
30    }
31}
32
33#[derive(Debug, Serialize, Deserialize)]
34pub struct CrashPingsResponse {
35    pub channel: IndexedStrings,
36    pub process: IndexedStrings,
37    pub ipc_actor: NullableIndexedStrings,
38    pub clientid: IndexedStrings,
39    pub crashid: Vec<String>,
40    pub version: IndexedStrings,
41    pub os: IndexedStrings,
42    pub osversion: IndexedStrings,
43    pub arch: IndexedStrings,
44    pub date: IndexedStrings,
45    pub reason: NullableIndexedStrings,
46    #[serde(rename = "type")]
47    pub crash_type: NullableIndexedStrings,
48    pub minidump_sha256_hash: Vec<Option<String>>,
49    pub startup_crash: Vec<Option<bool>>,
50    pub build_id: IndexedStrings,
51    pub signature: IndexedStrings,
52}
53
54impl CrashPingsResponse {
55    pub fn len(&self) -> usize {
56        self.crashid.len()
57    }
58
59    pub fn is_empty(&self) -> bool {
60        self.crashid.is_empty()
61    }
62
63    pub fn signature(&self, i: usize) -> &str {
64        self.signature.get(i)
65    }
66
67    pub fn channel(&self, i: usize) -> &str {
68        self.channel.get(i)
69    }
70
71    pub fn os(&self, i: usize) -> &str {
72        self.os.get(i)
73    }
74
75    pub fn process(&self, i: usize) -> &str {
76        self.process.get(i)
77    }
78
79    pub fn version(&self, i: usize) -> &str {
80        self.version.get(i)
81    }
82
83    pub fn arch(&self, i: usize) -> &str {
84        self.arch.get(i)
85    }
86
87    pub fn matches_filters(&self, i: usize, filters: &CrashPingFilters) -> bool {
88        if let Some(ref ch) = filters.channel {
89            if !self.channel(i).eq_ignore_ascii_case(ch) {
90                return false;
91            }
92        }
93        if let Some(ref os) = filters.os {
94            if !self.os(i).eq_ignore_ascii_case(os) {
95                return false;
96            }
97        }
98        if let Some(ref proc) = filters.process {
99            if !self.process(i).eq_ignore_ascii_case(proc) {
100                return false;
101            }
102        }
103        if let Some(ref ver) = filters.version {
104            if self.version(i) != ver {
105                return false;
106            }
107        }
108        if let Some(ref sig) = filters.signature {
109            let ping_sig = self.signature(i);
110            if let Some(pattern) = sig.strip_prefix('~') {
111                if !ping_sig.to_lowercase().contains(&pattern.to_lowercase()) {
112                    return false;
113                }
114            } else if ping_sig != sig {
115                return false;
116            }
117        }
118        if let Some(ref arch) = filters.arch {
119            if !self.arch(i).eq_ignore_ascii_case(arch) {
120                return false;
121            }
122        }
123        true
124    }
125
126    pub fn facet_value(&self, i: usize, facet: &str) -> String {
127        match facet {
128            "signature" => self.signature(i).to_string(),
129            "channel" => self.channel(i).to_string(),
130            "os" => self.os(i).to_string(),
131            "process" => self.process(i).to_string(),
132            "version" => self.version(i).to_string(),
133            "arch" => self.arch(i).to_string(),
134            "osversion" => self.osversion.get(i).to_string(),
135            "build_id" => self.build_id.get(i).to_string(),
136            "ipc_actor" => self.ipc_actor.get(i).unwrap_or("(none)").to_string(),
137            "reason" => self.reason.get(i).unwrap_or("(none)").to_string(),
138            "type" => self.crash_type.get(i).unwrap_or("(none)").to_string(),
139            _ => "(unknown facet)".to_string(),
140        }
141    }
142}
143
144// --- Stack trace types ---
145
146#[derive(Debug, Serialize, Deserialize)]
147pub struct CrashPingStackResponse {
148    pub stack: Option<Vec<CrashPingFrame>>,
149    pub java_exception: Option<serde_json::Value>,
150}
151
152#[derive(Debug, Serialize, Deserialize)]
153pub struct CrashPingFrame {
154    pub function: Option<String>,
155    pub function_offset: Option<String>,
156    pub file: Option<String>,
157    pub line: Option<u32>,
158    pub module: Option<String>,
159    pub module_offset: Option<String>,
160    pub offset: Option<String>,
161    #[serde(default)]
162    pub omitted: Option<serde_json::Value>,
163    #[serde(default)]
164    pub error: Option<String>,
165}
166
167// --- Filter parameters ---
168
169#[derive(Debug, Default)]
170pub struct CrashPingFilters {
171    pub channel: Option<String>,
172    pub os: Option<String>,
173    pub process: Option<String>,
174    pub version: Option<String>,
175    pub signature: Option<String>,
176    pub arch: Option<String>,
177}
178
179// --- Summary types for display ---
180
181#[derive(Debug, Serialize)]
182pub struct CrashPingsSummary {
183    pub date: String,
184    pub total: usize,
185    pub filtered_total: usize,
186    pub signature_filter: Option<String>,
187    pub facet_name: String,
188    pub items: Vec<CrashPingsItem>,
189}
190
191#[derive(Debug, Serialize)]
192pub struct CrashPingsItem {
193    pub label: String,
194    pub count: usize,
195    pub percentage: f64,
196}
197
198#[derive(Debug, Serialize)]
199pub struct CrashPingStackSummary {
200    pub crash_id: String,
201    pub date: String,
202    pub frames: Vec<CrashPingFrame>,
203    pub java_exception: Option<serde_json::Value>,
204}
205
206#[cfg(test)]
207mod tests {
208    use super::*;
209    use serde_json::json;
210
211    fn sample_response_json() -> serde_json::Value {
212        json!({
213            "channel": {
214                "strings": ["release", "beta", "nightly"],
215                "values": [0, 0, 1, 2]
216            },
217            "process": {
218                "strings": ["main", "content", "gpu"],
219                "values": [0, 1, 0, 2]
220            },
221            "ipc_actor": {
222                "strings": [null, "windows-file-dialog"],
223                "values": [0, 1, 0, 0]
224            },
225            "clientid": {
226                "strings": ["client1", "client2", "client3", "client4"],
227                "values": [0, 1, 2, 3]
228            },
229            "crashid": ["crash-1", "crash-2", "crash-3", "crash-4"],
230            "version": {
231                "strings": ["147.0", "148.0"],
232                "values": [0, 0, 1, 1]
233            },
234            "os": {
235                "strings": ["Windows", "Linux", "Mac"],
236                "values": [0, 0, 1, 2]
237            },
238            "osversion": {
239                "strings": ["10.0.19045", "6.1", "15.0"],
240                "values": [0, 0, 1, 2]
241            },
242            "arch": {
243                "strings": ["x86_64", "aarch64"],
244                "values": [0, 0, 0, 1]
245            },
246            "date": {
247                "strings": ["2026-02-12"],
248                "values": [0, 0, 0, 0]
249            },
250            "reason": {
251                "strings": [null, "OOM"],
252                "values": [0, 1, 0, 1]
253            },
254            "type": {
255                "strings": [null, "SIGSEGV"],
256                "values": [0, 1, 0, 0]
257            },
258            "minidump_sha256_hash": ["hash1", null, "hash3", null],
259            "startup_crash": [false, true, false, false],
260            "build_id": {
261                "strings": ["20260210103000", "20260211103000"],
262                "values": [0, 0, 1, 1]
263            },
264            "signature": {
265                "strings": ["OOM | small", "setup_stack_prot", "js::gc::SomeFunc"],
266                "values": [0, 0, 1, 2]
267            }
268        })
269    }
270
271    #[test]
272    fn test_deserialize_response() {
273        let data = sample_response_json();
274        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
275        assert_eq!(resp.len(), 4);
276        assert_eq!(resp.crashid.len(), 4);
277    }
278
279    #[test]
280    fn test_indexed_strings_get() {
281        let data = sample_response_json();
282        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
283        assert_eq!(resp.signature(0), "OOM | small");
284        assert_eq!(resp.signature(1), "OOM | small");
285        assert_eq!(resp.signature(2), "setup_stack_prot");
286        assert_eq!(resp.signature(3), "js::gc::SomeFunc");
287    }
288
289    #[test]
290    fn test_nullable_indexed_strings() {
291        let data = sample_response_json();
292        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
293        assert_eq!(resp.ipc_actor.get(0), None);
294        assert_eq!(resp.ipc_actor.get(1), Some("windows-file-dialog"));
295    }
296
297    #[test]
298    fn test_channel_accessor() {
299        let data = sample_response_json();
300        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
301        assert_eq!(resp.channel(0), "release");
302        assert_eq!(resp.channel(2), "beta");
303        assert_eq!(resp.channel(3), "nightly");
304    }
305
306    #[test]
307    fn test_os_accessor() {
308        let data = sample_response_json();
309        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
310        assert_eq!(resp.os(0), "Windows");
311        assert_eq!(resp.os(2), "Linux");
312        assert_eq!(resp.os(3), "Mac");
313    }
314
315    #[test]
316    fn test_filter_no_filters() {
317        let data = sample_response_json();
318        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
319        let filters = CrashPingFilters::default();
320        for i in 0..resp.len() {
321            assert!(resp.matches_filters(i, &filters));
322        }
323    }
324
325    #[test]
326    fn test_filter_by_channel() {
327        let data = sample_response_json();
328        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
329        let filters = CrashPingFilters {
330            channel: Some("release".to_string()),
331            ..Default::default()
332        };
333        assert!(resp.matches_filters(0, &filters));
334        assert!(resp.matches_filters(1, &filters));
335        assert!(!resp.matches_filters(2, &filters));
336        assert!(!resp.matches_filters(3, &filters));
337    }
338
339    #[test]
340    fn test_filter_by_os() {
341        let data = sample_response_json();
342        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
343        let filters = CrashPingFilters {
344            os: Some("Linux".to_string()),
345            ..Default::default()
346        };
347        assert!(!resp.matches_filters(0, &filters));
348        assert!(resp.matches_filters(2, &filters));
349    }
350
351    #[test]
352    fn test_filter_by_signature_exact() {
353        let data = sample_response_json();
354        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
355        let filters = CrashPingFilters {
356            signature: Some("OOM | small".to_string()),
357            ..Default::default()
358        };
359        assert!(resp.matches_filters(0, &filters));
360        assert!(resp.matches_filters(1, &filters));
361        assert!(!resp.matches_filters(2, &filters));
362    }
363
364    #[test]
365    fn test_filter_by_signature_contains() {
366        let data = sample_response_json();
367        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
368        let filters = CrashPingFilters {
369            signature: Some("~oom".to_string()),
370            ..Default::default()
371        };
372        assert!(resp.matches_filters(0, &filters));
373        assert!(!resp.matches_filters(2, &filters));
374    }
375
376    #[test]
377    fn test_filter_combined() {
378        let data = sample_response_json();
379        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
380        let filters = CrashPingFilters {
381            channel: Some("release".to_string()),
382            os: Some("Windows".to_string()),
383            ..Default::default()
384        };
385        assert!(resp.matches_filters(0, &filters));
386        assert!(resp.matches_filters(1, &filters));
387        assert!(!resp.matches_filters(2, &filters)); // beta
388        assert!(!resp.matches_filters(3, &filters)); // nightly + Mac
389    }
390
391    #[test]
392    fn test_filter_case_insensitive() {
393        let data = sample_response_json();
394        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
395        let filters = CrashPingFilters {
396            os: Some("windows".to_string()),
397            ..Default::default()
398        };
399        assert!(resp.matches_filters(0, &filters));
400    }
401
402    #[test]
403    fn test_facet_value() {
404        let data = sample_response_json();
405        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
406        assert_eq!(resp.facet_value(0, "signature"), "OOM | small");
407        assert_eq!(resp.facet_value(0, "os"), "Windows");
408        assert_eq!(resp.facet_value(0, "channel"), "release");
409        assert_eq!(resp.facet_value(1, "ipc_actor"), "windows-file-dialog");
410        assert_eq!(resp.facet_value(0, "ipc_actor"), "(none)");
411    }
412
413    #[test]
414    fn test_deserialize_stack_response() {
415        let data = json!({
416            "stack": [
417                {
418                    "function": "KiRaiseUserExceptionDispatcher",
419                    "function_offset": "0x000000000000003a",
420                    "file": null,
421                    "line": null,
422                    "module": "ntdll.dll",
423                    "module_offset": "0x00000000000a14fa",
424                    "omitted": null,
425                    "error": null,
426                    "offset": "0x00007ffbeeef14fa"
427                },
428                {
429                    "function": "mozilla::SomeFunc",
430                    "function_offset": null,
431                    "file": "SomeFile.cpp",
432                    "line": 42,
433                    "module": "xul.dll",
434                    "module_offset": "0x1234",
435                    "omitted": null,
436                    "error": null,
437                    "offset": "0x1234"
438                }
439            ],
440            "java_exception": null
441        });
442        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
443        let stack = resp.stack.unwrap();
444        assert_eq!(stack.len(), 2);
445        assert_eq!(
446            stack[0].function.as_deref(),
447            Some("KiRaiseUserExceptionDispatcher")
448        );
449        assert_eq!(stack[0].module.as_deref(), Some("ntdll.dll"));
450        assert!(stack[0].file.is_none());
451        assert_eq!(stack[1].file.as_deref(), Some("SomeFile.cpp"));
452        assert_eq!(stack[1].line, Some(42));
453    }
454
455    #[test]
456    fn test_deserialize_stack_response_null_stack() {
457        let data = json!({
458            "stack": null,
459            "java_exception": null
460        });
461        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
462        assert!(resp.stack.is_none());
463    }
464
465    #[test]
466    fn test_deserialize_stack_response_with_java_exception() {
467        let data = json!({
468            "stack": null,
469            "java_exception": {"message": "OutOfMemoryError", "frames": []}
470        });
471        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
472        assert!(resp.java_exception.is_some());
473    }
474
475    #[test]
476    fn test_crash_pings_summary() {
477        let summary = CrashPingsSummary {
478            date: "2026-02-12".to_string(),
479            total: 88808,
480            filtered_total: 4523,
481            signature_filter: Some("OOM | small".to_string()),
482            facet_name: "os".to_string(),
483            items: vec![
484                CrashPingsItem {
485                    label: "Windows".to_string(),
486                    count: 3900,
487                    percentage: 86.24,
488                },
489                CrashPingsItem {
490                    label: "Linux".to_string(),
491                    count: 400,
492                    percentage: 8.85,
493                },
494            ],
495        };
496        assert_eq!(summary.items.len(), 2);
497        assert_eq!(summary.items[0].label, "Windows");
498    }
499
500    #[test]
501    fn test_filter_by_version() {
502        let data = sample_response_json();
503        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
504        let filters = CrashPingFilters {
505            version: Some("148.0".to_string()),
506            ..Default::default()
507        };
508        assert!(!resp.matches_filters(0, &filters));
509        assert!(resp.matches_filters(2, &filters));
510        assert!(resp.matches_filters(3, &filters));
511    }
512
513    #[test]
514    fn test_filter_by_arch() {
515        let data = sample_response_json();
516        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
517        let filters = CrashPingFilters {
518            arch: Some("aarch64".to_string()),
519            ..Default::default()
520        };
521        assert!(!resp.matches_filters(0, &filters));
522        assert!(resp.matches_filters(3, &filters));
523    }
524}