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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
use super::model_api::meta::Meta;
use models::stop::Stop;
use models::stop::StopTrip;
use models::trip::Trip;
use models::dropoff::DropOff;
use models::pickup::PickUp;
use super::model_api::error::Error;
use super::model_api::result::Result;
use super::model_api::resultarray::ResultArray;
use super::super::Json;
use super::super::Pool;
use super::super::PostgresConnectionManager;
use super::super::RoutesHandler;
use super::super::State;
use chrono::NaiveTime;
use postgres::rows::Row;
use postgres::types::ToSql;
use std::ops::Deref;
use std::ops::DerefMut;
use std::collections::BTreeMap;
use std::collections::HashMap;
use models::api::search::trip::TripSearch;
use models::boundingbox::BoundingBox;
use num_traits as num;
use models::api::paginatedvec::PaginatedVec;
use models::api::pagination::Pagination;
use models::query::Query;
use models::api::search::ascdesc::AscDesc;
use std::str::FromStr;
use models::api::sort::tripsort::TripSort;
use std::cmp::Ordering;
fn add_stop_times_to_query(has_times: &mut bool, has_stop: &mut bool, query: &mut Query) {
if !*has_times {
query.join_v.push(String::from_str(
"INNER JOIN stop_time ON stop_time.trip_id = tid AND stop_time.feed_id = tfid"
).unwrap());
*has_times = true;
}
if !*has_stop {
query.join_v.push(String::from_str(
"INNER JOIN stop ON stop_time.stop_id = stop.id AND stop_time.feed_id = stop.feed_id"
).unwrap());
*has_stop = true;
}
}
fn trips_query_filter(ts: &TripSearch,
query: Query,
params: Vec<&ToSql>,
pool: &Pool<PostgresConnectionManager>,
has_times: bool,
has_stop: bool
) -> PaginatedVec<Trip> {
let select_has_times = has_times;
let select_has_stop = has_stop;
let mut has_times = has_times;
let mut has_stop = has_stop;
let mut query : Query = query.clone();
let mut trips_result: Vec<Trip> = Vec::new();
let mut ints : Vec<i64> = Vec::new();
let mut values: Vec<String> = Vec::new();
let mut times : Vec<NaiveTime> = Vec::new();
let mut i = params.len();
let mut params: Vec<&ToSql> = Vec::from(params);
let mut addition: String;
if ts.departure_after.is_some() {
let departure_after_str = ts.departure_after.as_ref().unwrap();
let departure_after =
NaiveTime::parse_from_str(&departure_after_str,
"%H:%M:%S"
).unwrap();
add_stop_times_to_query(&mut has_times, &mut has_stop, &mut query);
i += 1;
addition = format!(
"stop_time.departure_time >= ${} ",
&i
);
times.push(departure_after);
query.where_v.push(addition);
}
if ts.arrival_before.is_some() {
let arrival_before =
NaiveTime::parse_from_str(ts.arrival_before.as_ref().unwrap(),
"%H:%M:%S"
).unwrap();
add_stop_times_to_query(&mut has_times, &mut has_stop, &mut query);
i += 1;
addition = format!(
"stop_time.arrival_time <= ${} ",
&i
);
times.push(arrival_before);
query.where_v.push(addition);
}
for time in × {
params.push(time);
}
if ts.route.is_some() {
i+= 1;
let route_uid : String = ts.route.as_ref().unwrap().to_string();
addition = format!("ruid = ${}", &i);
values.push(route_uid);
query.where_v.push(addition);
}
if ts.stops_visited.is_some() {
let mut where_string = String::new();
addition = format!("tuid IN ( ");
where_string.push_str(&addition);
let split_stops: Vec<&str> = ts.stops_visited.as_ref().unwrap().split(",").collect();
let mut first = true;
for stop in split_stops {
if first {
first = !first;
} else {
addition = format!(" INTERSECT ");
where_string.push_str(&addition);
}
i += 1;
addition = format!(
"SELECT
trip.uid as tuid
FROM trip
INNER JOIN stop_time ON (trip.trip_id = stop_time.trip_id)
INNER JOIN stop ON (stop_time.stop_id = stop.id)
WHERE
trip.feed_id = stop_time.feed_id
AND
stop_time.feed_id = stop.feed_id
AND
stop.uid = ${}",
&i
);
where_string.push_str(&addition);
values.push(String::from(stop));
println!("Stop {} ", stop);
}
addition = format!(" )");
where_string.push_str(&addition);
query.where_v.push(where_string);
}
if ts.sort_by.as_ref().is_some() {
if !(select_has_stop && select_has_times) {
match ts.sort_by.as_ref().unwrap() {
&TripSort::ServiceId => {
query.select_v.push(String::from("tsid"));
query.order_v.push(String::from("tsid"));
query.order_v.rotate_right(1);
},
&TripSort::RouteId => {
query.order_v.push(String::from("ruid"));
query.order_v.rotate_right(1);
},
&TripSort::Uid => {
query.order_v.push(String::from("tuid"));
query.order_v.rotate_right(1);
},
&TripSort::DirectionId => {
query.order_v.push(String::from("td"));
query.order_v.rotate_right(1);
}
&TripSort::DepartureTime => {
query.select_v.push(String::from("stop_time.departure_time"));
query.order_v.push(String::from("stop_time.departure_time"));
query.order_v.rotate_right(1);
},
&TripSort::ArrivalTime => {
query.select_v.push(String::from("stop_time.arrival_time"));
query.order_v.push(String::from("stop_time.arrival_time"));
query.order_v.rotate_right(1);
},
_ => {}
}
}
}
if ts.sort_order.as_ref().is_some() {
query.sort_order = ts.sort_order.as_ref().unwrap().clone();
}
let mut offset : i64 = 0;
let mut limit: i64 = 50;
if ts.offset.is_some() {
let c_offset = ts.offset.unwrap();
if c_offset > 0 {
offset = c_offset;
}
}
if ts.per_page.is_some() {
let c_limit = ts.per_page.unwrap();
if c_limit > 0 && c_limit <= 1000 {
limit = c_limit;
}
}
for value in &values {
params.push(value);
}
addition = format!(" LIMIT ${} OFFSET ${}", &i, &(i + 1));
query.limit = limit;
query.offset = offset;
for value in &ints {
params.push(value);
}
println!("Query: {}", query.format());
let conn = pool.clone().get().unwrap();
let trips = conn.query(&query.format(), ¶ms);
if select_has_times && select_has_stop {
let mut trips_hm: BTreeMap<Trip, Vec<StopTrip>> = BTreeMap::new();
let mut i: i32 = 0;
for row in trips.expect("Query failed").iter() {
let uid: String = row.get(0);
parse_stop_trip_trip_row(&mut trips_hm, &row);
i += 1;
}
for (k, v) in trips_hm.iter() {
let mut t = (*k).clone();
t.stop_sequence = Some(v.clone());
trips_result.push(t);
}
if ts.sort_by.is_some() {
let v_v = ts.sort_by.as_ref().unwrap();
match v_v {
&TripSort::ArrivalTime => {
trips_result.sort_by(|a, b| {
if a.stop_sequence.as_ref().is_some() {
if b.stop_sequence.as_ref().is_some() {
let at_a = a.stop_sequence.as_ref().unwrap().get(0)
.unwrap().arrival_time;
let at_b = b.stop_sequence.as_ref().unwrap().get(0)
.unwrap().arrival_time;
return at_a.cmp(&at_b);
} else {
return Ordering::Less;
}
} else {
return Ordering::Greater;
}
})
},
&TripSort::DepartureTime => {
trips_result.sort_by(|a, b| {
if a.stop_sequence.as_ref().is_some() {
if b.stop_sequence.as_ref().is_some() {
let at_a = a.stop_sequence.as_ref().unwrap().get(0)
.unwrap().departure_time;
let at_b = b.stop_sequence.as_ref().unwrap().get(0)
.unwrap().departure_time;
return at_a.cmp(&at_b);
} else {
return Ordering::Less;
}
} else {
return Ordering::Greater;
}
})
},
&TripSort::DirectionId => {
trips_result.sort_by(|a, b| {
return a.direction_id.cmp(&b.direction_id);
})
},
_ => {}
}
if ts.sort_order.is_some() &&
ts.sort_order.as_ref().unwrap() == &AscDesc::DESC {
trips_result.reverse();
}
}
} else {
for row in trips.expect("Query failed").iter() {
let mut route = parse_trip_row(&row);
route.stop_sequence = Option::None;
trips_result.push(route);
}
}
return PaginatedVec {
vec: trips_result,
pag: Some(Pagination{
limit,
offset
})
};
}
#[get("/trips")]
pub fn trips(rh: State<RoutesHandler>) -> Json<ResultArray<Trip>> {
let query = "SELECT \
t.uid,\
r.uid,\
c.uid,\
trip_id,\
headsign,\
t.short_name,\
direction_id,\
t.feed_id \
FROM trip as t \
INNER JOIN calendar as c ON c.service_id=t.service_id \
INNER JOIN route as r ON r.id = t.route_id \
WHERE c.feed_id = t.feed_id \
AND r.feed_id = t.feed_id \
LIMIT 50";
let conn = rh.pool.clone().get().unwrap();
let trips = conn.query(query, &[]);
let mut trips_result: Vec<Trip> = Vec::new();
for row in trips.expect("Query failed").iter() {
let sequence: Vec<StopTrip>;
let mut route = parse_trip_row(&row);
let route_uid = route.uid.clone();
sequence = get_stop_trip(route_uid, &rh.pool);
route.stop_sequence = Some(sequence);
trips_result.push(route);
}
let rr = ResultArray::<Trip> {
result: Some(trips_result),
meta: Meta {
success: true,
error: Option::None,
pagination: Option::None
},
};
Json(rr)
}
#[get("/trips?<query>")]
pub fn trips_by_query(rh: State<RoutesHandler>, query: TripSearch) -> Json<ResultArray<Trip>> {
let trips_result: PaginatedVec<Trip> = get_trips_by_query(&query, &rh.pool);
let rr = ResultArray::<Trip> {
result: Some(trips_result.vec),
meta: Meta {
success: true,
error: Option::None,
pagination: trips_result.pag
},
};
Json(rr)
}
#[get("/trips/by-stop/<stop_id>")]
pub fn trips_stopid(rh: State<RoutesHandler>, stop_id: String) -> Json<ResultArray<Trip>> {
let query = "SELECT \
t.uid,\
r.uid,\
c.uid,\
trip_id,\
headsign,\
t.short_name,\
direction_id,\
t.feed_id \
FROM trip as t \
INNER JOIN calendar as c ON c.service_id=t.service_id \
INNER JOIN route as r ON r.id = t.route_id \
WHERE trip_id IN \
(SELECT trip_id FROM stop_time WHERE \
stop_id=(SELECT stop.id FROM stop WHERE uid=$1) \
AND \
feed_id = (SELECT stop.feed_id FROM stop WHERE uid=$1) \
GROUP BY trip_id \
) \
AND c.feed_id = t.feed_id \
AND r.feed_id = t.feed_id \
LIMIT 50";
let conn = rh.pool.clone().get().unwrap();
let trips = conn.query(query, &[&stop_id]);
let mut trips_result: Vec<Trip> = Vec::new();
for row in trips.expect("Query failed").iter() {
let sequence: Vec<StopTrip>;
let mut route = parse_trip_row(&row);
let route_uid = route.uid.clone();
sequence = get_stop_trip(route_uid, &rh.pool);
route.stop_sequence = Some(sequence);
trips_result.push(route);
}
let rr = ResultArray::<Trip> {
result: Some(trips_result),
meta: Meta {
success: true,
error: Option::None,
pagination: Option::None
},
};
Json(rr)
}
fn get_trips_by_query(ts: &TripSearch, pool: &Pool<PostgresConnectionManager>) -> PaginatedVec<Trip> {
let mut query : Query = Query {
select_v: Vec::new(),
from_v: Vec::new(),
where_v: Vec::new(),
join_v: Vec::new(),
order_v: Vec::new(),
limit: 0,
offset: 0,
format: String::new(),
sort_order: AscDesc::ASC,
};
query.format = String::from(r#"SELECT DISTINCT
{0}
FROM {1}
{2}
WHERE {3}
{4}
{5}
"#);
query.select_v.push(String::from("tuid"));
query.select_v.push(String::from("r.uid as ruid"));
query.select_v.push(String::from("c.uid as cuid"));
query.select_v.push(String::from("tid"));
query.select_v.push(String::from("ths"));
query.select_v.push(String::from("tsn"));
query.select_v.push(String::from("td"));
query.select_v.push(String::from("tfid"));
query.from_v.push(String::from(r#"(
SELECT
trip.uid as tuid,
trip.trip_id as tid,
trip.short_name as tsn,
trip.feed_id as tfid,
trip.direction_id as td,
trip.headsign as ths,
trip.route_id as truid,
trip.service_id as tsid
FROM trip) as t"#));
query.join_v.push(
String::from(
"INNER JOIN calendar as c ON c.service_id=tsid"
)
);
query.join_v.push(
String::from(
"INNER JOIN route as r ON r.id = truid"
)
);
query.where_v.push(
String::from(
"c.feed_id = tfid"
)
);
query.where_v.push(
String::from(
"r.feed_id = tfid"
)
);
return trips_query_filter(ts, query, vec![], pool, false, false);
}
#[get("/trips/<trip_id>")]
pub fn trip(rh: State<RoutesHandler>, trip_id: String) -> Json<Result<Trip>> {
let query = "SELECT \
trip.uid,\
route.uid,\
trip.service_id,\
trip.trip_id,\
trip.headsign,\
trip.short_name,\
trip.direction_id,\
trip.feed_id \
FROM trip, route
WHERE trip.uid = $1 AND \
route.feed_id = trip.feed_id AND \
route.id = trip.route_id";
let conn = rh.pool.clone().get().unwrap();
let trips = conn.query(query, &[&trip_id]);
let trips = &trips.unwrap();
if trips.len() == 0 {
return Json(Result::<Trip> {
result: Option::None,
meta: Meta {
success: false,
error: Some(Error {
code: 1,
message: String::from("Trip not found"),
}),
pagination: Option::None
},
});
}
let sequence: Vec<StopTrip>;
let mut trip = parse_trip_row(&(trips).get(0));
let trip_uid = trip.uid.clone();
sequence = get_stop_trip(String::from(trip_uid), &rh.pool);
trip.stop_sequence = Some(sequence);
let result = Result::<Trip> {
result: Some(trip),
meta: Meta {
success: true,
error: Option::None,
pagination: Option::None
},
};
Json(result)
}
#[get("/trips/by-route/<route_uid>")]
pub fn trips_by_route(rh: State<RoutesHandler>, route_uid: String) -> Json<ResultArray<Trip>> {
let query = "SELECT \
trip.uid, \
route.uid, \
calendar.uid, \
trip.trip_id, \
trip.headsign, \
trip.short_name, \
trip.direction_id, \
trip.feed_id \
FROM trip, route, calendar \
WHERE route.uid = $1 AND \
trip.route_id = route.id AND \
trip.feed_id = route.feed_id AND \
calendar.feed_id = trip.feed_id AND \
calendar.service_id = trip.service_id
LIMIT 50";
let conn = rh.pool.clone().get().unwrap();
let trips = conn.query(query, &[&route_uid]);
let trips = &trips.unwrap();
if trips.len() == 0 {
return Json(ResultArray::<Trip> {
result: Option::None,
meta: Meta {
success: false,
error: Some(Error {
code: 1,
message: String::from("Trip not found"),
}),
pagination: Option::None
},
});
}
let mut trips_result: Vec<Trip> = Vec::new();
for trip_row in trips {
let sequence: Vec<StopTrip>;
let mut trip = parse_trip_row(&trip_row);
let trip_uid = trip.uid.clone();
sequence = get_stop_trip(String::from(trip_uid), &rh.pool);
trip.stop_sequence = Some(sequence);
trips_result.push(trip);
}
let result = ResultArray::<Trip> {
result: Some(trips_result),
meta: Meta {
success: true,
error: Option::None,
pagination: Option::None
},
};
Json(result)
}
#[get("/trips/in/<bbox>")]
pub fn trips_by_bbox(rh: State<RoutesHandler>, bbox: BoundingBox) -> Json<ResultArray<Trip>> {
return trips_by_bbox_query(rh, bbox, TripSearch {
stops_visited: None,
route: None,
departure_after: None,
arrival_before: None,
offset: None,
per_page: None,
sort_by: None,
sort_order: None,
});
}
#[get("/trips/in/<bbox>?<ts>")]
pub fn trips_by_bbox_query(rh: State<RoutesHandler>, bbox: BoundingBox, ts: TripSearch)
-> Json<ResultArray<Trip>> {
let mut query : Query = Query {
select_v: Vec::new(),
from_v: Vec::new(),
where_v: Vec::new(),
join_v: Vec::new(),
order_v: Vec::new(),
limit: 0,
offset: 0,
format: String::new(),
sort_order: AscDesc::ASC,
};
let mut ts = ts.clone();
ts.per_page = Some(1000);
query.format = String::from(r#"SELECT
{0}
FROM {1}
{2}
WHERE {3}
{4}
"#);
query.select_v.push(String::from_str("tuid").unwrap());
query.select_v.push(String::from_str("ruid").unwrap());
query.select_v.push(String::from_str("cuid").unwrap());
query.select_v.push(String::from_str("ths").unwrap());
query.select_v.push(String::from_str("tsn").unwrap());
query.select_v.push(String::from_str("td").unwrap());
query.select_v.push(String::from_str("tfid").unwrap());
query.select_v.push(String::from_str("stop.uid as suid").unwrap());
query.select_v.push(String::from_str("stop.id as sid").unwrap());
query.select_v.push(String::from_str("stop.\"name\" as sname").unwrap());
query.select_v.push(String::from_str("ST_Y(stop.position::geometry) as slat").unwrap());
query.select_v.push(String::from_str("ST_X(stop.position::geometry) as slng").unwrap());
query.select_v.push(String::from_str("stop.\"type\" as st").unwrap());
query.select_v.push(String::from_str("pstop.uid").unwrap());
query.select_v.push(String::from_str("stop_time.arrival_time as st_at").unwrap());
query.select_v.push(String::from_str("stop_time.departure_time as st_dt").unwrap());
query.select_v.push(String::from_str("stop_time.stop_sequence as st_ss").unwrap());
query.select_v.push(String::from_str("stop_time.drop_off_type as st_do").unwrap());
query.select_v.push(String::from_str("stop_time.pickup_type as st_pu").unwrap());
let mut limited_range = false;
let mut addition = "";
if ts.departure_after.is_some() && ts.arrival_before.is_some() {
limited_range = true;
addition = "AND st.departure_time >= $5 AND st.arrival_time <= $6";
}
let mut formatted_query = r#"(SELECT DISTINCT
trip.uid as tuid,
route.uid as ruid,
calendar.uid as cuid,
trip.trip_id as tid,
trip.headsign as ths,
trip.short_name as tsn,
trip.direction_id as td,
trip.feed_id as tfid
FROM trip, route, calendar
WHERE trip.uid IN (
SELECT trip.uid
FROM trip
WHERE EXISTS (
SELECT 1
FROM stop AS s
INNER JOIN stop_time AS st
ON s.id = st.stop_id AND s.feed_id = st.feed_id
WHERE ST_Within(s.position::geometry,
ST_MakeEnvelope($1, $2, $3, $4, 4326))
AND st.trip_id = trip.trip_id AND trip.feed_id = st.feed_id
{addition_goes_here}
)
{0}
)
AND
route.feed_id = trip.feed_id AND
calendar.feed_id = trip.feed_id AND
route.id = trip.route_id AND
calendar.service_id = trip.service_id
GROUP BY tuid, ruid, cuid, tid, ths, tsn, td, tfid
) as t"#.replace("{addition_goes_here}", addition);
query.from_v.push(String::from_str(&formatted_query).unwrap());
query.join_v.push(
String::from_str(
"INNER JOIN stop_time ON stop_time.trip_id = tid AND stop_time.feed_id = tfid"
).unwrap()
);
query.join_v.push(
String::from_str(
"INNER JOIN stop ON stop_time.stop_id = stop.id AND stop_time.feed_id = stop.feed_id"
).unwrap()
);
query.join_v.push(
String::from_str(
"LEFT JOIN stop as pstop ON pstop.id = stop.parent_stop AND pstop.feed_id = tfid"
).unwrap()
);
let mut da : NaiveTime;
let mut ab : NaiveTime;
let mut params: Vec<&ToSql> =
vec![&bbox.p1.lng, &bbox.p1.lat, &bbox.p2.lng, &bbox.p2.lat];
if limited_range {
da =
NaiveTime::parse_from_str(&ts.departure_after.clone().unwrap(),
"%H:%M:%S"
).unwrap();
ab =
NaiveTime::parse_from_str(&ts.arrival_before.clone().unwrap(),
"%H:%M:%S"
).unwrap();
ts.departure_after = None;
ts.arrival_before = None;
params.push(&da);
params.push(&ab);
}
let paginated_result = trips_query_filter(&ts,
query,
params,
&rh.pool,
true,
true);
let result = ResultArray::<Trip> {
result: Some(paginated_result.vec),
meta: Meta {
success: true,
error: Option::None,
pagination: paginated_result.pag
},
};
return Json(
result
);
}
fn get_stop_trip(trip_uid: String, pool: &Pool<PostgresConnectionManager>) -> Vec<StopTrip> {
let query =
r#"SELECT
stop.uid,
stop.name,
ST_Y(position::geometry) as lat,
ST_X(position::geometry) as lng,
stop."type",
(SELECT stop.uid FROM stop as s WHERE s.id = stop.parent_stop AND s.feed_id = stop.feed_id) as parent_stop,
stop_time.stop_sequence,
stop_time.drop_off_type,
stop_time.pickup_type,
stop_time.arrival_time,
stop_time.departure_time
FROM stop_time, stop
WHERE stop.id = stop_time.stop_id
AND stop_time.trip_id = (SELECT trip.trip_id FROM trip WHERE trip.uid = $1 AND trip.feed_id = stop.feed_id)
AND stop.feed_id = stop_time.feed_id
ORDER BY stop_sequence ASC"#;
println!("Query (trip_uid = {}): {}", trip_uid, query);
let connection = pool.clone().get().unwrap();
let stop_trips = connection.query(query, &[&trip_uid]);
let mut stop_trip_result: Vec<StopTrip> = Vec::new();
for row in stop_trips.expect("Query failed").iter() {
let route = parse_stop_trip_row(&row);
stop_trip_result.push(route);
}
stop_trip_result
}
fn parse_trip_row(row: &Row) -> Trip {
let mut t = Trip::new(
row.get(0),
row.get(1),
row.get(2),
row.get(4),
row.get(5),
row.get(6),
);
t.set_id(row.get(3));
t.set_feed_id(row.get(7));
t
}
fn parse_stop_trip_trip_row<'a>(trips: &'a mut BTreeMap<Trip, Vec<StopTrip>>, row: &Row) {
let mut new_vec: Vec<StopTrip>;
let mut stop_time_v = Vec::new();
let mut stop = Stop::new(
row.get(7),
row.get(9),
row.get(10),
row.get(11),
row.get(12),
row.get(13),
);
let mut t = Trip::new(
row.get(0),
row.get(1),
row.get(2),
row.get(3),
row.get(4),
row.get(5),
);
t.set_feed_id(row.get(6));
stop.set_id(row.get(8));
stop.set_feed_id(row.get(6));
stop.set_feed_id(row.get(6));
let drop_off_i: Option<i32> = row.get(17);
let pickup_i: Option<i32> = row.get(18);
let drop_off: DropOff = match drop_off_i {
Some(n) => {
num::FromPrimitive::from_i32(n).unwrap()
},
None => {
DropOff::RegularlyScheduled
}
};
let pickup: PickUp = match pickup_i {
Some(n) => {
num::FromPrimitive::from_i32(n).unwrap()
},
None => {
PickUp::RegularlyScheduled
}
};
let arrival_time: NaiveTime = row.get(14);
let departure_time: NaiveTime = row.get(15);
let stop_trip = StopTrip {
stop,
arrival_time,
departure_time,
stop_sequence: row.get(16),
drop_off,
pickup,
};
if trips.contains_key(&t) {
let value: Vec<StopTrip> = trips.get(&t).unwrap().to_vec();
new_vec = value.to_vec();
new_vec.push(stop_trip);
trips.insert(t, new_vec);
} else {
stop_time_v.push(stop_trip);
trips.insert(t, stop_time_v);
}
}
fn parse_stop_trip_row(row: &Row) -> StopTrip {
let stop = Stop::new(
row.get(0),
row.get(1),
row.get(2),
row.get(3),
row.get(4),
row.get(5),
);
let drop_off_i: Option<i32> = row.get(7);
let pickup_i: Option<i32> = row.get(8);
let drop_off : DropOff;
let pickup : PickUp;
if drop_off_i.is_some() {
drop_off = num::FromPrimitive::from_i32(drop_off_i.unwrap()).unwrap();
} else {
drop_off = DropOff::RegularlyScheduled;
}
if pickup_i.is_some() {
pickup = num::FromPrimitive::from_i32(pickup_i.unwrap()).unwrap();
} else {
pickup = PickUp::RegularlyScheduled;
}
let arrival_time: NaiveTime = row.get(9);
let departure_time: NaiveTime = row.get(10);
let st = StopTrip {
stop,
arrival_time,
departure_time,
stop_sequence: row.get(6),
drop_off,
pickup,
};
st
}