Skip to content

Commit

Permalink
Add generateelementissues RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 2, 2024
1 parent c376b3c commit 3b5a996
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/element/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use rusqlite::{named_params, Connection, OptionalExtension, Row};
use serde::Serialize;
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::time::Instant;
use std::thread::sleep;
use std::time::{Duration, Instant};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use tracing::{debug, info};

Expand Down Expand Up @@ -161,6 +162,7 @@ impl Element {
tags: &Map<String, Value>,
conn: &Connection,
) -> crate::Result<Element> {
sleep(Duration::from_millis(10));
let query = format!(
r#"
UPDATE {TABLE} SET {COL_TAGS} = json_patch({COL_TAGS}, :tags) WHERE {COL_ROWID} = :id
Expand Down Expand Up @@ -209,6 +211,7 @@ impl Element {
}

pub fn remove_tag(id: i64, name: &str, conn: &Connection) -> Result<Element> {
sleep(Duration::from_millis(10));
let query = format!(
r#"
UPDATE {TABLE}
Expand Down
27 changes: 22 additions & 5 deletions src/element/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use serde_json::json;
use serde_json::Value;
use time::macros::format_description;
use time::Date;
use time::OffsetDateTime;
use tracing::info;

pub fn remove_areas_tag(area: &Area, conn: &mut Connection) -> Result<()> {
Expand Down Expand Up @@ -167,26 +168,42 @@ pub struct Issue {
pub description: String,
}

pub fn generate_issues(elements: Vec<&Element>, conn: &Connection) -> Result<()> {
pub struct GenerateIssuesResult {
pub started_at: OffsetDateTime,
pub finished_at: OffsetDateTime,
pub time_s: f64,
pub affected_elements: i64,
}

pub fn generate_issues(elements: Vec<&Element>, conn: &Connection) -> Result<GenerateIssuesResult> {
let started_at = OffsetDateTime::now_utc();
let mut affected_elements = 0;
for element in elements {
let issues = crate::element::service::get_issues(&element);
// No current issues, no saved issues, nothing to do here
if issues.is_empty() && !element.tags.contains_key("issues") {
return Ok(());
continue;
}
// No current issues found but an element has some old issues which need to be deleted
if issues.is_empty() && element.tags.contains_key("issues") {
Element::remove_tag(element.id, "issues", conn)?;
return Ok(());
affected_elements += 1;
continue;
}
let issues = serde_json::to_value(&issues)?;
// We should avoid toucing the elements if the issues didn't change
if element.tag("issues") != &issues {
Element::set_tag(element.id, "issues", &issues, conn)?;
affected_elements += 1;
}
}

Ok(())
let finished_at = OffsetDateTime::now_utc();
Ok(GenerateIssuesResult{
started_at,
finished_at,
time_s: (finished_at - started_at).as_seconds_f64(),
affected_elements,
})
}

fn get_issues(element: &Element) -> Vec<Issue> {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use tracing_subscriber::EnvFilter;
mod area;
mod review;
mod vacuum;
mod rpc;

pub type Result<T, E = Error> = std::result::Result<T, E>;

Expand Down
62 changes: 62 additions & 0 deletions src/rpc/generate_element_issues.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::{
auth::Token,
discord,
element::{self, Element},
Result,
};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use time::OffsetDateTime;
use tracing::info;

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

#[derive(Serialize)]
pub struct Res {
#[serde(with = "time::serde::rfc3339")]
pub started_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub finished_at: OffsetDateTime,
pub time_s: f64,
pub affected_elements: i64,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let elements: Vec<Element> = pool
.get()
.await?
.interact(move |conn| Element::select_all(None, conn))
.await??;
let elements: Vec<Element> = elements
.into_iter()
.filter(|it| it.deleted_at.is_none())
.collect();
let res = pool
.get()
.await?
.interact(move |conn| element::service::generate_issues(elements.iter().collect(), conn))
.await??;
let log_message = format!(
"{} generated element issues, affecting {} elements",
token.owner, res.affected_elements,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(Res {
started_at: res.started_at,
finished_at: res.finished_at,
time_s: res.time_s,
affected_elements: res.affected_elements,
})
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod generate_element_issues;
3 changes: 2 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::db;
use crate::{area, element, error, user};
use crate::{area, element, error, rpc, user};
use crate::{event, tile};
use crate::{report, Result};
use actix_governor::{Governor, GovernorConfigBuilder, KeyExtractor, SimpleKeyExtractionError};
Expand Down Expand Up @@ -86,6 +86,7 @@ pub async fn run() -> Result<()> {
.with_method("removeelementtag", element::rpc::remove_tag)
.with_method("boostelement", element::rpc::boost)
.with_method("createelementreview", element::rpc::create_review)
.with_method("generateelementissues", rpc::generate_element_issues::run)
.with_method("createarea", area::rpc::create)
.with_method("getarea", area::rpc::get)
.with_method("setareatag", area::rpc::set_tag)
Expand Down

0 comments on commit 3b5a996

Please sign in to comment.