Skip to content

Commit

Permalink
Fix chapter stats api
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Feb 9, 2024
1 parent 1c23255 commit 55343c1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use rocket::{post, State, serde::json::Json};
use std::collections::HashMap;

use crate::learning::{
models::{
user_stat::UserStat,
Expand All @@ -14,13 +16,25 @@ use crate::domain::verse::analytics_skel_by_chapter_verse::analytics_skel_by_cha
#[post("/chapter-stats-analytics?<ranges>", format = "json", data = "<user_stats>")]
pub fn chapter_stats_analytics(
dbs: &State<Database>,
config: &State<LearningConfig>,
user_stats: Json<Vec<UserStat>>,
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 user_progress_analytics = compute_user_stats_analytics(&**config, &user_stats);
let analytics_map: std::collections::HashMap<String, Analytic> = user_progress_analytics.clone()
.into_iter()
.map(|analytic| (analytic.id.clone(), analytic))
.collect();

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

// First, incorporate user progress analytics into the chapter progress map
for analytic in &user_progress_analytics {
let chapter_no = extract_chapter_no(&analytic.id);
chapter_progress_map.entry(chapter_no).or_insert_with(Vec::new).push(analytic.progress);
}

let parsed_ranges = ranges.as_ref().map(|r| parse_ranges(r)).unwrap_or_else(Vec::new);
// Process each range and fetch analytics
for range in parsed_ranges.iter() {
let chapter_no_start = range.0;
Expand All @@ -29,8 +43,12 @@ pub fn chapter_stats_analytics(
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 {
// Here, you might adjust the logic to either replace or skip adding
// analytics for verses already covered by user progress, depending on your needs
let chapter_no = extract_chapter_no(&analytic.id);
chapter_progress_map.entry(chapter_no).or_insert_with(Vec::new).push(analytic.progress);
if !analytics_map.contains_key(&analytic.id) {
chapter_progress_map.entry(chapter_no).or_insert_with(Vec::new).push(analytic.progress);
}
}
}
}
Expand All @@ -47,7 +65,7 @@ pub fn chapter_stats_analytics(
Analytic {
id: chapter_no.to_string(),
progress: average_progress,
category: Some("V".to_string()), // Adjust as needed
category: Some("V".to_string()),
}
}).collect();

Expand Down
3 changes: 2 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod get_labels;
pub mod verse_by_chapter;
pub mod verse_similar_by_chapter;
pub mod ping;
pub mod verse_stats_analytics;
pub mod verse_stats_analytics;
pub mod chapter_stats_analytics;
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::api::{
generate_exercise_endpoint::generate_exercises_endpoint,
verse_similar_by_chapter::get_verse_similar_by_chapter_route,
verse_stats_analytics::verse_stats_analytics,
chapter_stats_analytics::chapter_stats_analytics,
};

pub struct CORS;
Expand Down Expand Up @@ -75,6 +76,7 @@ fn rocket() -> Rocket<Build> {
get_labels,
ping,
verse_stats_analytics,
chapter_stats_analytics,
get_verse_similar_by_chapter_route,
])
}

0 comments on commit 55343c1

Please sign in to comment.