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_from: String,
184    pub date_to: String,
185    pub total: usize,
186    pub filtered_total: usize,
187    pub signature_filter: Option<String>,
188    pub facet_name: String,
189    pub items: Vec<CrashPingsItem>,
190}
191
192#[derive(Debug, Serialize)]
193pub struct CrashPingsItem {
194    pub label: String,
195    pub count: usize,
196    pub percentage: f64,
197}
198
199#[derive(Debug, Serialize)]
200pub struct CrashPingStackSummary {
201    pub crash_id: String,
202    pub date: String,
203    pub frames: Vec<CrashPingFrame>,
204    pub java_exception: Option<serde_json::Value>,
205}
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210    use serde_json::json;
211
212    fn sample_response_json() -> serde_json::Value {
213        json!({
214            "channel": {
215                "strings": ["release", "beta", "nightly"],
216                "values": [0, 0, 1, 2]
217            },
218            "process": {
219                "strings": ["main", "content", "gpu"],
220                "values": [0, 1, 0, 2]
221            },
222            "ipc_actor": {
223                "strings": [null, "windows-file-dialog"],
224                "values": [0, 1, 0, 0]
225            },
226            "clientid": {
227                "strings": ["client1", "client2", "client3", "client4"],
228                "values": [0, 1, 2, 3]
229            },
230            "crashid": ["crash-1", "crash-2", "crash-3", "crash-4"],
231            "version": {
232                "strings": ["147.0", "148.0"],
233                "values": [0, 0, 1, 1]
234            },
235            "os": {
236                "strings": ["Windows", "Linux", "Mac"],
237                "values": [0, 0, 1, 2]
238            },
239            "osversion": {
240                "strings": ["10.0.19045", "6.1", "15.0"],
241                "values": [0, 0, 1, 2]
242            },
243            "arch": {
244                "strings": ["x86_64", "aarch64"],
245                "values": [0, 0, 0, 1]
246            },
247            "date": {
248                "strings": ["2026-02-12"],
249                "values": [0, 0, 0, 0]
250            },
251            "reason": {
252                "strings": [null, "OOM"],
253                "values": [0, 1, 0, 1]
254            },
255            "type": {
256                "strings": [null, "SIGSEGV"],
257                "values": [0, 1, 0, 0]
258            },
259            "minidump_sha256_hash": ["hash1", null, "hash3", null],
260            "startup_crash": [false, true, false, false],
261            "build_id": {
262                "strings": ["20260210103000", "20260211103000"],
263                "values": [0, 0, 1, 1]
264            },
265            "signature": {
266                "strings": ["OOM | small", "setup_stack_prot", "js::gc::SomeFunc"],
267                "values": [0, 0, 1, 2]
268            }
269        })
270    }
271
272    #[test]
273    fn test_deserialize_response() {
274        let data = sample_response_json();
275        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
276        assert_eq!(resp.len(), 4);
277        assert_eq!(resp.crashid.len(), 4);
278    }
279
280    #[test]
281    fn test_indexed_strings_get() {
282        let data = sample_response_json();
283        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
284        assert_eq!(resp.signature(0), "OOM | small");
285        assert_eq!(resp.signature(1), "OOM | small");
286        assert_eq!(resp.signature(2), "setup_stack_prot");
287        assert_eq!(resp.signature(3), "js::gc::SomeFunc");
288    }
289
290    #[test]
291    fn test_nullable_indexed_strings() {
292        let data = sample_response_json();
293        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
294        assert_eq!(resp.ipc_actor.get(0), None);
295        assert_eq!(resp.ipc_actor.get(1), Some("windows-file-dialog"));
296    }
297
298    #[test]
299    fn test_channel_accessor() {
300        let data = sample_response_json();
301        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
302        assert_eq!(resp.channel(0), "release");
303        assert_eq!(resp.channel(2), "beta");
304        assert_eq!(resp.channel(3), "nightly");
305    }
306
307    #[test]
308    fn test_os_accessor() {
309        let data = sample_response_json();
310        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
311        assert_eq!(resp.os(0), "Windows");
312        assert_eq!(resp.os(2), "Linux");
313        assert_eq!(resp.os(3), "Mac");
314    }
315
316    #[test]
317    fn test_filter_no_filters() {
318        let data = sample_response_json();
319        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
320        let filters = CrashPingFilters::default();
321        for i in 0..resp.len() {
322            assert!(resp.matches_filters(i, &filters));
323        }
324    }
325
326    #[test]
327    fn test_filter_by_channel() {
328        let data = sample_response_json();
329        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
330        let filters = CrashPingFilters {
331            channel: Some("release".to_string()),
332            ..Default::default()
333        };
334        assert!(resp.matches_filters(0, &filters));
335        assert!(resp.matches_filters(1, &filters));
336        assert!(!resp.matches_filters(2, &filters));
337        assert!(!resp.matches_filters(3, &filters));
338    }
339
340    #[test]
341    fn test_filter_by_os() {
342        let data = sample_response_json();
343        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
344        let filters = CrashPingFilters {
345            os: Some("Linux".to_string()),
346            ..Default::default()
347        };
348        assert!(!resp.matches_filters(0, &filters));
349        assert!(resp.matches_filters(2, &filters));
350    }
351
352    #[test]
353    fn test_filter_by_signature_exact() {
354        let data = sample_response_json();
355        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
356        let filters = CrashPingFilters {
357            signature: Some("OOM | small".to_string()),
358            ..Default::default()
359        };
360        assert!(resp.matches_filters(0, &filters));
361        assert!(resp.matches_filters(1, &filters));
362        assert!(!resp.matches_filters(2, &filters));
363    }
364
365    #[test]
366    fn test_filter_by_signature_contains() {
367        let data = sample_response_json();
368        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
369        let filters = CrashPingFilters {
370            signature: Some("~oom".to_string()),
371            ..Default::default()
372        };
373        assert!(resp.matches_filters(0, &filters));
374        assert!(!resp.matches_filters(2, &filters));
375    }
376
377    #[test]
378    fn test_filter_combined() {
379        let data = sample_response_json();
380        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
381        let filters = CrashPingFilters {
382            channel: Some("release".to_string()),
383            os: Some("Windows".to_string()),
384            ..Default::default()
385        };
386        assert!(resp.matches_filters(0, &filters));
387        assert!(resp.matches_filters(1, &filters));
388        assert!(!resp.matches_filters(2, &filters)); // beta
389        assert!(!resp.matches_filters(3, &filters)); // nightly + Mac
390    }
391
392    #[test]
393    fn test_filter_case_insensitive() {
394        let data = sample_response_json();
395        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
396        let filters = CrashPingFilters {
397            os: Some("windows".to_string()),
398            ..Default::default()
399        };
400        assert!(resp.matches_filters(0, &filters));
401    }
402
403    #[test]
404    fn test_facet_value() {
405        let data = sample_response_json();
406        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
407        assert_eq!(resp.facet_value(0, "signature"), "OOM | small");
408        assert_eq!(resp.facet_value(0, "os"), "Windows");
409        assert_eq!(resp.facet_value(0, "channel"), "release");
410        assert_eq!(resp.facet_value(1, "ipc_actor"), "windows-file-dialog");
411        assert_eq!(resp.facet_value(0, "ipc_actor"), "(none)");
412    }
413
414    #[test]
415    fn test_deserialize_stack_response() {
416        let data = json!({
417            "stack": [
418                {
419                    "function": "KiRaiseUserExceptionDispatcher",
420                    "function_offset": "0x000000000000003a",
421                    "file": null,
422                    "line": null,
423                    "module": "ntdll.dll",
424                    "module_offset": "0x00000000000a14fa",
425                    "omitted": null,
426                    "error": null,
427                    "offset": "0x00007ffbeeef14fa"
428                },
429                {
430                    "function": "mozilla::SomeFunc",
431                    "function_offset": null,
432                    "file": "SomeFile.cpp",
433                    "line": 42,
434                    "module": "xul.dll",
435                    "module_offset": "0x1234",
436                    "omitted": null,
437                    "error": null,
438                    "offset": "0x1234"
439                }
440            ],
441            "java_exception": null
442        });
443        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
444        let stack = resp.stack.unwrap();
445        assert_eq!(stack.len(), 2);
446        assert_eq!(
447            stack[0].function.as_deref(),
448            Some("KiRaiseUserExceptionDispatcher")
449        );
450        assert_eq!(stack[0].module.as_deref(), Some("ntdll.dll"));
451        assert!(stack[0].file.is_none());
452        assert_eq!(stack[1].file.as_deref(), Some("SomeFile.cpp"));
453        assert_eq!(stack[1].line, Some(42));
454    }
455
456    #[test]
457    fn test_deserialize_stack_response_null_stack() {
458        let data = json!({
459            "stack": null,
460            "java_exception": null
461        });
462        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
463        assert!(resp.stack.is_none());
464    }
465
466    #[test]
467    fn test_deserialize_stack_response_with_java_exception() {
468        let data = json!({
469            "stack": null,
470            "java_exception": {"message": "OutOfMemoryError", "frames": []}
471        });
472        let resp: CrashPingStackResponse = serde_json::from_value(data).unwrap();
473        assert!(resp.java_exception.is_some());
474    }
475
476    #[test]
477    fn test_crash_pings_summary() {
478        let summary = CrashPingsSummary {
479            date_from: "2026-02-12".to_string(),
480            date_to: "2026-02-12".to_string(),
481            total: 88808,
482            filtered_total: 4523,
483            signature_filter: Some("OOM | small".to_string()),
484            facet_name: "os".to_string(),
485            items: vec![
486                CrashPingsItem {
487                    label: "Windows".to_string(),
488                    count: 3900,
489                    percentage: 86.24,
490                },
491                CrashPingsItem {
492                    label: "Linux".to_string(),
493                    count: 400,
494                    percentage: 8.85,
495                },
496            ],
497        };
498        assert_eq!(summary.items.len(), 2);
499        assert_eq!(summary.items[0].label, "Windows");
500    }
501
502    #[test]
503    fn test_filter_by_version() {
504        let data = sample_response_json();
505        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
506        let filters = CrashPingFilters {
507            version: Some("148.0".to_string()),
508            ..Default::default()
509        };
510        assert!(!resp.matches_filters(0, &filters));
511        assert!(resp.matches_filters(2, &filters));
512        assert!(resp.matches_filters(3, &filters));
513    }
514
515    #[test]
516    fn test_filter_by_arch() {
517        let data = sample_response_json();
518        let resp: CrashPingsResponse = serde_json::from_value(data).unwrap();
519        let filters = CrashPingFilters {
520            arch: Some("aarch64".to_string()),
521            ..Default::default()
522        };
523        assert!(!resp.matches_filters(0, &filters));
524        assert!(resp.matches_filters(3, &filters));
525    }
526}