Fix summary generation when empty strings are present.
Also corrects step ordering when steps have equal durations.
This commit is contained in:
parent
aab1aad8f4
commit
f251f93a11
@ -71,6 +71,7 @@ module.exports = function () {
|
|||||||
r.status = res.statusCode === 200 ? 'x' : null;
|
r.status = res.statusCode === 200 ? 'x' : null;
|
||||||
if (r.status) {
|
if (r.status) {
|
||||||
r.route = this.wayList(r.json.routes[0]);
|
r.route = this.wayList(r.json.routes[0]);
|
||||||
|
r.summary = r.json.routes[0].legs.map(l => l.summary).join(',');
|
||||||
|
|
||||||
if (r.route.split(',')[0] === util.format('w%d', i)) {
|
if (r.route.split(',')[0] === util.format('w%d', i)) {
|
||||||
r.time = r.json.routes[0].duration;
|
r.time = r.json.routes[0].duration;
|
||||||
|
@ -127,6 +127,12 @@ module.exports = function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.summary = (instructions) => {
|
||||||
|
if (instructions) {
|
||||||
|
return instructions.legs.map(l => l.summary).join(',');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.wayList = (instructions) => {
|
this.wayList = (instructions) => {
|
||||||
return this.extractInstructionList(instructions, s => s.name);
|
return this.extractInstructionList(instructions, s => s.name);
|
||||||
};
|
};
|
||||||
|
@ -31,7 +31,7 @@ module.exports = function () {
|
|||||||
var afterRequest = (err, res, body) => {
|
var afterRequest = (err, res, body) => {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (body && body.length) {
|
if (body && body.length) {
|
||||||
var instructions, bearings, turns, modes, times, distances;
|
var instructions, bearings, turns, modes, times, distances, summary;
|
||||||
|
|
||||||
var json = JSON.parse(body);
|
var json = JSON.parse(body);
|
||||||
|
|
||||||
@ -44,6 +44,7 @@ module.exports = function () {
|
|||||||
modes = this.modeList(json.routes[0]);
|
modes = this.modeList(json.routes[0]);
|
||||||
times = this.timeList(json.routes[0]);
|
times = this.timeList(json.routes[0]);
|
||||||
distances = this.distanceList(json.routes[0]);
|
distances = this.distanceList(json.routes[0]);
|
||||||
|
summary = this.summary(json.routes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headers.has('status')) {
|
if (headers.has('status')) {
|
||||||
@ -66,6 +67,10 @@ module.exports = function () {
|
|||||||
if (headers.has('route')) {
|
if (headers.has('route')) {
|
||||||
got.route = (instructions || '').trim();
|
got.route = (instructions || '').trim();
|
||||||
|
|
||||||
|
if (headers.has('summary')) {
|
||||||
|
got.summary = (summary || '').trim();
|
||||||
|
}
|
||||||
|
|
||||||
if (headers.has('alternative')) {
|
if (headers.has('alternative')) {
|
||||||
// TODO examine more than first alternative?
|
// TODO examine more than first alternative?
|
||||||
got.alternative ='';
|
got.alternative ='';
|
||||||
|
54
features/testbot/summary.feature
Normal file
54
features/testbot/summary.feature
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
@routing @basic @testbot
|
||||||
|
Feature: Basic Routing
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given the profile "testbot"
|
||||||
|
|
||||||
|
@smallest
|
||||||
|
Scenario: Checking
|
||||||
|
Given the node map
|
||||||
|
| a | b | | c | d | e |
|
||||||
|
|
||||||
|
And the ways
|
||||||
|
| nodes |
|
||||||
|
| ab |
|
||||||
|
| bc |
|
||||||
|
| cd |
|
||||||
|
| de |
|
||||||
|
|
||||||
|
When I route I should get
|
||||||
|
| from | to | route | summary |
|
||||||
|
| a | e | ab,bc,cd,de,de | ab, bc |
|
||||||
|
| e | a | de,cd,bc,ab,ab | de, bc |
|
||||||
|
| a | b | ab,ab | ab |
|
||||||
|
| b | d | bc,cd,cd | bc, cd |
|
||||||
|
|
||||||
|
@smallest
|
||||||
|
Scenario: Check handling empty values
|
||||||
|
Given the node map
|
||||||
|
| a | b | | c | d | e | f |
|
||||||
|
|
||||||
|
And the ways
|
||||||
|
| nodes | name |
|
||||||
|
| ab | ab |
|
||||||
|
| bc | bc |
|
||||||
|
| cd | cd |
|
||||||
|
| de | de |
|
||||||
|
| ef | |
|
||||||
|
|
||||||
|
When I route I should get
|
||||||
|
| f | a | ,de,cd,bc,ab,ab | de, bc |
|
||||||
|
|
||||||
|
@smallest @todo
|
||||||
|
Scenario: Summaries when routing on a simple network
|
||||||
|
Given the node map
|
||||||
|
| a | b |
|
||||||
|
|
||||||
|
And the ways
|
||||||
|
| nodes |
|
||||||
|
| ab |
|
||||||
|
|
||||||
|
When I route I should get
|
||||||
|
| from | to | route | summary |
|
||||||
|
| a | b | ab,ab | ab |
|
||||||
|
| b | a | ab,ab | ab |
|
@ -34,6 +34,7 @@ struct NamedSegment
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t SegmentNumber>
|
template <std::size_t SegmentNumber>
|
||||||
|
|
||||||
std::array<std::uint32_t, SegmentNumber> summarizeRoute(const std::vector<PathData> &route_data)
|
std::array<std::uint32_t, SegmentNumber> summarizeRoute(const std::vector<PathData> &route_data)
|
||||||
{
|
{
|
||||||
// merges segments with same name id
|
// merges segments with same name id
|
||||||
@ -41,7 +42,14 @@ std::array<std::uint32_t, SegmentNumber> summarizeRoute(const std::vector<PathDa
|
|||||||
{
|
{
|
||||||
auto out = segments.begin();
|
auto out = segments.begin();
|
||||||
auto end = segments.end();
|
auto end = segments.end();
|
||||||
for (auto in = segments.begin(); in != end; ++in)
|
|
||||||
|
// Do nothing if we were given an empty array
|
||||||
|
if (out == end)
|
||||||
|
{
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto in = std::next(out); in != end; ++in)
|
||||||
{
|
{
|
||||||
if (in->name_id == out->name_id)
|
if (in->name_id == out->name_id)
|
||||||
{
|
{
|
||||||
@ -54,7 +62,8 @@ std::array<std::uint32_t, SegmentNumber> summarizeRoute(const std::vector<PathDa
|
|||||||
*out = *in;
|
*out = *in;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
BOOST_ASSERT(out != end);
|
||||||
|
return ++out;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<NamedSegment> segments(route_data.size());
|
std::vector<NamedSegment> segments(route_data.size());
|
||||||
@ -72,10 +81,19 @@ std::array<std::uint32_t, SegmentNumber> summarizeRoute(const std::vector<PathDa
|
|||||||
});
|
});
|
||||||
auto new_end = collapse_segments(segments);
|
auto new_end = collapse_segments(segments);
|
||||||
segments.resize(new_end - segments.begin());
|
segments.resize(new_end - segments.begin());
|
||||||
|
|
||||||
|
// Filter out segments with an empty name (name_id == 0)
|
||||||
|
new_end = std::remove_if(segments.begin(), segments.end(), [](const NamedSegment &segment)
|
||||||
|
{
|
||||||
|
return segment.name_id == 0;
|
||||||
|
});
|
||||||
|
segments.resize(new_end - segments.begin());
|
||||||
|
|
||||||
// sort descending
|
// sort descending
|
||||||
std::sort(segments.begin(), segments.end(), [](const NamedSegment &lhs, const NamedSegment &rhs)
|
std::sort(segments.begin(), segments.end(), [](const NamedSegment &lhs, const NamedSegment &rhs)
|
||||||
{
|
{
|
||||||
return lhs.duration > rhs.duration;
|
return lhs.duration > rhs.duration ||
|
||||||
|
(lhs.duration == rhs.duration && lhs.position < rhs.position);
|
||||||
});
|
});
|
||||||
|
|
||||||
// make sure the segments are sorted by position
|
// make sure the segments are sorted by position
|
||||||
|
Loading…
Reference in New Issue
Block a user