Skip to main content

codeint_config/
regression_limits.rs

1use std::collections::BTreeSet;
2
3pub use codeint_model::NewFindingsRule;
4use codeint_model::{Metric, SourceSet};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
9#[serde(transparent)]
10pub struct RatioRise(f64);
11
12impl<'de> Deserialize<'de> for RatioRise {
13    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<RatioRise, D::Error> {
14        let value = f64::deserialize(deserializer)?;
15        Ok(RatioRise(if value == 0.0 { 0.0 } else { value }))
16    }
17}
18
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct RegressionLimit {
21    pub ratio_rise: RatioRise,
22    pub new_findings: NewFindingsRule,
23}
24
25#[derive(Debug, Clone, Default, PartialEq)]
26pub struct RegressionLimits {
27    per_file: bool,
28    every_source_set: Vec<(String, RegressionLimitSetting)>,
29    one_source_set: Vec<(SourceSet, String, RegressionLimitSetting)>,
30}
31
32#[derive(Debug, Clone, Copy, Default, PartialEq, Deserialize, Serialize, JsonSchema)]
33#[serde(deny_unknown_fields)]
34pub(crate) struct RegressionLimitSetting {
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    #[schemars(
37        with = "f64",
38        range(min = 0, max = 1),
39        description = "The highest rise of the ratio, from 0 to 1, as the report shows it, rounded to 4 decimal places. A rise equal to the limit passes."
40    )]
41    ratio_rise: Option<RatioRise>,
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    #[schemars(
44        with = "NewFindingsRule",
45        description = "When new Findings or a numerator rise fail: any_new_finding fails on any new Finding or numerator rise; numerator_rise fails whenever the numerator rises (rounded to 4 decimal places), so a Finding that only moves passes; numerator_rise_with_new_findings is a deprecated alias for numerator_rise; never lets new Findings and numerator rises pass, so that with ratio_rise 1 this Metric never makes a Regression here."
46    )]
47    new_findings: Option<NewFindingsRule>,
48}
49
50impl RatioRise {
51    pub const DEFAULT: RatioRise = RatioRise(0.01);
52
53    pub fn ratio(self) -> f64 {
54        if self.0 == 0.0 { 0.0 } else { self.0 }
55    }
56
57    pub fn admits(self, displayed_rise: f64) -> bool {
58        displayed_rise <= self.0
59    }
60}
61
62impl RegressionLimit {
63    pub const DEFAULT: RegressionLimit = RegressionLimit {
64        ratio_rise: RatioRise::DEFAULT,
65        new_findings: NewFindingsRule::NumeratorRise,
66    };
67
68    pub const DEFAULT_FOR_IMPORT_CYCLES: RegressionLimit = RegressionLimit {
69        ratio_rise: RatioRise::DEFAULT,
70        new_findings: NewFindingsRule::AnyNewFinding,
71    };
72
73    pub const fn default_for(metric: Metric) -> RegressionLimit {
74        RegressionLimit {
75            ratio_rise: RatioRise::DEFAULT,
76            new_findings: metric.default_new_findings_rule(),
77        }
78    }
79
80    fn default_for_key(metric_key: &str) -> RegressionLimit {
81        match Metric::from_key(metric_key) {
82            Some(metric) => RegressionLimit::default_for(metric),
83            None => RegressionLimit::DEFAULT,
84        }
85    }
86}
87
88impl RegressionLimitSetting {
89    pub(crate) fn is_unset(&self) -> bool {
90        self.ratio_rise.is_none() && self.new_findings.is_none()
91    }
92}
93
94impl RegressionLimits {
95    pub(crate) fn new(
96        per_file: bool,
97        every_source_set: Vec<(String, RegressionLimitSetting)>,
98        one_source_set: Vec<(SourceSet, String, RegressionLimitSetting)>,
99    ) -> RegressionLimits {
100        RegressionLimits {
101            per_file,
102            every_source_set,
103            one_source_set,
104        }
105    }
106
107    pub fn per_file(&self) -> bool {
108        self.per_file
109    }
110
111    pub fn with_per_file(mut self, per_file: bool) -> RegressionLimits {
112        self.per_file = per_file;
113        self
114    }
115
116    pub fn limit(&self, source_set: SourceSet, metric: Metric) -> RegressionLimit {
117        self.limit_for_key(source_set, metric.key())
118    }
119
120    pub fn limit_for_key(&self, source_set: SourceSet, metric_key: &str) -> RegressionLimit {
121        self.resolved_limit_for_key(source_set, metric_key).limit
122    }
123
124    pub(crate) fn resolved_limit_for_key(
125        &self,
126        source_set: SourceSet,
127        metric_key: &str,
128    ) -> ResolvedRegressionLimit {
129        let for_every_source_set = self
130            .every_source_set
131            .iter()
132            .find(|(set_for, _)| set_for == metric_key)
133            .map(|(_, setting)| *setting);
134        let for_this_source_set = self
135            .one_source_set
136            .iter()
137            .find(|(set_in, set_for, _)| *set_in == source_set && set_for == metric_key)
138            .map(|(_, _, setting)| *setting);
139        let base = RegressionLimit::default_for_key(metric_key);
140        let ratio_rise_override = for_this_source_set
141            .and_then(|setting| setting.ratio_rise)
142            .or_else(|| for_every_source_set.and_then(|setting| setting.ratio_rise));
143        let new_findings_override = for_this_source_set
144            .and_then(|setting| setting.new_findings)
145            .or_else(|| for_every_source_set.and_then(|setting| setting.new_findings));
146        ResolvedRegressionLimit {
147            limit: RegressionLimit {
148                ratio_rise: ratio_rise_override.unwrap_or(base.ratio_rise),
149                new_findings: new_findings_override.unwrap_or(base.new_findings),
150            },
151            ratio_rise_from_file: ratio_rise_override.is_some(),
152            new_findings_from_file: new_findings_override.is_some(),
153        }
154    }
155
156    pub(crate) fn extra_metric_keys(&self) -> Vec<String> {
157        let mut keys = BTreeSet::new();
158        for (_, key) in self.metric_keys() {
159            if Metric::from_key(key).is_none() {
160                keys.insert(key.to_owned());
161            }
162        }
163        keys.into_iter().collect()
164    }
165
166    pub fn limit_for_dependency(&self, metric_key: &str) -> RegressionLimit {
167        self.resolved_limit_for_dependency(metric_key).limit
168    }
169
170    pub(crate) fn resolved_limit_for_dependency(
171        &self,
172        metric_key: &str,
173    ) -> ResolvedRegressionLimit {
174        let for_metric = self
175            .every_source_set
176            .iter()
177            .find(|(set_for, _)| set_for == metric_key)
178            .map(|(_, setting)| *setting);
179        let base = RegressionLimit::default_for_key(metric_key);
180        let ratio_rise_override = for_metric.and_then(|setting| setting.ratio_rise);
181        let new_findings_override = for_metric.and_then(|setting| setting.new_findings);
182        ResolvedRegressionLimit {
183            limit: RegressionLimit {
184                ratio_rise: ratio_rise_override.unwrap_or(base.ratio_rise),
185                new_findings: new_findings_override.unwrap_or(base.new_findings),
186            },
187            ratio_rise_from_file: ratio_rise_override.is_some(),
188            new_findings_from_file: new_findings_override.is_some(),
189        }
190    }
191
192    pub(crate) fn metric_keys(&self) -> impl Iterator<Item = (Option<SourceSet>, &str)> {
193        self.every_source_set
194            .iter()
195            .map(|(metric, _)| (None, metric.as_str()))
196            .chain(
197                self.one_source_set
198                    .iter()
199                    .map(|(source_set, metric, _)| (Some(*source_set), metric.as_str())),
200            )
201    }
202}
203
204#[derive(Debug, Clone, Copy, PartialEq)]
205pub(crate) struct ResolvedRegressionLimit {
206    pub(crate) limit: RegressionLimit,
207    pub(crate) ratio_rise_from_file: bool,
208    pub(crate) new_findings_from_file: bool,
209}