Skip to content

Commit 66cbff5

Browse files
Bycobmergify[bot]
authored andcommitted
feat(api): add labels in service info
labels are not returned per default, call service info with qery parameter labels=1
1 parent 8cfd86b commit 66cbff5

File tree

7 files changed

+57
-13
lines changed

7 files changed

+57
-13
lines changed

src/dto/info.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ namespace dd
116116
DTO_FIELD(Object<ServiceModel>, model_stats);
117117
DTO_FIELD(Vector<DTOApiData>, jobs);
118118

119+
DTO_FIELD_INFO(labels)
120+
{
121+
info->description
122+
= "Labels for classification / detection / segmentation services";
123+
}
124+
DTO_FIELD(Vector<String>, labels);
125+
119126
DTO_FIELD(String, repository);
120127
DTO_FIELD(Int32, width);
121128
DTO_FIELD(Int32, height);

src/http/controller.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DedeController : public oatpp::web::server::api::ApiController
7878
oatpp::String qs_status = queryParams.get("status");
7979
bool status = false;
8080
if (qs_status)
81-
status = boost::lexical_cast<bool>(std::string(qs_status));
81+
status = boost::lexical_cast<bool>(*qs_status);
8282

8383
auto hit = _oja->_mlservices.begin();
8484
while (hit != _oja->_mlservices.end())
@@ -96,9 +96,20 @@ class DedeController : public oatpp::web::server::api::ApiController
9696
info->summary = "Retrieve a service detail";
9797
}
9898
ENDPOINT("GET", "services/{service-name}", get_service,
99-
PATH(oatpp::String, service_name, "service-name"))
99+
PATH(oatpp::String, service_name, "service-name"),
100+
QUERIES(QueryParams, queryParams))
100101
{
101-
auto janswer = _oja->service_status(service_name);
102+
oatpp::String qs_status = queryParams.get("status");
103+
bool status = true;
104+
if (qs_status)
105+
status = boost::lexical_cast<bool>(*qs_status);
106+
107+
oatpp::String qs_labels = queryParams.get("labels");
108+
bool labels = false;
109+
if (qs_labels)
110+
labels = boost::lexical_cast<bool>(*qs_labels);
111+
112+
auto janswer = _oja->service_status(service_name, status, labels);
102113
return _oja->jdoc_to_response(janswer);
103114
}
104115

src/jsonapi.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ namespace dd
972972
return jsc;
973973
}
974974

975-
JDoc JsonAPI::service_status(const std::string &snamein)
975+
JDoc JsonAPI::service_status(const std::string &snamein, bool status,
976+
bool labels)
976977
{
977978
std::string sname(snamein);
978979
std::transform(snamein.begin(), snamein.end(), sname.begin(), ::tolower);
@@ -982,8 +983,8 @@ namespace dd
982983
if (!this->service_exists(sname))
983984
return dd_service_not_found_1002(sname);
984985
auto hit = this->get_service_it(sname);
985-
auto status_dto
986-
= mapbox::util::apply_visitor(visitor_info(true), (*hit).second);
986+
auto status_dto = mapbox::util::apply_visitor(visitor_info(status, labels),
987+
(*hit).second);
987988
JDoc jst = dd_ok_200();
988989
JVal jbody(rapidjson::kObjectType);
989990
oatpp_utils::dtoToJVal(status_dto, jst, jbody);
@@ -994,7 +995,6 @@ namespace dd
994995
JDoc JsonAPI::service_delete(const std::string &snamein,
995996
const std::string &jstr)
996997
{
997-
998998
std::string sname(snamein);
999999
std::transform(snamein.begin(), snamein.end(), sname.begin(), ::tolower);
10001000

src/jsonapi.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ namespace dd
9595
// return a JSON document for every API call
9696
JDoc info(const std::string &jstr) const;
9797
JDoc service_create(const std::string &sname, const std::string &jstr);
98-
JDoc service_status(const std::string &sname);
98+
JDoc service_status(const std::string &sname, bool status = true,
99+
bool labels = false);
100+
JDoc service_labels(const std::string &sname);
99101
JDoc service_delete(const std::string &sname, const std::string &jstr);
100-
101102
JDoc service_predict(const std::string &jstr);
102103

103104
JDoc service_train(const std::string &jstr);
@@ -129,7 +130,8 @@ namespace dd
129130
class visitor_info
130131
{
131132
public:
132-
visitor_info(const bool &status) : _status(status)
133+
visitor_info(const bool &status, const bool &labels = false)
134+
: _status(status), _labels(labels)
133135
{
134136
}
135137
~visitor_info()
@@ -138,9 +140,10 @@ namespace dd
138140

139141
template <typename T> oatpp::Object<DTO::Service> operator()(T &mllib)
140142
{
141-
return mllib.info(_status);
143+
return mllib.info(_status, _labels);
142144
}
143145
bool _status = false;
146+
bool _labels = false;
144147
};
145148
}
146149

src/mlservice.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ namespace dd
193193
* \brief get info about the service
194194
* @return info data object
195195
*/
196-
oatpp::Object<DTO::Service> info(const bool &status) const
196+
oatpp::Object<DTO::Service> info(const bool &status,
197+
const bool &labels = false) const
197198
{
198199
// general info
199200
auto serv_dto = DTO::Service::createShared();
@@ -272,6 +273,23 @@ namespace dd
272273
}
273274
}
274275

276+
// labels
277+
if (labels)
278+
{
279+
auto labels_vec = oatpp::Vector<oatpp::String>::createShared();
280+
281+
if (!this->_mlmodel._hcorresp.empty())
282+
{
283+
labels_vec->reserve(this->_mlmodel._hcorresp.size());
284+
285+
for (const auto &kv : this->_mlmodel._hcorresp)
286+
{
287+
labels_vec->push_back(kv.second);
288+
}
289+
}
290+
serv_dto->labels = labels_vec;
291+
}
292+
275293
// stats
276294
this->_stats.to(serv_dto);
277295
return serv_dto;

tests/ut-oatpp.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
7979
ASSERT_EQ(201, d["status"]["code"].GetInt());
8080

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

103105
// info call
104106
response = client->get_info();

tests/ut-oatpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class DedeApiTestClient : public oatpp::web::client::ApiClient
9797
API_CALL("GET", "/info", get_info)
9898
API_CALL("GET", "/services/{service-name}", get_services,
9999
PATH(oatpp::String, service_name, "service-name"))
100+
API_CALL("GET", "/services/{service-name}", get_service_with_labels,
101+
PATH(oatpp::String, service_name, "service-name"),
102+
QUERY(String, labels))
100103
API_CALL("POST", "/services/{service-name}", post_services,
101104
PATH(oatpp::String, service_name, "service-name"),
102105
BODY_STRING(oatpp::String, service_data))

0 commit comments

Comments
 (0)