Skip to content

Commit

Permalink
Refactor sync_elements rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Feb 2, 2025
1 parent 4d17ccc commit 03faf42
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async fn main() -> Result<()> {
rpc::generate_element_issues::NAME,
rpc::generate_element_issues::run,
)
.with_method(rpc::sync_elements::NAME, rpc::sync_elements::run)
// area
.with_method(rpc::add_area::NAME, rpc::add_area::run)
.with_method(rpc::get_area::NAME, rpc::get_area::run)
Expand Down Expand Up @@ -133,7 +134,6 @@ async fn main() -> Result<()> {
"generate_element_categories",
rpc::generate_element_categories::run,
)
.with_method("sync_elements", rpc::sync_elements::run)
.with_method("add_admin", rpc::add_admin::run)
.with_method("add_admin_action", rpc::add_admin_action::run)
.with_method("remove_admin_action", rpc::remove_admin_action::run)
Expand Down
5 changes: 5 additions & 0 deletions src/rpc/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum RpcMethod {
BoostElement,
AddElementComment,
GenerateElementIssues,
SyncElements,
// area
AddArea,
GetArea,
Expand Down Expand Up @@ -148,6 +149,10 @@ async fn handle(req: Json<Value>, pool: Data<Pool>, conf: Data<Conf>) -> Result<
req.id.clone(),
super::generate_element_issues::run_internal(params(req.params)?, &pool, &conf).await?,
),
RpcMethod::SyncElements => RpcResponse::from(
req.id.clone(),
super::sync_elements::run_internal(params(req.params)?, &pool, &conf).await?,
),
RpcMethod::AddArea => RpcResponse::from(
req.id.clone(),
super::add_area::run_internal(params(req.params)?, &pool, &conf).await?,
Expand Down
42 changes: 24 additions & 18 deletions src/rpc/sync_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use crate::osm::overpass;
use crate::{admin, sync::MergeResult};
use crate::{db, discord, sync, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use jsonrpc_v2::Data;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::info;

const NAME: &str = "sync_elements";
pub const NAME: &str = "sync_elements";

#[derive(Deserialize)]
pub struct Args {
pub struct Params {
pub password: String,
}

Expand All @@ -22,28 +21,35 @@ pub struct Res {
pub merge_result: MergeResult,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let admin = admin::service::check_rpc(args.password, NAME, &pool).await?;
info!(admin.name, "Admin requested element sync");
pub async fn run(
jsonrpc_v2::Params(params): jsonrpc_v2::Params<Params>,
pool: Data<Arc<Pool>>,
conf: Data<Arc<Conf>>,
) -> Result<Res> {
run_internal(params, &pool, &conf).await
}

pub async fn run_internal(params: Params, pool: &Pool, conf: &Conf) -> Result<Res> {
let admin = admin::service::check_rpc(params.password, NAME, &pool).await?;
let overpass_res = overpass::query_bitcoin_merchants().await?;
let overpass_elements_len = overpass_res.elements.len();
let mut conn = db::open_connection()?;
let merge_res = sync::merge_overpass_elements(overpass_res.elements, &mut conn).await?;
if merge_res.elements_created.len()
+ merge_res.elements_updated.len()
+ merge_res.elements_deleted.len()
> 3
> 5
{
let log_message = format!(
"Admin {} ran a sync with high number of changes (created: {}, updated: {}, deleted: {})",
admin.name,
merge_res.elements_created.len(),
merge_res.elements_updated.len(),
merge_res.elements_deleted.len(),
);
info!(log_message);
let conf = Conf::select_async(&pool).await?;
discord::post_message(conf.discord_webhook_api, log_message).await;
discord::post_message(
&conf.discord_webhook_api,
format!(
"Admin {} ran a sync with a high number of changes (created: {}, updated: {}, deleted: {})",
admin.name,
merge_res.elements_created.len(),
merge_res.elements_updated.len(),
merge_res.elements_deleted.len()
)
).await;
}
Ok(Res {
overpass_query_time_s: overpass_res.time_s,
Expand Down

0 comments on commit 03faf42

Please sign in to comment.