Skip to content

Commit

Permalink
editoast: split track section endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sim51 committed May 16, 2024
1 parent 31078d8 commit 21906b8
Show file tree
Hide file tree
Showing 15 changed files with 1,104 additions and 57 deletions.
8 changes: 4 additions & 4 deletions editoast/editoast_schemas/src/infra/neutral_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ pub struct NeutralSectionExtensions {
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct NeutralSectionNeutralSncfExtension {
announcement: Vec<Sign>,
exe: Sign,
end: Vec<Sign>,
rev: Vec<Sign>,
pub announcement: Vec<Sign>,
pub exe: Sign,
pub end: Vec<Sign>,
pub rev: Vec<Sign>,
}

impl OSRDTyped for NeutralSection {
Expand Down
58 changes: 58 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,36 @@ components:
- status
- message
type: object
EditoastEditionErrorSplitTrackSectionBadOffset:
properties:
context:
properties:
infra_id:
type: integer
tracksection_id:
type: string
tracksection_length:
type: number
required:
- infra_id
- tracksection_id
- tracksection_length
type: object
message:
type: string
status:
enum:
- 400
type: integer
type:
enum:
- editoast:infra:edition:SplitTrackSectionBadOffset
type: string
required:
- type
- status
- message
type: object
EditoastEditoastUrlErrorInvalidUrl:
properties:
context:
Expand Down Expand Up @@ -828,6 +858,7 @@ components:
- $ref: '#/components/schemas/EditoastCoreErrorUnparsableErrorOutput'
- $ref: '#/components/schemas/EditoastDocumentErrorsNotFound'
- $ref: '#/components/schemas/EditoastEditionErrorInfraIsLocked'
- $ref: '#/components/schemas/EditoastEditionErrorSplitTrackSectionBadOffset'
- $ref: '#/components/schemas/EditoastEditoastUrlErrorInvalidUrl'
- $ref: '#/components/schemas/EditoastElectricalProfilesErrorNotFound'
- $ref: '#/components/schemas/EditoastGeometryErrorUnexpectedGeometry'
Expand Down Expand Up @@ -9989,6 +10020,33 @@ paths:
summary: Returns the set of speed limit tags for a given infra
tags:
- infra
/infra/{infra_id}/split_track_section/:
post:
parameters:
- description: An existing infra ID
in: path
name: infra_id
required: true
schema:
format: int64
type: integer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TrackOffset'
required: true
responses:
'200':
content:
application/json:
schema:
items:
type: string
type: array
description: ID of the trackSections created
tags:
- infra
/infra/{infra_id}/switch_types/:
get:
parameters:
Expand Down
8 changes: 4 additions & 4 deletions editoast/src/generated_data/sql/insert_neutral_sign_layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WITH signs AS (
FROM infra_object_neutral_section
WHERE infra_id = $1
AND obj_id = ANY($2)
AND infra_object_neutral_section.data @ ? '$.extensions.neutral_sncf.announcement'
AND infra_object_neutral_section.data @? '$.extensions.neutral_sncf.announcement'
UNION
SELECT obj_id AS sc_id,
(
Expand All @@ -41,7 +41,7 @@ WITH signs AS (
FROM infra_object_neutral_section
WHERE infra_id = $1
AND obj_id = ANY($2)
AND infra_object_neutral_section.data @ ? '$.extensions.neutral_sncf.rev'
AND infra_object_neutral_section.data @? '$.extensions.neutral_sncf.rev'
UNION
SELECT obj_id AS sc_id,
(
Expand All @@ -57,7 +57,7 @@ WITH signs AS (
FROM infra_object_neutral_section
WHERE infra_id = $1
AND obj_id = ANY($2)
AND infra_object_neutral_section.data @ ? '$.extensions.neutral_sncf.end'
AND infra_object_neutral_section.data @? '$.extensions.neutral_sncf.end'
UNION
SELECT obj_id AS sc_id,
(
Expand All @@ -71,7 +71,7 @@ WITH signs AS (
FROM infra_object_neutral_section
WHERE infra_id = $1
AND obj_id = ANY($2)
AND infra_object_neutral_section.data @ ? '$.extensions.neutral_sncf.exe'
AND infra_object_neutral_section.data @? '$.extensions.neutral_sncf.exe'
),
collect AS (
SELECT signs.sc_id,
Expand Down
42 changes: 41 additions & 1 deletion editoast/src/infra_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use editoast_schemas::infra::DoubleSlipSwitch;
use editoast_schemas::infra::Electrification;
use editoast_schemas::infra::Endpoint;
use editoast_schemas::infra::Link;
use editoast_schemas::infra::NeutralSection;
use editoast_schemas::infra::OperationalPointPart;
use editoast_schemas::infra::PointSwitch;
use editoast_schemas::infra::Route;
Expand Down Expand Up @@ -89,14 +90,15 @@ pub enum ObjectCache {
OperationalPoint(OperationalPointCache),
SwitchType(SwitchType),
Electrification(Electrification),
NeutralSection(NeutralSection),
}

impl From<RailjsonObject> for ObjectCache {
fn from(railjson: RailjsonObject) -> Self {
match railjson {
RailjsonObject::TrackSection { railjson } => ObjectCache::TrackSection(railjson.into()),
RailjsonObject::Signal { railjson } => ObjectCache::Signal(railjson.into()),
RailjsonObject::NeutralSection { .. } => unimplemented!(),
RailjsonObject::NeutralSection { railjson } => ObjectCache::NeutralSection(railjson),
RailjsonObject::SpeedSection { railjson } => ObjectCache::SpeedSection(railjson),
RailjsonObject::Switch { railjson } => ObjectCache::Switch(railjson.into()),
RailjsonObject::SwitchType { railjson } => ObjectCache::SwitchType(railjson),
Expand Down Expand Up @@ -130,6 +132,7 @@ impl OSRDIdentified for ObjectCache {
ObjectCache::OperationalPoint(obj) => obj.get_id(),
ObjectCache::SwitchType(obj) => obj.get_id(),
ObjectCache::Electrification(obj) => obj.get_id(),
ObjectCache::NeutralSection(obj) => obj.get_id(),
}
}
}
Expand All @@ -147,6 +150,7 @@ impl OSRDObject for ObjectCache {
ObjectCache::OperationalPoint(_) => ObjectType::OperationalPoint,
ObjectCache::SwitchType(_) => ObjectType::SwitchType,
ObjectCache::Electrification(_) => ObjectType::Electrification,
ObjectCache::NeutralSection(_) => ObjectType::NeutralSection,
}
}
}
Expand All @@ -166,6 +170,9 @@ impl ObjectCache {
ObjectCache::Electrification(electrification) => {
electrification.get_track_referenced_id()
}
ObjectCache::NeutralSection(neutral_section) => {
neutral_section.get_track_referenced_id()
}
}
}

Expand Down Expand Up @@ -248,6 +255,14 @@ impl ObjectCache {
_ => panic!("ObjectCache is not a Electrification"),
}
}

/// Unwrap a neutral section from the object cache
pub fn unwrap_neutral_section(&self) -> &NeutralSection {
match self {
ObjectCache::NeutralSection(neutral_section) => neutral_section,
_ => panic!("ObjectCache is not a NeutralSection"),
}
}
}

#[derive(QueryableByName, Debug, Clone)]
Expand Down Expand Up @@ -376,6 +391,11 @@ impl InfraCache {
&self.objects[ObjectType::SpeedSection]
}

/// Retrieve the cache of neutral sections
pub fn neutral_sections(&self) -> &HashMap<String, ObjectCache> {
&self.objects[ObjectType::NeutralSection]
}

/// Retrieve the cache of routes
pub fn routes(&self) -> &HashMap<String, ObjectCache> {
&self.objects[ObjectType::Route]
Expand Down Expand Up @@ -442,6 +462,12 @@ impl InfraCache {
.into_iter()
.try_for_each(|speed| infra_cache.add(speed))?;

// Load speed sections tracks references
find_all_schemas::<NeutralSection, Vec<_>>(conn, infra_id)
.await?
.into_iter()
.try_for_each(|neutralsection| infra_cache.add(neutralsection))?;

// Load routes tracks references
find_all_schemas::<_, Vec<Route>>(conn, infra_id)
.await?
Expand Down Expand Up @@ -594,6 +620,9 @@ impl InfraCache {
ObjectCache::Electrification(electrification) => {
self.add::<Electrification>(electrification)?
}
ObjectCache::NeutralSection(neutral_section) => {
self.add::<NeutralSection>(neutral_section)?
}
}
Ok(())
}
Expand Down Expand Up @@ -643,6 +672,17 @@ impl InfraCache {
.unwrap_speed_section())
}

pub fn get_neutral_section(&self, neutral_section_id: &str) -> Result<&NeutralSection> {
Ok(self
.neutral_sections()
.get(neutral_section_id)
.ok_or_else(|| InfraCacheEditoastError::ObjectNotFound {
obj_type: ObjectType::NeutralSection.to_string(),
obj_id: neutral_section_id.to_string(),
})?
.unwrap_neutral_section())
}

pub fn get_detector(&self, detector_id: &str) -> Result<&DetectorCache> {
Ok(self
.detectors()
Expand Down
8 changes: 2 additions & 6 deletions editoast/src/infra_cache/object_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod buffer_stop_cache;
mod detector_cache;
mod electrification_cache;
mod neutral_section_cache;
mod operational_point_cache;
mod route_cache;
mod signal_cache;
Expand All @@ -12,12 +13,7 @@ mod track_section_cache;
pub use buffer_stop_cache::BufferStopCache;
pub use detector_cache::DetectorCache;
pub use operational_point_cache::OperationalPointCache;
pub use operational_point_cache::OperationalPointPartCache;
pub use signal_cache::SignalCache;
pub use switch_cache::SwitchCache;
pub use track_section_cache::TrackSectionCache;

cfg_if! {
if #[cfg(test)] {
pub use operational_point_cache::OperationalPointPartCache;
}
}
21 changes: 21 additions & 0 deletions editoast/src/infra_cache/object_cache/neutral_section_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::infra_cache::Cache;
use crate::infra_cache::ObjectCache;
use editoast_schemas::infra::NeutralSection;

impl Cache for NeutralSection {
fn get_track_referenced_id(&self) -> Vec<&String> {
let mut res: Vec<_> = self.track_ranges.iter().map(|tr| &*tr.track).collect();
res.extend(self.announcement_track_ranges.iter().map(|tr| &*tr.track));
if let Some(ext) = &self.extensions.neutral_sncf {
res.push(&*ext.exe.track);
res.extend(ext.announcement.iter().map(|sign| &*sign.track));
res.extend(ext.end.iter().map(|sign| &*sign.track));
res.extend(ext.rev.iter().map(|sign| &*sign.track));
}
res
}

fn get_object_cache(&self) -> ObjectCache {
ObjectCache::NeutralSection(self.clone())
}
}
8 changes: 8 additions & 0 deletions editoast/src/infra_cache/operation/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ impl From<SpeedSection> for RailjsonObject {
}
}

impl From<NeutralSection> for RailjsonObject {
fn from(neutralsection: NeutralSection) -> Self {
RailjsonObject::NeutralSection {
railjson: neutralsection,
}
}
}

impl From<Switch> for RailjsonObject {
fn from(switch: Switch) -> Self {
RailjsonObject::Switch { railjson: switch }
Expand Down
2 changes: 0 additions & 2 deletions editoast/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate cfg_if;

mod client;
mod core;
Expand Down
13 changes: 6 additions & 7 deletions editoast/src/views/infra/auto_fixes/electrifications.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use serde_json::from_value;
use serde_json::json;
use json_patch::Patch;
use json_patch::PatchOperation;
use json_patch::RemoveOperation;
use std::collections::HashMap;
use tracing::debug;
use tracing::error;
Expand Down Expand Up @@ -53,11 +54,9 @@ pub fn fix_electrification(
Operation::Update(UpdateOperation {
obj_id: electrification.get_id().clone(),
obj_type: electrification.get_type(),
railjson_patch: from_value(json!([{
"op": "remove",
"path": format!("/track_ranges/{track_range_idx}"),
}]))
.unwrap(),
railjson_patch: Patch(vec![PatchOperation::Remove(RemoveOperation {
path: format!("/track_ranges/{track_range_idx}").parse().unwrap(),
})]),
})
}
OrderedOperation::Delete => {
Expand Down
13 changes: 6 additions & 7 deletions editoast/src/views/infra/auto_fixes/speed_section.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use serde_json::from_value;
use serde_json::json;
use json_patch::Patch;
use json_patch::PatchOperation;
use json_patch::RemoveOperation;
use std::collections::HashMap;
use tracing::debug;
use tracing::error;
Expand Down Expand Up @@ -53,11 +54,9 @@ pub fn fix_speed_section(
Operation::Update(UpdateOperation {
obj_id: speed_section.get_id().clone(),
obj_type: speed_section.get_type(),
railjson_patch: from_value(json!([{
"op": "remove",
"path": format!("/track_ranges/{track_range_idx}"),
}]))
.unwrap(),
railjson_patch: Patch(vec![PatchOperation::Remove(RemoveOperation {
path: format!("/track_ranges/{track_range_idx}").parse().unwrap(),
})]),
})
}
OrderedOperation::Delete => {
Expand Down
Loading

0 comments on commit 21906b8

Please sign in to comment.