Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimum and maximum implementation for the Nexit metric #712

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions rust-code-analysis-web/src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
Expand All @@ -661,7 +661,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
Expand Down Expand Up @@ -714,7 +714,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
Expand Down Expand Up @@ -763,7 +763,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
Expand All @@ -790,7 +790,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/cyclomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Serialize for Stats {
where
S: Serializer,
{
let mut st = serializer.serialize_struct("cyclomatic", 2)?;
let mut st = serializer.serialize_struct("cyclomatic", 4)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

st.serialize_field("sum", &self.cyclomatic_sum())?;
st.serialize_field("average", &self.cyclomatic_average())?;
st.serialize_field("min", &self.cyclomatic_min())?;
Expand Down
85 changes: 73 additions & 12 deletions src/metrics/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ use crate::*;
#[derive(Debug, Clone)]
pub struct Stats {
exit: usize,
exit_sum: usize,
total_space_functions: usize,
exit_min: usize,
exit_max: usize,
}

impl Default for Stats {
fn default() -> Self {
Self {
exit: 0,
exit_sum: 0,
total_space_functions: 1,
exit_min: usize::MAX,
exit_max: 0,
}
}
}
Expand All @@ -29,29 +35,52 @@ impl Serialize for Stats {
where
S: Serializer,
{
let mut st = serializer.serialize_struct("nexits", 2)?;
let mut st = serializer.serialize_struct("nexits", 4)?;
st.serialize_field("sum", &self.exit())?;
st.serialize_field("average", &self.exit_average())?;
st.serialize_field("min", &self.exit())?;
st.serialize_field("max", &self.exit_average())?;
st.end()
}
}

impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "sum: {}, average: {}", self.exit(), self.exit_average())
write!(
f,
"sum: {}, average: {} min: {}, max: {}",
self.exit_sum(),
self.exit_average(),
self.exit_min(),
self.exit_max()
)
}
}

impl Stats {
/// Merges a second `NExit` metric into the first one
pub fn merge(&mut self, other: &Stats) {
self.exit += other.exit;
self.exit_max = self.exit_max.max(other.exit_max);
self.exit_min = self.exit_min.min(other.exit_min);
self.exit_sum += other.exit_sum;
}

/// Returns the `NExit` metric value
pub fn exit(&self) -> f64 {
self.exit as f64
}
/// Returns the `NExit` metric sum value
pub fn exit_sum(&self) -> f64 {
self.exit_sum as f64
}
/// Returns the `NExit` metric minimum value
pub fn exit_min(&self) -> f64 {
self.exit_min as f64
}
/// Returns the `NExit` metric maximum value
pub fn exit_max(&self) -> f64 {
self.exit_max as f64
}

/// Returns the `NExit` metric average value
///
Expand All @@ -60,9 +89,13 @@ impl Stats {
///
/// If there are no functions in a code, its value is `NAN`.
pub fn exit_average(&self) -> f64 {
self.exit() / self.total_space_functions as f64
self.exit_sum() / self.total_space_functions as f64
}
pub fn compute_minmax(&mut self) {
self.exit_max = self.exit_max.max(self.exit);
self.exit_min = self.exit_min.min(self.exit);
self.exit_sum += self.exit;
}

pub(crate) fn finalize(&mut self, total_space_functions: usize) {
self.total_space_functions = total_space_functions;
}
Expand Down Expand Up @@ -151,7 +184,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
Expand All @@ -163,7 +200,11 @@ mod tests {
"foo.rs",
RustParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
Expand All @@ -175,7 +216,11 @@ mod tests {
"foo.c",
CppParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
Expand All @@ -187,7 +232,11 @@ mod tests {
"foo.js",
JavascriptParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
Expand All @@ -201,7 +250,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 1, usize)],
[
(exit_sum, 1, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 1.0)] // 1 function
);
}
Expand All @@ -218,7 +271,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 2, usize)],
[
(exit_sum, 2, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 1.0)] // 2 functions
);
}
Expand All @@ -235,7 +292,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 2, usize)],
[
(exit_sum, 2, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 0.5)] // 2 functions + 2 lambdas = 4
);
}
Expand Down
1 change: 1 addition & 0 deletions src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ fn compute_averages(state: &mut State) {
#[inline(always)]
fn compute_minmax(state: &mut State) {
state.space.metrics.cyclomatic.compute_minmax();
state.space.metrics.nexits.compute_minmax();
}

fn finalize<T: ParserTrait>(state_stack: &mut Vec<State>, diff_level: usize) {
Expand Down