Skip to content

Commit

Permalink
feat: Add multiple methods of new services (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiakiteaneo authored Feb 19, 2024
2 parents 2d2c012 + f9b8d52 commit 3d41a08
Show file tree
Hide file tree
Showing 12 changed files with 652 additions and 18 deletions.
21 changes: 21 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/header/events/EventsClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "events_common.pb.h"
#include "events_service.grpc.pb.h"

namespace armonik {
namespace api {
namespace client {
class EventsClient {
public:
explicit EventsClient(std::unique_ptr<armonik::api::grpc::v1::events::Events::StubInterface> stub)
: stub(std::move(stub)) {}

void wait_for_result_availability(std::string session_id, std::vector<std::string> result_ids);

private:
std::unique_ptr<armonik::api::grpc::v1::events::Events::StubInterface> stub;
};
} // namespace client
} // namespace api
} // namespace armonik
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "partitions_common.pb.h"
#include "partitions_service.grpc.pb.h"

namespace armonik {
namespace api {
namespace client {

class PartitionsClient {
public:
explicit PartitionsClient(std::unique_ptr<armonik::api::grpc::v1::partitions::Partitions::StubInterface> stub)
: stub(std::move(stub)){};
std::vector<armonik::api::grpc::v1::partitions::PartitionRaw>
list_partitions(armonik::api::grpc::v1::partitions::Filters filters, int32_t &total, int32_t page = -1,
int32_t page_size = 500,
armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort = default_sort());

armonik::api::grpc::v1::partitions::PartitionRaw get_partition(std::string partition_id);

private:
std::unique_ptr<armonik::api::grpc::v1::partitions::Partitions::StubInterface> stub;
static armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort default_sort();
};
} // namespace client
} // namespace api
} // namespace armonik
51 changes: 51 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,57 @@ class SessionsClient {
int32_t page_size = 500,
armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort sort = default_sort);

/**
* Pause a session
*
* @param session_id Session Id
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw pause_session(std::string session_id);

/**
* Resume a session
*
* @param session_id Session Id
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw resume_session(std::string session_id);

/**
* Purge a session
*
* @param session_id Session Id
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw purge_session(std::string session_id);

/**
* Delete a session
*
* @param session_id Session Id
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw delete_session(std::string session_id);

/**
* Stop a new tasks submission in a session
*
* @param session_id Session Id
* @param client boolean to stop client's task submission
* @param worker boolean to stop worker's task submissions
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id, bool client = true,
bool worker = true);

/**
* Resume a session
*
* @param session_id Session Id
* @return SessionRaw object containing information about the session
*/
armonik::api::grpc::v1::sessions::SessionRaw close_session(std::string session_id);

private:
std::unique_ptr<armonik::api::grpc::v1::sessions::Sessions::StubInterface> stub;
static const armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort default_sort;
Expand Down
39 changes: 39 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "versions_common.pb.h"
#include "versions_service.grpc.pb.h"

namespace armonik {
namespace api {
namespace client {

/**
* @brief Data structure for components version
* @param api ArmoniK API version
* @param core ArmoniK CORE version
*/
struct versions_info {
std::string api;
std::string core;
};

/**
* Versions Client wrapper
*/
class VersionsClient {
public:
explicit VersionsClient(std::unique_ptr<armonik::api::grpc::v1::versions::Versions::StubInterface> stub)
: stub(std::move(stub)){};

/**
* Get versions of ArmoniK components
* @return Mapping between component names and their versions
*/
versions_info list_versions();

private:
std::unique_ptr<armonik::api::grpc::v1::versions::Versions::StubInterface> stub;
};
} // namespace client
} // namespace api
} // namespace armonik
82 changes: 82 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "events/EventsClient.h"
#include "events_common.pb.h"
#include "events_service.grpc.pb.h"
#include "exceptions/ArmoniKApiException.h"
#include "objects.pb.h"

using armonik::api::grpc::v1::events::EventSubscriptionRequest;
using armonik::api::grpc::v1::events::EventSubscriptionResponse;
using armonik::api::grpc::v1::result_status::ResultStatus;
using namespace armonik::api::grpc::v1::events;

namespace armonik {
namespace api {
namespace client {

void EventsClient::wait_for_result_availability(std::string session_id, std::vector<std::string> result_ids) {

::grpc::ClientContext context;
EventSubscriptionRequest request;

EventSubscriptionResponse response;

armonik::api::grpc::v1::results::Filters filters;
armonik::api::grpc::v1::results::FilterField filter_field;
filter_field.mutable_field()->mutable_result_raw_field()->set_field(
armonik::api::grpc::v1::results::RESULT_RAW_ENUM_FIELD_RESULT_ID);
filter_field.mutable_filter_string()->set_operator_(grpc::v1::FILTER_STRING_OPERATOR_EQUAL);
for (auto &&result_id : result_ids) {
filter_field.mutable_filter_string()->set_value(result_id);
*filters.mutable_or_()->Add()->mutable_and_()->Add() = filter_field;
}

*request.mutable_session_id() = std::move(session_id);
*request.mutable_results_filters() = filters;
request.add_returned_events(static_cast<EventsEnum>(EventSubscriptionResponse::UpdateCase::kResultStatusUpdate));
request.add_returned_events(static_cast<EventsEnum>(EventSubscriptionResponse::UpdateCase::kNewResult));

auto stream = stub->GetEvents(&context, request);
if (!stream) {
throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted");
}

while (stream->Read(&response)) {
std::string update_or_new;
switch (response.update_case()) {
case EventSubscriptionResponse::UpdateCase::kResultStatusUpdate:
switch (response.mutable_result_status_update()->status()) {
case ResultStatus::RESULT_STATUS_COMPLETED:
update_or_new = response.mutable_result_status_update()->result_id();
break;
case ResultStatus::RESULT_STATUS_ABORTED:
throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted");
default:
break;
}
break;
case EventSubscriptionResponse::UpdateCase::kNewResult:
switch (response.mutable_new_result()->status()) {
case ResultStatus::RESULT_STATUS_COMPLETED:
update_or_new = response.mutable_new_result()->result_id();
break;
case ResultStatus::RESULT_STATUS_ABORTED:
throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted");
default:
break;
}
break;
default:
break;
}
if (!update_or_new.empty()) {
result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), update_or_new), result_ids.end());
if (result_ids.empty()) {
break;
}
}
}
}

} // namespace client
} // namespace api
} // namespace armonik
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <utility>

#include "exceptions/ArmoniKApiException.h"
#include "partitions/PartitionsClient.h"

using namespace armonik::api::grpc::v1::partitions;

namespace armonik {
namespace api {
namespace client {

std::vector<PartitionRaw> PartitionsClient::list_partitions(Filters filters, int32_t &total, int32_t page,
int32_t page_size, ListPartitionsRequest::Sort sort) {
::grpc::ClientContext context;
ListPartitionsRequest request;
ListPartitionsResponse response;

*request.mutable_filters() = std::move(filters);
*request.mutable_sort() = std::move(sort);
request.set_page_size(page_size);

if (page >= 0) {
request.set_page(page);
::grpc::ClientContext context;
auto status = stub->ListPartitions(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list partitions " +
status.error_message());
}
total = response.total();
return {std::make_move_iterator(response.partitions().begin()),
std::make_move_iterator(response.partitions().end())};
} else {
std::vector<PartitionRaw> rawPartitions;
int current_page = 0;
do {
request.set_page(current_page);
::grpc::ClientContext context;
auto status = stub->ListPartitions(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list partitions " +
status.error_message());
}
rawPartitions.insert(rawPartitions.end(), std::make_move_iterator(response.partitions().begin()),
std::make_move_iterator(response.partitions().end()));
if (response.partitions_size() >= page_size) {
current_page++;
}

response.clear_partitions();
} while ((int32_t)rawPartitions.size() < response.total());

total = response.total();

return rawPartitions;
}
}

PartitionRaw PartitionsClient::get_partition(std::string partition_id) {
::grpc::ClientContext context;
GetPartitionRequest request;
GetPartitionResponse response;

*request.mutable_id() = std::move(partition_id);
auto status = stub->GetPartition(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Could not get partition : " + status.error_message());
}

return std::move(*response.mutable_partition());
}

ListPartitionsRequest::Sort PartitionsClient::default_sort() {
ListPartitionsRequest::Sort sort;
sort.set_direction(grpc::v1::sort_direction::SORT_DIRECTION_ASC);
sort.mutable_field()->mutable_partition_raw_field()->set_field(
grpc::v1::partitions::PARTITION_RAW_ENUM_FIELD_PRIORITY);
return sort;
}

} // namespace client
} // namespace api
} // namespace armonik
Loading

0 comments on commit 3d41a08

Please sign in to comment.