Skip to content

Commit

Permalink
feat: handle analytics data after expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed Mar 19, 2024
1 parent c293a20 commit 22b7dc1
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 20 deletions.
65 changes: 59 additions & 6 deletions src/endpoints/analytics/get_quest_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct GetQuestsQuery {
}

#[route(
get,
"/analytics/get_quest_activity",
crate::endpoints::analytics::get_quest_activity
get,
"/analytics/get_quest_activity",
crate::endpoints::analytics::get_quest_activity
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Expand All @@ -32,9 +32,29 @@ pub async fn handler(
"quest_id": quest_id
}
},
doc! {
"$lookup": doc! {
"from": "quests",
"localField": "quest_id",
"foreignField": "id",
"as": "questDetails"
}
},
doc! {
"$set": doc! {
"expiry": doc! {
"$arrayElemAt": [
"$questDetails.expiry",
0
]
}
}
},
doc! {
"$group": doc! {
"_id": null,
"_id": doc! {
"expiry": "$expiry"
},
"ids": doc! {
"$push": "$id"
}
Expand All @@ -43,8 +63,39 @@ pub async fn handler(
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"let": doc! {
"localIds": "$ids",
"expiry": "$_id.expiry"
},
"pipeline": [
doc! {
"$addFields": doc! {
"refactoredTimestamp": doc! {
"$toDate": "$timestamp"
}
}
},
doc! {
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
"$$localIds"
]
},
doc! {
"$lt": [
"$refactoredTimestamp",
"$$expiry"
]
}
]
}
}
}
],
"as": "matching_documents"
}
},
Expand Down Expand Up @@ -138,7 +189,9 @@ pub async fn handler(
{
Ok(mut cursor) => {
let mut day_wise_distribution = Vec::new();
println!("{:?}", cursor);
while let Some(result) = cursor.next().await {
println!("{:?}", result);
match result {
Ok(document) => {
day_wise_distribution.push(document);
Expand Down
74 changes: 63 additions & 11 deletions src/endpoints/analytics/get_quest_participation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,29 @@ pub async fn handler(
"quest_id": quest_id
}
},
doc! {
"$lookup": doc! {
"from": "quests",
"localField": "quest_id",
"foreignField": "id",
"as": "questDetails"
}
},
doc! {
"$set": doc! {
"expiry": doc! {
"$arrayElemAt": [
"$questDetails.expiry",
0
]
}
}
},
doc! {
"$group": doc! {
"_id": null,
"_id": doc! {
"expiry": "$expiry"
},
"ids": doc! {
"$push": "$id"
},
Expand All @@ -46,8 +66,39 @@ pub async fn handler(
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"let": doc! {
"localIds": "$ids",
"expiry": "$_id.expiry"
},
"pipeline": [
doc! {
"$addFields": doc! {
"refactoredTimestamp": doc! {
"$toDate": "$timestamp"
}
}
},
doc! {
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
"$$localIds"
]
},
doc! {
"$lt": [
"$refactoredTimestamp",
"$$expiry"
]
}
]
}
}
}
],
"as": "matching_documents"
}
},
Expand Down Expand Up @@ -93,7 +144,7 @@ pub async fn handler(
"$matching_documents",
"$otherDetails",
doc! {
"participants": "$count"
"count": "$count"
}
]
}
Expand All @@ -102,17 +153,18 @@ pub async fn handler(
doc! {
"$project": doc! {
"otherDetails": 0,
"_id":0,
"verify_endpoint": 0,
"verify_endpoint_type": 0,
"verify_redirect":0,
"_id":0,
"verify_endpoint": 0,
"verify_endpoint_type": 0,
"verify_redirect":0,
"href": 0,
"cta": 0,
"id": 0,
"quest_id": 0,

}
},
"questDetails": 0,
"expiry":0
}
},
];

match state
Expand Down
25 changes: 22 additions & 3 deletions src/endpoints/get_quest_participants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]

pub struct GetQuestParticipantsQuery {
quest_id: u32,
}
Expand Down Expand Up @@ -49,10 +48,30 @@ pub async fn handler(
let tasks_count = tasks_ids.len();

let pipeline = vec![
doc! {
"$addFields": doc! {
"refactoredTimestamp": doc! {
"$toDate": "$timestamp"
}
}
},
doc! {
"$match": {
"task_id": {
"$in": tasks_ids
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
tasks_ids
]
},
doc! {
"$lt": [
"$refactoredTimestamp",
"$$expiry"
]
}
]
}
}
},
Expand Down

0 comments on commit 22b7dc1

Please sign in to comment.