Skip to content

Commit 5271615

Browse files
railviz realtime architecture (#558)
* railviz realtime architecture * wip
1 parent 1305aea commit 5271615

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

modules/nigiri/src/railviz.cc

+24-16
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ struct rt_transport_geo_index {
176176
rt_rtree rtree_{};
177177
};
178178

179+
struct realtime {
180+
std::array<rt_transport_geo_index, n::kNumClasses> rt_geo_indices_;
181+
n::vector_map<n::rt_transport_idx_t, float> rt_distances_{};
182+
std::shared_ptr<n::rt_timetable> rtt_;
183+
};
184+
179185
struct railviz::impl {
180186
impl(tag_lookup const& tags, n::timetable const& tt,
181187
std::unique_ptr<n::shapes_storage>&& shapes_data)
@@ -191,7 +197,8 @@ struct railviz::impl {
191197
using motis::railviz::RailVizTripsRequest;
192198
auto const* req = motis_content(RailVizTripsRequest, msg);
193199

194-
auto rtt = rtt__;
200+
auto const rt = rt_;
201+
auto const rtt = rt->rtt_;
195202

196203
auto runs = std::vector<stop_pair>{};
197204
for (auto const t : *req->trips()) {
@@ -213,7 +220,7 @@ struct railviz::impl {
213220
});
214221
}
215222
}
216-
return create_response(rtt, runs);
223+
return create_response(*rt, *rtt, runs);
217224
}
218225

219226
mm::msg_ptr get_trains(mm::msg_ptr const& msg) {
@@ -235,7 +242,8 @@ struct railviz::impl {
235242

236243
mm::msg_ptr get_trains(n::interval<n::unixtime_t> time_interval,
237244
geo::box const& area, int const zoom_level) {
238-
auto const rtt = rtt__;
245+
auto const rt = rt_;
246+
auto const rtt = rt->rtt_;
239247

240248
auto runs = std::vector<stop_pair>{};
241249
for (auto c = int_clasz{0U}; c != n::kNumClasses; ++c) {
@@ -247,8 +255,8 @@ struct railviz::impl {
247255

248256
if (rtt != nullptr) {
249257
for (auto const& rt_t :
250-
rt_geo_indices_[c].get_rt_transports(*rtt, area)) {
251-
if (should_display(cl, zoom_level, rt_distances_[rt_t])) {
258+
rt->rt_geo_indices_[c].get_rt_transports(*rtt, area)) {
259+
if (should_display(cl, zoom_level, rt->rt_distances_[rt_t])) {
252260
add_rt_transports(rtt, rt_t, time_interval, area, runs);
253261
}
254262
}
@@ -260,10 +268,10 @@ struct railviz::impl {
260268
}
261269
}
262270
}
263-
return create_response(rtt, runs);
271+
return create_response(*rt, *rtt, runs);
264272
}
265273

266-
mm::msg_ptr create_response(std::shared_ptr<n::rt_timetable> const& rtt,
274+
mm::msg_ptr create_response(realtime const& rt, n::rt_timetable const& rtt,
267275
std::vector<stop_pair> const& runs) const {
268276
geo::polyline_encoder<6> enc;
269277

@@ -291,7 +299,7 @@ struct railviz::impl {
291299
auto fbs_polylines = std::vector<fbs::Offset<fbs::String>>{
292300
mc.CreateString("") /* no zero, zero doesn't have a sign=direction */};
293301
auto const trains = utl::to_vec(runs, [&](stop_pair const& r) {
294-
auto const fr = n::rt::frun{tt_, rtt.get(), r.r_};
302+
auto const fr = n::rt::frun{tt_, &rtt, r.r_};
295303

296304
auto const from = fr[r.from_];
297305
auto const to = fr[r.to_];
@@ -311,7 +319,7 @@ struct railviz::impl {
311319
return motis::railviz::CreateTrain(
312320
mc, mc.CreateVector(std::vector{mc.CreateString(fr.name())}),
313321
static_cast<int>(fr.get_clasz()),
314-
fr.is_rt() ? rt_distances_[fr.rt_]
322+
fr.is_rt() ? rt.rt_distances_[fr.rt_]
315323
: static_distances_[tt_.transport_route_[fr.t_.t_idx_]],
316324
mc.CreateString(get_station_id(tags_, tt_, from_l)),
317325
mc.CreateString(get_station_id(tags_, tt_, to_l)),
@@ -413,22 +421,22 @@ struct railviz::impl {
413421
}
414422

415423
void update(std::shared_ptr<n::rt_timetable> const& rtt) {
416-
rtt__ = rtt;
417-
rt_distances_.resize(rtt__->rt_transport_location_seq_.size());
424+
auto rt = std::make_shared<realtime>();
425+
rt->rtt_ = rtt;
426+
rt->rt_distances_.resize(rtt->rt_transport_location_seq_.size());
418427
for (auto c = int_clasz{0U}; c != n::kNumClasses; ++c) {
419-
rt_geo_indices_[c] =
420-
rt_transport_geo_index{tt_, *rtt__, n::clasz{c}, rt_distances_};
428+
rt->rt_geo_indices_[c] =
429+
rt_transport_geo_index{tt_, *rtt, n::clasz{c}, rt->rt_distances_};
421430
}
431+
rt_ = rt;
422432
}
423433

424434
tag_lookup const& tags_;
425435
n::timetable const& tt_;
426436
std::unique_ptr<n::shapes_storage> shapes_data_;
427-
std::shared_ptr<n::rt_timetable> rtt__;
437+
std::shared_ptr<realtime> rt_;
428438
std::array<route_geo_index, n::kNumClasses> static_geo_indices_;
429-
std::array<rt_transport_geo_index, n::kNumClasses> rt_geo_indices_;
430439
n::vector_map<n::route_idx_t, float> static_distances_{};
431-
n::vector_map<n::rt_transport_idx_t, float> rt_distances_{};
432440
};
433441

434442
railviz::railviz(tag_lookup const& tags, n::timetable const& tt,

0 commit comments

Comments
 (0)