Skip to content

Commit

Permalink
Add get most commented countries RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 9, 2024
1 parent 2423027 commit 3e0ff2f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/rpc/get_most_commented_countries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{area::Area, auth::Token, element::Element, element_comment::ElementComment, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

#[derive(Deserialize)]
pub struct Args {
pub token: String,
pub period_start: String,
pub period_end: String,
}

#[derive(Serialize)]
pub struct Res {
id: i64,
name: String,
comments: i64,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Vec<Res>> {
pool.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let period_start =
OffsetDateTime::parse(&format!("{}T00:00:00Z", args.period_start), &Rfc3339).unwrap();
let period_end =
OffsetDateTime::parse(&format!("{}T00:00:00Z", args.period_end), &Rfc3339).unwrap();
pool.get()
.await?
.interact(move |conn| get_most_commented_countries(&period_start, &period_end, conn))
.await?
}

fn get_most_commented_countries(
period_start: &OffsetDateTime,
period_end: &OffsetDateTime,
conn: &Connection,
) -> Result<Vec<Res>> {
let comments = ElementComment::select_updated_since(period_start, None, conn)?;
let comments: Vec<ElementComment> = comments
.into_iter()
.filter(|it| it.created_at < *period_end)
.collect();
let mut areas_to_comments: HashMap<i64, Vec<&ElementComment>> = HashMap::new();
for comment in &comments {
let element = Element::select_by_id(comment.element_id, conn)?.unwrap();
if element.tags.contains_key("areas") {
let areas = element.tag("areas").as_array().unwrap();
for area in areas {
let area_id = area["id"].as_i64().unwrap();
if !areas_to_comments.contains_key(&area_id) {
areas_to_comments.insert(area_id, vec![]);
}
let area_comments = areas_to_comments.get_mut(&area_id).unwrap();
area_comments.push(comment);
}
}
}
let areas_to_comments: Vec<(Area, Vec<&ElementComment>)> = areas_to_comments
.into_iter()
.map(|(k, v)| (Area::select_by_id(k, conn).unwrap().unwrap(), v))
.collect();
let mut res: Vec<Res> = areas_to_comments
.iter()
.filter(|it| {
it.0.tags.contains_key("type") && it.0.tags["type"].as_str() == Some("country")
})
.map(|it| Res {
id: it.0.id,
name: it.0.name(),
comments: it.1.len() as i64,
})
.collect();
res.sort_by(|x, y| y.comments.cmp(&x.comments));
Ok(res)
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod boost_element;
pub mod generate_element_issues;
pub mod get_area;
pub mod get_element;
pub mod get_most_commented_countries;
pub mod get_trending_communities;
pub mod get_trending_countries;
pub mod remove_area;
Expand Down
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub async fn run() -> Result<()> {
.with_method("setareatag", rpc::set_area_tag::run)
.with_method("removeareatag", rpc::remove_area_tag::run)
.with_method("gettrendingcountries", rpc::get_trending_countries::run)
.with_method("getmostcommentedcountries", rpc::get_most_commented_countries::run)
.with_method("gettrendingcommunities", rpc::get_trending_communities::run)
.with_method("removearea", rpc::remove_area::run)
.finish()
Expand Down

0 comments on commit 3e0ff2f

Please sign in to comment.