Refactor the server
This commit is contained in:
parent
3b9ad11d71
commit
01ec17ff04
15 changed files with 939 additions and 1226 deletions
198
server-rs/src/routes/properties.rs
Normal file
198
server-rs/src/routes/properties.rs
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::Query;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::Json;
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::filter::{parse_filters, row_passes_filters};
|
||||
use crate::state::AppState;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct HexagonPropertiesParams {
|
||||
pub h3: String,
|
||||
pub resolution: u8,
|
||||
pub filters: Option<String>,
|
||||
pub limit: Option<usize>,
|
||||
pub offset: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Property {
|
||||
// String fields
|
||||
pub address: Option<String>,
|
||||
pub postcode: Option<String>,
|
||||
pub property_type: Option<String>,
|
||||
pub built_form: Option<String>,
|
||||
pub duration: Option<String>,
|
||||
pub current_energy_rating: Option<String>,
|
||||
pub potential_energy_rating: Option<String>,
|
||||
|
||||
// Numeric fields
|
||||
pub lat: f64,
|
||||
pub lon: f64,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub features: FxHashMap<String, f64>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct HexagonPropertiesResponse {
|
||||
pub properties: Vec<Property>,
|
||||
pub total: usize,
|
||||
pub limit: usize,
|
||||
pub offset: usize,
|
||||
pub truncated: bool,
|
||||
}
|
||||
|
||||
pub async fn get_hexagon_properties(
|
||||
state: Arc<AppState>,
|
||||
Query(params): Query<HexagonPropertiesParams>,
|
||||
) -> Result<Json<HexagonPropertiesResponse>, (StatusCode, String)> {
|
||||
let cell = h3o::CellIndex::from_str(¶ms.h3)
|
||||
.map_err(|e| {
|
||||
warn!(h3 = %params.h3, error = %e, "Invalid H3 cell index");
|
||||
(StatusCode::BAD_REQUEST, format!("Invalid H3 cell: {}", e))
|
||||
})?;
|
||||
let cell_u64: u64 = cell.into();
|
||||
|
||||
let resolution = params.resolution as usize;
|
||||
if resolution >= state.h3_cells.len() || state.h3_cells[resolution].is_empty() {
|
||||
warn!(resolution, "Invalid or non-precomputed resolution for hexagon-properties");
|
||||
return Err((
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Invalid or non-precomputed resolution".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let h3_str = params.h3.clone();
|
||||
let filters_str = params.filters.clone();
|
||||
let (parsed_filters, parsed_enum_filters) = parse_filters(
|
||||
params.filters.as_deref(),
|
||||
&state.data.feature_names,
|
||||
&state.data.enum_features,
|
||||
);
|
||||
let num_filters = parsed_filters.len() + parsed_enum_filters.len();
|
||||
|
||||
let result = tokio::task::spawn_blocking(move || {
|
||||
let t0 = std::time::Instant::now();
|
||||
let h3_data = &state.h3_cells[resolution];
|
||||
let num_features = state.data.num_features;
|
||||
let feature_data = &state.data.feature_data;
|
||||
let enum_features = &state.data.enum_features;
|
||||
|
||||
let matching_rows: Vec<usize> = h3_data
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(idx, &h3_cell)| {
|
||||
if h3_cell == cell_u64 {
|
||||
if row_passes_filters(
|
||||
idx,
|
||||
&parsed_filters,
|
||||
&parsed_enum_filters,
|
||||
feature_data,
|
||||
num_features,
|
||||
enum_features,
|
||||
) {
|
||||
Some(idx)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let total = matching_rows.len();
|
||||
let limit = params.limit.unwrap_or(100).min(500);
|
||||
let offset = params.offset.unwrap_or(0);
|
||||
let truncated = total > offset + limit;
|
||||
|
||||
let properties: Vec<Property> = matching_rows
|
||||
.iter()
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.map(|&row| {
|
||||
let mut features = FxHashMap::default();
|
||||
let base = row * num_features;
|
||||
for (feat_idx, feat_name) in state.data.feature_names.iter().enumerate() {
|
||||
let v = feature_data[base + feat_idx];
|
||||
if v.is_finite() {
|
||||
features.insert(feat_name.clone(), v);
|
||||
}
|
||||
}
|
||||
|
||||
let get_string = |s: &str| -> Option<String> {
|
||||
let trimmed = s.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let get_enum_value = |names: &[&str]| -> Option<String> {
|
||||
for name in names {
|
||||
if let Some(val) = enum_features.iter().find_map(|ef| {
|
||||
if ef.name == *name {
|
||||
let idx = ef.data[row];
|
||||
if idx == 255 {
|
||||
None
|
||||
} else {
|
||||
ef.values.get(idx as usize).cloned()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
None
|
||||
};
|
||||
|
||||
Property {
|
||||
address: get_string(&state.data.address[row]),
|
||||
postcode: get_string(&state.data.postcode[row]),
|
||||
property_type: get_enum_value(&["Property type", "epc_property_type", "pp_property_type"]),
|
||||
built_form: get_enum_value(&["Property type/built form", "built_form"]),
|
||||
duration: get_enum_value(&["Leashold/Freehold", "duration"]),
|
||||
current_energy_rating: get_enum_value(&["Current energy rating", "current_energy_rating"]),
|
||||
potential_energy_rating: get_enum_value(&["Potential energy rating", "potential_energy_rating"]),
|
||||
lat: state.data.lat[row],
|
||||
lon: state.data.lon[row],
|
||||
features,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let elapsed = t0.elapsed();
|
||||
info!(
|
||||
h3 = %h3_str,
|
||||
resolution,
|
||||
total,
|
||||
returned = properties.len(),
|
||||
offset,
|
||||
filters = num_filters,
|
||||
filters_raw = filters_str.as_deref().unwrap_or("-"),
|
||||
ms = format_args!("{:.1}", elapsed.as_secs_f64() * 1000.0),
|
||||
"GET /api/hexagon-properties"
|
||||
);
|
||||
|
||||
HexagonPropertiesResponse {
|
||||
properties,
|
||||
total,
|
||||
limit,
|
||||
offset,
|
||||
truncated,
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue