Skip to content

Commit f0bb0c0

Browse files
committed
Add optional area scope for opening hour humanizer
1 parent a5adef9 commit f0bb0c0

5 files changed

Lines changed: 70 additions & 10 deletions

File tree

src/db/main/element/blocking_queries.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::schema::{self, Columns, Element};
2+
use crate::db::main::area_element::schema::{self as area_element_schema};
23
use crate::{service::overpass::OverpassElement, Result};
34
use rusqlite::{named_params, params, Connection};
45
use serde_json::{Map, Value};
@@ -177,6 +178,40 @@ pub fn select_with_opening_hours_without_humanization(
177178
.map_err(Into::into)
178179
}
179180

181+
pub fn select_with_opening_hours_without_humanization_by_area(
182+
area_id: i64,
183+
limit: i64,
184+
conn: &Connection,
185+
) -> Result<Vec<Element>> {
186+
let table = schema::TABLE_NAME;
187+
let sql = format!(
188+
r#"
189+
SELECT {table}.{id}, {table}.{overpass_data}, {table}.{tags}, {table}.{lat}, {table}.{lon}, {table}.{created_at}, {table}.{updated_at}, {table}.{deleted_at}
190+
FROM {table}
191+
INNER JOIN {area_element_table} ae ON ae.element_id = {table}.id AND ae.area_id = ?1 AND ae.deleted_at IS NULL
192+
WHERE json_extract({table}.{overpass_data}, '$.tags.opening_hours') IS NOT NULL
193+
AND json_extract({table}.{tags}, '$.opening_hours:en:human_readable') IS NULL
194+
AND {table}.{deleted_at} IS NULL
195+
ORDER BY RANDOM()
196+
LIMIT ?2
197+
"#,
198+
table = table,
199+
id = Columns::Id.as_str(),
200+
overpass_data = Columns::OverpassData.as_str(),
201+
tags = Columns::Tags.as_str(),
202+
lat = Columns::Lat.as_str(),
203+
lon = Columns::Lon.as_str(),
204+
created_at = Columns::CreatedAt.as_str(),
205+
updated_at = Columns::UpdatedAt.as_str(),
206+
deleted_at = Columns::DeletedAt.as_str(),
207+
area_element_table = area_element_schema::TABLE_NAME,
208+
);
209+
conn.prepare(&sql)?
210+
.query_map(params![area_id, limit], Element::mapper())?
211+
.collect::<Result<Vec<_>, _>>()
212+
.map_err(Into::into)
213+
}
214+
180215
pub fn select_by_id_or_osm_id(id: impl Into<String>, conn: &Connection) -> Result<Element> {
181216
let id: String = id.into();
182217
let id = id.as_str();

src/db/main/element/queries.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ pub async fn select_with_opening_hours_without_humanization(
8181
.await?
8282
}
8383

84+
pub async fn select_with_opening_hours_without_humanization_by_area(
85+
area_id: i64,
86+
limit: i64,
87+
pool: &Pool,
88+
) -> Result<Vec<Element>> {
89+
pool.get()
90+
.await?
91+
.interact(move |conn| {
92+
blocking_queries::select_with_opening_hours_without_humanization_by_area(
93+
area_id, limit, conn,
94+
)
95+
})
96+
.await?
97+
}
98+
8499
pub async fn select_by_osm_type_and_id(
85100
osm_type: String,
86101
osm_id: i64,

src/rpc/handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub enum RpcMethod {
5151
SyncElements,
5252
GenerateElementIcons,
5353
GenerateElementCategories,
54-
HumanizeWorkingHours,
54+
HumanizeOpeningHours,
5555
GetElementIssues,
5656
GenerateElementCommentCounts,
5757
// area
@@ -409,9 +409,9 @@ pub async fn handle(
409409
req.id.clone(),
410410
super::generate_element_categories::run(params(req.params)?, &pool).await?,
411411
),
412-
RpcMethod::HumanizeWorkingHours => RpcResponse::from(
412+
RpcMethod::HumanizeOpeningHours => RpcResponse::from(
413413
req.id.clone(),
414-
super::humanize_working_hours::run(params(req.params)?, &pool).await?,
414+
super::humanize_opening_hours::run(params(req.params)?, &pool).await?,
415415
),
416416
RpcMethod::GetElementIssues => RpcResponse::from(
417417
req.id.clone(),
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tracing::info;
1111
#[derive(Deserialize)]
1212
pub struct Params {
1313
max_places: i64,
14+
area: Option<i64>,
1415
}
1516

1617
#[derive(Serialize)]
@@ -29,11 +30,20 @@ pub struct Res {
2930
}
3031

3132
pub async fn run(params: Params, pool: &Pool) -> Result<Res> {
32-
let elements = db::main::element::queries::select_with_opening_hours_without_humanization(
33-
params.max_places,
34-
pool,
35-
)
36-
.await?;
33+
let elements = if let Some(area_id) = params.area {
34+
db::main::element::queries::select_with_opening_hours_without_humanization_by_area(
35+
area_id,
36+
params.max_places,
37+
pool,
38+
)
39+
.await?
40+
} else {
41+
db::main::element::queries::select_with_opening_hours_without_humanization(
42+
params.max_places,
43+
pool,
44+
)
45+
.await?
46+
};
3747

3848
let mut results = Vec::new();
3949

@@ -49,7 +59,7 @@ pub async fn run(params: Params, pool: &Pool) -> Result<Res> {
4959
Source format (OSM opening_hours specification - formal and hard to read):
5060
{}
5161
52-
Target format: Human readable, multi-line string where each day of the week is on a separate line, using full day names in English. Use 24-hour time format.
62+
Target format: Human readable, multi-line string where each day of the week is on a separate line, using full day names in English. Use 24-hour time format. Always list all days. For closed days just print "closed" instead of opening hours.
5363
5464
Example:
5565

src/rpc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod get_trending_communities;
1919
pub mod get_trending_countries;
2020
pub mod get_user_activity;
2121
pub mod handler;
22-
pub mod humanize_working_hours;
22+
pub mod humanize_opening_hours;
2323
pub mod import;
2424
pub mod invoice;
2525
pub mod log;

0 commit comments

Comments
 (0)