Skip to content

Commit

Permalink
chore: minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed Jan 12, 2024
1 parent 859cfeb commit 0500224
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,53 @@ pub async fn fetch_and_update_boosts_winner(
"expiry":{
"$lt": Utc::now().timestamp_millis()
},
"winner": {
"$eq": null,
},
// "winner": {
// "$eq": null,
// },
}
}];
// start timer
let start = std::time::Instant::now();
match boost_collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
while let Some(doc) = cursor.try_next().await.unwrap() {
let num_of_winners = doc.get("num_of_winners").unwrap().as_i32().unwrap();
match doc.get("quests") {
Some(quests_res) => {
let quests = quests_res.as_array().unwrap();
let or_conditions: Vec<_> = quests
.iter()
.map(|quest_id| {
doc! { "quest_id": quest_id }
})
.collect();
let mut address_list: Vec<String> = Vec::new();
let get_users_per_quest_pipeline = vec![
doc! {
"$lookup": doc! {
"from": "tasks",
"localField": "task_id",
"foreignField": "id",
"let": doc! {
"taskId": "$task_id"
},
"pipeline": [
doc! {
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$eq": [
"$id",
"$$taskId"
]
},
doc! {
"$or": or_conditions
}
]
}
}
}
],
"as": "associated_tasks"
}
},
Expand All @@ -523,16 +551,6 @@ pub async fn fetch_and_update_boosts_winner(
"path": "$associated_tasks"
}
},
doc! {
"$match": doc! {
"$expr": doc! {
"$in": [
"$associated_tasks.quest_id",
quests
]
}
}
},
doc! {
"$group": doc! {
"_id": "$address",
Expand All @@ -555,18 +573,10 @@ pub async fn fetch_and_update_boosts_winner(
}
}
},
doc! {
"$project": doc! {
"_id": 0,
"address": "$_id.address",
"quest_id": "$_id.quest_id",
"tasks_array": 1
}
},
doc! {
"$lookup": doc! {
"from": "tasks",
"localField": "quest_id",
"localField": "_id.quest_id",
"foreignField": "quest_id",
"as": "associatedTasks"
}
Expand All @@ -585,14 +595,9 @@ pub async fn fetch_and_update_boosts_winner(
}
}
},
doc! {
"$project": doc! {
"address": "$address"
}
},
doc! {
"$group": doc! {
"_id": "$address",
"_id": "$_id.address",
"weight": doc! {
"$sum": 1
}
Expand All @@ -612,11 +617,11 @@ pub async fn fetch_and_update_boosts_winner(
},
doc! {
"$sort": doc! {
"entropy": -1
"entropy": 1
}
},
doc! {
"$limit": (num_of_winners) as i64
"$limit": num_of_winners
},
];
match completed_tasks_collection
Expand All @@ -625,6 +630,7 @@ pub async fn fetch_and_update_boosts_winner(
{
Ok(mut cursor) => {
while let Some(doc) = cursor.try_next().await.unwrap() {
println!("doc: {:?}", doc);
let address = doc.get("_id").unwrap().as_str().unwrap();
let formatted_address =
FieldElement::from_str(address).unwrap();
Expand Down

0 comments on commit 0500224

Please sign in to comment.