Change AnalyticsRecorder to accept AttachmentContainer.

PiperOrigin-RevId: 639879717
This commit is contained in:
Francis Tsui
2024-06-03 12:48:22 -07:00
committed by Copybara-Service
parent 911d55ff63
commit 60b858d487
7 changed files with 127 additions and 193 deletions
-2
View File
@@ -31,11 +31,9 @@ cc_library(
"//sharing:attachments",
"//sharing:types",
"//sharing/common:enum",
"//sharing/internal/public:logging",
"//sharing/proto:enums_cc_proto",
"//sharing/proto/analytics:sharing_log_cc_proto",
"@com_google_absl//absl/random",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protobuf_lite",
],
+86 -112
View File
@@ -18,18 +18,16 @@
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "google/protobuf/duration.pb.h"
#include "absl/random/random.h"
#include "absl/strings/str_cat.h"
#include "proto/sharing_enums.pb.h"
#include "sharing/analytics/analytics_device_settings.h"
#include "sharing/analytics/analytics_information.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/file_attachment.h"
#include "sharing/internal/public/logging.h"
#include "sharing/proto/analytics/nearby_sharing_log.pb.h"
#include "sharing/proto/enums.pb.h"
#include "sharing/share_target.h"
@@ -172,116 +170,95 @@ analytics::proto::SharingLog::ShareTargetInfo* GetAllocatedShareTargetInfo(
}
analytics::proto::SharingLog::AttachmentsInfo* GenerateAllocatedAttachmentInfo(
const std::vector<std::unique_ptr<Attachment>>& attachments) {
const AttachmentContainer& attachments) {
auto attachments_info =
analytics::proto::SharingLog::AttachmentsInfo::default_instance().New();
for (auto& attachment : attachments) {
int64_t size = attachment->size();
if (attachment->family() == Attachment::Family::kText) {
analytics::proto::SharingLog::TextAttachment::Type type =
analytics::proto::SharingLog::TextAttachment::UNKNOWN_TEXT_TYPE;
switch (attachment->GetShareType()) {
case ShareType::kPhone:
type = analytics::proto::SharingLog::TextAttachment::PHONE_NUMBER;
break;
case ShareType::kUrl:
type = analytics::proto::SharingLog::TextAttachment::URL;
break;
case ShareType::kAddress:
type = analytics::proto::SharingLog::TextAttachment::ADDRESS;
break;
case ShareType::kText:
// Apply UNKNOWN_TEXT_TYPE for it based on analytics design.
break;
default:
break;
}
::google::protobuf::RepeatedPtrField<analytics::proto::SharingLog_TextAttachment>*
text_attachments = attachments_info->mutable_text_attachment();
analytics::proto::SharingLog_TextAttachment* text_attachment =
analytics::proto::SharingLog::TextAttachment::default_instance()
.New();
text_attachment->set_type(type);
text_attachment->set_size_bytes(size);
text_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment->source_type()));
text_attachment->set_batch_id(attachment->batch_id());
text_attachments->AddAllocated(text_attachment);
} else if (attachment->family() == Attachment::Family::kFile) {
analytics::proto::SharingLog::FileAttachment::Type type =
analytics::proto::SharingLog::FileAttachment::UNKNOWN_FILE_TYPE;
switch (attachment->GetShareType()) {
case ShareType::kImageFile:
type = analytics::proto::SharingLog::FileAttachment::IMAGE;
break;
case ShareType::kVideoFile:
type = analytics::proto::SharingLog::FileAttachment::VIDEO;
break;
case ShareType::kAudioFile:
type = analytics::proto::SharingLog::FileAttachment::AUDIO;
break;
case ShareType::kPdfFile:
case ShareType::kTextFile:
case ShareType::kGoogleDocsFile:
case ShareType::kGoogleSheetsFile:
case ShareType::kGoogleSlidesFile:
type = analytics::proto::SharingLog::FileAttachment::DOCUMENT;
break;
case ShareType::kUnknownFile:
// The default type is set to type.
break;
default:
break;
}
::google::protobuf::RepeatedPtrField<analytics::proto::SharingLog_FileAttachment>*
file_attachments = attachments_info->mutable_file_attachment();
analytics::proto::SharingLog_FileAttachment* file_attachment =
analytics::proto::SharingLog::FileAttachment::default_instance()
.New();
file_attachment->set_type(type);
file_attachment->set_size_bytes(size);
file_attachment->set_offset_bytes(0);
file_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment->source_type()));
file_attachment->set_batch_id(attachment->batch_id());
file_attachments->AddAllocated(file_attachment);
} else if (attachment->family() == Attachment::Family::kWifiCredentials) {
::google::protobuf::RepeatedPtrField<
analytics::proto::SharingLog_WifiCredentialsAttachment>*
wifi_credentials_attachments =
attachments_info->mutable_wifi_credentials_attachment();
analytics::proto::SharingLog_WifiCredentialsAttachment*
wifi_credentials_attachment =
analytics::proto::SharingLog::WifiCredentialsAttachment::
default_instance()
.New();
wifi_credentials_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment->source_type()));
wifi_credentials_attachment->set_batch_id(attachment->batch_id());
wifi_credentials_attachments->AddAllocated(wifi_credentials_attachment);
} else {
NL_LOG(WARNING)
<< __func__
<< "Unable to create event for attachment info. Unknown attachment "
<< attachment->id();
continue;
for (const auto& attachment : attachments.GetTextAttachments()) {
analytics::proto::SharingLog::TextAttachment::Type type =
analytics::proto::SharingLog::TextAttachment::UNKNOWN_TEXT_TYPE;
switch (attachment.GetShareType()) {
case ShareType::kPhone:
type = analytics::proto::SharingLog::TextAttachment::PHONE_NUMBER;
break;
case ShareType::kUrl:
type = analytics::proto::SharingLog::TextAttachment::URL;
break;
case ShareType::kAddress:
type = analytics::proto::SharingLog::TextAttachment::ADDRESS;
break;
case ShareType::kText:
// Apply UNKNOWN_TEXT_TYPE for it based on analytics design.
break;
default:
break;
}
::google::protobuf::RepeatedPtrField<analytics::proto::SharingLog_TextAttachment>*
text_attachments = attachments_info->mutable_text_attachment();
analytics::proto::SharingLog_TextAttachment* text_attachment =
analytics::proto::SharingLog::TextAttachment::default_instance().New();
text_attachment->set_type(type);
text_attachment->set_size_bytes(attachment.size());
text_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment.source_type()));
text_attachment->set_batch_id(attachment.batch_id());
text_attachments->AddAllocated(text_attachment);
}
if (attachments_info->file_attachment_size() == 0 &&
attachments_info->text_attachment_size() == 0 &&
attachments_info->wifi_credentials_attachment_size() == 0) {
std::string type =
attachments.empty()
? "NULL"
: absl::StrCat(static_cast<int>(attachments[0]->GetShareType()));
NL_LOG(WARNING) << __func__ << "attachmentInfo is empty, attachment size="
<< attachments.size() << ", type=" << type;
for (const auto& attachment : attachments.GetFileAttachments()) {
analytics::proto::SharingLog::FileAttachment::Type type =
analytics::proto::SharingLog::FileAttachment::UNKNOWN_FILE_TYPE;
switch (attachment.GetShareType()) {
case ShareType::kImageFile:
type = analytics::proto::SharingLog::FileAttachment::IMAGE;
break;
case ShareType::kVideoFile:
type = analytics::proto::SharingLog::FileAttachment::VIDEO;
break;
case ShareType::kAudioFile:
type = analytics::proto::SharingLog::FileAttachment::AUDIO;
break;
case ShareType::kPdfFile:
case ShareType::kTextFile:
case ShareType::kGoogleDocsFile:
case ShareType::kGoogleSheetsFile:
case ShareType::kGoogleSlidesFile:
type = analytics::proto::SharingLog::FileAttachment::DOCUMENT;
break;
case ShareType::kUnknownFile:
// The default type is set to type.
break;
default:
break;
}
::google::protobuf::RepeatedPtrField<analytics::proto::SharingLog_FileAttachment>*
file_attachments = attachments_info->mutable_file_attachment();
analytics::proto::SharingLog_FileAttachment* file_attachment =
analytics::proto::SharingLog::FileAttachment::default_instance().New();
file_attachment->set_type(type);
file_attachment->set_size_bytes(attachment.size());
file_attachment->set_offset_bytes(0);
file_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment.source_type()));
file_attachment->set_batch_id(attachment.batch_id());
file_attachments->AddAllocated(file_attachment);
}
for (const auto& attachment : attachments.GetWifiCredentialsAttachments()) {
::google::protobuf::RepeatedPtrField<
analytics::proto::SharingLog_WifiCredentialsAttachment>*
wifi_credentials_attachments =
attachments_info->mutable_wifi_credentials_attachment();
analytics::proto::SharingLog_WifiCredentialsAttachment*
wifi_credentials_attachment =
analytics::proto::SharingLog::WifiCredentialsAttachment::
default_instance()
.New();
wifi_credentials_attachment->set_source_type(
GetLoggerAttachmentSourceType(attachment.source_type()));
wifi_credentials_attachment->set_batch_id(attachment.batch_id());
wifi_credentials_attachments->AddAllocated(wifi_credentials_attachment);
}
return attachments_info;
}
@@ -437,7 +414,7 @@ void AnalyticsRecorder::NewAdvertiseDevicePresenceStart(
}
void AnalyticsRecorder::NewDescribeAttachments(
const std::vector<std::unique_ptr<Attachment>>& attachments) {
const AttachmentContainer& attachments) {
std::unique_ptr<SharingLog> sharing_log = CreateSharingLog(
EventCategory::SENDING_EVENT, EventType::DESCRIBE_ATTACHMENTS);
@@ -496,8 +473,7 @@ void AnalyticsRecorder::NewEnableNearbySharing(
}
void AnalyticsRecorder::NewOpenReceivedAttachments(
const std::vector<std::unique_ptr<Attachment>>& attachments,
int64_t session_id) {
const AttachmentContainer& attachments, int64_t session_id) {
std::unique_ptr<SharingLog> sharing_log = CreateSharingLog(
EventCategory::RECEIVING_EVENT, EventType::OPEN_RECEIVED_ATTACHMENTS);
@@ -555,8 +531,7 @@ void AnalyticsRecorder::NewReceiveAttachmentsEnd(
}
void AnalyticsRecorder::NewReceiveAttachmentsStart(
int64_t session_id,
const std::vector<std::unique_ptr<Attachment>>& attachments) {
int64_t session_id, const AttachmentContainer& attachments) {
std::unique_ptr<SharingLog> sharing_log = CreateSharingLog(
EventCategory::RECEIVING_EVENT, EventType::RECEIVE_ATTACHMENTS_START);
@@ -752,8 +727,7 @@ void AnalyticsRecorder::NewSendAttachmentsEnd(
}
void AnalyticsRecorder::NewSendAttachmentsStart(
int64_t session_id,
const std::vector<std::unique_ptr<Attachment>>& attachments,
int64_t session_id, const AttachmentContainer& attachments,
int transfer_position, int concurrent_connections) {
std::unique_ptr<SharingLog> sharing_log = CreateSharingLog(
EventCategory::SENDING_EVENT, EventType::SEND_ATTACHMENTS_START);
+10 -14
View File
@@ -19,13 +19,12 @@
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "internal/analytics/event_logger.h"
#include "proto/sharing_enums.pb.h"
#include "sharing/analytics/analytics_device_settings.h"
#include "sharing/analytics/analytics_information.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/analytics/nearby_sharing_log.pb.h"
#include "sharing/proto/enums.pb.h"
@@ -72,8 +71,7 @@ class AnalyticsRecorder {
nearby::sharing::proto::DataUsage data_usage,
std::optional<std::string> referrer_package);
void NewDescribeAttachments(
const std::vector<std::unique_ptr<Attachment>>& attachments);
void NewDescribeAttachments(const AttachmentContainer& attachments);
void NewDiscoverShareTarget(
ShareTarget share_target, int64_t session_id,
@@ -84,9 +82,8 @@ class AnalyticsRecorder {
void NewEnableNearbySharing(
location::nearby::proto::sharing::NearbySharingStatus status);
void NewOpenReceivedAttachments(
const std::vector<std::unique_ptr<Attachment>>& attachments,
int64_t session_id);
void NewOpenReceivedAttachments(const AttachmentContainer& attachments,
int64_t session_id);
void NewProcessReceivedAttachmentsEnd(
int64_t session_id,
@@ -98,9 +95,8 @@ class AnalyticsRecorder {
location::nearby::proto::sharing::AttachmentTransmissionStatus status,
std::optional<std::string> referrer_package);
void NewReceiveAttachmentsStart(
int64_t session_id,
const std::vector<std::unique_ptr<Attachment>>& attachments);
void NewReceiveAttachmentsStart(int64_t session_id,
const AttachmentContainer& attachments);
void NewReceiveFastInitialization(int64_t timeElapseSinceScreenUnlockMillis);
@@ -138,10 +134,10 @@ class AnalyticsRecorder {
connection_layer_status,
location::nearby::proto::sharing::OSType share_target_os_type);
void NewSendAttachmentsStart(
int64_t session_id,
const std::vector<std::unique_ptr<Attachment>>& attachments,
int transfer_position, int concurrent_connections);
void NewSendAttachmentsStart(int64_t session_id,
const AttachmentContainer& attachments,
int transfer_position,
int concurrent_connections);
void NewSendFastInitialization();
+27 -38
View File
@@ -16,10 +16,8 @@
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "google/protobuf/duration.pb.h"
#include "gmock/gmock.h"
@@ -30,7 +28,7 @@
#include "proto/sharing_enums.pb.h"
#include "sharing/analytics/analytics_device_settings.h"
#include "sharing/analytics/analytics_information.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/file_attachment.h"
#include "sharing/proto/analytics/nearby_sharing_log.pb.h"
@@ -38,7 +36,6 @@
#include "sharing/proto/wire_format.pb.h"
#include "sharing/share_target.h"
#include "sharing/text_attachment.h"
#include "google/protobuf/message_lite.h"
namespace nearby {
namespace sharing {
@@ -282,32 +279,27 @@ TEST_F(AnalyticsRecorderTest, NewDescribeAttachments) {
SharingLog::FileAttachment::DOCUMENT);
});
std::vector<std::unique_ptr<Attachment>> attachments;
attachments.push_back(std::make_unique<FileAttachment>(
1, 2, std::string(kFileName), "", service::proto::FileMetadata::IMAGE));
attachments.push_back(std::make_unique<FileAttachment>(
2, 3, std::string(kFileDocumentName), std::string(kFileMimeType),
service::proto::FileMetadata::DOCUMENT));
attachments.push_back(std::make_unique<FileAttachment>(
3, 4, std::string(kFileName), "", service::proto::FileMetadata::AUDIO));
attachments.push_back(std::make_unique<FileAttachment>(
4, 5, std::string(kFileName), std::string(kTextMimeType),
service::proto::FileMetadata::DOCUMENT));
attachments.push_back(std::make_unique<TextAttachment>(
5, service::proto::TextMetadata::TEXT, std::string(kTextBody),
kTextBody.size()));
attachments.push_back(std::make_unique<TextAttachment>(
6, service::proto::TextMetadata::PHONE_NUMBER, std::string(kTextBody),
kTextBody.size()));
attachments.push_back(std::make_unique<TextAttachment>(
7, service::proto::TextMetadata::URL, std::string(kTextBody),
kTextBody.size()));
attachments.push_back(std::make_unique<TextAttachment>(
8, service::proto::TextMetadata::ADDRESS, std::string(kTextBody),
kTextBody.size()));
attachments.push_back(std::make_unique<TextAttachment>(
9, service::proto::TextMetadata::UNKNOWN, std::string(kTextBody),
kTextBody.size()));
AttachmentContainer attachments(
{TextAttachment(5, service::proto::TextMetadata::TEXT,
std::string(kTextBody), kTextBody.size()),
TextAttachment(6, service::proto::TextMetadata::PHONE_NUMBER,
std::string(kTextBody), kTextBody.size()),
TextAttachment(7, service::proto::TextMetadata::URL,
std::string(kTextBody), kTextBody.size()),
TextAttachment(8, service::proto::TextMetadata::ADDRESS,
std::string(kTextBody), kTextBody.size()),
TextAttachment(9, service::proto::TextMetadata::UNKNOWN,
std::string(kTextBody), kTextBody.size())},
{FileAttachment(1, 2, std::string(kFileName), "",
service::proto::FileMetadata::IMAGE),
FileAttachment(2, 3, std::string(kFileDocumentName),
std::string(kFileMimeType),
service::proto::FileMetadata::DOCUMENT),
FileAttachment(3, 4, std::string(kFileName), "",
service::proto::FileMetadata::AUDIO),
FileAttachment(4, 5, std::string(kFileName), std::string(kTextMimeType),
service::proto::FileMetadata::DOCUMENT)},
{});
analytics_recoder().NewDescribeAttachments(attachments);
}
@@ -327,8 +319,7 @@ TEST_F(AnalyticsRecorderTest, EmptyDescribeAttachments) {
0);
});
std::vector<std::unique_ptr<Attachment>> attachments;
analytics_recoder().NewDescribeAttachments(attachments);
analytics_recoder().NewDescribeAttachments(AttachmentContainer());
}
TEST_F(AnalyticsRecorderTest, NewDiscoverShareTarget) {
@@ -397,8 +388,7 @@ TEST_F(AnalyticsRecorderTest, NewOpenReceivedAttachments) {
EXPECT_EQ(log.open_received_attachments().session_id(), 1);
});
analytics_recoder().NewOpenReceivedAttachments(
std::vector<std::unique_ptr<Attachment>>(), 1);
analytics_recoder().NewOpenReceivedAttachments(AttachmentContainer(), 1);
}
TEST_F(AnalyticsRecorderTest, NewProcessReceivedAttachmentsEnd) {
@@ -453,8 +443,7 @@ TEST_F(AnalyticsRecorderTest, NewReceiveAttachmentsStart) {
0);
});
analytics_recoder().NewReceiveAttachmentsStart(
1, std::vector<std::unique_ptr<Attachment>>());
analytics_recoder().NewReceiveAttachmentsStart(1, AttachmentContainer());
}
TEST_F(AnalyticsRecorderTest, NewReceiveFastInitialization) {
@@ -628,8 +617,8 @@ TEST_F(AnalyticsRecorderTest, NewSendAttachmentsStart) {
EXPECT_EQ(log.send_attachments_start().concurrent_connections(), 200);
});
analytics_recoder().NewSendAttachmentsStart(
1, std::vector<std::unique_ptr<Attachment>>(), 100, 200);
analytics_recoder().NewSendAttachmentsStart(1, AttachmentContainer(), 100,
200);
}
TEST_F(AnalyticsRecorderTest, NewSendFastInitialization) {
+4 -4
View File
@@ -751,7 +751,7 @@ void NearbySharingServiceImpl::SendAttachments(
ShareTarget share_target, bool success) {
// Log analytics event of describing attachments.
analytics_recorder_->NewDescribeAttachments(
share_target.GetAttachments());
share_target.attachment_container);
OnCreatePayloads(std::move(endpoint_info),
share_target, success);
@@ -971,7 +971,7 @@ void NearbySharingServiceImpl::Open(
// Log analytics event of opening received attachments.
ShareTargetInfo* info = GetShareTargetInfo(share_target.id);
analytics_recorder_->NewOpenReceivedAttachments(
share_target.GetAttachments(),
share_target.attachment_container,
info != nullptr ? info->session_id() : 0);
status_codes_callback(
@@ -2565,7 +2565,7 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendPayloads(
ShareTarget cached_share_target = info->share_target();
// Log analytics event of sending attachment start.
analytics_recorder_->NewSendAttachmentsStart(
info->session_id(), cached_share_target.GetAttachments(),
info->session_id(), cached_share_target.attachment_container,
/*transfer_position=*/GetConnectedShareTargetPos(share_target),
/*concurrent_connections=*/GetConnectedShareTargetCount());
@@ -2592,7 +2592,7 @@ void NearbySharingServiceImpl::OnPayloadPathsRegistered(
// Log analytics event of starting to receive payloads.
analytics_recorder_->NewReceiveAttachmentsStart(
receiving_session_id_, share_target.GetAttachments());
receiving_session_id_, share_target.attachment_container);
info->set_payload_tracker(std::make_shared<PayloadTracker>(
context_, share_target.id, share_target.attachment_container,
-20
View File
@@ -91,26 +91,6 @@ std::vector<int64_t> ShareTarget::GetAttachmentIds() const {
return attachment_ids;
}
std::vector<std::unique_ptr<Attachment>> ShareTarget::GetAttachments() const {
std::vector<std::unique_ptr<Attachment>> attachments;
attachments.reserve(attachment_container.GetAttachmentCount());
for (const auto& file : attachment_container.GetFileAttachments()) {
attachments.push_back(std::make_unique<FileAttachment>(file));
}
for (const auto& text : attachment_container.GetTextAttachments()) {
attachments.push_back(std::make_unique<TextAttachment>(text));
}
for (const auto& wifi_credentials :
attachment_container.GetWifiCredentialsAttachments()) {
attachments.push_back(
std::make_unique<WifiCredentialsAttachment>(wifi_credentials));
}
return attachments;
}
int64_t ShareTarget::GetTotalAttachmentsSize() const {
return attachment_container.GetTotalAttachmentsSize();
}
-3
View File
@@ -16,13 +16,11 @@
#define THIRD_PARTY_NEARBY_SHARING_SHARE_TARGET_H_
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "internal/network/url.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/file_attachment.h"
@@ -50,7 +48,6 @@ struct ShareTarget {
~ShareTarget();
std::vector<int64_t> GetAttachmentIds() const;
std::vector<std::unique_ptr<Attachment>> GetAttachments() const;
int64_t GetTotalAttachmentsSize() const;
std::string ToString() const;