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

analytics: Update the returned code for CreateUpgradedEndpointChannel of BWUHanlder and its enherited class for NC mediums. #3027

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions connections/implementation/analytics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ cc_test(
shard_count = 16,
deps = [
":analytics",
"//connections:core_types",
"//internal/analytics:mock_event_logger",
"//internal/platform:base",
"//internal/platform:error_code_recorder",
Expand Down
231 changes: 218 additions & 13 deletions connections/implementation/analytics/analytics_recorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <cstdint>
#include <iterator>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -48,7 +47,6 @@ namespace {
const char kVersion[] = "v1.0.0";
constexpr absl::string_view kOnStartClientSession = "OnStartClientSession";
const absl::Duration kConnectionTokenMaxLife = absl::Hours(24);
} // namespace

using ::location::nearby::analytics::proto::ConnectionsLog;
using ::location::nearby::proto::connections::ACCEPTED;
Expand Down Expand Up @@ -76,6 +74,8 @@ using ::location::nearby::proto::connections::INITIAL;
using ::location::nearby::proto::connections::Medium;
using ::location::nearby::proto::connections::MOVED_TO_NEW_MEDIUM;
using ::location::nearby::proto::connections::NOT_SENT;
using ::location::nearby::proto::connections::OperationResultCategory;
using ::location::nearby::proto::connections::OperationResultCode;
using ::location::nearby::proto::connections::OUTGOING;
using ::location::nearby::proto::connections::P2P_CLUSTER;
using ::location::nearby::proto::connections::P2P_POINT_TO_POINT;
Expand Down Expand Up @@ -103,6 +103,57 @@ using ::nearby::analytics::EventLogger;
using SafeDisconnectionResult = ::location::nearby::analytics::proto::
ConnectionsLog::EstablishedConnection::SafeDisconnectionResult;

// TODO(edwinwu): Add ifttt in Android counterpart.
OperationResultCategory GetOperationResultCateory(
OperationResultCode result_code) {
if (result_code == OperationResultCode::DETAIL_SUCCESS) {
return OperationResultCategory::CATEGORY_SUCCESS;
}
// Section of CATEGORY_NEARBY_ERROR, starting from 4500
if (result_code >=
OperationResultCode::NEARBY_BLE_ADVERTISEMENT_MAPPING_TO_MAC_ERROR) {
return OperationResultCategory::CATEGORY_NEARBY_ERROR;
}
// Section of CATEGORY_CONNECTIVITY_ERROR, starting from 3500 to 4499
if (result_code >=
OperationResultCode::CONNECTIVITY_WIFI_AWARE_ATTACH_FAILURE) {
return OperationResultCategory::CATEGORY_CONNECTIVITY_ERROR;
}
// Section of CATEGORY_IO_ERROR, from 3000 to 3499
if (result_code >= OperationResultCode::IO_FILE_OPENING_ERROR) {
return OperationResultCategory::CATEGORY_IO_ERROR;
}
// Section of CATEGORY_MISCELLANEOUS, from 2500 to 2999
if (result_code >=
OperationResultCode::MISCELLEANEOUS_BLUETOOTH_MAC_ADDRESS_NULL) {
return OperationResultCategory::CATEGORY_MISCELLANEOUS;
}
// Section of CATEGORY_CLIENT_ERROR, from 2000 to 2499
if (result_code >=
OperationResultCode::
CLIENT_WIFI_DIRECT_ALREADY_HOSTING_DIRECT_GROUP_FOR_THIS_CLIENT) {
return OperationResultCategory::CATEGORY_CLIENT_ERROR;
}
// Section of CATEGORY_MEDIUM_UNAVAILABLE, from 1500 to 1999
if (result_code >= OperationResultCode::
MEDIUM_UNAVAILABLE_WIFI_AWARE_RESOURCE_NOT_AVAILABLE) {
return OperationResultCategory::CATEGORY_MEDIUM_UNAVAILABLE;
}
// Section of CATEGORY_DEVICE_STATE_ERROR, from 1000 to 1499
if (result_code >=
OperationResultCode::DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS) {
return OperationResultCategory::CATEGORY_DEVICE_STATE_ERROR;
}
// Section of CATEGORY_CLIENT_CANCELLATION, from 500 to 999
if (result_code >=
OperationResultCode::CLIENT_CANCELLATION_REMOTE_IN_CANCELED_STATE) {
return OperationResultCategory::CATEGORY_CLIENT_CANCELLATION;
}
// Clarify other non success cases as unknown
return OperationResultCategory::CATEGORY_UNKNOWN;
}
} // namespace

AnalyticsRecorder::AnalyticsRecorder(EventLogger *event_logger)
: event_logger_(event_logger) {
NEARBY_LOGS(INFO) << "Start AnalyticsRecorder ctor event_logger_="
Expand Down Expand Up @@ -338,7 +389,7 @@ void AnalyticsRecorder::OnLocalEndpointRejected(
LocalEndpointRespondedLocked(remote_endpoint_id, REJECTED);
}

void AnalyticsRecorder::OnIncomingConnectionAttempt(
void AnalyticsRecorder::OnIncomingConnectionAttemptWithMetadata(
ConnectionAttemptType type, Medium medium, ConnectionAttemptResult result,
absl::Duration duration, const std::string &connection_token,
ConnectionAttemptMetadataParams *connection_attempt_metadata_params) {
Expand All @@ -351,6 +402,43 @@ void AnalyticsRecorder::OnIncomingConnectionAttempt(
"null current_strategy_session_";
return;
}

ConnectionAttemptMetadataParams default_params = {};
if (connection_attempt_metadata_params == nullptr) {
connection_attempt_metadata_params = &default_params;
}
OnIncomingConnectionAttemptLocked(type, medium, result, duration,
connection_token,
connection_attempt_metadata_params);
}

void AnalyticsRecorder::OnIncomingConnectionAttempt(
ConnectionAttemptType type, Medium medium, ConnectionAttemptResult result,
absl::Duration duration, const std::string &connection_token,
OperationResultCode operation_result_code) {
MutexLock lock(&mutex_);
if (!CanRecordAnalyticsLocked("OnIncomingConnectionAttempt")) {
return;
}
if (current_strategy_session_ == nullptr) {
NEARBY_LOGS(INFO) << "Unable to record incoming connection attempt due to "
"null current_strategy_session_";
return;
}

ConnectionAttemptMetadataParams default_params = {};
default_params.operation_result_code = operation_result_code;

OnIncomingConnectionAttemptLocked(type, medium, result, duration,
connection_token, &default_params);
}

void AnalyticsRecorder::OnIncomingConnectionAttemptLocked(
location::nearby::proto::connections::ConnectionAttemptType type,
location::nearby::proto::connections::Medium medium,
location::nearby::proto::connections::ConnectionAttemptResult result,
absl::Duration duration, const std::string &connection_token,
ConnectionAttemptMetadataParams *connection_attempt_metadata_params) {
auto *connection_attempt =
current_strategy_session_->add_connection_attempt();
connection_attempt->set_duration_millis(absl::ToInt64Milliseconds(duration));
Expand All @@ -360,10 +448,6 @@ void AnalyticsRecorder::OnIncomingConnectionAttempt(
connection_attempt->set_attempt_result(result);
connection_attempt->set_connection_token(connection_token);

ConnectionAttemptMetadataParams default_params = {};
if (connection_attempt_metadata_params == nullptr) {
connection_attempt_metadata_params = &default_params;
}
auto *connection_attempt_metadata =
connection_attempt->mutable_connection_attempt_metadata();
connection_attempt_metadata->set_technology(
Expand All @@ -390,9 +474,18 @@ void AnalyticsRecorder::OnIncomingConnectionAttempt(
connection_attempt_metadata_params->max_wifi_rx_speed);
connection_attempt_metadata->set_wifi_channel_width(
connection_attempt_metadata_params->channel_width);

auto operation_result_proto =
std::make_unique<ConnectionsLog::OperationResult>();
operation_result_proto->set_result_code(
connection_attempt_metadata_params->operation_result_code);
operation_result_proto->set_result_category(GetOperationResultCateory(
connection_attempt_metadata_params->operation_result_code));
connection_attempt->set_allocated_operation_result(
operation_result_proto.release());
}

void AnalyticsRecorder::OnOutgoingConnectionAttempt(
void AnalyticsRecorder::OnOutgoingConnectionAttemptWithMetadata(
const std::string &remote_endpoint_id, ConnectionAttemptType type,
Medium medium, ConnectionAttemptResult result, absl::Duration duration,
const std::string &connection_token,
Expand All @@ -406,6 +499,62 @@ void AnalyticsRecorder::OnOutgoingConnectionAttempt(
"null current_strategy_session_";
return;
}

ConnectionAttemptMetadataParams default_params = {};
if (connection_attempt_metadata_params == nullptr) {
connection_attempt_metadata_params = &default_params;
}

// For the case of transfer a big file and the upgrades always failure, then
// there will have repeating upgrade attempt and cause many same attempt value
// be log. So add a method to skip.
if (ConnectionAttemptResultCodeExistedLocked(
medium, OUTGOING, connection_token, type,
connection_attempt_metadata_params->operation_result_code)) {
return;
}

OnOutgoingConnectionAttemptLocked(remote_endpoint_id, type, medium, result,
duration, connection_token,
connection_attempt_metadata_params);
}

void AnalyticsRecorder::OnOutgoingConnectionAttempt(
const std::string &remote_endpoint_id, ConnectionAttemptType type,
Medium medium, ConnectionAttemptResult result, absl::Duration duration,
const std::string &connection_token,
OperationResultCode operation_result_code) {
MutexLock lock(&mutex_);
if (!CanRecordAnalyticsLocked("OnOutgoingConnectionAttempt")) {
return;
}
if (current_strategy_session_ == nullptr) {
NEARBY_LOGS(INFO) << "Unable to record outgoing connection attempt due to "
"null current_strategy_session_";
return;
}

ConnectionAttemptMetadataParams default_params = {};
default_params.operation_result_code = operation_result_code;

// For the case of transfer a big file and the upgrades always failure, then
// there will have repeating upgrade attempt and cause many same attempt value
// be log. So add a method to skip.
if (ConnectionAttemptResultCodeExistedLocked(
medium, OUTGOING, connection_token, type, operation_result_code)) {
return;
}

OnOutgoingConnectionAttemptLocked(remote_endpoint_id, type, medium, result,
duration, connection_token,
&default_params);
}

void AnalyticsRecorder::OnOutgoingConnectionAttemptLocked(
const std::string &remote_endpoint_id, ConnectionAttemptType type,
Medium medium, ConnectionAttemptResult result, absl::Duration duration,
const std::string &connection_token,
ConnectionAttemptMetadataParams *connection_attempt_metadata_params) {
auto *connection_attempt =
current_strategy_session_->add_connection_attempt();
connection_attempt->set_duration_millis(absl::ToInt64Milliseconds(duration));
Expand All @@ -415,10 +564,6 @@ void AnalyticsRecorder::OnOutgoingConnectionAttempt(
connection_attempt->set_attempt_result(result);
connection_attempt->set_connection_token(connection_token);

ConnectionAttemptMetadataParams default_params = {};
if (connection_attempt_metadata_params == nullptr) {
connection_attempt_metadata_params = &default_params;
}
auto *connection_attempt_metadata =
connection_attempt->mutable_connection_attempt_metadata();
connection_attempt_metadata->set_technology(
Expand Down Expand Up @@ -446,6 +591,15 @@ void AnalyticsRecorder::OnOutgoingConnectionAttempt(
connection_attempt_metadata->set_wifi_channel_width(
connection_attempt_metadata_params->channel_width);

auto operation_result_proto =
std::make_unique<ConnectionsLog::OperationResult>();
operation_result_proto->set_result_code(
connection_attempt_metadata_params->operation_result_code);
operation_result_proto->set_result_category(GetOperationResultCateory(
connection_attempt_metadata_params->operation_result_code));
connection_attempt->set_allocated_operation_result(
operation_result_proto.release());

if (type == INITIAL && result != RESULT_SUCCESS) {
auto it = outgoing_connection_requests_.find(remote_endpoint_id);
if (it != outgoing_connection_requests_.end()) {
Expand Down Expand Up @@ -757,7 +911,7 @@ AnalyticsRecorder::BuildConnectionAttemptMetadataParams(
int try_count, const std::string &network_operator,
const std::string &country_code, bool is_tdls_used,
bool wifi_hotspot_enabled, int max_wifi_tx_speed, int max_wifi_rx_speed,
int channel_width) {
int channel_width, OperationResultCode operation_result_code) {
auto params = std::make_unique<ConnectionAttemptMetadataParams>();
params->technology = technology;
params->band = band;
Expand All @@ -770,9 +924,37 @@ AnalyticsRecorder::BuildConnectionAttemptMetadataParams(
params->max_wifi_tx_speed = max_wifi_tx_speed;
params->max_wifi_rx_speed = max_wifi_rx_speed;
params->channel_width = channel_width;
params->operation_result_code = operation_result_code;
return params;
}

OperationResultCode AnalyticsRecorder::GetChannelIoErrorResultCodeFromMedium(
Medium medium) {
switch (medium) {
case Medium::BLUETOOTH:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_BT;
case Medium::WIFI_HOTSPOT:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_WIFI_HOTSPOT;
case Medium::BLE:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_BLE;
case Medium::BLE_L2CAP:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_BLE_L2CAP;
case Medium::WIFI_LAN:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_LAN;
case Medium::WIFI_AWARE:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_WIFI_AWARE;
case Medium::NFC:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_NFC;
case Medium::WIFI_DIRECT:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_WIFI_DIRECT;
case Medium::WEB_RTC:
return OperationResultCode::CONNECTIVITY_CHANNEL_IO_ERROR_ON_WEB_RTC;
default:
return OperationResultCode::
CONNECTIVITY_CHANNEL_IO_ERROR_ON_UNKNOWN_MEDIUM;
}
}

bool AnalyticsRecorder::CanRecordAnalyticsLocked(
absl::string_view method_name) {
NEARBY_VLOG(1) << "AnalyticsRecorder LogEvent " << method_name
Expand Down Expand Up @@ -1004,6 +1186,29 @@ void AnalyticsRecorder::MarkConnectionRequestIgnoredLocked(
}
}

bool AnalyticsRecorder::ConnectionAttemptResultCodeExistedLocked(
Medium medium, ConnectionAttemptDirection direction,
const std::string &connection_token, ConnectionAttemptType type,
OperationResultCode operation_result_code) {
if (current_strategy_session_ == nullptr ||
current_strategy_session_->connection_attempt_size() == 0) {
return false;
}
for (auto &connection_attempt :
current_strategy_session_->connection_attempt()) {
if (connection_attempt.medium() == medium &&
connection_attempt.direction() == direction &&
connection_attempt.connection_token() == connection_token &&
connection_attempt.type() == type &&
connection_attempt.operation_result().result_code() ==
operation_result_code) {
return true;
}
}

return false;
}

void AnalyticsRecorder::FinishUpgradeAttemptLocked(
const std::string &endpoint_id, BandwidthUpgradeResult result,
BandwidthUpgradeErrorStage error_stage, bool erase_item) {
Expand Down
Loading
Loading