-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsearch.rs
827 lines (785 loc) · 30.5 KB
/
search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// Clippy doesn't seem to understand the `Search` derive macro
#![allow(clippy::duplicated_attributes)]
//! Defines the route [search] that can efficiently search all objects declared
//! in `search.yml` in a generic way
//!
//! # Example
//!
//! We can search all operational points around le Mont St-Michel and the railway
//! station `PNO` in the infra with id #2 with a POST query with the following body:
//!
//! ```yaml
//! {
//! "object": "operationalpoint",
//! "query": ["and",
//! ["or",
//! ["search", ["name"], "mich st"],
//! ["search", ["trigram"], "PNO"]],
//! ["=", ["infra_id"], 2]]
//! }
//! ```
//!
//! The query will be deserialized, type-checked and converted to a bunch of
//! constraints that will apply to the SQL request that produces the response.
//!
//! The next sections show how the request is processed.
//!
//! # Schema
//!
//! ```text
//! JSON query from /search payload
//!
//! │
//! │ Functions Trusted columns Search table
//! │ ────┬──── ───────┬─────── ──────┬─────
//! ▼ │ │ │
//! │ │ │
//! ┌───────────────────────┐ │ │ │
//! │Conversion to SearchAst│ │ │ │
//! └───────────────────────┘ └────────┐ │ ┌───────────┘
//! │ │ │
//! │ │ │ │
//! ▼ │ │ │
//! ▼ ▼ ▼
//! ┌─────────────┐
//! │Type checking│ ◄───────────────┬──── Evaluation context
//! └─────────────┘ │
//! │
//! │ │
//! ▼ │
//! │
//! ┌───────────────────┐ │
//! │Query evaluation │ │
//! │- function calls │ ◄───────────┘
//! │- SQL AST building │
//! └───────────────────┘
//!
//! │
//! ▼
//! ┌────────────────────────────────┐
//! │ Conversion to SQL code │
//! │(in our case, a where statement)│
//! └────────────────────────────────┘
//!
//! │
//! ▼
//!
//! ┌─────────────────────────────────────────┐
//! │String binding to prevent SQL injections │
//! └─────────────────────────────────────────┘
//!
//! │
//! ▼
//!
//! ┌────────────────────────────┐
//! │ Query execution and │
//! │endpoint response generation│
//! └────────────────────────────┘
//!
//! │
//! │
//! │
//! ▼
//!
//! HTTP response w/ search results
//! ```
//!
//! # Configuration file
//!
//! The config file `search.yml` defines:
//!
//! - the name of the searchable objects
//! - the columns that can be used as a search criteria alongside their type
//! - the table with these columns to search into
//! - the data returned for each object that matches the query
//!
//! Example:
//!
//! ```yaml
//! operationalpoint:
//! table: search_operational_point
//! columns:
//! obj_id: string
//! infra_id: integer
//! name: string
//! uic: integer
//! trigram: string
//! result:
//! joins: |
//! INNER JOIN infra_object_operational_point AS opm ON opm.id = search_operational_point.id
//! INNER JOIN infra_layer_operational_point AS opl ON opm.obj_id = opl.obj_id AND opm.infra_id = opl.infra_id
//! columns:
//! obj_id: opm.obj_id
//! infra_id: opm.infra_id
//! uic: opm.data#>>'{extensions,identifier,uic}'
//! name: opm.data#>>'{extensions,identifier,name}'
//! trigram: opm.data#>>'{extensions,sncf,trigram}'
//! ch: opm.data#>>'{extensions,sncf,ch}'
//! geographic: ST_AsGeoJSON(ST_Transform(opl.geographic, 4326))::json
//! ```
//!
//! In order to create the aforementioned search table (or materialized views), check
//! out the function `run_sql_create_infra_search_table` in API's Django migrations.
//!
//! # Query parsing
//!
//! The query in the payload is a JSON value that represent a boolean expression
//! with operators and function calls in prefix notation. It is first checked
//! and converted to a [editoast_search::SearchAst] which is a formalization of the AST.
//!
//! See [editoast_search::searchast].
//!
//! # Type checking
//!
//! Before turning the query into SQL code, we need to make sure of two things:
//!
//! 1. The query needs to typecheck. For instance, we disallow using the AND
//! operator with an operand that is not either a boolean of `null`.
//! The same goes for the LIKE operator that expects as a second operand
//! only `null` or a string.
//! 2. The query needs to represent a constraint that can be inserted as a WHERE
//! clause in the final SQL query. Therefore, the type of the whole query
//! needs to be a boolean, or `null`.
//!
//! However, to accurately determine the type of every part of the request, we
//! need some extra information:
//!
//! - The type of each column involved in the request: `["=", ["col"], 12]` only
//! makes sense if the column `col` represents an integer.
//! - The signature of each function in the query: `["like", ["name"], 12]` doesn't
//! makes sense because the SQL LIKE operator expects a string pattern and
//! not an integer.
//!
//! The structure that represents that context is [editoast_search::QueryContext] which provides
//! the function [editoast_search::QueryContext::typecheck_search_query()]. The functions the
//! route [search] uses are defined by [editoast_search::create_processing_context()] and
//! the columns defined in the `search.yml` configuration file are added to
//! that context.
//!
//! If the query typechecks in that particular context, it can be evaluated.
//!
//! # Evaluation
//!
//! In order to turn the ✨typechecked✨ query into an SQL statement, it is "evaluated"
//! within a [editoast_search::QueryContext] ; which contains several things:
//!
//! - The list of expected columns and their type. That data is extracted from `search.yml`.
//! - The name of the search table (e.g.: `search_operational_point`). That
//! information is useful to prevent ambiguities in the SQL query because of
//! column name conflicts.
//! - The list of functions (or operators) the query can use, their type signatures
//! (with possible overloads) and their definition.
//!
//! The transformation of the search expression into a valid SQL query occurs
//! in [editoast_search::QueryContext::search_ast_to_sql()], which will evaluate every function call,
//! each one using the input arguments to build an SQL expression that will
//! be part of the final SQL query.
//!
//! # SQL query construction and execution
//!
//! The evaluation step produces an [editoast_search::sqlquery::SqlQuery] object that can
//! be converted to a string containing valid PostgreSQL code ready to be inserted
//! into the search query's WHERE statement.
//!
//! Now, with the WHERE statement representing all the constraints expressed by our
//! search query ready, we build the rest of the SQL query that will produce
//! the search result to return. To do that the `result:` section
//! of `search.yml` is used. It contains all the data to be returned alongside the SQL
//! code to compute their value. It is also possible to join other tables.
//!
//! The last step is to bind all user strings to the SQL query in order to
//! prevent any SQL injection. That way, no malicious search query can be forged.
//!
//! The SQL request is now complete and ready to be executed in Postgres.
//! The resulting table of the request will then be converted to a JSON array of
//! mappings that constitutes the payload of the HTTP response.
// TODO: the documentation of this file needs to be updated (no more search.yml)
use std::ops::DerefMut;
use axum::extract::Json;
use axum::extract::Query;
use axum::extract::State;
use axum::Extension;
use chrono::DateTime;
use chrono::NaiveDateTime;
use chrono::Utc;
use diesel::pg::Pg;
use diesel::sql_query;
use diesel::sql_types::Jsonb;
use diesel::sql_types::Text;
use diesel::QueryableByName;
use diesel_async::RunQueryDsl;
use editoast_authz::BuiltinRole;
use editoast_common::geometry::GeoJsonPoint;
use editoast_derive::EditoastError;
use editoast_derive::Search;
use editoast_derive::SearchConfigStore;
use editoast_schemas::train_schedule::Margins;
use editoast_schemas::train_schedule::PathItem;
use editoast_schemas::train_schedule::PowerRestrictionItem;
use editoast_schemas::train_schedule::ScheduleItem;
use editoast_schemas::train_schedule::TrainScheduleOptions;
use editoast_search::query_into_sql;
use editoast_search::SearchConfigStore as _;
use editoast_search::SearchError;
use serde::Deserialize;
use serde::Serialize;
use serde_json::value::Value as JsonValue;
use std::collections::HashSet;
use utoipa::ToSchema;
use crate::error::Result;
use crate::views::pagination::PaginationQueryParam;
use crate::views::AuthenticationExt;
use crate::views::AuthorizationError;
use editoast_models::DbConnectionPoolV2;
crate::routes! {
"/search" => search,
}
editoast_common::schemas! {
SearchPayload,
SearchQuery,
SearchResultItem::schemas(),
}
#[derive(Debug, thiserror::Error, EditoastError)]
#[editoast_error(base_id = "search")]
enum SearchApiError {
#[error("object type '{object_type}' is invalid")]
ObjectType { object_type: String },
#[error(transparent)]
SearchEngineError(#[from] SearchError),
}
/// A search query
#[derive(ToSchema, Serialize)]
#[schema(example = json!(["and", ["=", ["infra_id"], 2], ["search", ["name"], "plop"]]))]
#[serde(untagged)]
#[allow(unused)] // only used as an OpenAPI schema
enum SearchQuery {
Boolean(bool),
Number(f64),
Int(i64),
String(String),
Array(Vec<Option<SearchQuery>>),
}
/// The payload of a search request
#[derive(Debug, Clone, Deserialize, ToSchema)]
#[schema(example = json!({
"object": "operationalpoint",
"query": ["and", ["=", ["infra_id"], 2], ["search", ["name"], "plop"]]
}))]
pub struct SearchPayload {
/// The object kind to query - run `editoast search list` to get all possible values
object: String,
/// The query to run
#[schema(value_type = SearchQuery)]
query: JsonValue,
/// Whether to return the SQL query instead of executing it
///
/// Only available in debug builds.
#[serde(default)]
dry: bool,
}
#[derive(QueryableByName)]
struct SearchDBResult {
#[diesel(sql_type = Jsonb)]
result: diesel_json::Json<serde_json::Value>,
}
/// Returns all infra objects of some type according to a hierarchical query.
///
/// # Payload
///
/// {
/// "object": string,
/// "query": query,
/// "dry": boolean, # default: false
/// }
///
/// Where:
/// - `object` can be any search object declared in `search.yml`
/// - `query` is a JSON document which can be deserialized into a [editoast_search::SearchAst].
/// Check out examples below.
///
/// # Response
///
/// The response structure depends on the `object`.
///
/// # Query language
///
/// The query itself is defined using a language made up of nested JSON arrays.
/// It will be parsed and transformed into a PostgreSQL WHERE statement.
/// The language consist of a single boolean expression of comparison statements
/// in prefix notation. For example, the query `["like", ["name"], "%ari%"]`
/// will generate a WHERE statement like `WHERE "name" LIKE '%ari%'`.
/// For more information on the query language itself, check out examples below.
///
/// # Available functions
///
/// See [editoast_search::create_processing_context()].
///
/// # A few query examples
///
/// * The railway station PNO: `["=", ["trigram"], "pno"]`
/// * The railway stations with either "Paris" or "Lyon" (or both) in their name:
/// `["or", ["search", ["name"], "Paris"], ["search", ["name"], "Lyon"]]`
/// * All railway stations with "Paris" in their name but not PNO :
/// `["and", ["search", ["name"], "Paris"], ["not", ["=", ["trigram"], "pno"]]]`
///
/// See [editoast_search::SearchAst] for a more detailed view of the query language.
#[utoipa::path(
post, path = "",
tag = "search",
params(PaginationQueryParam),
request_body = SearchPayload,
responses(
(status = 200, body = Vec<SearchResultItem>, description = "The search results"),
)
)]
async fn search(
State(db_pool): State<DbConnectionPoolV2>,
Extension(auth): AuthenticationExt,
Query(query_params): Query<PaginationQueryParam>,
Json(SearchPayload { object, query, dry }): Json<SearchPayload>,
) -> Result<Json<serde_json::Value>> {
let roles: HashSet<BuiltinRole> = match object.as_str() {
"track" | "operationalpoint" | "signal" => HashSet::from([BuiltinRole::InfraRead]),
"trainschedule" => HashSet::from([BuiltinRole::TimetableRead]),
"project" | "study" | "scenario" => HashSet::from([BuiltinRole::OpsRead]),
_ => {
return Err(SearchApiError::ObjectType {
object_type: object.to_owned(),
}
.into())
}
};
let authorized = auth
.check_roles(roles)
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}
let (page, per_page) = query_params.validate(1000)?.warn_page_size(100).unpack();
let search_config =
SearchConfigFinder::find(&object).ok_or_else(|| SearchApiError::ObjectType {
object_type: object.to_owned(),
})?;
let offset = (page - 1) * per_page;
let (sql, bindings) = query_into_sql(query, &search_config, per_page, offset, "result")
.map_err(SearchApiError::from)?;
let mut query = sql_query(sql).into_boxed();
for string in bindings {
query = query.bind::<Text, _>(string.to_owned());
}
if cfg!(debug_assertions) && dry {
tracing::debug!("not running query");
let query = diesel::debug_query::<Pg, _>(&query).to_string();
return Ok(Json(serde_json::to_value(query).unwrap()));
}
let objects = query
.load::<SearchDBResult>(&mut db_pool.get().await?.write().await.deref_mut())
.await?;
let results: Vec<_> = objects.into_iter().map(|r| r.result).collect();
Ok(Json(serde_json::to_value(results).unwrap()))
}
// NOTE: every structure deriving `Search` here might have to `#[allow(unused)]`
// because while the name and type information of the fields are read by the macro,
// they might not be explicitly used in the code. (Their JSON representation extracted
// from the DB query is direcly forwarded into the endpoint response, so these
// structs are never deserialized, hence their "non-usage".)
//
// These structs also derive Serialize because utoipa reads some `#[serde(...)]`
// annotations to alter the schema. That's not ideal since none of them are ever
// serialized, but that's life.
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_track",
column(name = "infra_id", data_type = "INT"),
column(name = "line_code", data_type = "INT"),
column(name = "line_name", data_type = "TEXT")
)]
#[allow(unused)]
/// A search result item for a query with `object = "track"`
///
// **IMPORTANT**: Please note that any modification to this struct should be reflected in [crate::models::infra::Infra::clone]
pub(super) struct SearchResultItemTrack {
#[search(sql = "search_track.infra_id")]
infra_id: i64,
#[search(sql = "search_track.unprocessed_line_name")]
line_name: String,
#[search(sql = "search_track.line_code")]
line_code: i64,
}
#[derive(Search, Serialize, ToSchema)]
#[search(
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 = "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"`
///
// **IMPORTANT**: Please note that any modification to this struct should be reflected in [crate::models::infra::Infra::clone]
pub(super) struct SearchResultItemOperationalPoint {
#[search(sql = "OP.obj_id")]
obj_id: String,
#[search(sql = "OP.infra_id")]
infra_id: i64,
#[search(sql = "OP.data->'extensions'->'identifier'->'uic'")]
uic: i64,
#[search(sql = "OP.data#>>'{extensions,identifier,name}'")]
name: String,
#[search(sql = "OP.data#>>'{extensions,sncf,trigram}'")]
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 = "OP.data->'parts'")]
#[schema(inline)]
track_sections: Vec<SearchResultItemOperationalPointTrackSections>,
}
#[derive(Serialize, ToSchema)]
#[allow(unused)]
pub(super) struct SearchResultItemOperationalPointTrackSections {
track: String,
position: f64,
}
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_signal",
migration(
src_table = "infra_object_signal",
query_joins = "
INNER JOIN infra_object_track_section AS track_section
ON track_section.infra_id = infra_object_signal.infra_id
AND track_section.obj_id = infra_object_signal.data->>'track'",
),
column(
name = "label",
data_type = "text",
sql = "infra_object_signal.data->'extensions'->'sncf'->>'label'",
textual_search
),
column(
name = "line_name",
data_type = "text",
sql = "track_section.data->'extensions'->'sncf'->>'line_name'",
textual_search
),
column(
name = "infra_id",
data_type = "integer",
sql = "infra_object_signal.infra_id"
),
column(
name = "obj_id",
data_type = "VARCHAR(255)",
sql = "infra_object_signal.obj_id"
),
column(
name = "signaling_systems",
data_type = "TEXT[]",
sql = "ARRAY(SELECT jsonb_path_query(infra_object_signal.data, '$.logical_signals[*].signaling_system')->>0)"
),
column(
name = "settings",
data_type = "TEXT[]",
sql = "ARRAY(SELECT jsonb_path_query(infra_object_signal.data, '$.logical_signals[*].settings.keyvalue().key')->>0)"
),
column(
name = "line_code",
data_type = "integer",
sql = "(track_section.data->'extensions'->'sncf'->>'line_code')::integer"
),
joins = "
INNER JOIN infra_object_signal AS sig ON sig.id = search_signal.id
INNER JOIN infra_object_track_section AS track_section ON track_section.obj_id = sig.data->>'track' AND track_section.infra_id = sig.infra_id
INNER JOIN infra_layer_signal AS lay ON lay.infra_id = sig.infra_id AND lay.obj_id = sig.obj_id"
)]
#[allow(unused)]
/// A search result item for a query with `object = "signal"`
///
// **IMPORTANT**: Please note that any modification to this struct should be reflected in [crate::models::infra::Infra::clone]
pub(super) struct SearchResultItemSignal {
#[search(sql = "sig.infra_id")]
infra_id: i64,
#[search(sql = "sig.data->'extensions'->'sncf'->>'label'")]
label: String,
#[search(sql = "search_signal.signaling_systems")]
signaling_systems: Vec<String>,
#[search(sql = "search_signal.settings")]
settings: Vec<String>,
#[search(sql = "search_signal.line_code")]
line_code: u64,
#[search(sql = "track_section.data->'extensions'->'sncf'->>'line_name'")]
line_name: String,
#[search(sql = "ST_AsGeoJSON(ST_Transform(lay.geographic, 4326))::json")]
geographic: GeoJsonPoint,
#[search(sql = "lay.signaling_system")]
sprite_signaling_system: Option<String>,
#[search(sql = "lay.sprite")]
sprite: Option<String>,
}
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_project",
joins = "INNER JOIN project ON project.id = search_project.id",
column(name = "id", data_type = "integer"),
column(name = "name", data_type = "string"),
column(name = "description", data_type = "string"),
column(name = "tags", data_type = "string")
)]
#[allow(unused)]
/// A search result item for a query with `object = "project"`
pub(super) struct SearchResultItemProject {
#[search(sql = "project.id")]
id: u64,
#[search(sql = "project.image_id")]
#[schema(required)]
image: Option<u64>,
#[search(sql = "project.name")]
name: String,
#[search(
sql = "(SELECT COUNT(study.id) FROM study WHERE search_project.id = study.project_id)"
)]
studies_count: u64,
#[search(sql = "project.description")]
description: String,
#[search(sql = "project.last_modification")]
last_modification: NaiveDateTime,
#[search(sql = "project.tags")]
tags: Vec<String>,
}
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_study",
migration(src_table = "study"),
joins = "INNER JOIN study ON study.id = search_study.id",
column(name = "name", data_type = "TEXT", sql = "study.name"),
column(name = "description", data_type = "TEXT", sql = "study.description"),
column(
name = "tags",
data_type = "TEXT",
sql = "osrd_prepare_for_search_tags(study.tags)"
),
column(name = "project_id", data_type = "INTEGER", sql = "study.project_id")
)]
#[allow(unused)]
/// A search result item for a query with `object = "study"`
pub(super) struct SearchResultItemStudy {
#[search(sql = "study.id")]
id: u64,
#[search(sql = "study.project_id")]
project_id: u64,
#[search(sql = "study.name")]
name: String,
#[search(
sql = "(SELECT COUNT(scenario.id) FROM scenario WHERE search_study.id = scenario.study_id)"
)]
scenarios_count: u64,
#[search(sql = "study.description")]
#[schema(required)]
description: Option<String>,
#[search(sql = "study.last_modification")]
last_modification: NaiveDateTime,
#[search(sql = "study.tags")]
tags: Vec<String>,
#[search(sql = "study.budget")]
#[schema(required)]
budget: Option<u32>,
}
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_scenario",
migration(src_table = "scenario"),
joins = "
INNER JOIN scenario ON scenario.id = search_scenario.id
INNER JOIN infra ON infra.id = scenario.infra_id",
column(
name = "name",
data_type = "TEXT",
sql = "scenario.name",
textual_search
),
column(
name = "description",
data_type = "TEXT",
sql = "scenario.description",
textual_search
),
column(
name = "tags",
data_type = "TEXT",
sql = "osrd_prepare_for_search_tags(scenario.tags)"
),
column(name = "study_id", data_type = "INTEGER", sql = "scenario.study_id")
)]
#[allow(unused)]
/// A search result item for a query with `object = "scenario"`
pub(super) struct SearchResultItemScenario {
#[search(sql = "scenario.id")]
id: u64,
#[search(sql = "scenario.study_id")]
study_id: u64,
#[search(sql = "scenario.name")]
name: String,
#[search(sql = "scenario.electrical_profile_set_id")]
#[schema(required)]
electrical_profile_set_id: Option<u64>,
#[search(sql = "scenario.infra_id")]
infra_id: u64,
#[search(sql = "infra.name")]
infra_name: String,
#[search(
sql = "(SELECT COUNT(trains.id) FROM train_schedule AS trains WHERE scenario.timetable_id = trains.timetable_id)"
)]
trains_count: u64,
#[search(sql = "scenario.description")]
description: String,
#[search(sql = "scenario.last_modification")]
last_modification: NaiveDateTime,
#[search(sql = "scenario.tags")]
tags: Vec<String>,
}
#[derive(Search, Serialize, ToSchema)]
#[cfg_attr(test, derive(serde::Deserialize))]
#[search(
table = "train_schedule",
column(name = "timetable_id", data_type = "integer"),
column(name = "train_name", data_type = "string")
)]
#[allow(unused)]
/// A search result item for a query with `object = "trainschedule"`
pub(super) struct SearchResultItemTrainSchedule {
#[search(sql = "train_schedule.id")]
id: u64,
#[search(sql = "train_schedule.train_name")]
train_name: String,
#[search(sql = "train_schedule.labels")]
labels: Vec<Option<String>>,
#[search(sql = "train_schedule.rolling_stock_name")]
rolling_stock_name: String,
#[search(sql = "train_schedule.timetable_id")]
timetable_id: i64,
#[search(sql = "train_schedule.start_time")]
start_time: DateTime<Utc>,
#[search(sql = "train_schedule.schedule")]
schedule: Vec<ScheduleItem>,
#[search(sql = "train_schedule.margins")]
margins: Margins,
#[search(sql = "train_schedule.initial_speed")]
initial_speed: f64,
#[search(sql = "train_schedule.comfort")]
comfort: i64,
#[search(sql = "train_schedule.path")]
path: Vec<PathItem>,
#[search(sql = "train_schedule.constraint_distribution")]
constraint_distribution: i64,
#[search(sql = "train_schedule.speed_limit_tag")]
speed_limit_tag: Option<String>,
#[search(sql = "train_schedule.power_restrictions")]
power_restrictions: Vec<PowerRestrictionItem>,
#[search(sql = "train_schedule.options")]
options: TrainScheduleOptions,
}
/// See [editoast_search::SearchConfigStore::find]
#[derive(SearchConfigStore)]
#[search_config_store(
object(name = "track", config = SearchResultItemTrack),
object(name = "operationalpoint", config = SearchResultItemOperationalPoint),
object(name = "signal", config = SearchResultItemSignal),
object(name = "project", config = SearchResultItemProject),
object(name = "study", config = SearchResultItemStudy),
object(name = "scenario", config = SearchResultItemScenario),
object(name = "trainschedule", config = SearchResultItemTrainSchedule),
)]
pub struct SearchConfigFinder;
#[cfg(test)]
pub mod tests {
use axum::http::StatusCode;
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::json;
use super::*;
use crate::models::fixtures::{create_simple_train_schedule, create_timetable};
use crate::views::test_app::TestAppBuilder;
#[rstest]
async fn search_trainschedule_post_found() {
let app = TestAppBuilder::default_app();
let pool = app.db_pool();
// Create the timetable in the database
let timetable = create_timetable(&mut pool.get_ok()).await;
let timetable_id = timetable.id;
// Add a train_schedule in the database
let train = create_simple_train_schedule(&mut pool.get_ok(), timetable_id).await;
// The body
let request = app.post("/search").json(&json!({
"object": "trainschedule",
"query": ["and", ["=", ["train_name"], train.train_name],
["=", ["timetable_id"], timetable_id]],
}));
let response: Vec<SearchResultItemTrainSchedule> =
app.fetch(request).assert_status(StatusCode::OK).json_into();
assert_eq!(response.len(), 1);
assert_eq!(response[0].train_name, train.train_name);
}
#[rstest]
async fn search_trainschedule_post_not_found() {
let app = TestAppBuilder::default_app();
let pool = app.db_pool();
// Create the timetable in the database
let timetable = create_timetable(&mut pool.get_ok()).await;
let timetable_id = timetable.id;
// Add a train_schedule in the database
create_simple_train_schedule(&mut pool.get_ok(), timetable_id).await;
let train_name = "NonExistingTrain";
// The body
let request = app.post("/search").json(&json!({
"object": "trainschedule",
"query": ["and", ["=", ["train_name"], train_name],
["=", ["timetable_id"], timetable_id]],
}));
let response: Vec<SearchResultItemTrainSchedule> =
app.fetch(request).assert_status(StatusCode::OK).json_into();
assert_eq!(response.len(), 0);
}
}