diff --git a/connections/implementation/analytics/analytics_recorder.cc b/connections/implementation/analytics/analytics_recorder.cc index 91234b92..fa138441 100644 --- a/connections/implementation/analytics/analytics_recorder.cc +++ b/connections/implementation/analytics/analytics_recorder.cc @@ -206,6 +206,17 @@ void AnalyticsRecorder::ResetClientSessionLoggingResoucesLocked() { current_discovery_phase_ = nullptr; } +int AnalyticsRecorder::GetLatestUpdateIndexLocked( + const std::vector &list) { + int latest_update_index = 0; + for (const auto &operation_result_with_medium : list) { + if (operation_result_with_medium.update_index() > latest_update_index) { + latest_update_index = operation_result_with_medium.update_index(); + } + } + return latest_update_index; +} + void AnalyticsRecorder::OnStartAdvertising( connections::Strategy strategy, const std::vector &mediums, AdvertisingMetadataParams *advertising_metadata_params) { @@ -255,6 +266,19 @@ void AnalyticsRecorder::OnStopAdvertising() { RecordAdvertisingPhaseDurationAndReasonLocked(/* on_stop= */ true); } +int AnalyticsRecorder::GetNextAdvertisingUpdateIndex() { + MutexLock lock(&mutex_); + + if (current_advertising_phase_ == nullptr) { + return 0; + } + return GetLatestUpdateIndexLocked( + std::vector( + current_advertising_phase_->adv_dis_result().begin(), + current_advertising_phase_->adv_dis_result().end())) + + 1; +} + void AnalyticsRecorder::OnStartDiscovery( connections::Strategy strategy, const std::vector &mediums, DiscoveryMetadataParams *discovery_metadata_params) { @@ -304,6 +328,18 @@ void AnalyticsRecorder::OnStopDiscovery() { RecordDiscoveryPhaseDurationAndReasonLocked(/*on_stop=*/true); } +int AnalyticsRecorder::GetNextDiscoveryUpdateIndex() { + MutexLock lock(&mutex_); + if (current_discovery_phase_ == nullptr) { + return 0; + } + return GetLatestUpdateIndexLocked( + std::vector( + current_discovery_phase_->adv_dis_result().begin(), + current_discovery_phase_->adv_dis_result().end())) + + 1; +} + void AnalyticsRecorder::OnStartedIncomingConnectionListening( connections::Strategy strategy) { MutexLock lock(&mutex_); diff --git a/connections/implementation/analytics/analytics_recorder.h b/connections/implementation/analytics/analytics_recorder.h index d936978d..eed3b4ea 100644 --- a/connections/implementation/analytics/analytics_recorder.h +++ b/connections/implementation/analytics/analytics_recorder.h @@ -57,6 +57,12 @@ class AnalyticsRecorder { ABSL_LOCKS_EXCLUDED(mutex_); void OnStopAdvertising() ABSL_LOCKS_EXCLUDED(mutex_); + // In case the client calls the {@link BasePcp#updateAdvertisingOptions()} + // multiple times, adds one index value to group the mediums results within + // the same UpdateAdvertisingOptions call, this API is to return the largest + // index value in current_advertising_phase. + int GetNextAdvertisingUpdateIndex() ABSL_LOCKS_EXCLUDED(mutex_); + // Connection listening void OnStartedIncomingConnectionListening(connections::Strategy strategy) ABSL_LOCKS_EXCLUDED(mutex_); @@ -69,6 +75,12 @@ class AnalyticsRecorder { DiscoveryMetadataParams *discovery_metadata_params) ABSL_LOCKS_EXCLUDED(mutex_); void OnStopDiscovery() ABSL_LOCKS_EXCLUDED(mutex_); + + // In case the client calls the {@link BasePcp#updateDiscoveryOptions()} + // multiple times, adds one index value to group the medium results within the + // same UpdateDiscoveryOptions call, this + // API is to return the latest index value in current_discovery_phase. + int GetNextDiscoveryUpdateIndex() ABSL_LOCKS_EXCLUDED(mutex_); void OnEndpointFound(location::nearby::proto::connections::Medium medium) ABSL_LOCKS_EXCLUDED(mutex_); @@ -441,6 +453,11 @@ class AnalyticsRecorder { void ResetClientSessionLoggingResoucesLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); + int GetLatestUpdateIndexLocked( + const std::vector &list) + ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); + location::nearby::proto::connections::ConnectionsStrategy StrategyToConnectionStrategy(connections::Strategy strategy); location::nearby::proto::connections::PayloadType diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index 35bad1a3..925be6be 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -1364,8 +1364,8 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( std::vector started_mediums; std::vector operation_result_with_mediums; - // TODO(edwinwu): Modify the update_index with a new function. - int update_index = 1; + int update_index = + client_proxy->GetAnalyticsRecorder().GetNextAdvertisingUpdateIndex(); if (options.enable_bluetooth_listening && !bluetooth_medium_.IsAcceptingConnections(std::string(service_id))) { ErrorOr bluetooth_result = @@ -1550,9 +1550,8 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( std::vector restarted_mediums; std::vector operation_result_with_mediums; - // TODO(edwinwu): Modify the update_index with a new function : - // client->GetAnalyticsRecorder()->GetNextAdvertisingUpdateIndex(); - int update_index = 1; + int update_index = + client->GetAnalyticsRecorder().GetNextAdvertisingUpdateIndex(); Status status = {Status::kSuccess}; WebRtcState web_rtc_state = webrtc_medium_.IsAvailable() ? WebRtcState::kConnectable