Skip to content

Commit

Permalink
move calcuating coverage points into constructor
Browse files Browse the repository at this point in the history
I thought `coverage_point_calculator::calculate_coverage_points()`
didn't read very well.

And since we now have a single struct to care about (outside of
providing arguments), it seemed to me
`coverage_point_calculator::CoveragePoints::new()` read rather nicely.
  • Loading branch information
michaeldjeffrey committed Jun 7, 2024
1 parent 611a929 commit bbe19bd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 49 deletions.
97 changes: 51 additions & 46 deletions coverage_point_calculator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,40 +110,45 @@ pub struct CoveragePoints {
pub covered_hexes: Vec<CoveredHex>,
}

pub fn calculate_coverage_points(
radio_type: RadioType,
radio_threshold: RadioThreshold,
speedtests: Vec<Speedtest>,
trust_scores: Vec<LocationTrust>,
ranked_coverage: Vec<coverage_map::RankedCoverage>,
) -> Result<CoveragePoints> {
let location_trust_scores = location::clean_trust_scores(trust_scores, &ranked_coverage);
let location_trust_multiplier = location::multiplier(radio_type, &location_trust_scores);

let boost_eligibility =
BoostedHexStatus::new(&radio_type, location_trust_multiplier, &radio_threshold);

let covered_hexes = hexes::clean_covered_hexes(radio_type, ranked_coverage, boost_eligibility)?;
let hex_coverage_points = hexes::calculated_coverage_points(&covered_hexes);

let speedtests = speedtest::clean_speedtests(speedtests);
let speedtest_multiplier = speedtest::multiplier(&speedtests);

let coverage_points = hex_coverage_points * location_trust_multiplier * speedtest_multiplier;
let total_coverage_points = coverage_points.round_dp_with_strategy(2, RoundingStrategy::ToZero);

Ok(CoveragePoints {
total_coverage_points,
hex_coverage_points,
location_trust_multiplier,
speedtest_multiplier,
radio_type,
radio_threshold,
boosted_hex_eligibility: boost_eligibility,
speedtests,
location_trust_scores,
covered_hexes,
})
impl CoveragePoints {
pub fn new(
radio_type: RadioType,
radio_threshold: RadioThreshold,
speedtests: Vec<Speedtest>,
trust_scores: Vec<LocationTrust>,
ranked_coverage: Vec<coverage_map::RankedCoverage>,
) -> Result<CoveragePoints> {
let location_trust_scores = location::clean_trust_scores(trust_scores, &ranked_coverage);
let location_trust_multiplier = location::multiplier(radio_type, &location_trust_scores);

let boost_eligibility =
BoostedHexStatus::new(&radio_type, location_trust_multiplier, &radio_threshold);

let covered_hexes =
hexes::clean_covered_hexes(radio_type, ranked_coverage, boost_eligibility)?;
let hex_coverage_points = hexes::calculated_coverage_points(&covered_hexes);

let speedtests = speedtest::clean_speedtests(speedtests);
let speedtest_multiplier = speedtest::multiplier(&speedtests);

let coverage_points =
hex_coverage_points * location_trust_multiplier * speedtest_multiplier;
let total_coverage_points =
coverage_points.round_dp_with_strategy(2, RoundingStrategy::ToZero);

Ok(CoveragePoints {
total_coverage_points,
hex_coverage_points,
location_trust_multiplier,
speedtest_multiplier,
radio_type,
radio_threshold,
boosted_hex_eligibility: boost_eligibility,
speedtests,
location_trust_scores,
covered_hexes,
})
}
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -270,7 +275,7 @@ mod tests {
#[test]
fn hip_84_radio_meets_minimum_subscriber_threshold_for_boosted_hexes() {
let calculate_wifi = |radio_verified: RadioThreshold| {
calculate_coverage_points(
CoveragePoints::new(
RadioType::IndoorWifi,
radio_verified,
speedtest_maximum(),
Expand Down Expand Up @@ -306,7 +311,7 @@ mod tests {
#[test]
fn hip_93_wifi_with_low_location_score_receives_no_boosted_hexes() {
let calculate_wifi = |location_trust_scores: Vec<LocationTrust>| {
calculate_coverage_points(
CoveragePoints::new(
RadioType::IndoorWifi,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -344,7 +349,7 @@ mod tests {
#[test]
fn speedtest() {
let calculate_indoor_cbrs = |speedtests: Vec<Speedtest>| {
calculate_coverage_points(
CoveragePoints::new(
RadioType::IndoorCbrs,
RadioThreshold::Verified,
speedtests,
Expand Down Expand Up @@ -432,7 +437,7 @@ mod tests {
}

use Assignment::*;
let indoor_cbrs = calculate_coverage_points(
let indoor_cbrs = CoveragePoints::new(
RadioType::IndoorCbrs,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -489,7 +494,7 @@ mod tests {
#[case] rank: usize,
#[case] expected_points: Decimal,
) {
let outdoor_wifi = calculate_coverage_points(
let outdoor_wifi = CoveragePoints::new(
radio_type,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -518,7 +523,7 @@ mod tests {
#[case] rank: usize,
#[case] expected_points: Decimal,
) {
let indoor_wifi = calculate_coverage_points(
let indoor_wifi = CoveragePoints::new(
radio_type,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -561,7 +566,7 @@ mod tests {
#[test]
fn location_trust_score_multiplier() {
// Location scores are averaged together
let indoor_wifi = calculate_coverage_points(
let indoor_wifi = CoveragePoints::new(
RadioType::IndoorWifi,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -605,7 +610,7 @@ mod tests {
boosted: NonZeroU32::new(4),
},
];
let indoor_wifi = calculate_coverage_points(
let indoor_wifi = CoveragePoints::new(
RadioType::IndoorWifi,
RadioThreshold::Verified,
speedtest_maximum(),
Expand All @@ -628,7 +633,7 @@ mod tests {
#[case] signal_level: SignalLevel,
#[case] expected: Decimal,
) {
let outdoor_cbrs = calculate_coverage_points(
let outdoor_cbrs = CoveragePoints::new(
RadioType::OutdoorCbrs,
RadioThreshold::Verified,
speedtest_maximum(),
Expand All @@ -655,7 +660,7 @@ mod tests {
#[case] signal_level: SignalLevel,
#[case] expected: Decimal,
) {
let indoor_cbrs = calculate_coverage_points(
let indoor_cbrs = CoveragePoints::new(
RadioType::IndoorCbrs,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down Expand Up @@ -684,7 +689,7 @@ mod tests {
#[case] signal_level: SignalLevel,
#[case] expected: Decimal,
) {
let outdoor_wifi = calculate_coverage_points(
let outdoor_wifi = CoveragePoints::new(
RadioType::OutdoorWifi,
RadioThreshold::Verified,
speedtest_maximum(),
Expand All @@ -711,7 +716,7 @@ mod tests {
#[case] signal_level: SignalLevel,
#[case] expected: Decimal,
) {
let indoor_wifi = calculate_coverage_points(
let indoor_wifi = CoveragePoints::new(
RadioType::IndoorWifi,
RadioThreshold::Verified,
speedtest_maximum(),
Expand Down
6 changes: 3 additions & 3 deletions coverage_point_calculator/tests/coverage_point_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::num::NonZeroU32;
use chrono::Utc;
use coverage_map::{RankedCoverage, SignalLevel};
use coverage_point_calculator::{
calculate_coverage_points, BytesPs, LocationTrust, RadioThreshold, RadioType, Speedtest,
BytesPs, CoveragePoints, LocationTrust, RadioThreshold, RadioType, Speedtest,
};
use hex_assignments::{assignment::HexAssignments, Assignment};
use rust_decimal_macros::dec;
Expand Down Expand Up @@ -49,7 +49,7 @@ fn base_radio_coverage_points() {
(RadioType::OutdoorWifi, dec!(16)),
(RadioType::OutdoorCbrs, dec!(4)),
] {
let coverage_points = calculate_coverage_points(
let coverage_points = CoveragePoints::new(
radio_type,
RadioThreshold::Verified,
speedtests.clone(),
Expand Down Expand Up @@ -110,7 +110,7 @@ fn radios_with_coverage() {
(RadioType::OutdoorWifi, 25),
(RadioType::OutdoorCbrs, 100),
] {
let coverage_points = calculate_coverage_points(
let coverage_points = CoveragePoints::new(
radio_type,
RadioThreshold::Verified,
default_speedtests.clone(),
Expand Down

0 comments on commit bbe19bd

Please sign in to comment.