From a9cf508c331ca85466da01de698cd65cb20c4f8d Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Tue, 12 Oct 2021 19:01:25 +0300 Subject: [PATCH] refactor: improve debug logging (#264) --- README.md | 2 +- src/bin/main.rs | 13 +++++++------ src/composite_source.rs | 2 +- src/config.rs | 2 +- src/db.rs | 24 +++++++++++++----------- src/function_source.rs | 9 ++++++--- src/server.rs | 21 +++++++++++++-------- src/table_source.rs | 11 +++++++---- src/utils.rs | 4 ++-- 9 files changed, 51 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 092df600..b5231da9 100755 --- a/README.md +++ b/README.md @@ -277,7 +277,7 @@ DECLARE BEGIN SELECT INTO mvt ST_AsMVT(tile, 'public.function_source', 4096, 'geom') FROM ( SELECT - ST_AsMVTGeom(ST_Transform(geom, 3857), TileBBox(z, x, y, 3857), 4096, 64, true) AS geom + ST_AsMVTGeom(ST_Transform(ST_CurveToLine(geom), 3857), TileBBox(z, x, y, 3857), 4096, 64, true) AS geom FROM public.table_source WHERE geom && TileBBox(z, x, y, 4326) ) as tile WHERE geom IS NOT NULL; diff --git a/src/bin/main.rs b/src/bin/main.rs index 9c9ad87d..ca45aecc 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -78,14 +78,14 @@ pub fn generate_config(args: Args, pool: &Pool) -> io::Result { } fn setup_from_config(file_name: String) -> io::Result<(Config, Pool)> { - let config = read_config(&file_name).map_err(prettify_error("Can't read config"))?; + let config = read_config(&file_name).map_err(prettify_error("Can't read config".to_owned()))?; let pool = setup_connection_pool( &config.connection_string, Some(config.pool_size), config.danger_accept_invalid_certs, ) - .map_err(prettify_error("Can't setup connection pool"))?; + .map_err(prettify_error("Can't setup connection pool".to_owned()))?; if let Some(table_sources) = &config.table_sources { for table_source in table_sources.values() { @@ -127,10 +127,11 @@ fn setup_from_args(args: Args) -> io::Result<(Config, Pool)> { args.flag_pool_size, args.flag_danger_accept_invalid_certs, ) - .map_err(prettify_error("Can't setup connection pool"))?; + .map_err(prettify_error("Can't setup connection pool".to_owned()))?; info!("Scanning database"); - let config = generate_config(args, &pool).map_err(prettify_error("Can't generate config"))?; + let config = + generate_config(args, &pool).map_err(prettify_error("Can't generate config".to_owned()))?; Ok((config, pool)) } @@ -168,7 +169,7 @@ fn start(args: Args) -> io::Result { }; let matches = check_postgis_version(REQUIRED_POSTGIS_VERSION, &pool) - .map_err(prettify_error("Can't check PostGIS version"))?; + .map_err(prettify_error("Can't check PostGIS version".to_owned()))?; if !matches { std::process::exit(-1); @@ -187,7 +188,7 @@ fn main() -> io::Result<()> { let args = Docopt::new(USAGE) .and_then(|d| d.deserialize::()) - .map_err(prettify_error("Can't parse CLI arguments"))?; + .map_err(prettify_error("Can't parse CLI arguments".to_owned()))?; let args = parse_env(args); diff --git a/src/composite_source.rs b/src/composite_source.rs index b08ab49b..044683f7 100644 --- a/src/composite_source.rs +++ b/src/composite_source.rs @@ -93,7 +93,7 @@ impl Source for CompositeSource { let tile: Tile = conn .query_one(tile_query.as_str(), &[]) .map(|row| row.get("tile")) - .map_err(prettify_error("Can't get composite source tile"))?; + .map_err(prettify_error("Can't get composite source tile".to_owned()))?; Ok(tile) } diff --git a/src/config.rs b/src/config.rs index 4caad6e0..20d299f5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,7 +57,7 @@ pub fn read_config(file_name: &str) -> io::Result { file.read_to_string(&mut contents)?; let config_builder: ConfigBuilder = serde_yaml::from_str(contents.as_str()) - .map_err(prettify_error("Can't read config file"))?; + .map_err(prettify_error("Can't read config file".to_owned()))?; Ok(config_builder.finalize()) } diff --git a/src/db.rs b/src/db.rs index 46e4f32d..28e4ee63 100755 --- a/src/db.rs +++ b/src/db.rs @@ -18,7 +18,7 @@ fn make_tls_connector(danger_accept_invalid_certs: bool) -> io::Result io::Result { let config = postgres::config::Config::from_str(cn_str) - .map_err(prettify_error("Can't parse connection string"))?; + .map_err(prettify_error("Can't parse connection string".to_owned()))?; let tls_connector = make_tls_connector(danger_accept_invalid_certs)?; let manager = PostgresConnectionManager::new(config, tls_connector); @@ -38,15 +38,15 @@ pub fn setup_connection_pool( let pool = r2d2::Pool::builder() .max_size(pool_size.unwrap_or(20)) .build(manager) - .map_err(prettify_error("Can't build connection pool"))?; + .map_err(prettify_error("Can't build connection pool".to_owned()))?; Ok(pool) } pub fn get_connection(pool: &Pool) -> io::Result { - let connection = pool - .get() - .map_err(prettify_error("Can't retrieve connection from the pool"))?; + let connection = pool.get().map_err(prettify_error( + "Can't retrieve connection from the pool".to_owned(), + ))?; Ok(connection) } @@ -57,7 +57,7 @@ pub fn select_postgis_verion(pool: &Pool) -> io::Result { let version = connection .query_one(include_str!("scripts/get_postgis_version.sql"), &[]) .map(|row| row.get::<_, String>("postgis_version")) - .map_err(prettify_error("Can't get PostGIS version"))?; + .map_err(prettify_error("Can't get PostGIS version".to_owned()))?; Ok(version) } @@ -65,11 +65,13 @@ pub fn select_postgis_verion(pool: &Pool) -> io::Result { pub fn check_postgis_version(required_postgis_version: &str, pool: &Pool) -> io::Result { let postgis_version = select_postgis_verion(pool)?; - let req = VersionReq::parse(required_postgis_version) - .map_err(prettify_error("Can't parse required PostGIS version"))?; + let req = VersionReq::parse(required_postgis_version).map_err(prettify_error( + "Can't parse required PostGIS version".to_owned(), + ))?; - let version = Version::parse(postgis_version.as_str()) - .map_err(prettify_error("Can't parse database PostGIS version"))?; + let version = Version::parse(postgis_version.as_str()).map_err(prettify_error( + "Can't parse database PostGIS version".to_owned(), + ))?; let matches = req.matches(&version); diff --git a/src/function_source.rs b/src/function_source.rs index aa13be58..737a832b 100644 --- a/src/function_source.rs +++ b/src/function_source.rs @@ -67,13 +67,16 @@ impl Source for FunctionSource { &[Type::INT4, Type::INT4, Type::INT4, Type::JSON], ) .map_err(prettify_error( - "Can't create prepared statement for the tile", + "Can't create prepared statement for the tile".to_owned(), ))?; let tile = conn .query_one(&query, &[&xyz.x, &xyz.y, &xyz.z, &query_json]) .map(|row| row.get(self.function.as_str())) - .map_err(prettify_error("Can't get function source tile"))?; + .map_err(prettify_error(format!( + "Can't get \"{}\" tile at /{}/{}/{} with {:?} params", + self.id, &xyz.z, &xyz.x, &xyz.z, &query_json + )))?; Ok(tile) } @@ -84,7 +87,7 @@ pub fn get_function_sources(conn: &mut Connection) -> Result(e: T) -> Error { + error!("{}", e.to_string()); + error::ErrorInternalServerError(e.to_string()) +} + async fn get_health() -> Result { let response = HttpResponse::Ok().body("OK"); Ok(response) @@ -77,8 +82,8 @@ async fn get_table_sources(state: web::Data) -> Result Ok(HttpResponse::NoContent() @@ -218,8 +223,8 @@ async fn get_function_sources(state: web::Data) -> Result Ok(HttpResponse::NoContent() diff --git a/src/table_source.rs b/src/table_source.rs index 1d2ed934..d57336cf 100644 --- a/src/table_source.rs +++ b/src/table_source.rs @@ -31,7 +31,7 @@ impl TableSource { let mercator_bounds = utils::tilebbox(xyz); let properties = if self.properties.is_empty() { - "".to_string() + String::new() } else { let properties = self .properties @@ -111,7 +111,10 @@ impl Source for TableSource { let tile: Tile = conn .query_one(tile_query.as_str(), &[]) .map(|row| row.get("st_asmvt")) - .map_err(utils::prettify_error("Can't get table source tile"))?; + .map_err(utils::prettify_error(format!( + "Can't get \"{}\" tile at /{}/{}/{}", + self.id, &xyz.z, &xyz.x, &xyz.z + )))?; Ok(tile) } @@ -126,7 +129,7 @@ pub fn get_table_sources(conn: &mut Connection) -> Result Result(message: &'static str) -> impl Fn(E) -> std::io::Error { +pub fn prettify_error(message: String) -> impl Fn(E) -> std::io::Error { move |error| std::io::Error::new(std::io::ErrorKind::Other, format!("{}: {}", message, error)) } @@ -35,7 +35,7 @@ pub fn json_to_hashmap(value: &serde_json::Value) -> HashMap { let object = value.as_object().unwrap(); for (key, value) in object { let string_value = value.as_str().unwrap(); - hashmap.insert(key.to_string(), string_value.to_string()); + hashmap.insert(key.to_owned(), string_value.to_owned()); } hashmap