Skip to content

Commit

Permalink
editoast: add ci code to operational point search
Browse files Browse the repository at this point in the history
  • Loading branch information
leovalais committed Dec 19, 2023
1 parent e3b89a8 commit 7a6c048
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE infra_object_operational_point
RENAME TO infra_object_operationalpoint;

ALTER TRIGGER search_operational_point__ins_trig ON infra_object_operational_point
RENAME TO search_operationalpoint__ins_trig;

ALTER TRIGGER search_operational_point__upd_trig ON infra_object_operational_point
RENAME TO search_operationalpoint__upd_trig;

ALTER FUNCTION search_operational_point__ins_trig_fun ()
RENAME TO search_operationalpoint__ins_trig_fun;

ALTER FUNCTION search_operational_point__upd_trig_fun ()
RENAME TO search_operationalpoint__upd_trig_fun;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE infra_object_operationalpoint
RENAME TO infra_object_operational_point;

ALTER TRIGGER search_operationalpoint__ins_trig ON infra_object_operational_point
RENAME TO search_operational_point__ins_trig;

ALTER TRIGGER search_operationalpoint__upd_trig ON infra_object_operational_point
RENAME TO search_operational_point__upd_trig;

ALTER FUNCTION search_operationalpoint__ins_trig_fun ()
RENAME TO search_operational_point__ins_trig_fun;

ALTER FUNCTION search_operationalpoint__upd_trig_fun ()
RENAME TO search_operational_point__upd_trig_fun;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- DO NOT EDIT THIS FILE MANUALLY!

DROP TABLE IF EXISTS "search_operationalpoint";
DROP TRIGGER IF EXISTS search_operationalpoint__ins_trig ON "infra_object_operational_point";
DROP TRIGGER IF EXISTS search_operationalpoint__upd_trig ON "infra_object_operational_point";
DROP FUNCTION IF EXISTS search_operationalpoint__ins_trig_fun;
DROP FUNCTION IF EXISTS search_operationalpoint__upd_trig_fun;
85 changes: 85 additions & 0 deletions editoast/migrations/2023-12-19-122428_search_op_ci_code/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- DO NOT EDIT THIS FILE MANUALLY!
-- To change the migration's content, use `editoast search make-migration`.
-- To add custom SQL code, check out `#[derive(Search)]` attributes `prepend_sql` and `append_sql`.

DROP TABLE IF EXISTS "search_operationalpoint";

CREATE TABLE "search_operationalpoint" (
id BIGINT PRIMARY KEY REFERENCES "infra_object_operational_point"("id") ON UPDATE CASCADE ON DELETE CASCADE,
"obj_id" varchar(255),
"infra_id" integer,
"uic" integer,
"trigram" varchar(3),
"ci" integer,
"ch" text,
"name" text
);

CREATE INDEX "search_operationalpoint_obj_id" ON "search_operationalpoint" ("obj_id");
CREATE INDEX "search_operationalpoint_infra_id" ON "search_operationalpoint" ("infra_id");
CREATE INDEX "search_operationalpoint_uic" ON "search_operationalpoint" ("uic");
CREATE INDEX "search_operationalpoint_trigram" ON "search_operationalpoint" ("trigram");
CREATE INDEX "search_operationalpoint_ci" ON "search_operationalpoint" ("ci");
CREATE INDEX "search_operationalpoint_ch" ON "search_operationalpoint" ("ch");
CREATE INDEX "search_operationalpoint_name" ON "search_operationalpoint" USING gin ("name" gin_trgm_ops);

CREATE OR REPLACE FUNCTION search_operationalpoint__ins_trig_fun()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO "search_operationalpoint" (id, obj_id, infra_id, uic, trigram, ci, ch, name)
SELECT "infra_object_operational_point".id AS id, (infra_object_operational_point.obj_id) AS obj_id,
(infra_object_operational_point.infra_id) AS infra_id,
((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer) AS uic,
(infra_object_operational_point.data->'extensions'->'sncf'->>'trigram') AS trigram,
((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer) AS ci,
(infra_object_operational_point.data->'extensions'->'sncf'->>'ch') AS ch,
osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name') AS name
FROM (SELECT NEW.*) AS "infra_object_operational_point"
;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER search_operationalpoint__ins_trig
AFTER INSERT ON "infra_object_operational_point"
FOR EACH ROW EXECUTE FUNCTION search_operationalpoint__ins_trig_fun();


CREATE OR REPLACE FUNCTION search_operationalpoint__upd_trig_fun()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE "search_operationalpoint"
SET "obj_id" = (infra_object_operational_point.obj_id),
"infra_id" = (infra_object_operational_point.infra_id),
"uic" = ((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer),
"trigram" = (infra_object_operational_point.data->'extensions'->'sncf'->>'trigram'),
"ci" = ((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer),
"ch" = (infra_object_operational_point.data->'extensions'->'sncf'->>'ch'),
"name" = osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name')
FROM (SELECT NEW.*) AS "infra_object_operational_point"

WHERE "infra_object_operational_point".id = "search_operationalpoint".id;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER search_operationalpoint__upd_trig
AFTER UPDATE ON "infra_object_operational_point"
FOR EACH ROW EXECUTE FUNCTION search_operationalpoint__upd_trig_fun();



INSERT INTO "search_operationalpoint"
SELECT
"infra_object_operational_point"."id" AS id,
(infra_object_operational_point.obj_id) AS obj_id
, (infra_object_operational_point.infra_id) AS infra_id
, ((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer) AS uic
, (infra_object_operational_point.data->'extensions'->'sncf'->>'trigram') AS trigram
, ((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer) AS ci
, (infra_object_operational_point.data->'extensions'->'sncf'->>'ch') AS ch
, osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name') AS name
FROM "infra_object_operational_point"
;
5 changes: 5 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,10 @@ components:
properties:
ch:
type: string
ci:
format: int64
minimum: 0
type: integer
geographic:
$ref: '#/components/schemas/GeoJsonPoint'
infra_id:
Expand Down Expand Up @@ -2424,6 +2428,7 @@ components:
- name
- trigram
- ch
- ci
- geographic
- schematic
- track_sections
Expand Down
20 changes: 20 additions & 0 deletions editoast/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;

search_operationalpoint (id) {
id -> Int8,
#[max_length = 255]
obj_id -> Nullable<Varchar>,
infra_id -> Nullable<Int4>,
uic -> Nullable<Int4>,
#[max_length = 3]
trigram -> Nullable<Varchar>,
ci -> Nullable<Int4>,
ch -> Nullable<Text>,
name -> Nullable<Text>,
}
}

diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
Expand Down Expand Up @@ -674,6 +692,7 @@ diesel::joinable!(scenario -> infra (infra_id));
diesel::joinable!(scenario -> study (study_id));
diesel::joinable!(scenario -> timetable (timetable_id));
diesel::joinable!(search_operational_point -> infra_object_operational_point (id));
diesel::joinable!(search_operationalpoint -> infra_object_operational_point (id));
diesel::joinable!(search_project -> project (id));
diesel::joinable!(search_scenario -> scenario (id));
diesel::joinable!(search_signal -> infra_object_signal (id));
Expand Down Expand Up @@ -717,6 +736,7 @@ diesel::allow_tables_to_appear_in_same_query!(
rolling_stock_separate_image,
scenario,
search_operational_point,
search_operationalpoint,
search_project,
search_scenario,
search_signal,
Expand Down
45 changes: 39 additions & 6 deletions editoast/src/views/search/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,47 @@ pub(super) struct SearchResultItemTrack {
#[search(
name = "operationalpoint",
table = "search_operational_point",
migration(src_table = "infra_object_operational_point"),
joins = "
INNER JOIN infra_object_operational_point AS OP ON OP.id = search_operational_point.id
INNER JOIN (SELECT DISTINCT ON (infra_id, obj_id) * FROM infra_layer_operational_point)
AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id",
column(name = "obj_id", data_type = "string"),
column(name = "infra_id", data_type = "integer"),
column(name = "name", data_type = "string"),
column(name = "uic", data_type = "integer"),
column(name = "ch", data_type = "string"),
column(name = "trigram", data_type = "string")
column(
name = "obj_id",
data_type = "varchar(255)",
sql = "infra_object_operational_point.obj_id",
),
column(
name = "infra_id",
data_type = "integer",
sql = "infra_object_operational_point.infra_id",
),
column(
name = "uic",
data_type = "integer",
sql = "(infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer",
),
column(
name = "trigram",
data_type = "varchar(3)",
sql = "infra_object_operational_point.data->'extensions'->'sncf'->>'trigram'",
),
column(
name = "ci",
data_type = "integer",
sql = "(infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer",
),
column(
name = "ch",
data_type = "text",
sql = "infra_object_operational_point.data->'extensions'->'sncf'->>'ch'",
),
column(
name = "name",
data_type = "text",
sql = "infra_object_operational_point.data->'extensions'->'identifier'->>'name'",
textual_search,
)
)]
#[allow(unused)]
/// A search result item for a query with `object = "operationalpoint"`
Expand All @@ -64,6 +95,8 @@ pub(super) struct SearchResultItemOperationalPoint {
trigram: String,
#[search(sql = "OP.data#>>'{extensions,sncf,ch}'")]
ch: String,
#[search(sql = "OP.data#>>'{extensions,sncf,ci}'")]
ci: u64,
#[search(sql = "ST_AsGeoJSON(ST_Transform(lay.geographic, 4326))::json")]
geographic: GeoJsonPoint,
#[search(sql = "ST_AsGeoJSON(ST_Transform(lay.schematic, 4326))::json")]
Expand Down
6 changes: 4 additions & 2 deletions editoast/src/views/search/search_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ impl SearchConfig {
};
let insert_trigger_name = format!("{table}__ins_trig");
let update_trigger_name = format!("{table}__upd_trig");
let insert_trigger_function = format!("{table}__ins_trig_fun");
let update_trigger_function = format!("{table}__upd_trig_fun");
let insert_trigger = format!(
include_str!("sql/insert_trigger_template.sql"),
trigger = insert_trigger_name,
Expand Down Expand Up @@ -166,8 +168,8 @@ CREATE TABLE "{table}" (
DROP TABLE IF EXISTS "{table}";
DROP TRIGGER IF EXISTS {insert_trigger_name} ON "{src_table}";
DROP TRIGGER IF EXISTS {update_trigger_name} ON "{src_table}";
DROP FUNCTION IF EXISTS {insert_trigger_name};
DROP FUNCTION IF EXISTS {update_trigger_name};
DROP FUNCTION IF EXISTS {insert_trigger_function};
DROP FUNCTION IF EXISTS {update_trigger_function};
{append_down}"#
);
(up, down)
Expand Down
1 change: 1 addition & 0 deletions front/src/common/api/osrdEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ export type SearchResultItemTrack = {
};
export type SearchResultItemOperationalPoint = {
ch: string;
ci: number;
geographic: GeoJsonPoint;
infra_id: number;
name: string;
Expand Down

0 comments on commit 7a6c048

Please sign in to comment.