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

feat: add quest completion activity #200

Merged
merged 1 commit into from
Mar 8, 2024
Merged
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
154 changes: 105 additions & 49 deletions src/endpoints/analytics/get_quest_activity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::{QuestTaskDocument};
use crate::models::QuestTaskDocument;
use crate::{models::AppState, utils::get_error};
use axum::{
extract::{Query, State},
Expand All @@ -8,78 +8,134 @@ use axum::{
use axum_auto_routes::route;
use futures::StreamExt;
use mongodb::bson::doc;
use std::sync::Arc;
use serde::Deserialize;
use std::sync::Arc;

#[derive(Deserialize)]
pub struct GetQuestsQuery {
id: u32,
}


#[route(get, "/analytics/get_quest_activity", crate::endpoints::analytics::get_quest_activity)]
pub async fn handler(State(state): State<Arc<AppState>>,
Query(query): Query<GetQuestsQuery>,
#[route(
get,
"/analytics/get_quest_activity",
crate::endpoints::analytics::get_quest_activity
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let quest_id = query.id;
let day_wise_distribution = vec![
doc! {
"$match": doc! {
"quest_id": quest_id
}
},
"$match": doc! {
"quest_id": quest_id
}
},
doc! {
"$group": doc! {
"_id": null,
"ids": doc! {
"$push": "$id"
"$group": doc! {
"_id": null,
"ids": doc! {
"$push": "$id"
}
}
}
},
},
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"as": "matching_documents"
}
},
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"as": "matching_documents"
}
},
doc! {
"$unwind": "$matching_documents"
},
"$unwind": "$matching_documents"
},
doc! {
"$replaceRoot": doc! {
"newRoot": "$matching_documents"
}
},
"$replaceRoot": doc! {
"newRoot": doc! {
"$mergeObjects": [
"$$ROOT",
"$matching_documents"
]
}
}
},
doc! {
"$group": doc! {
"_id": doc! {
"_id": "$address",
"ids": "$ids"
},
"maxTimestamp": doc! {
"$max": "$timestamp"
},
"tasks": doc! {
"$addToSet": "$task_id"
},
"count": doc! {
"$sum": 1
}
}
},
doc! {
"$addFields": doc! {
"createdDate": doc! {
"$toDate": "$timestamp"
"$addFields": doc! {
"createdDate": doc! {
"$toDate": "$maxTimestamp"
}
}
}
},
},
doc! {
"$group": doc! {
"_id": doc! {
"$dateToString": doc! {
"format": "%Y-%m-%d %d",
"date": "$createdDate"
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$eq": [
doc! {
"$size": "$tasks"
},
doc! {
"$size": "$_id.ids"
}
]
}
]
}
},
"participants": doc! {
"$sum": 1
}
}
},
},
doc! {
"$sort": doc! {
"_id": 1
}
},
"$group": doc! {
"_id": doc! {
"$dateToString": doc! {
"format": "%Y-%m-%d %d",
"date": "$createdDate"
}
},
"count": doc! {
"$sum": 1
}
}
},
doc! {
"$sort": doc! {
"_id": 1
}
},
doc! {
"$project": doc! {
"_id": 0,
"date": "$_id",
"participants": "$count"
}
},
];

match state.db.collection::<QuestTaskDocument>("tasks").aggregate(day_wise_distribution, None).await {
match state
.db
.collection::<QuestTaskDocument>("tasks")
.aggregate(day_wise_distribution, None)
.await
{
Ok(mut cursor) => {
let mut day_wise_distribution = Vec::new();
while let Some(result) = cursor.next().await {
Expand Down
Loading