Group entries into error classes and build the report.
(entries: &[HookErrorEntry], lines: usize, start: u64)
| 493 | |
| 494 | /// Group entries into error classes and build the report. |
| 495 | fn aggregate(entries: &[HookErrorEntry], lines: usize, start: u64) -> HookErrors { |
| 496 | // Group on a coarse signature: the error text with digits, hex and |
| 497 | // paths collapsed, so that "turn 78 is not running" and "turn 92 is not |
| 498 | // running" are one class rather than hundreds of singletons. |
| 499 | let mut order: Vec<String> = Vec::new(); |
| 500 | let mut buckets: BTreeMap<String, (usize, String)> = BTreeMap::new(); |
| 501 | |
| 502 | for entry in entries { |
| 503 | let signature = error_signature(&entry.message); |
| 504 | match buckets.get_mut(&signature) { |
| 505 | Some((count, _)) => *count += 1, |
| 506 | None => { |
| 507 | order.push(signature.clone()); |
| 508 | buckets.insert(signature, (1, entry.message.clone())); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | let mut classes: Vec<ErrorClass> = order |
| 514 | .into_iter() |
| 515 | .filter_map(|signature| { |
| 516 | buckets.get(&signature).map(|(count, example)| ErrorClass { |
| 517 | signature, |
| 518 | example: example.clone(), |
| 519 | count: *count, |
| 520 | }) |
| 521 | }) |
| 522 | .collect(); |
| 523 | classes.sort_by(|a, b| b.count.cmp(&a.count).then(a.signature.cmp(&b.signature))); |
| 524 | |
| 525 | let mut recent: Vec<HookErrorEntry> = entries.to_vec(); |
| 526 | recent.reverse(); |
| 527 | |
| 528 | let window = if start > 0 { |
| 529 | format!( |
| 530 | "last {lines} lines, {k} entries (file is {kb} KiB; older entries not read)", |
| 531 | k = entries.len(), |
| 532 | kb = (start + 64 * 1024) / 1024, |
| 533 | ) |
| 534 | } else { |
| 535 | format!("all {lines} lines, {k} entries", k = entries.len()) |
| 536 | }; |
| 537 | |
| 538 | HookErrors { |
| 539 | total: entries.len(), |
| 540 | lines, |
| 541 | first_seen: entries.first().map(|e| e.at.clone()), |
| 542 | last_seen: entries.last().map(|e| e.at.clone()), |
| 543 | window, |
| 544 | classes: classes.into_iter().take(10).collect(), |
| 545 | recent: recent.into_iter().take(10).collect(), |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /// Collapse a message to a grouping key. |
| 550 | /// |