use rustc_hash::{FxHashMap, FxHashSet};
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use super::health_categories::{
HEALTH_FINDING_DIMENSIONS, HealthFindingCategory, health_finding_categories,
health_finding_counts, severity_counts_for_dimension, severity_index,
};
use super::identity_counts::{PlaceholderLoan, placeholder_loans};
use super::{
HealthBaselineCount, HealthBaselineData, HealthBaselineMode, HealthBaselineOverlap,
HealthFindingCountMap, health_bucket_key, health_finding_key, moved_identity_bucket_remaps,
};
const SEVERITIES: [fallow_output::FindingSeverity; 3] = [
fallow_output::FindingSeverity::Moderate,
fallow_output::FindingSeverity::High,
fallow_output::FindingSeverity::Critical,
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct SeveritySlotMatch {
pub(super) used: [usize; 3],
pub(super) overflow: [bool; 3],
}
pub(super) fn match_severity_slots(current: [usize; 3], saved: [usize; 3]) -> SeveritySlotMatch {
let mut available = saved;
let mut overflow = [false; 3];
for severity_idx in 0..3 {
let compatible = available[severity_idx..].iter().sum::<usize>();
overflow[severity_idx] = compatible < current[severity_idx];
let mut matched = current[severity_idx].min(compatible);
for slot in available.iter_mut().skip(severity_idx) {
let taken = matched.min(*slot);
*slot -= taken;
matched -= taken;
if matched == 0 {
break;
}
}
}
SeveritySlotMatch {
used: [
saved[0] - available[0],
saved[1] - available[1],
saved[2] - available[2],
],
overflow,
}
}
pub(super) struct HealthMatch {
overflow: FxHashMap<String, FxHashSet<&'static str>>,
used: HealthFindingCountMap,
moved_entries: usize,
}
impl HealthMatch {
fn overflows(&self, bucket: &str, category: &str) -> bool {
self.overflow
.get(bucket)
.is_some_and(|categories| categories.contains(category))
}
pub(super) fn used_slots(&self, bucket: &str, category: &str) -> usize {
self.used
.get(bucket)
.and_then(|counts| counts.get(category))
.map_or(0, |entry| entry.count)
}
fn overlap(&self) -> HealthBaselineOverlap {
HealthBaselineOverlap {
matched_entries: self.used.values().map(slot_total).sum(),
moved_entries: self.moved_entries,
}
}
}
fn slot_total(counts: &BTreeMap<String, HealthBaselineCount>) -> usize {
counts.values().map(|entry| entry.count).sum()
}
fn add_slots(counts: &mut HealthFindingCountMap, bucket: &str, category: &str, count: usize) {
counts
.entry(bucket.to_owned())
.or_default()
.entry(category.to_owned())
.or_insert(HealthBaselineCount { count: 0 })
.count += count;
}
pub(super) fn match_health_baseline(
saved: &HealthFindingCountMap,
current: &HealthFindingCountMap,
mode: HealthBaselineMode,
root: &Path,
) -> HealthMatch {
let identity = mode == HealthBaselineMode::Identity;
let remaps = if identity {
moved_identity_bucket_remaps(saved, current, root)
} else {
Vec::new()
};
let mut pool = slots_under_current_keys(saved, &remaps);
let loans = if identity {
placeholder_loans(&pool, current)
} else {
Vec::new()
};
for loan in &loans {
add_slots(&mut pool, &loan.to, loan.category, loan.count);
}
let (overflow, mut used) = match_buckets(&pool, current);
return_loans(&mut used, &loans);
let mut moved_entries = 0;
for (saved_key, current_key) in remaps {
if let Some(counts) = used.remove(¤t_key) {
moved_entries += slot_total(&counts);
used.insert(saved_key, counts);
}
}
HealthMatch {
overflow,
used,
moved_entries,
}
}
fn slots_under_current_keys(
saved: &HealthFindingCountMap,
remaps: &[(String, String)],
) -> HealthFindingCountMap {
let current_key: FxHashMap<&str, &str> = remaps
.iter()
.map(|(saved_key, current_key)| (saved_key.as_str(), current_key.as_str()))
.collect();
saved
.iter()
.map(|(key, counts)| {
let key = current_key
.get(key.as_str())
.copied()
.unwrap_or(key.as_str());
(key.to_owned(), counts.clone())
})
.collect()
}
fn match_buckets(
pool: &HealthFindingCountMap,
current: &HealthFindingCountMap,
) -> (
FxHashMap<String, FxHashSet<&'static str>>,
HealthFindingCountMap,
) {
let mut overflow: FxHashMap<String, FxHashSet<&'static str>> = FxHashMap::default();
let mut used = HealthFindingCountMap::new();
let buckets: BTreeSet<&String> = pool.keys().chain(current.keys()).collect();
for bucket in buckets {
for dimension in HEALTH_FINDING_DIMENSIONS {
let matched = match_severity_slots(
severity_counts_for_dimension(current.get(bucket), dimension),
severity_counts_for_dimension(pool.get(bucket), dimension),
);
for severity in SEVERITIES {
let index = severity_index(severity);
let category = HealthFindingCategory {
dimension,
severity,
}
.key();
if matched.overflow[index] {
overflow.entry(bucket.clone()).or_default().insert(category);
}
if matched.used[index] > 0 {
add_slots(&mut used, bucket, category, matched.used[index]);
}
}
}
}
(overflow, used)
}
fn return_loans(used: &mut HealthFindingCountMap, loans: &[PlaceholderLoan]) {
for loan in loans {
if let Some(entry) = used
.get_mut(&loan.to)
.and_then(|counts| counts.get_mut(loan.category))
{
entry.count = entry.count.saturating_sub(loan.count);
}
add_slots(used, &loan.from, loan.category, loan.count);
}
used.retain(|_, counts| {
counts.retain(|_, entry| entry.count > 0);
!counts.is_empty()
});
}
pub struct HealthBaselineComparison {
pub new_findings: Vec<fallow_output::ComplexityViolation>,
pub overlap: HealthBaselineOverlap,
}
impl HealthBaselineData {
pub(crate) fn compare(
&self,
mut findings: Vec<fallow_output::ComplexityViolation>,
root: &Path,
mode: HealthBaselineMode,
) -> HealthBaselineComparison {
let saved = self.counts_for(mode);
if saved.is_empty() {
let keys: FxHashSet<&str> = self.findings.iter().map(String::as_str).collect();
let before = findings.len();
findings.retain(|finding| !keys.contains(health_finding_key(finding, root).as_str()));
return HealthBaselineComparison {
overlap: HealthBaselineOverlap {
matched_entries: before - findings.len(),
moved_entries: 0,
},
new_findings: findings,
};
}
let counting = self.file_length_counting();
let current = health_finding_counts(&findings, root, mode, counting);
let matched = match_health_baseline(saved, ¤t, mode, root);
findings.retain(|finding| {
let bucket = health_bucket_key(finding, root, mode);
health_finding_categories(finding, counting)
.into_iter()
.flatten()
.any(|category| matched.overflows(&bucket, category.key()))
});
HealthBaselineComparison {
overlap: matched.overlap(),
new_findings: findings,
}
}
}
#[cfg(test)]
pub fn filter_new_health_findings(
findings: Vec<fallow_output::ComplexityViolation>,
baseline: &HealthBaselineData,
root: &Path,
mode: HealthBaselineMode,
) -> Vec<fallow_output::ComplexityViolation> {
baseline.compare(findings, root, mode).new_findings
}
#[cfg(test)]
impl HealthBaselineData {
pub(crate) fn overlap_entries(
&self,
findings: &[fallow_output::ComplexityViolation],
root: &Path,
mode: HealthBaselineMode,
) -> HealthBaselineOverlap {
self.compare(findings.to_vec(), root, mode).overlap
}
}
#[cfg(test)]
#[path = "health_match_tests.rs"]
mod tests;