osrm-backend/src/server/service/table_service.cpp
Kajari Ghosh 2e17f3010a
Add a multiplier to the matrix (#5298)
* add a multiplier to the matrix

* add rounding

* remove scale_factor restrictions

* clamp for overflow error

* update check to match error message

* enforce clamping on < 0 and increase test coverage

* add an invalid scale_factor value to node tests

* increase test coverage

* changelog
2018-12-10 13:41:44 -05:00

105 lines
2.9 KiB
C++

#include "server/service/table_service.hpp"
#include "server/api/parameters_parser.hpp"
#include "engine/api/table_parameters.hpp"
#include "util/json_container.hpp"
#include <boost/format.hpp>
namespace osrm
{
namespace server
{
namespace service
{
namespace
{
const constexpr char PARAMETER_SIZE_MISMATCH_MSG[] =
"Number of elements in %1% size %2% does not match coordinate size %3%";
template <typename ParamT>
bool constrainParamSize(const char *msg_template,
const char *name,
const ParamT &param,
const std::size_t target_size,
std::string &help)
{
if (param.size() > 0 && param.size() != target_size)
{
help = (boost::format(msg_template) % name % param.size() % target_size).str();
return true;
}
return false;
}
std::string getWrongOptionHelp(const engine::api::TableParameters &parameters)
{
std::string help;
const auto coord_size = parameters.coordinates.size();
const bool param_size_mismatch =
constrainParamSize(
PARAMETER_SIZE_MISMATCH_MSG, "hints", parameters.hints, coord_size, help) ||
constrainParamSize(
PARAMETER_SIZE_MISMATCH_MSG, "bearings", parameters.bearings, coord_size, help) ||
constrainParamSize(
PARAMETER_SIZE_MISMATCH_MSG, "radiuses", parameters.radiuses, coord_size, help) ||
constrainParamSize(
PARAMETER_SIZE_MISMATCH_MSG, "approaches", parameters.approaches, coord_size, help);
if (!param_size_mismatch && parameters.coordinates.size() < 2)
{
help = "Number of coordinates needs to be at least two.";
}
if (parameters.fallback_speed < 0)
{
help = "fallback_speed must be > 0";
}
if (parameters.scale_factor <= 0)
{
help = "scale_factor must be > 0";
}
return help;
}
} // anon. ns
engine::Status
TableService::RunQuery(std::size_t prefix_length, std::string &query, ResultT &result)
{
result = util::json::Object();
auto &json_result = result.get<util::json::Object>();
auto query_iterator = query.begin();
auto parameters =
api::parseParameters<engine::api::TableParameters>(query_iterator, query.end());
if (!parameters || query_iterator != query.end())
{
const auto position = std::distance(query.begin(), query_iterator);
json_result.values["code"] = "InvalidQuery";
json_result.values["message"] =
"Query string malformed close to position " + std::to_string(prefix_length + position);
return engine::Status::Error;
}
BOOST_ASSERT(parameters);
if (!parameters->IsValid())
{
json_result.values["code"] = "InvalidOptions";
json_result.values["message"] = getWrongOptionHelp(*parameters);
return engine::Status::Error;
}
BOOST_ASSERT(parameters->IsValid());
return BaseService::routing_machine.Table(*parameters, json_result);
}
}
}
}