Skip to content

Commit

Permalink
chore: Move Tenderer Maps logic out of r038.rs, to make it available …
Browse files Browse the repository at this point in the history
…to R025 in isolation, closes #62
  • Loading branch information
jpmckinney committed Jul 21, 2023
1 parent 6caaf8b commit 0fecdeb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
12 changes: 1 addition & 11 deletions src/indicators/r038.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ impl Calculate for R038 {
if let Some(Value::String(id)) = tenderer.get("id") {
let fraction = item.r038_tenderer.entry(id.clone()).or_default();
*fraction += fraction!(increment, 1);
if item.map {
item.maps
.ocid_tenderer
.entry(ocid.to_owned())
.or_default()
.insert(id.clone());
}
}
}
}
Expand Down Expand Up @@ -134,17 +127,14 @@ impl Calculate for R038 {
mediant!(item, other, r038_procuring_entity);
mediant!(item, other, r038_tenderer);

// If each OCID appears on only one line of the file, no overwriting will occur.
if item.map {
// If each OCID appears on only one line of the file, no overwriting will occur.
item.maps
.ocid_buyer_r038
.extend(std::mem::take(&mut other.maps.ocid_buyer_r038));
item.maps
.ocid_procuringentity_r038
.extend(std::mem::take(&mut other.maps.ocid_procuringentity_r038));
item.maps
.ocid_tenderer
.extend(std::mem::take(&mut other.maps.ocid_tenderer));
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/indicators/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,43 @@ use serde_json::{Map, Value};

use crate::indicators::{Calculate, Indicators, Settings};

#[derive(Default)]
pub struct Tenderers {}

#[derive(Default)]
pub struct SecondLowestBidRatio {
currency: Option<String>,
}

impl Calculate for Tenderers {
fn new(_settings: &mut Settings) -> Self {
Self::default()
}

fn fold(&self, item: &mut Indicators, release: &Map<String, Value>, ocid: &str) {
for bid in Indicators::get_submitted_bids(release) {
if let Some(Value::Array(tenderers)) = bid.get("tenderers") {
for tenderer in tenderers {
if let Some(Value::String(id)) = tenderer.get("id") {
item.maps
.ocid_tenderer
.entry(ocid.to_owned())
.or_default()
.insert(id.clone());
}
}
}
}
}

fn reduce(&self, item: &mut Indicators, other: &mut Indicators) {
// If each OCID appears on only one line of the file, no overwriting will occur.
item.maps
.ocid_tenderer
.extend(std::mem::take(&mut other.maps.ocid_tenderer));
}
}

impl Calculate for SecondLowestBidRatio {
fn new(settings: &mut Settings) -> Self {
Self {
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::indicators::r035::R035;
use crate::indicators::r036::R036;
use crate::indicators::r038::R038;
use crate::indicators::r058::R058;
use crate::indicators::util::SecondLowestBidRatio;
use crate::indicators::util::{SecondLowestBidRatio, Tenderers};
pub use crate::indicators::{Calculate, Codelist, Group, Indicator, Indicators, Settings};
use crate::queue::Job;
use crate::standard::{AWARD_STATUS, BID_STATUS};
Expand Down Expand Up @@ -161,6 +161,10 @@ impl Indicators {
pub fn run(buffer: impl BufRead + Send, mut settings: Settings, map: &bool) -> Result<Self> {
let mut indicators: Vec<Box<dyn Calculate + Sync>> = vec![];

if *map && (settings.R025.is_some() || settings.R038.is_some()) {
indicators.push(Box::new(Tenderers::new(&mut settings)));
}

// Must run before indicator initialization, which mutates settings.
if settings.R024.is_some() || settings.R058.is_some() {
indicators.push(Box::new(SecondLowestBidRatio::new(&mut settings)));
Expand Down

0 comments on commit 0fecdeb

Please sign in to comment.