Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added list and delete results to cpp #423

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aneoconsultingfr/armonik.api.angular",
"version": "3.13.0",
"version": "3.13.1",
"description": "gRPC API to interact with ArmoniK built for Angular",
"license": "Apache-2.0",
"homepage": "https://github.com/aneoconsulting/ArmoniK.Api#readme",
Expand Down
27 changes: 27 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/header/submitter/ResultsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,35 @@ class ResultsClient {
const std::vector<std::string> &names);
void upload_result_data(const std::string &session_id, const std::string &result_id, absl::string_view payload);

/**
* Deletes the results data
* @param session_id Session id
* @param result_ids Result ids
*/
void delete_results(const std::string &session_id, const std::vector<std::string> &result_ids);

/**
* List the results
* @param filters Filter to be used
* @param total Output for the total of results available from this request (used for pagination)
* @param page Page to request, use -1 to get all pages.
* @param page_size Size of the requested page, ignored if page is -1
* @param sort How the results are sorted, ascending creation date by default
* @return List of results
*
* @note If the results corresponding to the filters change while this call is going for page==-1,
* or between calls, then the returned values may not be consistent depending on the sorting used.
* For example, a sort by ascending creation date (the default) will be stable if results are being created in between
* requests.
*/
std::vector<armonik::api::grpc::v1::results::ResultRaw>
list_results(const armonik::api::grpc::v1::results::Filters &filters, int32_t &total, int32_t page = -1,
int32_t page_size = 500,
const armonik::api::grpc::v1::results::ListResultsRequest::Sort &sort = get_default_sort());

private:
std::unique_ptr<armonik::api::grpc::v1::results::Results::Stub> stub;
static armonik::api::grpc::v1::results::ListResultsRequest::Sort get_default_sort();
};
} // namespace client
} // namespace api
Expand Down
71 changes: 71 additions & 0 deletions packages/cpp/ArmoniK.Api.Client/source/submitter/ResultsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,77 @@ void ResultsClient::upload_result_data(const std::string &session_id, const std:
status.error_message());
}
}

void ResultsClient::delete_results(const std::string &session_id, const std::vector<std::string> &result_ids) {
if (result_ids.empty()) {
return;
}

::grpc::ClientContext context;
armonik::api::grpc::v1::results::DeleteResultsDataRequest request;
armonik::api::grpc::v1::results::DeleteResultsDataResponse response;
*request.mutable_session_id() = session_id;
request.mutable_result_id()->Add(result_ids.begin(), result_ids.end());

auto status = stub->DeleteResultsData(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Unable to delete results " + status.error_message());
}
}

std::vector<armonik::api::grpc::v1::results::ResultRaw>
aneojgurhem marked this conversation as resolved.
Show resolved Hide resolved
ResultsClient::list_results(const grpc::v1::results::Filters &filters, int32_t &total, int32_t page, int32_t page_size,
const grpc::v1::results::ListResultsRequest::Sort &sort) {
armonik::api::grpc::v1::results::ListResultsRequest request;
armonik::api::grpc::v1::results::ListResultsResponse response;

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

if (page >= 0) {
request.set_page(page);
::grpc::ClientContext context;
auto status = stub->ListResults(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list results " + status.error_message());
}
total = response.total();
return {response.results().begin(), response.results().end()};
} else {
std::vector<armonik::api::grpc::v1::results::ResultRaw> rawResults;
int current_page = 0;
do {
request.set_page(current_page);
::grpc::ClientContext context;
auto status = stub->ListResults(&context, request, &response);
if (!status.ok()) {
throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list results " + status.error_message());
}
// Append only the additional results
// If the current_page is a re-request, this will add only the new information
rawResults.insert(rawResults.end(),
response.results().begin() + ((int32_t)rawResults.size() - current_page * page_size),
response.results().end());
if (response.results_size() >= page_size) {
++current_page;
}

response.clear_results();
} while ((int32_t)rawResults.size() < response.total());
total = response.total();
return rawResults;
}
}
armonik::api::grpc::v1::results::ListResultsRequest::Sort ResultsClient::get_default_sort() {
static armonik::api::grpc::v1::results::ListResultsRequest::Sort sort;
if (sort.direction() == grpc::v1::sort_direction::SORT_DIRECTION_UNSPECIFIED) {
sort.set_direction(grpc::v1::sort_direction::SORT_DIRECTION_ASC);
sort.mutable_field()->mutable_result_raw_field()->set_field(grpc::v1::results::RESULT_RAW_ENUM_FIELD_CREATED_AT);
}
return sort;
}

} // namespace client
} // namespace api
} // namespace armonik
8 changes: 0 additions & 8 deletions packages/cpp/ArmoniK.Api.Tests/header/SubmitterClientTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,3 @@ class MockStubInterface : public armonik::api::grpc::v1::submitter::Submitter::S
::armonik::api::grpc::v1::submitter::WatchResultStream> *),
PrepareAsyncWatchResultsRaw, (::grpc::ClientContext * context, ::grpc::CompletionQueue *cq));
};

/**
* @brief Initializes task options creates channel with server address
*
* @param channel The gRPC channel to communicate with the server.
* @param default_task_options The default task options.
*/
void init(std::shared_ptr<grpc::Channel> &channel, armonik::api::grpc::v1::TaskOptions &task_options);
15 changes: 15 additions & 0 deletions packages/cpp/ArmoniK.Api.Tests/header/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "logger/base.h"
#include "objects.pb.h"
#include <grpcpp/channel.h>
#include <memory>

/**
* @brief Initializes task options creates channel with server address
*
* @param channel The gRPC channel to communicate with the server.
* @param default_task_options The default task options.
*/
void init(std::shared_ptr<grpc::Channel> &channel, armonik::api::grpc::v1::TaskOptions &task_options,
armonik::api::common::logger::ILogger &logger);
89 changes: 89 additions & 0 deletions packages/cpp/ArmoniK.Api.Tests/source/ResultsClientTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "common.h"
#include "logger/formatter.h"
#include "logger/logger.h"
#include "logger/writer.h"

#include "results_service.grpc.pb.h"
#include "submitter/ResultsClient.h"
#include "submitter/SubmitterClient.h"

using Logger = armonik::api::common::logger::Logger;

TEST(Results, test_results_created) {
Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)};
std::shared_ptr<::grpc::Channel> channel;
armonik::api::grpc::v1::TaskOptions task_options;

init(channel, task_options, log);

auto client = armonik::api::client::ResultsClient(armonik::api::grpc::v1::results::Results::NewStub(channel));
auto sub_client =
armonik::api::client::SubmitterClient(armonik::api::grpc::v1::submitter::Submitter::NewStub(channel));
auto session_id = sub_client.create_session(task_options, {});
auto map = client.create_results(session_id, std::vector<std::string>{"0", "1", "2", "3"});
ASSERT_TRUE(!map.empty());
ASSERT_EQ(map.size(), 4);
}

TEST(Results, test_results_list) {
Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)};
std::shared_ptr<::grpc::Channel> channel;
armonik::api::grpc::v1::TaskOptions task_options;

init(channel, task_options, log);

auto client = armonik::api::client::ResultsClient(armonik::api::grpc::v1::results::Results::NewStub(channel));
auto sub_client =
armonik::api::client::SubmitterClient(armonik::api::grpc::v1::submitter::Submitter::NewStub(channel));
auto session_id = sub_client.create_session(task_options, {});
auto map = client.create_results(session_id, std::vector<std::string>{"0", "1", "2", "3"});
ASSERT_TRUE(!map.empty());
ASSERT_EQ(map.size(), 4);

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_SESSION_ID);
filter_field.mutable_filter_string()->set_value(session_id);
filter_field.mutable_filter_string()->set_operator_(armonik::api::grpc::v1::FILTER_STRING_OPERATOR_EQUAL);
*filters.mutable_or_()->Add()->mutable_and_()->Add() = filter_field;
int total;
auto list = client.list_results(filters, total);
ASSERT_EQ(list.size(), 4);
ASSERT_EQ(list.size(), total);
}

TEST(Results, test_results_list_small_page) {
Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)};
std::shared_ptr<::grpc::Channel> channel;
armonik::api::grpc::v1::TaskOptions task_options;

init(channel, task_options, log);

auto client = armonik::api::client::ResultsClient(armonik::api::grpc::v1::results::Results::NewStub(channel));
auto sub_client =
armonik::api::client::SubmitterClient(armonik::api::grpc::v1::submitter::Submitter::NewStub(channel));
auto session_id = sub_client.create_session(task_options, {});
auto map = client.create_results(session_id, std::vector<std::string>{"0", "1", "2", "3", "4"});
ASSERT_TRUE(!map.empty());
ASSERT_EQ(map.size(), 5);

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_SESSION_ID);
filter_field.mutable_filter_string()->set_value(session_id);
filter_field.mutable_filter_string()->set_operator_(armonik::api::grpc::v1::FILTER_STRING_OPERATOR_EQUAL);
*filters.mutable_or_()->Add()->mutable_and_()->Add() = filter_field;
int total;
auto list = client.list_results(filters, total, 0, 2);
ASSERT_EQ(list.size(), 2);
ASSERT_EQ(total, 5);

list = client.list_results(filters, total, -1, 2);
ASSERT_EQ(list.size(), 5);
ASSERT_EQ(total, 5);
}
36 changes: 1 addition & 35 deletions packages/cpp/ArmoniK.Api.Tests/source/SubmitterClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "utils/EnvConfiguration.h"
#include "utils/GuuId.h"

#include "common.h"
#include "results_common.pb.h"
#include "results_service.grpc.pb.h"
#include "submitter/ResultsClient.h"
Expand All @@ -39,41 +40,6 @@ using ::testing::AtLeast;

namespace logger = armonik::api::common::logger;

/**
* @brief Initializes task options creates channel with server address
*
* @param channel The gRPC channel to communicate with the server.
* @param default_task_options The default task options.
*/
void init(std::shared_ptr<Channel> &channel, TaskOptions &default_task_options, logger::ILogger &logger) {

Configuration configuration;
// auto server = std::make_shared<EnvConfiguration>(configuration_t);

configuration.add_json_configuration("appsettings.json").add_env_configuration();

std::string server_address = configuration.get("Grpc__EndPoint");

logger.info(" Server address {address}", {{"address", server_address}});

channel = grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());

// stub_ = Submitter::NewStub(channel);

default_task_options.mutable_options()->insert({"key1", "value1"});
default_task_options.mutable_options()->insert({"key2", "value2"});
default_task_options.mutable_max_duration()->set_seconds(3600);
default_task_options.mutable_max_duration()->set_nanos(0);
default_task_options.set_max_retries(1);
default_task_options.set_priority(1);
default_task_options.set_partition_id("");
default_task_options.set_application_name("my-app");
default_task_options.set_application_version("1.0");
default_task_options.set_application_namespace("my-namespace");
default_task_options.set_application_service("my-service");
default_task_options.set_engine_type("Unified");
}

TEST(testMock, createSession) {
// MockStubInterface stub;
std::shared_ptr<Channel> channel;
Expand Down
39 changes: 39 additions & 0 deletions packages/cpp/ArmoniK.Api.Tests/source/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "common.h"
#include "utils/Configuration.h"
#include <grpcpp/create_channel.h>

/**
* @brief Initializes task options creates channel with server address
*
* @param channel The gRPC channel to communicate with the server.
* @param default_task_options The default task options.
*/
void init(std::shared_ptr<::grpc::Channel> &channel, armonik::api::grpc::v1::TaskOptions &default_task_options,
armonik::api::common::logger::ILogger &logger) {

armonik::api::common::utils::Configuration configuration;
// auto server = std::make_shared<EnvConfiguration>(configuration_t);

configuration.add_json_configuration("appsettings.json").add_env_configuration();

std::string server_address = configuration.get("Grpc__EndPoint");

logger.info(" Server address {address}", {{"address", server_address}});

channel = ::grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());

// stub_ = Submitter::NewStub(channel);

default_task_options.mutable_options()->insert({"key1", "value1"});
default_task_options.mutable_options()->insert({"key2", "value2"});
default_task_options.mutable_max_duration()->set_seconds(3600);
default_task_options.mutable_max_duration()->set_nanos(0);
default_task_options.set_max_retries(1);
default_task_options.set_priority(1);
default_task_options.set_partition_id("");
default_task_options.set_application_name("my-app");
default_task_options.set_application_version("1.0");
default_task_options.set_application_namespace("my-namespace");
default_task_options.set_application_service("my-service");
default_task_options.set_engine_type("Unified");
}
2 changes: 1 addition & 1 deletion packages/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.22)
if (POLICY CMP0135)
cmake_policy(SET CMP0135 OLD)
endif ()
set(version 3.13.0)
set(version 3.13.1)
string(REGEX MATCHALL "[0-9]+" version_list ${version})
list(GET version_list 0 version_major)
list(GET version_list 1 version_minor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Optimize>true</Optimize>
<DebugType>Embedded</DebugType>
<IncludeSymbols>true</IncludeSymbols>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Nullable>enable</Nullable>
<AssemblyOriginatorKeyFile>../kp.snk</AssemblyOriginatorKeyFile>
<Optimize>true</Optimize>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<PackageOutputPath>../publish</PackageOutputPath>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Nullable>enable</Nullable>
<AssemblyOriginatorKeyFile>../kp.snk</AssemblyOriginatorKeyFile>
<Optimize>true</Optimize>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion packages/csharp/ArmoniK.Api.Core/ArmoniK.Api.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<AssemblyOriginatorKeyFile>../kp.snk</AssemblyOriginatorKeyFile>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion packages/csharp/ArmoniK.Api.Mock/ArmoniK.Api.Mock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<AssemblyOriginatorKeyFile>../kp.snk</AssemblyOriginatorKeyFile>
<PackageVersion>3.13.0</PackageVersion>
<PackageVersion>3.13.1</PackageVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
Loading
Loading