Skip to content

Commit 9a434f3

Browse files
edwinwugoogcopybara-github
authored andcommitted
analytics: Implement to pass values for OperatoinResultWithMedium.
PiperOrigin-RevId: 704304596
1 parent 196dda9 commit 9a434f3

File tree

9 files changed

+153
-24
lines changed

9 files changed

+153
-24
lines changed

connections/implementation/analytics/analytics_recorder.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ using ::location::nearby::proto::connections::START_CLIENT_SESSION;
9191
using ::location::nearby::proto::connections::START_STRATEGY_SESSION;
9292
using ::location::nearby::proto::connections::STOP_CLIENT_SESSION;
9393
using ::location::nearby::proto::connections::STOP_STRATEGY_SESSION;
94-
using ::location::nearby::proto::connections::STREAM;
9594
using ::location::nearby::proto::connections::StopAdvertisingReason;
9695
using ::location::nearby::proto::connections::StopDiscoveringReason;
96+
using ::location::nearby::proto::connections::STREAM;
9797
using ::location::nearby::proto::connections::UNFINISHED;
9898
using ::location::nearby::proto::connections::UNFINISHED_ERROR;
9999
using ::location::nearby::proto::connections::UNKNOWN_MEDIUM;
@@ -108,7 +108,7 @@ using SafeDisconnectionResult = ::location::nearby::analytics::proto::
108108
ConnectionsLog::EstablishedConnection::SafeDisconnectionResult;
109109

110110
// TODO(edwinwu): Add ifttt in Android counterpart.
111-
OperationResultCategory GetOperationResultCateory(
111+
OperationResultCategory ConvertToOperationResultCateory(
112112
OperationResultCode result_code) {
113113
if (result_code == OperationResultCode::DETAIL_SUCCESS) {
114114
return OperationResultCategory::CATEGORY_SUCCESS;
@@ -301,7 +301,7 @@ void AnalyticsRecorder::OnStopDiscovery() {
301301
if (!CanRecordAnalyticsLocked("OnStopDiscovery")) {
302302
return;
303303
}
304-
RecordDiscoveryPhaseDurationAndReasonLocked(/*on_stop=*/ true);
304+
RecordDiscoveryPhaseDurationAndReasonLocked(/*on_stop=*/true);
305305
}
306306

307307
void AnalyticsRecorder::OnStartedIncomingConnectionListening(
@@ -501,7 +501,7 @@ void AnalyticsRecorder::OnIncomingConnectionAttemptLocked(
501501
std::make_unique<ConnectionsLog::OperationResult>();
502502
operation_result_proto->set_result_code(
503503
connection_attempt_metadata_params->operation_result_code);
504-
operation_result_proto->set_result_category(GetOperationResultCateory(
504+
operation_result_proto->set_result_category(ConvertToOperationResultCateory(
505505
connection_attempt_metadata_params->operation_result_code));
506506
connection_attempt->set_allocated_operation_result(
507507
operation_result_proto.release());
@@ -589,7 +589,7 @@ void AnalyticsRecorder::OnOutgoingConnectionAttemptLocked(
589589
std::make_unique<ConnectionsLog::OperationResult>();
590590
operation_result_proto->set_result_code(
591591
connection_attempt_metadata_params->operation_result_code);
592-
operation_result_proto->set_result_category(GetOperationResultCateory(
592+
operation_result_proto->set_result_category(ConvertToOperationResultCateory(
593593
connection_attempt_metadata_params->operation_result_code));
594594
connection_attempt->set_allocated_operation_result(
595595
operation_result_proto.release());
@@ -1144,7 +1144,7 @@ void AnalyticsRecorder::FinishDiscoveryPhaseLocked() {
11441144
MarkConnectionRequestIgnoredLocked(connection_request.get());
11451145
UpdateDiscovererConnectionRequestLocked(connection_request.get());
11461146
}
1147-
RecordDiscoveryPhaseDurationAndReasonLocked(/* on_stop=*/ false);
1147+
RecordDiscoveryPhaseDurationAndReasonLocked(/* on_stop=*/false);
11481148
*current_strategy_session_->add_discovery_phase() =
11491149
*std::move(current_discovery_phase_);
11501150
}
@@ -1323,7 +1323,7 @@ void AnalyticsRecorder::FinishUpgradeAttemptLocked(
13231323
std::make_unique<ConnectionsLog::OperationResult>();
13241324
operation_result_proto->set_result_code(operation_result_code);
13251325
operation_result_proto->set_result_category(
1326-
GetOperationResultCateory(operation_result_code));
1326+
ConvertToOperationResultCateory(operation_result_code));
13271327
attempt->set_allocated_operation_result(operation_result_proto.release());
13281328
*current_strategy_session_->add_upgrade_attempt() = *attempt;
13291329
if (erase_item) {
@@ -1426,7 +1426,7 @@ ConnectionsLog::Payload AnalyticsRecorder::PendingPayload::GetProtoPayload(
14261426
std::make_unique<ConnectionsLog::OperationResult>();
14271427
operation_result_proto->set_result_code(operation_result_code_);
14281428
operation_result_proto->set_result_category(
1429-
GetOperationResultCateory(operation_result_code_));
1429+
ConvertToOperationResultCateory(operation_result_code_));
14301430
payload.set_allocated_operation_result(operation_result_proto.release());
14311431

14321432
return payload;
@@ -1696,6 +1696,11 @@ AnalyticsRecorder::LogicalConnection::GetPendingPayloadResultCodeFromReason(
16961696
}
16971697
}
16981698

1699+
OperationResultCategory AnalyticsRecorder::GetOperationResultCateory(
1700+
location::nearby::proto::connections::OperationResultCode result_code) {
1701+
return ConvertToOperationResultCateory(result_code);
1702+
}
1703+
16991704
void AnalyticsRecorder::Sync() {
17001705
CountDownLatch latch(1);
17011706
serial_executor_.Execute([&]() { latch.CountDown(); });

connections/implementation/analytics/analytics_recorder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ class AnalyticsRecorder {
214214

215215
bool IsSessionLogged();
216216

217+
location::nearby::proto::connections::OperationResultCategory
218+
GetOperationResultCateory(
219+
location::nearby::proto::connections::OperationResultCode result_code);
220+
217221
// Waits until all logs are sent to the backend.
218222
// For testing only.
219223
void Sync();

connections/implementation/base_pcp_handler.cc

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ std::string AuthenticationStatusToString(nearby::AuthenticationStatus status) {
9999
}
100100
} // namespace
101101

102+
using ::location::nearby::analytics::proto::ConnectionsLog;
102103
using ::location::nearby::connections::ConnectionRequestFrame;
103104
using ::location::nearby::connections::ConnectionResponseFrame;
104105
using ::location::nearby::connections::ConnectionsDevice;
@@ -267,9 +268,11 @@ Status BasePcpHandler::StartAdvertising(
267268
// Save the advertising options for local reference in later process
268269
// like upgrading bandwidth.
269270
advertising_listener_ = info.listener;
270-
client->StartedAdvertising(service_id, GetStrategy(), info.listener,
271-
absl::MakeSpan(result.mediums),
272-
compatible_advertising_options);
271+
client->StartedAdvertising(
272+
service_id, GetStrategy(), info.listener,
273+
absl::MakeSpan(result.mediums),
274+
std::move(result.operation_result_with_mediums),
275+
compatible_advertising_options);
273276
client->UpdateLocalEndpointInfo(info.endpoint_info.string_data());
274277
response.Set({Status::kSuccess});
275278
});
@@ -451,7 +454,9 @@ Status BasePcpHandler::StartDiscovery(ClientProxy* client,
451454
}
452455
client->StartedDiscovery(
453456
service_id, GetStrategy(), std::move(listener),
454-
absl::MakeSpan(result.mediums), stripped_discovery_options);
457+
absl::MakeSpan(result.mediums),
458+
std::move(result.operation_result_with_mediums),
459+
stripped_discovery_options);
455460
response.Set({Status::kSuccess});
456461
});
457462
return WaitForResult(absl::StrCat("StartDiscovery(", service_id, ")"),
@@ -1142,6 +1147,25 @@ void BasePcpHandler::StripOutUnavailableMediums(
11421147
}
11431148
}
11441149

1150+
ConnectionsLog::OperationResultWithMedium
1151+
BasePcpHandler::GetOperationResultWithMediumByResultCode(
1152+
ClientProxy* client, location::nearby::proto::connections::Medium medium,
1153+
int update_index,
1154+
location::nearby::proto::connections::OperationResultCode
1155+
operation_result_code,
1156+
location::nearby::proto::connections::ConnectionMode connection_mode) {
1157+
ConnectionsLog::OperationResultWithMedium operation_result_with_medium;
1158+
operation_result_with_medium.set_medium(medium);
1159+
operation_result_with_medium.set_result_code(operation_result_code);
1160+
operation_result_with_medium.set_result_category(
1161+
client->GetAnalyticsRecorder().GetOperationResultCateory(
1162+
operation_result_code));
1163+
operation_result_with_medium.set_connection_mode(connection_mode);
1164+
operation_result_with_medium.set_update_index(update_index);
1165+
1166+
return operation_result_with_medium;
1167+
}
1168+
11451169
void BasePcpHandler::StripOutUnavailableMediums(
11461170
DiscoveryOptions& discovery_options) {
11471171
BooleanMediumSelector& allowed = discovery_options.allowed;

connections/implementation/base_pcp_handler.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ class BasePcpHandler : public PcpHandler,
195195
// If success, the mediums on which we are now advertising/discovering, for
196196
// analytics.
197197
std::vector<location::nearby::proto::connections::Medium> mediums;
198+
std::vector<location::nearby::analytics::proto::ConnectionsLog::
199+
OperationResultWithMedium>
200+
operation_result_with_mediums;
198201
};
199202

200203
// Represents an endpoint that we've discovered. Typically, the implementation
@@ -415,6 +418,15 @@ class BasePcpHandler : public PcpHandler,
415418

416419
void StripOutWifiHotspotMedium(ConnectionInfo& connection_info);
417420

421+
location::nearby::analytics::proto::ConnectionsLog::OperationResultWithMedium
422+
GetOperationResultWithMediumByResultCode(
423+
ClientProxy* client, location::nearby::proto::connections::Medium medium,
424+
int update_index,
425+
location::nearby::proto::connections::OperationResultCode
426+
operation_result_code,
427+
location::nearby::proto::connections::ConnectionMode connection_mode =
428+
location::nearby::proto::connections::ConnectionMode::LEGACY);
429+
418430
// Test only.
419431
int GetEndpointLostByMediumAlarmsCount() RUN_ON_PCP_HANDLER_THREAD() {
420432
return endpoint_lost_by_medium_alarms_.size();

connections/implementation/client_proxy.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace nearby {
7070
namespace connections {
7171

7272
namespace {
73+
using ::location::nearby::analytics::proto::ConnectionsLog;
7374
using ::location::nearby::connections::OsInfo;
7475

7576
constexpr char kEndpointIdChars[] = {
@@ -214,6 +215,8 @@ void ClientProxy::StartedAdvertising(
214215
const std::string& service_id, Strategy strategy,
215216
const ConnectionListener& listener,
216217
absl::Span<location::nearby::proto::connections::Medium> mediums,
218+
const std::vector<ConnectionsLog::OperationResultWithMedium>&
219+
operation_result_with_mediums,
217220
const AdvertisingOptions& advertising_options) {
218221
MutexLock lock(&mutex_);
219222
NEARBY_LOGS(INFO) << "ClientProxy [StartedAdvertising]: client="
@@ -245,7 +248,8 @@ void ClientProxy::StartedAdvertising(
245248
std::unique_ptr<AdvertisingMetadataParams> advertising_metadata_params;
246249
advertising_metadata_params =
247250
GetAnalyticsRecorder().BuildAdvertisingMetadataParams();
248-
// TODO(edwinwu): Implement to pass real values for OperatoinResultWithMedium.
251+
advertising_metadata_params->operation_result_with_mediums =
252+
std::move(operation_result_with_mediums);
249253
analytics_recorder_->OnStartAdvertising(strategy, medium_vector,
250254
advertising_metadata_params.get());
251255
}
@@ -366,6 +370,8 @@ void ClientProxy::StartedDiscovery(
366370
const std::string& service_id, Strategy strategy,
367371
DiscoveryListener listener,
368372
absl::Span<location::nearby::proto::connections::Medium> mediums,
373+
const std::vector<ConnectionsLog::OperationResultWithMedium>&
374+
operation_result_with_mediums,
369375
const DiscoveryOptions& discovery_options) {
370376
MutexLock lock(&mutex_);
371377
discovery_info_ = DiscoveryInfo{service_id, std::move(listener)};
@@ -376,10 +382,10 @@ void ClientProxy::StartedDiscovery(
376382
std::unique_ptr<DiscoveryMetadataParams> discovery_metadata_params;
377383
discovery_metadata_params =
378384
GetAnalyticsRecorder().BuildDiscoveryMetadataParams();
379-
// TODO(edwinwu): Implement to pass real values for OperatoinResultWithMedium.
380-
analytics_recorder_->OnStartDiscovery(
381-
strategy, medium_vector,
382-
discovery_metadata_params.get());
385+
discovery_metadata_params->operation_result_with_mediums =
386+
std::move(operation_result_with_mediums);
387+
analytics_recorder_->OnStartDiscovery(strategy, medium_vector,
388+
discovery_metadata_params.get());
383389
}
384390

385391
void ClientProxy::StoppedDiscovery() {

connections/implementation/client_proxy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "absl/types/span.h"
5454
#include "internal/platform/os_name.h"
5555
#include "internal/platform/scheduled_executor.h"
56+
#include "internal/proto/analytics/connections_log.pb.h"
5657

5758
namespace nearby {
5859
namespace connections {
@@ -101,6 +102,9 @@ class ClientProxy final {
101102
const std::string& service_id, Strategy strategy,
102103
const ConnectionListener& connection_lifecycle_listener,
103104
absl::Span<location::nearby::proto::connections::Medium> mediums,
105+
const std::vector<location::nearby::analytics::proto::ConnectionsLog::
106+
OperationResultWithMedium>&
107+
operation_result_with_medium,
104108
const AdvertisingOptions& advertising_options = AdvertisingOptions{});
105109
// Marks this client as not advertising.
106110
void StoppedAdvertising();
@@ -123,6 +127,9 @@ class ClientProxy final {
123127
const std::string& service_id, Strategy strategy,
124128
DiscoveryListener discovery_listener,
125129
absl::Span<location::nearby::proto::connections::Medium> mediums,
130+
const std::vector<location::nearby::analytics::proto::ConnectionsLog::
131+
OperationResultWithMedium>&
132+
operation_result_with_medium,
126133
const DiscoveryOptions& discovery_options = DiscoveryOptions{});
127134
// Marks this client as not discovering at all.
128135
void StoppedDiscovery();

connections/implementation/client_proxy_test.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ class ClientProxyTest : public ::testing::TestWithParam<FeatureFlags::Flags> {
210210
.info = ByteArray{"advertising endpoint name"},
211211
.id = client->GetLocalEndpointId(),
212212
};
213-
client->StartedAdvertising(service_id_, strategy_, listener,
214-
absl::MakeSpan(mediums_), advertising_options);
213+
client->StartedAdvertising(
214+
service_id_, strategy_, listener, absl::MakeSpan(mediums_),
215+
/*operation_result_with_medium=*/{}, advertising_options);
215216
return endpoint;
216217
}
217218

@@ -256,7 +257,8 @@ class ClientProxyTest : public ::testing::TestWithParam<FeatureFlags::Flags> {
256257
.id = client->GetLocalEndpointId(),
257258
};
258259
client->StartedDiscovery(service_id_, strategy_, std::move(listener),
259-
absl::MakeSpan(mediums_));
260+
absl::MakeSpan(mediums_),
261+
/*operation_result_with_medium=*/{});
260262
return endpoint;
261263
}
262264

@@ -607,7 +609,7 @@ TEST_F(ClientProxyTest, ResetClearsState) {
607609
}
608610

609611
TEST_F(ClientProxyTest, StartedAdvertisingChangesStateFromIdle) {
610-
client1()->StartedAdvertising(service_id_, strategy_, {}, {});
612+
client1()->StartedAdvertising(service_id_, strategy_, {}, {}, {});
611613

612614
EXPECT_TRUE(client1()->IsAdvertising());
613615
EXPECT_FALSE(client1()->IsDiscovering());
@@ -616,7 +618,7 @@ TEST_F(ClientProxyTest, StartedAdvertisingChangesStateFromIdle) {
616618
}
617619

618620
TEST_F(ClientProxyTest, StartedDiscoveryChangesStateFromIdle) {
619-
client1()->StartedDiscovery(service_id_, strategy_, {}, {});
621+
client1()->StartedDiscovery(service_id_, strategy_, {}, {}, {});
620622

621623
EXPECT_FALSE(client1()->IsAdvertising());
622624
EXPECT_TRUE(client1()->IsDiscovering());

0 commit comments

Comments
 (0)