Skip to content

Commit

Permalink
Sort proposals before serialization, to avoid randomization of propos…
Browse files Browse the repository at this point in the history
…al order
  • Loading branch information
dkijania committed Jan 17, 2022
1 parent 07849a3 commit 035271b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/bin/cli/ideascale/mod.rs
Expand Up @@ -142,7 +142,7 @@ impl Import {

let funds = build_fund(*fund as i32, fund_goal.clone(), *threshold);
let challenges = build_challenges(*fund as i32, &idescale_data, sponsors);
let proposals = build_proposals(
let mut proposals = build_proposals(
&idescale_data,
&challenges,
&scores,
Expand All @@ -155,6 +155,7 @@ impl Import {
// even if final id type is string, they are just sequentially added, so it should be safe
// to parse and unwrap here
challenges.sort_by_key(|c| c.id.parse::<i32>().unwrap());
proposals.sort_by_key(|p| p.proposal_id.clone());

dump_content_to_file(
funds,
Expand Down
15 changes: 9 additions & 6 deletions src/ideascale/mod.rs
Expand Up @@ -37,7 +37,7 @@ pub struct IdeaScaleData {
pub funnels: HashMap<u32, Funnel>,
pub fund: Fund,
pub challenges: HashMap<u32, Challenge>,
pub proposals: HashMap<u32, Proposal>,
pub proposals: Vec<Proposal>,
}

pub async fn fetch_all(
Expand All @@ -47,7 +47,7 @@ pub async fn fetch_all(
excluded_proposals: &HashSet<u32>,
api_token: String,
) -> Result<IdeaScaleData, Error> {
if !tokio::spawn(fetch::is_token_valid(api_token.clone())).await?? {
if !fetch::is_token_valid(api_token.clone()).await? {
return Err(Error::InvalidToken);
}

Expand All @@ -74,7 +74,7 @@ pub async fn fetch_all(
.collect();

let matches = regex::Regex::new(&stages_filters.join("|"))?;
let proposals = futures::future::try_join_all(proposals_tasks)
let mut proposals: Vec<Proposal> = futures::future::try_join_all(proposals_tasks)
.await?
.into_iter()
// forcefully unwrap to pop errors directly
Expand All @@ -83,7 +83,10 @@ pub async fn fetch_all(
.flatten()
// filter out non approved or staged proposals
.filter(|p| p.approved && filter_proposal_by_stage_type(&p.stage_type, &matches))
.filter(|p| !excluded_proposals.contains(&p.proposal_id));
.filter(|p| !excluded_proposals.contains(&p.proposal_id))
.collect();

proposals.sort_by_key(|p| p.proposal_id);

let mut stages: Vec<_> = fetch::get_stages(api_token.clone()).await?;
stages.retain(|stage| filter_stages(stage, stage_label, &funnels));
Expand All @@ -99,7 +102,7 @@ pub async fn fetch_all(
.enumerate()
.map(|(idx, c)| ((idx + 1) as u32, c))
.collect(),
proposals: proposals.map(|p| (p.proposal_id, p)).collect(),
proposals,
})
}

Expand Down Expand Up @@ -161,7 +164,7 @@ pub fn build_proposals(
) -> Vec<models::se::Proposal> {
ideascale_data
.proposals
.values()
.iter()
.enumerate()
.map(|(i, p)| {
let challenge = &built_challenges.get(&p.challenge_id).unwrap_or_else(|| {
Expand Down

0 comments on commit 035271b

Please sign in to comment.