Skip to content

Commit

Permalink
feat(api): add labels in service info
Browse files Browse the repository at this point in the history
labels are not returned per default, call service info with qery parameter labels=1
  • Loading branch information
Bycob authored and mergify[bot] committed Mar 13, 2023
1 parent 8cfd86b commit 66cbff5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/dto/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ namespace dd
DTO_FIELD(Object<ServiceModel>, model_stats);
DTO_FIELD(Vector<DTOApiData>, jobs);

DTO_FIELD_INFO(labels)
{
info->description
= "Labels for classification / detection / segmentation services";
}
DTO_FIELD(Vector<String>, labels);

DTO_FIELD(String, repository);
DTO_FIELD(Int32, width);
DTO_FIELD(Int32, height);
Expand Down
17 changes: 14 additions & 3 deletions src/http/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DedeController : public oatpp::web::server::api::ApiController
oatpp::String qs_status = queryParams.get("status");
bool status = false;
if (qs_status)
status = boost::lexical_cast<bool>(std::string(qs_status));
status = boost::lexical_cast<bool>(*qs_status);

auto hit = _oja->_mlservices.begin();
while (hit != _oja->_mlservices.end())
Expand All @@ -96,9 +96,20 @@ class DedeController : public oatpp::web::server::api::ApiController
info->summary = "Retrieve a service detail";
}
ENDPOINT("GET", "services/{service-name}", get_service,
PATH(oatpp::String, service_name, "service-name"))
PATH(oatpp::String, service_name, "service-name"),
QUERIES(QueryParams, queryParams))
{
auto janswer = _oja->service_status(service_name);
oatpp::String qs_status = queryParams.get("status");
bool status = true;
if (qs_status)
status = boost::lexical_cast<bool>(*qs_status);

oatpp::String qs_labels = queryParams.get("labels");
bool labels = false;
if (qs_labels)
labels = boost::lexical_cast<bool>(*qs_labels);

auto janswer = _oja->service_status(service_name, status, labels);
return _oja->jdoc_to_response(janswer);
}

Expand Down
8 changes: 4 additions & 4 deletions src/jsonapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,8 @@ namespace dd
return jsc;
}

JDoc JsonAPI::service_status(const std::string &snamein)
JDoc JsonAPI::service_status(const std::string &snamein, bool status,
bool labels)
{
std::string sname(snamein);
std::transform(snamein.begin(), snamein.end(), sname.begin(), ::tolower);
Expand All @@ -982,8 +983,8 @@ namespace dd
if (!this->service_exists(sname))
return dd_service_not_found_1002(sname);
auto hit = this->get_service_it(sname);
auto status_dto
= mapbox::util::apply_visitor(visitor_info(true), (*hit).second);
auto status_dto = mapbox::util::apply_visitor(visitor_info(status, labels),
(*hit).second);
JDoc jst = dd_ok_200();
JVal jbody(rapidjson::kObjectType);
oatpp_utils::dtoToJVal(status_dto, jst, jbody);
Expand All @@ -994,7 +995,6 @@ namespace dd
JDoc JsonAPI::service_delete(const std::string &snamein,
const std::string &jstr)
{

std::string sname(snamein);
std::transform(snamein.begin(), snamein.end(), sname.begin(), ::tolower);

Expand Down
11 changes: 7 additions & 4 deletions src/jsonapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ namespace dd
// return a JSON document for every API call
JDoc info(const std::string &jstr) const;
JDoc service_create(const std::string &sname, const std::string &jstr);
JDoc service_status(const std::string &sname);
JDoc service_status(const std::string &sname, bool status = true,
bool labels = false);
JDoc service_labels(const std::string &sname);
JDoc service_delete(const std::string &sname, const std::string &jstr);

JDoc service_predict(const std::string &jstr);

JDoc service_train(const std::string &jstr);
Expand Down Expand Up @@ -129,7 +130,8 @@ namespace dd
class visitor_info
{
public:
visitor_info(const bool &status) : _status(status)
visitor_info(const bool &status, const bool &labels = false)
: _status(status), _labels(labels)
{
}
~visitor_info()
Expand All @@ -138,9 +140,10 @@ namespace dd

template <typename T> oatpp::Object<DTO::Service> operator()(T &mllib)
{
return mllib.info(_status);
return mllib.info(_status, _labels);
}
bool _status = false;
bool _labels = false;
};
}

Expand Down
20 changes: 19 additions & 1 deletion src/mlservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ namespace dd
* \brief get info about the service
* @return info data object
*/
oatpp::Object<DTO::Service> info(const bool &status) const
oatpp::Object<DTO::Service> info(const bool &status,
const bool &labels = false) const
{
// general info
auto serv_dto = DTO::Service::createShared();
Expand Down Expand Up @@ -272,6 +273,23 @@ namespace dd
}
}

// labels
if (labels)
{
auto labels_vec = oatpp::Vector<oatpp::String>::createShared();

if (!this->_mlmodel._hcorresp.empty())
{
labels_vec->reserve(this->_mlmodel._hcorresp.size());

for (const auto &kv : this->_mlmodel._hcorresp)
{
labels_vec->push_back(kv.second);
}
}
serv_dto->labels = labels_vec;
}

// stats
this->_stats.to(serv_dto);
return serv_dto;
Expand Down
4 changes: 3 additions & 1 deletion tests/ut-oatpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
ASSERT_EQ(201, d["status"]["code"].GetInt());

// service info
response = client->get_services(serv.c_str());
response = client->get_service_with_labels(serv.c_str(), "1");
message = response->readBodyToString();
ASSERT_TRUE(message != nullptr);
std::cout << "jstr=" << *message << std::endl;
Expand All @@ -99,6 +99,8 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
ASSERT_TRUE(d["body"]["parameters"].HasMember("output"));
ASSERT_EQ(d["body"]["parameters"]["input"]["connector"].GetString(),
std::string("image"));
ASSERT_TRUE(d["body"].HasMember("labels"));
ASSERT_EQ(d["body"]["labels"].Size(), 0);

// info call
response = client->get_info();
Expand Down
3 changes: 3 additions & 0 deletions tests/ut-oatpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class DedeApiTestClient : public oatpp::web::client::ApiClient
API_CALL("GET", "/info", get_info)
API_CALL("GET", "/services/{service-name}", get_services,
PATH(oatpp::String, service_name, "service-name"))
API_CALL("GET", "/services/{service-name}", get_service_with_labels,
PATH(oatpp::String, service_name, "service-name"),
QUERY(String, labels))
API_CALL("POST", "/services/{service-name}", post_services,
PATH(oatpp::String, service_name, "service-name"),
BODY_STRING(oatpp::String, service_data))
Expand Down

0 comments on commit 66cbff5

Please sign in to comment.