Skip to content

Commit

Permalink
Merge pull request #202 from starknet-id/ayush/handle-expiry-data
Browse files Browse the repository at this point in the history
feat: handle analytics data after expiry
  • Loading branch information
Th0rgal authored Apr 25, 2024
2 parents 784d17d + 087fece commit c006acd
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 38 deletions.
62 changes: 56 additions & 6 deletions src/endpoints/analytics/get_quest_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ 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>>,
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let current_time = chrono::Utc::now().timestamp_millis();
let quest_id = query.id;
let day_wise_distribution = vec![
doc! {
"$match": doc! {
"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 +64,37 @@ pub async fn handler(
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"let": doc! {
"localIds": "$ids",
"expiry": "$_id.expiry"
},
"pipeline": [
doc! {
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
"$$localIds"
]
},
doc! {
"$lte": [
"$timestamp",
doc! {
"$ifNull": [
"$$expiry",
current_time
]
}
]
}
]
}
}
}
],
"as": "matching_documents"
}
},
Expand Down
87 changes: 69 additions & 18 deletions src/endpoints/analytics/get_quest_participation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ pub struct GetQuestsQuery {
}

#[route(
get,
"/analytics/get_quest_participation",
crate::endpoints::analytics::get_quest_participation
get,
"/analytics/get_quest_participation",
crate::endpoints::analytics::get_quest_participation
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let current_time = chrono::Utc::now().timestamp_millis();
let quest_id = query.id;
let day_wise_distribution = vec![
doc! {
"$match": doc! {
"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 +67,37 @@ pub async fn handler(
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"localField": "ids",
"foreignField": "task_id",
"let": doc! {
"localIds": "$ids",
"expiry": "$_id.expiry"
},
"pipeline": [
doc! {
"$match": doc! {
"$expr": doc! {
"$and": [
doc! {
"$in": [
"$task_id",
"$$localIds"
]
},
doc! {
"$lte": [
"$timestamp",
doc! {
"$ifNull": [
"$$expiry",
current_time
]
}
]
}
]
}
}
}
],
"as": "matching_documents"
}
},
Expand Down Expand Up @@ -93,24 +143,25 @@ pub async fn handler(
"$matching_documents",
"$otherDetails",
doc! {
"participants": "$count"
"count": "$count"
}
]
}
}
},
doc! {
"$project": doc! {
"otherDetails": 0,
"_id":0,
"verify_endpoint": 0,
"verify_endpoint_type": 0,
"verify_redirect":0,
"href": 0,
"cta": 0,
"id": 0,
"quest_id": 0,

"$project": doc! {
"otherDetails": 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
}
},
];
Expand Down
20 changes: 15 additions & 5 deletions src/endpoints/get_quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ pub async fn handler(
Query(query): Query<GetQuestsQuery>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestDocument>("quests");

let pipeline = [
doc! {
"$match": {
"disabled": false,
"id": query.id
"id": query.id,
}
},
doc! {
Expand All @@ -38,8 +39,18 @@ pub async fn handler(
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
doc! {
"$gte": [
"$expiry",
0
]
},
doc! {
"$lt": [
"$expiry",
"$$NOW"
]
}
]
},
true,
Expand All @@ -57,8 +68,7 @@ pub async fn handler(
Ok(document) => {
if let Ok(mut quest) = from_document::<QuestDocument>(document) {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
quest.expiry_timestamp = Some(expiry.to_string());
}
return (StatusCode::OK, Json(quest)).into_response();
}
Expand Down
23 changes: 18 additions & 5 deletions src/endpoints/get_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ pub struct NFTItem {

#[route(get, "/get_quests", crate::endpoints::get_quests)]
pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let current_time = chrono::Utc::now().timestamp_millis();

let pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
"start_time": {
"$lte":current_time
}
}
},
doc! {
Expand All @@ -36,8 +40,18 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
"$cond": [
{
"$and": [
{ "$gte": ["$expiry", 0] },
{ "$lt": ["$expiry", "$$NOW"] },
doc! {
"$gte": [
"$expiry",
0
]
},
doc! {
"$lt": [
"$expiry",
"$$NOW"
]
}
]
},
true,
Expand All @@ -57,8 +71,7 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
Ok(document) => {
if let Ok(mut quest) = from_document::<QuestDocument>(document) {
if let Some(expiry) = &quest.expiry {
let timestamp = expiry.timestamp_millis().to_string();
quest.expiry_timestamp = Some(timestamp);
quest.expiry_timestamp = Some(expiry.to_string());
}
quests.push(quest);
}
Expand Down
6 changes: 5 additions & 1 deletion src/endpoints/get_trending_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ pub async fn handler(
Some(addr) => addr.to_string(),
None => "".to_string(),
};
let current_time = chrono::Utc::now().timestamp_millis();

let mut pipeline = vec![
doc! {
"$match": {
"disabled": false,
"hidden": false,
"is_trending": true,
"start_time": doc! {
"$lte": current_time
}
}
},
doc! {
Expand Down
8 changes: 5 additions & 3 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mongodb::{bson, Database};
use mongodb::{Database};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use starknet::{
Expand Down Expand Up @@ -34,13 +34,15 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument {
rewards_nfts: Vec<NFTItem>,
img_card: String,
title_card: String,
hidden: bool,
hidden: Option<bool>,
disabled: bool,
expiry: Option<bson::DateTime>,
expiry: Option<i64>,
expiry_timestamp: Option<String>,
mandatory_domain: Option<String>,
expired: Option<bool>,
experience: i64,
start_time: i64,

});

pub_struct!(Deserialize; CompletedTasks {
Expand Down

0 comments on commit c006acd

Please sign in to comment.