Skip to content

Commit

Permalink
Add delete signal to operational patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoMinto committed Feb 21, 2022
1 parent 0de804b commit f41e313
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
71 changes: 52 additions & 19 deletions components/brave_federated/operational_patterns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void OperationalPatterns::Start() {
void OperationalPatterns::Stop() {
simulate_local_training_step_timer_.reset();
collection_slot_periodic_timer_.reset();

SendDelete();
}

void OperationalPatterns::LoadPrefs() {
Expand All @@ -123,14 +125,7 @@ void OperationalPatterns::OnSimulateLocalTrainingStepTimerFired() {
SendCollectionSlot();
}

void OperationalPatterns::SendCollectionSlot() {
current_collected_slot_ = GetCurrentCollectionSlot();
if (current_collected_slot_ == last_checked_slot_) {
return;
}

MaybeResetCollectionId();

void OperationalPatterns::Send(std::string payload, bool delete_signal) {
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL(federatedLearningUrl);
resource_request->headers.SetHeader("X-Brave-FL-Operational-Patterns", "?1");
Expand All @@ -140,22 +135,42 @@ void OperationalPatterns::SendCollectionSlot() {

url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), GetNetworkTrafficAnnotationTag());
url_loader_->AttachStringForUpload(BuildPayload(), "application/json");
url_loader_->AttachStringForUpload(payload, "application/json");

url_loader_->DownloadHeadersOnly(
url_loader_factory_.get(),
base::BindOnce(&OperationalPatterns::OnUploadComplete,
base::Unretained(this)));
base::Unretained(this), std::move(delete_signal)));
}

void OperationalPatterns::SendCollectionSlot() {
current_collected_slot_ = GetCurrentCollectionSlot();
if (current_collected_slot_ == last_checked_slot_) {
return;
}

MaybeResetCollectionId();

Send(BuildPayload(), false);
}

void OperationalPatterns::SendDelete() {
Send(BuildDeletePayload(), true);
}

void OperationalPatterns::OnUploadComplete(
bool delete_signal,
scoped_refptr<net::HttpResponseHeaders> headers) {
int response_code = -1;
if (headers)
response_code = headers->response_code();
if (response_code == 200) {
last_checked_slot_ = current_collected_slot_;
SavePrefs();
if (!delete_signal) {
last_checked_slot_ = current_collected_slot_;
SavePrefs();
} else {
ResetCollectionId();
}
}
}

Expand All @@ -174,6 +189,20 @@ std::string OperationalPatterns::BuildPayload() const {
return result;
}

std::string OperationalPatterns::BuildDeletePayload() const {
base::Value root(base::Value::Type::DICTIONARY);

root.SetKey("collection_id", base::Value(collection_id_));
root.SetKey("delete", base::Value(true));
root.SetKey("wiki-link", base::Value("https://github.com/brave/brave-browser/"
"wiki/Operational-Patterns"));

std::string result;
base::JSONWriter::Write(root, &result);

return result;
}

int OperationalPatterns::GetCurrentCollectionSlot() const {
base::Time::Exploded now;
base::Time::Now().LocalExplode(&now);
Expand All @@ -186,14 +215,18 @@ void OperationalPatterns::MaybeResetCollectionId() {
const base::Time now = base::Time::Now();
if (collection_id_.empty() || (!collection_id_expiration_time_.is_null() &&
now > collection_id_expiration_time_)) {
collection_id_ =
base::ToUpperASCII(base::UnguessableToken::Create().ToString());
collection_id_expiration_time_ =
now +
base::Seconds(brave_federated::features::GetCollectionIdLifetime() *
24 * 60 * 60);
SavePrefs();
ResetCollectionId();
}
}

void OperationalPatterns::ResetCollectionId() {
const base::Time now = base::Time::Now();
collection_id_ =
base::ToUpperASCII(base::UnguessableToken::Create().ToString());
collection_id_expiration_time_ =
now + base::Seconds(brave_federated::features::GetCollectionIdLifetime() *
24 * 60 * 60);
SavePrefs();
}

} // namespace brave_federated
9 changes: 7 additions & 2 deletions components/brave_federated/operational_patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@ class OperationalPatterns final {
private:
void OnCollectionSlotStartTimerFired();
void OnSimulateLocalTrainingStepTimerFired();
void OnUploadComplete(scoped_refptr<net::HttpResponseHeaders> headers);
void OnUploadComplete(bool delete_signal,
scoped_refptr<net::HttpResponseHeaders> headers);

void Send(std::string payload, bool delete_signal);
void SendCollectionSlot();
void SendDelete();

void SavePrefs();
void LoadPrefs();

std::string BuildPayload() const;
std::string BuildDeletePayload() const;
int GetCurrentCollectionSlot() const;


void ResetCollectionId();
void MaybeResetCollectionId();

raw_ptr<PrefService> pref_service_ = nullptr;
Expand Down

0 comments on commit f41e313

Please sign in to comment.