Skip to content

Commit

Permalink
editoast: utoipa for /layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristramg committed Nov 22, 2023
1 parent 1c48fa1 commit 3d3365e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 135 deletions.
54 changes: 28 additions & 26 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4399,82 +4399,84 @@ paths:
- infra
/layers/layer/{layer_slug}/mvt/{view_slug}/:
get:
description: Returns layer view metadata to query tiles
parameters:
- in: query
name: infra
required: true
schema:
format: int64
type: integer
- in: path
name: layer_slug
required: true
schema:
title: Layer Slug
type: string
- in: path
name: view_slug
required: true
schema:
title: View Slug
type: string
- in: query
name: infra
required: true
schema:
title: Infra id
type: integer
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/ViewMetadata'
description: Successful Response
summary: Mvt View Metadata
description: ''
summary: Returns layer view metadata to query tiles
tags:
- layers
/layers/tile/{layer_slug}/{view_slug}/{z}/{x}/{y}/:
get:
description: Gets mvt tile from the cache if possible, otherwise gets data from the data base and caches it in redis
parameters:
- in: query
name: infra
required: true
schema:
format: int64
type: integer
- in: path
name: layer_slug
required: true
schema:
title: Layer Slug
type: string
- in: path
name: view_slug
required: true
schema:
title: View Slug
type: string
- in: path
name: z
required: true
schema:
title: Z
type: integer
- in: path
name: x
required: true
schema:
title: X
format: int64
minimum: 0
type: integer
- in: path
name: y
required: true
schema:
title: Y
format: int64
minimum: 0
type: integer
- in: query
name: infra
- in: path
name: z
required: true
schema:
title: Infra
format: int64
minimum: 0
type: integer
responses:
'200':
content:
application/x-octet-stream:
application/octet-stream:
schema:
format: binary
type: string
description: Successful Response
summary: Mvt tile from the cache or the database, cache data in redis if needed
description: ''
summary: Gets mvt tile from the cache if possible, otherwise gets data from the data base and caches it in redis
tags:
- layers
/light_rolling_stock/:
Expand Down
82 changes: 0 additions & 82 deletions editoast/openapi_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,88 +21,6 @@ tags:
description: Train Schedule

paths:
/layers/layer/{layer_slug}/mvt/{view_slug}/:
get:
tags:
- layers
summary: Mvt View Metadata
parameters:
- required: true
schema:
title: Layer Slug
type: string
name: layer_slug
in: path
- required: true
schema:
title: View Slug
type: string
name: view_slug
in: path
- required: true
schema:
title: Infra id
type: integer
name: infra
in: query
responses:
200:
description: Successful Response
content:
application/json:
schema:
$ref: "#/components/schemas/ViewMetadata"

/layers/tile/{layer_slug}/{view_slug}/{z}/{x}/{y}/:
get:
tags:
- layers
summary: Mvt tile from the cache or the database, cache data in redis if needed
parameters:
- required: true
schema:
title: Layer Slug
type: string
name: layer_slug
in: path
- required: true
schema:
title: View Slug
type: string
name: view_slug
in: path
- required: true
schema:
title: Z
type: integer
name: z
in: path
- required: true
schema:
title: X
type: integer
name: x
in: path
- required: true
schema:
title: Y
type: integer
name: y
in: path
- required: true
schema:
title: Infra
type: integer
name: infra
in: query
responses:
200:
description: Successful Response
content:
application/x-octet-stream:
schema:
type: string

/infra/:
get:
tags:
Expand Down
99 changes: 78 additions & 21 deletions editoast/src/views/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ use crate::error::Result;
use crate::map::redis_utils::RedisClient;
use crate::map::{get, get_cache_tile_key, get_view_cache_prefix, set, Layer, MapLayers, Tile};
use crate::DbPool;
use actix_web::dev::HttpServiceFactory;
use actix_web::web::{scope, Data, Json, Path, Query};
use actix_web::web::{Data, Json, Path, Query};
use actix_web::{get, HttpResponse};
use diesel::sql_query;
use diesel::sql_types::Integer;
use diesel_async::RunQueryDsl;
use editoast_derive::EditoastError;
use mvt_utils::{create_and_fill_mvt_tile, get_geo_json_sql_query, GeoJsonAndData};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};
use thiserror::Error;
use utoipa::{IntoParams, ToSchema};

/// Returns `/layers` routes
pub fn routes() -> impl HttpServiceFactory {
scope("/layers").service((layer_view, cache_and_get_mvt_tile))
crate::routes! {
"/layers" => {
"/layer/{layer_slug}/mvt/{view_slug}" => {
layer_view,
},
"/tile/{layer_slug}/{view_slug}/{z}/{x}/{y}" => {
cache_and_get_mvt_tile,
},
}
}

#[derive(Debug, Error, EditoastError)]
Expand Down Expand Up @@ -56,19 +62,53 @@ impl LayersError {
}
}

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, IntoParams)]
#[into_params(parameter_in = Query)]
struct InfraQueryParam {
infra: i64,
}

#[derive(Deserialize, IntoParams)]
#[allow(unused)]
struct LayerViewParams {
layer_slug: String,
view_slug: String,
}

#[derive(Serialize, ToSchema)]
struct ViewMetadata {
#[serde(rename = "type")]
data_type: String,
#[schema(example = "track_sections")]
name: String,
#[serde(rename = "promoteId")]
#[schema(example = json!({track_sections: "id"}))]
promote_id: JsonValue,
#[schema(example = "xyz")]
scheme: String,
#[schema(example = json!(["http://localhost:7070/tile/track_sections/geo/{z}/{x}/{y}/?infra=1"]))]
tiles: Vec<String>,
attribution: String,
minzoom: u64,
#[schema(example = 15)]
maxzoom: u64,
}

/// Returns layer view metadata to query tiles
#[get("/layer/{layer_slug}/mvt/{view_slug}")]
#[utoipa::path(
tag = "layers",
params(InfraQueryParam, LayerViewParams),
responses(
(status = 200, body = ViewMetadata, description = "Successful Response"),
)
)]
#[get("")]
async fn layer_view(
path: Path<(String, String)>,
params: Query<InfraQueryParam>,
map_layers: Data<MapLayers>,
map_layers_config: Data<MapLayersConfig>,
) -> Result<Json<JsonValue>> {
) -> Result<Json<ViewMetadata>> {
let (layer_slug, view_slug) = path.into_inner();
let infra = params.infra;
let layer = match map_layers.layers.get(&layer_slug) {
Expand All @@ -88,20 +128,37 @@ async fn layer_view(
let tiles_url_pattern =
format!("{root_url}layers/tile/{layer_slug}/{view_slug}/{{z}}/{{x}}/{{y}}/?infra={infra}");

Ok(Json(json!({
"type": "vector",
"name": layer_slug,
"promoteId": {layer_slug: layer.id_field},
"scheme": "xyz",
"tiles": [tiles_url_pattern],
"attribution": layer.attribution.clone().unwrap_or_default(),
"minzoom": 5,
"maxzoom": map_layers_config.max_zoom,
})))
Ok(Json(ViewMetadata {
data_type: "vector".to_owned(),
name: layer_slug,
promote_id: json!({"layer_slug": layer.id_field}),
scheme: "xyz".to_owned(),
tiles: vec![tiles_url_pattern],
attribution: layer.attribution.clone().unwrap_or_default(),
minzoom: 5,
maxzoom: map_layers_config.max_zoom,
}))
}

#[derive(Deserialize, IntoParams)]
#[allow(unused)]
struct TileParams {
layer_slug: String,
view_slug: String,
x: u64,
y: u64,
z: u64,
}

/// Gets mvt tile from the cache if possible, otherwise gets data from the data base and caches it in redis
#[get("/tile/{layer_slug}/{view_slug}/{z}/{x}/{y}")]
/// Mvt tile from the cache if possible, otherwise gets data from the database and caches it in redis
#[utoipa::path(
tag = "layers",
params(InfraQueryParam, TileParams),
responses(
(status = 200, body = Vec<u8>, description = "Successful Response"),
)
)]
#[get("")]
async fn cache_and_get_mvt_tile(
path: Path<(String, String, u64, u64, u64)>,
params: Query<InfraQueryParam>,
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn routes_v2() -> Routes<impl HttpServiceFactory> {
rolling_stocks::routes(),
light_rolling_stocks::routes(),
electrical_profiles::routes(),
layers::routes(),
}
routes()
}
Expand All @@ -59,7 +60,6 @@ pub fn routes() -> impl HttpServiceFactory {
services![
routes_v2(),
infra::routes(),
layers::routes(),
pathfinding::routes_v1(),
train_schedule::routes(),
stdcm::routes(),
Expand Down
9 changes: 4 additions & 5 deletions front/src/common/api/osrdEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,21 +955,20 @@ export type GetInfraByIdVoltagesApiArg = {
/** include rolling stocks modes or not */
includeRollingStockModes?: boolean;
};
export type GetLayersLayerByLayerSlugMvtAndViewSlugApiResponse =
/** status 200 Successful Response */ ViewMetadata;
export type GetLayersLayerByLayerSlugMvtAndViewSlugApiResponse = /** status 200 */ ViewMetadata;
export type GetLayersLayerByLayerSlugMvtAndViewSlugApiArg = {
infra: number;
layerSlug: string;
viewSlug: string;
infra: number;
};
export type GetLayersTileByLayerSlugAndViewSlugZXYApiResponse = unknown;
export type GetLayersTileByLayerSlugAndViewSlugZXYApiArg = {
infra: number;
layerSlug: string;
viewSlug: string;
z: number;
x: number;
y: number;
infra: number;
z: number;
};
export type GetLightRollingStockApiResponse =
/** status 200 */ PaginatedResponseOfLightRollingStockWithLiveries;
Expand Down

0 comments on commit 3d3365e

Please sign in to comment.