Skip to content

Commit 27030eb

Browse files
Extend the DataResponse with NetworkStatistics (#1353)
Enable the GetData API to return NetworkStatistics along with response Relates-To: OAM-1718 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent f1f579d commit 27030eb

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ using PartitionsResponseCallback =
7979
/// The `Data` alias type.
8080
using DataResult = model::Data;
8181
/// The data response alias.
82-
using DataResponse = Response<DataResult>;
82+
using DataResponse = Response<DataResult, client::NetworkStatistics>;
8383
/// The callback type of the data response.
84-
using DataResponseCallback = Callback<DataResult>;
84+
using DataResponseCallback = Callback<DataResult, client::NetworkStatistics>;
8585

8686
/// The aggregated data response alias.
8787
using AggregatedDataResponse =

olp-cpp-sdk-dataservice-read/src/repositories/DataRepository.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ DataResponse DataRepository::GetVersionedTile(
6464
storage_);
6565
auto response = repository.GetTile(request, version, context);
6666

67+
auto network_statistics = response.GetPayload();
68+
6769
if (!response.IsSuccessful()) {
6870
OLP_SDK_LOG_WARNING_F(
6971
kLogTag,
7072
"GetVersionedDataTile partition request failed, hrn='%s', key='%s'",
7173
catalog_.ToCatalogHRNString().c_str(),
7274
request.CreateKey(layer_id).c_str());
73-
return response.GetError();
75+
return DataResponse(response.GetError(), network_statistics);
7476
}
7577

7678
// Get the data using a data handle for requested tile
@@ -79,27 +81,39 @@ DataResponse DataRepository::GetVersionedTile(
7981
.WithDataHandle(partition.GetDataHandle())
8082
.WithFetchOption(request.GetFetchOption());
8183

82-
return GetBlobData(layer_id, kBlobService, data_request, std::move(context));
84+
auto data_response =
85+
GetBlobData(layer_id, kBlobService, data_request, std::move(context));
86+
network_statistics += data_response.GetPayload();
87+
88+
if (data_response) {
89+
return DataResponse(data_response.MoveResult(), network_statistics);
90+
} else {
91+
return DataResponse(data_response.GetError(), network_statistics);
92+
}
8393
}
8494

8595
BlobApi::DataResponse DataRepository::GetVersionedData(
8696
const std::string& layer_id, const DataRequest& request, int64_t version,
8797
client::CancellationContext context, const bool fail_on_cache_error) {
8898
if (request.GetDataHandle() && request.GetPartitionId()) {
89-
return {{client::ErrorCode::PreconditionFailed,
90-
"Both data handle and partition id specified"}};
99+
return client::ApiError::PreconditionFailed(
100+
"Both data handle and partition id specified");
91101
}
92102

93103
auto blob_request = request;
104+
client::NetworkStatistics network_statistics;
105+
94106
if (!request.GetDataHandle()) {
95107
// get data handle for a partition to be queried
96108
PartitionsRepository repository(catalog_, layer_id, settings_,
97109
lookup_client_, storage_);
98110
auto partitions_response =
99111
repository.GetPartitionById(request, version, context);
100112

113+
network_statistics = partitions_response.GetPayload();
114+
101115
if (!partitions_response.IsSuccessful()) {
102-
return partitions_response.GetError();
116+
return DataResponse(partitions_response.GetError(), network_statistics);
103117
}
104118

105119
const auto& partitions = partitions_response.GetResult().GetPartitions();
@@ -112,16 +126,25 @@ BlobApi::DataResponse DataRepository::GetVersionedData(
112126
catalog_.ToCatalogHRNString().c_str(),
113127
request.CreateKey(layer_id, version).c_str());
114128

115-
return {{client::ErrorCode::NotFound, "Partition not found"}};
129+
return DataResponse(client::ApiError::NotFound("Partition not found"),
130+
network_statistics);
116131
}
117132

118133
blob_request.WithDataHandle(partitions.front().GetDataHandle());
119134
}
120135

121136
// finally get the data using a data handle
122-
return repository::DataRepository::GetBlobData(
137+
auto data_response = repository::DataRepository::GetBlobData(
123138
layer_id, kBlobService, blob_request, std::move(context),
124139
fail_on_cache_error);
140+
141+
network_statistics += data_response.GetPayload();
142+
143+
if (data_response) {
144+
return DataResponse(data_response.MoveResult(), network_statistics);
145+
} else {
146+
return DataResponse(data_response.GetError(), network_statistics);
147+
}
125148
}
126149

127150
BlobApi::DataResponse DataRepository::GetBlobData(
@@ -132,7 +155,7 @@ BlobApi::DataResponse DataRepository::GetBlobData(
132155
const auto& data_handle = request.GetDataHandle();
133156

134157
if (!data_handle) {
135-
return {{client::ErrorCode::PreconditionFailed, "Data handle is missing"}};
158+
return client::ApiError::PreconditionFailed("Data handle is missing");
136159
}
137160

138161
NamedMutex mutex(storage_, catalog_.ToString() + layer + *data_handle);
@@ -157,8 +180,8 @@ BlobApi::DataResponse DataRepository::GetBlobData(
157180
OLP_SDK_LOG_INFO_F(
158181
kLogTag, "GetBlobData not found in cache, hrn='%s', key='%s'",
159182
catalog_.ToCatalogHRNString().c_str(), data_handle->c_str());
160-
return {{client::ErrorCode::NotFound,
161-
"CacheOnly: resource not found in cache"}};
183+
return client::ApiError::NotFound(
184+
"CacheOnly: resource not found in cache");
162185
}
163186
}
164187

@@ -231,8 +254,8 @@ BlobApi::DataResponse DataRepository::GetVolatileData(
231254
const std::string& layer_id, const DataRequest& request,
232255
client::CancellationContext context, const bool fail_on_cache_error) {
233256
if (request.GetDataHandle() && request.GetPartitionId()) {
234-
return {{client::ErrorCode::PreconditionFailed,
235-
"Both data handle and partition id specified"}};
257+
return client::ApiError::PreconditionFailed(
258+
"Both data handle and partition id specified");
236259
}
237260

238261
auto blob_request = request;
@@ -255,7 +278,7 @@ BlobApi::DataResponse DataRepository::GetVolatileData(
255278
catalog_.ToCatalogHRNString().c_str(),
256279
request.CreateKey(layer_id, boost::none).c_str());
257280

258-
return {{client::ErrorCode::NotFound, "Partition not found"}};
281+
return client::ApiError::NotFound("Partition not found");
259282
}
260283

261284
blob_request.WithDataHandle(partitions.front().GetDataHandle());

tests/integration/olp-cpp-sdk-dataservice-read/VersionedLayerClientTest.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,12 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetDataFromPartitionAsync) {
284284
EXPECT_CALL(*network_mock_, Send(_, _, _, _, _))
285285
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
286286
HTTP_RESPONSE_LOOKUP))
287-
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
288-
kHttpResponsePartition_269))
289-
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
290-
kHttpResponseBlobData_269));
287+
.WillOnce(
288+
ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK, 1024, 123),
289+
kHttpResponsePartition_269))
290+
.WillOnce(
291+
ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK, 1024, 123),
292+
kHttpResponseBlobData_269));
291293

292294
auto client = std::make_shared<read::VersionedLayerClient>(
293295
kCatalog, kTestLayer, kTestVersion, settings_);
@@ -306,6 +308,10 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetDataFromPartitionAsync) {
306308
ASSERT_NE(response.GetResult(), nullptr);
307309
ASSERT_NE(response.GetResult()->size(), 0u);
308310

311+
const auto& network_stats = response.GetPayload();
312+
EXPECT_EQ(network_stats.GetBytesDownloaded(), 2048);
313+
EXPECT_EQ(network_stats.GetBytesUploaded(), 246);
314+
309315
const auto cache_key = olp::cache::KeyGenerator::CreatePartitionKey(
310316
GetTestCatalog(), kTestLayer, kTestPartition, kTestVersion);
311317
EXPECT_TRUE(settings_.cache->Contains(cache_key));
@@ -2862,13 +2868,14 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetTile) {
28622868

28632869
EXPECT_CALL(*network_mock_,
28642870
Send(IsGetRequest(kHttpQueryTreeIndex_23064), _, _, _, _))
2865-
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
2866-
kHttpSubQuads_23064));
2871+
.WillOnce(
2872+
ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK, 1024, 123),
2873+
kHttpSubQuads_23064));
28672874

28682875
EXPECT_CALL(*network_mock_,
28692876
Send(IsGetRequest(kHttpResponseBlobData_5904591), _, _, _, _))
2870-
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
2871-
"someData"));
2877+
.WillOnce(ReturnHttpResponse(
2878+
GetResponse(http::HttpStatusCode::OK, 1024, 123), "someData"));
28722879

28732880
auto request =
28742881
read::TileRequest().WithTileKey(geo::TileKey::FromHereTile("5904591"));
@@ -2880,6 +2887,10 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetTile) {
28802887
std::string data_string(data_response.GetResult()->begin(),
28812888
data_response.GetResult()->end());
28822889
ASSERT_EQ("someData", data_string);
2890+
2891+
const auto& network_stats = data_response.GetPayload();
2892+
EXPECT_EQ(network_stats.GetBytesDownloaded(), 2048);
2893+
EXPECT_EQ(network_stats.GetBytesUploaded(), 246);
28832894
}
28842895

28852896
TEST_F(DataserviceReadVersionedLayerClientTest, GetTileConcurrent) {
@@ -3463,7 +3474,7 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetAggregatedData) {
34633474
*network_mock_,
34643475
Send(IsGetRequest(URL_BLOB_AGGREGATE_DATA_23618364), _, _, _, _))
34653476
.WillOnce(ReturnHttpResponse(
3466-
GetResponse(http::HttpStatusCode::OK, 10240000, 123), "some_data"));
3477+
GetResponse(http::HttpStatusCode::OK, 1024, 123), "some_data"));
34673478

34683479
auto client = read::VersionedLayerClient(kCatalog, kTestLayer, kTestVersion,
34693480
settings_);
@@ -3481,7 +3492,7 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetAggregatedData) {
34813492
ASSERT_EQ(result.GetTile(), tile_key);
34823493

34833494
const auto& network_stats = response.GetPayload();
3484-
EXPECT_EQ(network_stats.GetBytesDownloaded(), 10241024);
3495+
EXPECT_EQ(network_stats.GetBytesDownloaded(), 2048);
34853496
EXPECT_EQ(network_stats.GetBytesUploaded(), 246);
34863497

34873498
testing::Mock::VerifyAndClearExpectations(network_mock_.get());

0 commit comments

Comments
 (0)