Skip to content

Commit

Permalink
Speed up area queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 28, 2023
1 parent 00f7c7b commit 56a887d
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 197 deletions.
1 change: 1 addition & 0 deletions migrations/47.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX idx_area_updated_at ON area(updated_at);
7 changes: 0 additions & 7 deletions src/command/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ pub fn open_connection() -> Result<Connection> {
Ok(conn)
}

#[cfg(test)]
pub fn setup_connection() -> Result<Connection> {
let mut conn = Connection::open_in_memory()?;
self::migrate(&mut conn)?;
Ok(conn)
}

pub fn get_file_path() -> Result<PathBuf> {
let project_dirs = match ProjectDirs::from("org", "BTC Map", "BTC Map") {
Some(project_dirs) => project_dirs,
Expand Down
6 changes: 5 additions & 1 deletion src/command/fix_tags.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use rusqlite::Connection;
use serde_json::Value;
use tracing::warn;
Expand All @@ -15,7 +17,9 @@ pub fn run(conn: &Connection) -> Result<()> {
warn!(area.id, "Found improperly formatted geo_json tag");
let unescaped = geo_json.as_str().unwrap().replace("\\\"", "\"");
let geo_json: Value = serde_json::from_str(&unescaped)?;
Area::set_tag(area.id, "geo_json", &geo_json, &conn)?;
let mut patch_set = HashMap::new();
patch_set.insert("geo_json".into(), geo_json);
area.patch_tags(&patch_set, &conn)?;
warn!(area.id, "Fixed geo_json tag");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/import_countries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn run(path: &str, conn: &mut Connection) -> Result<()> {

match Area::select_by_url_alias(&json.id, &tx)? {
Some(area) => {
Area::patch_tags(area.id, &json.tags, &tx)?;
area.patch_tags(&json.tags, &tx)?;
info!(json.id, "Patched tags for an existing area");
}
None => {
Expand Down
22 changes: 13 additions & 9 deletions src/controller/area_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct PostJsonArgs {

#[derive(Deserialize)]
pub struct GetArgs {
updated_since: Option<String>,
#[serde(with = "time::serde::rfc3339::option")]
updated_since: Option<OffsetDateTime>,
limit: Option<i32>,
}

Expand Down Expand Up @@ -163,7 +164,7 @@ async fn patch_by_url_alias(

match Area::select_by_url_alias(&area_url_alias, &conn)? {
Some(area) => {
Area::patch_tags(area.id, &args.tags, &conn)?;
area.patch_tags(&args.tags, &conn)?;
}
None => {
return Err(ApiError::new(
Expand Down Expand Up @@ -192,7 +193,7 @@ async fn patch_tags(
);

match Area::select_by_url_alias(&url_alias, &conn)? {
Some(area) => Area::patch_tags(area.id, &args, &conn)?,
Some(area) => area.patch_tags(&args, &conn)?,
None => {
return Err(ApiError::new(
404,
Expand Down Expand Up @@ -228,9 +229,11 @@ async fn post_tags(
match area {
Some(area) => {
if args.value.len() > 0 {
Area::set_tag(area.id, &args.name, &args.value.clone().into(), &conn)?;
let mut patch_set = HashMap::new();
patch_set.insert(args.name.clone(), Value::String(args.value.clone()));
area.patch_tags(&patch_set, &conn)?;
} else {
Area::remove_tag(area.id, &args.name, &conn)?;
area.remove_tag(&args.name, &conn)?;
}

Ok(HttpResponse::Created())
Expand All @@ -257,7 +260,7 @@ async fn delete_by_url_alias(

match area {
Some(area) => {
Area::set_deleted_at(area.id, Some(OffsetDateTime::now_utc()), &conn)?;
area.set_deleted_at(Some(OffsetDateTime::now_utc()), &conn)?;
Ok(HttpResponse::Ok())
}
None => Err(ApiError::new(
Expand All @@ -272,6 +275,7 @@ mod tests {
use super::*;
use crate::command::db;
use crate::model::token;
use crate::test::mock_conn;
use crate::Result;
use actix_web::http::StatusCode;
use actix_web::test::TestRequest;
Expand Down Expand Up @@ -352,7 +356,7 @@ mod tests {

#[actix_web::test]
async fn get_one_row() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();

let mut tags = HashMap::new();
tags.insert("url_alias".into(), "test".into());
Expand All @@ -374,7 +378,7 @@ mod tests {

#[actix_web::test]
async fn get_with_limit() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();

let mut tags = HashMap::new();
tags.insert("url_alias".into(), "test".into());
Expand All @@ -398,7 +402,7 @@ mod tests {

#[actix_web::test]
async fn get_by_id() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let area_url_alias = "test";
let mut tags = HashMap::new();
tags.insert("url_alias".into(), Value::String(area_url_alias.into()));
Expand Down
13 changes: 7 additions & 6 deletions src/controller/element_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ mod test {
use super::*;
use crate::command::db;
use crate::model::token;
use crate::test::mock_conn;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::scope;
Expand Down Expand Up @@ -217,7 +218,7 @@ mod test {

#[actix_web::test]
async fn get_one_row() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let element = Element::insert(&OverpassElement::mock(1), &conn)?;
let app = test::init_service(
App::new()
Expand All @@ -234,7 +235,7 @@ mod test {

#[actix_web::test]
async fn get_with_limit() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
Element::insert(&OverpassElement::mock(1), &conn)?;
Element::insert(&OverpassElement::mock(2), &conn)?;
Element::insert(&OverpassElement::mock(3), &conn)?;
Expand All @@ -253,7 +254,7 @@ mod test {

#[actix_web::test]
async fn get_updated_since() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
Element::insert(&OverpassElement::mock(1), &conn)?
.set_updated_at(&datetime!(2022-01-05 00:00 UTC), &conn)?;
Element::insert(&OverpassElement::mock(2), &conn)?
Expand All @@ -274,7 +275,7 @@ mod test {

#[actix_web::test]
async fn get_by_id() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let element = Element::insert(&OverpassElement::mock(1), &conn)?;
let app = test::init_service(
App::new()
Expand All @@ -292,7 +293,7 @@ mod test {

#[actix_web::test]
async fn patch_tags() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let admin_token = "test";
conn.execute(
token::INSERT,
Expand All @@ -317,7 +318,7 @@ mod test {

#[actix_web::test]
async fn post_tags() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let admin_token = "test";
conn.execute(
token::INSERT,
Expand Down
10 changes: 5 additions & 5 deletions src/controller/element_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn get(
#[cfg(test)]
mod test {
use super::*;
use crate::command::db;
use crate::test::mock_conn;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::scope;
Expand All @@ -96,7 +96,7 @@ mod test {

#[actix_web::test]
async fn get_empty_array() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let app = test::init_service(
App::new()
.app_data(Data::new(conn))
Expand All @@ -111,7 +111,7 @@ mod test {

#[actix_web::test]
async fn get_not_empty_array() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let element = Element::insert(&OverpassElement::mock(1), &conn)?;
let app = test::init_service(
App::new()
Expand All @@ -128,7 +128,7 @@ mod test {

#[actix_web::test]
async fn get_with_limit() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let element_1 = Element::insert(&OverpassElement::mock(1), &conn)?;
let element_2 = Element::insert(&OverpassElement::mock(2), &conn)?;
let element_3 = Element::insert(&OverpassElement::mock(3), &conn)?;
Expand All @@ -149,7 +149,7 @@ mod test {

#[actix_web::test]
async fn get_updated_since() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let element_1 = Element::insert(&OverpassElement::mock(1), &conn)?
.set_updated_at(&datetime!(2022-01-05 00:00 UTC), &conn)?;
let element_2 = Element::insert(&OverpassElement::mock(2), &conn)?
Expand Down
5 changes: 3 additions & 2 deletions src/controller/event_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ mod tests {
use super::*;
use crate::command::db;
use crate::model::token;
use crate::test::mock_conn;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::scope;
Expand Down Expand Up @@ -153,7 +154,7 @@ mod tests {

#[actix_web::test]
async fn get_one_row() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();

Event::insert(0, "", "", &conn)?;

Expand Down Expand Up @@ -224,7 +225,7 @@ mod tests {

#[actix_web::test]
async fn get_by_id() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let event_id = 1;
Event::insert(0, "", "", &conn)?;
let app = test::init_service(
Expand Down
3 changes: 2 additions & 1 deletion src/controller/report_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mod tests {
use super::*;
use crate::command::db;
use crate::model::{token, Area};
use crate::test::mock_conn;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::scope;
Expand Down Expand Up @@ -164,7 +165,7 @@ mod tests {

#[actix_web::test]
async fn get_one_row() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();
let mut area_tags = HashMap::new();
area_tags.insert("url_alias".into(), "test".into());
Area::insert(&area_tags, &conn)?;
Expand Down
5 changes: 3 additions & 2 deletions src/controller/user_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ mod tests {
use super::*;
use crate::command::db;
use crate::model::token;
use crate::test::mock_conn;
use crate::Result;
use actix_web::test::TestRequest;
use actix_web::web::scope;
Expand Down Expand Up @@ -151,7 +152,7 @@ mod tests {

#[actix_web::test]
async fn get_one_row() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();

User::insert(1, &OsmUser::mock(), &conn)?;

Expand All @@ -171,7 +172,7 @@ mod tests {

#[actix_web::test]
async fn get_updated_since() -> Result<()> {
let conn = db::setup_connection()?;
let conn = mock_conn();

conn.execute(
"INSERT INTO user (rowid, osm_json, updated_at) VALUES (1, json(?), '2022-01-05T00:00:00Z')",
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl From<TryFromIntError> for Error {
}
}

impl From<time::error::Format> for Error {
fn from(_: time::error::Format) -> Self {
Error::Other("Time formatting error".into())
}
}

#[derive(Debug)]
pub struct ApiError {
pub http_code: StatusCode,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod discord;
mod error;
mod model;
mod service;
#[cfg(test)]
mod test;
use rusqlite::Connection;
use std::env;
use std::process::ExitCode;
Expand Down
Loading

0 comments on commit 56a887d

Please sign in to comment.