Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Feb 8, 2024
1 parent e54ed65 commit 195d769
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
59 changes: 59 additions & 0 deletions src/api/chapter-stats-analytics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use rocket::{post, State, serde::json::Json};
use crate::learning::{
models::{
user_stat::UserStat,
analytic::Analytic,
learning_config::LearningConfig
},
compute_user_stats_analytics::compute_user_stats_analytics
};
use crate::models::database::Database;
use crate::utils::parse_ranges::parse_ranges;
use crate::domain::verse::analytics_skel_by_chapter_verse::analytics_skel_by_chapter_verse;

#[post("/chapter-stats-analytics?<ranges>", format = "json", data = "<user_stats>")]
pub fn chapter_stats_analytics(
dbs: &State<Database>,
ranges: Option<String>,
) -> Json<Vec<Analytic>> {
// Parse ranges
let parsed_ranges = ranges.as_ref().map(|r| parse_ranges(r)).unwrap_or_else(Vec::new);

let mut chapter_progress_map: std::collections::HashMap<u32, Vec<f32>> = HashMap::new();

// Process each range and fetch analytics
for range in parsed_ranges.iter() {
let chapter_no_start = range.0;
let chapter_no_end = range.1;

for chapter_no in chapter_no_start..=chapter_no_end {
if let Ok(chapter_analytics) = analytics_skel_by_chapter_verse(dbs, chapter_no) {
for analytic in chapter_analytics {
let chapter_no = extract_chapter_no(&analytic.id);
chapter_progress_map.entry(chapter_no).or_insert_with(Vec::new).push(analytic.progress);
}
}
}
}

// Aggregate progress by chapter
let aggregated_analytics: Vec<Analytic> = chapter_progress_map.iter().map(|(&chapter_no, progresses)| {
let average_progress = if !progresses.is_empty() {
progresses.iter().sum::<f32>() / progresses.len() as f32
} else {
0.0
};

Analytic {
id: chapter_no.to_string(),
progress: average_progress,
category: Some("V".to_string()), // Adjust as needed
}
}).collect();

Json(aggregated_analytics)
}

fn extract_chapter_no(id: &str) -> u32 {
id.split(':').next().and_then(|c| c.parse::<u32>().ok()).unwrap_or(0)
}
2 changes: 1 addition & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod get_labels;
pub mod verse_by_chapter;
pub mod verse_similar_by_chapter;
pub mod ping;
pub mod user_stats_analytics;
pub mod verse_stats_analytics;
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::learning::{
};
use crate::models::database::Database;
use crate::utils::parse_ranges::parse_ranges;
use crate::domain::verse::analytics_by_chapter::analytics_by_chapter;
use crate::domain::verse::analytics_skel_by_chapter_verse::analytics_skel_by_chapter_verse;

#[post("/user-stats-analytics?<ranges>", format = "json", data = "<user_stats>")]
pub fn user_stats_analytics(
#[post("/verse_stats_analytics?<ranges>", format = "json", data = "<user_stats>")]
pub fn verse_stats_analytics(
dbs: &State<Database>,
config: &State<LearningConfig>,
user_stats: Json<Vec<UserStat>>,
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn user_stats_analytics(
let chapter_no_end = range.1;

for chapter_no in chapter_no_start..=chapter_no_end {
if let Ok(chapter_analytics) = analytics_by_chapter(dbs, chapter_no as u8) {
if let Ok(chapter_analytics) = analytics_skel_by_chapter_verse(dbs, chapter_no as u8) {
for analytic in chapter_analytics {
// println!("THE ID : {:?}", &analytic.id);
// Check if the analytic already exists in the map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sled::Result as SledResult;

use crate::learning::models::analytic::Analytic;

pub fn analytics_by_chapter(dbs: &State<Database>, chapter_no: u8) -> SledResult<Vec<Analytic>> {
pub fn analytics_skel_by_chapter_verse(dbs: &State<Database>, chapter_no: u8) -> SledResult<Vec<Analytic>> {
let verse_db = &dbs.verse_db;
let prefix = format!("{}:", chapter_no);
let mut analytics = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/domain/verse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod verses_by_chapter;
pub mod count_verses_in_chapter;
pub mod analytics_by_chapter;
pub mod analytics_skel_by_chapter_verse;
4 changes: 2 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::api::{
get_labels::get_labels,
generate_exercise_endpoint::generate_exercises_endpoint,
verse_similar_by_chapter::get_verse_similar_by_chapter_route,
user_stats_analytics::user_stats_analytics,
verse_stats_analytics::verse_stats_analytics,
};

pub struct CORS;
Expand Down Expand Up @@ -74,7 +74,7 @@ fn rocket() -> Rocket<Build> {
get_chapters,
get_labels,
ping,
user_stats_analytics,
verse_stats_analytics,
get_verse_similar_by_chapter_route,
])
}

0 comments on commit 195d769

Please sign in to comment.