-
Notifications
You must be signed in to change notification settings - Fork 869
/
ads_serve.cc
246 lines (184 loc) · 6.33 KB
/
ads_serve.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <utility>
#include "bat/ads/internal/ads_serve.h"
#include "bat/ads/internal/static_values.h"
#include "bat/ads/internal/bundle.h"
#include "bat/ads/internal/logging.h"
#include "base/time/time.h"
#include "brave_base/random.h"
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
namespace ads {
AdsServe::AdsServe(AdsImpl* ads, AdsClient* ads_client, Bundle* bundle) :
url_(""),
next_catalog_check_timestamp_in_seconds(0),
next_retry_start_timer_in_(0),
catalog_last_updated_(0),
ads_(ads),
ads_client_(ads_client),
bundle_(bundle) {
BuildUrl();
}
AdsServe::~AdsServe() = default;
void AdsServe::BuildUrl() {
switch (_environment) {
case Environment::PRODUCTION: {
url_ = PRODUCTION_SERVER;
break;
}
case Environment::STAGING: {
url_ = STAGING_SERVER;
break;
}
case Environment::DEVELOPMENT: {
url_ = DEVELOPMENT_SERVER;
break;
}
}
url_ += CATALOG_PATH;
}
void AdsServe::DownloadCatalog() {
auto callback = std::bind(&AdsServe::OnCatalogDownloaded,
this, url_, _1, _2, _3);
ads_client_->URLRequest(url_, {}, "", "", URLRequestMethod::GET, callback);
}
void AdsServe::OnCatalogDownloaded(
const std::string& url,
const int response_status_code,
const std::string& response,
const std::map<std::string, std::string>& headers) {
auto should_retry = false;
if (response_status_code / 100 == 2) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Catalog downloaded', [ 'version', 'catalog', 'status' ]
if (!response.empty()) {
BLOG(INFO) << "Successfully downloaded catalog";
}
if (!ProcessCatalog(response)) {
should_retry = true;
}
} else if (response_status_code == 304) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Catalog current', { method, server, path }
BLOG(INFO) << "Catalog is already up to dates";
UpdateNextCatalogCheck();
} else {
// TODO(Terry Mancey): Implement Log (#44)
// 'Catalog download failed', { error, method, server, path }
std::string formatted_headers = "";
for (auto header = headers.begin(); header != headers.end(); ++header) {
formatted_headers += header->first + ": " + header->second;
if (header != headers.end()) {
formatted_headers += ", ";
}
}
BLOG(ERROR) << "Failed to download catalog from:"
<< std::endl << " url: " << url
<< std::endl << " response_status_code: " << response_status_code
<< std::endl << " response: " << response
<< std::endl << " headers: " << formatted_headers;
should_retry = true;
}
if (should_retry) {
RetryDownloadingCatalog();
return;
}
}
uint64_t AdsServe::CatalogLastUpdated() const {
return catalog_last_updated_;
}
void AdsServe::Reset() {
ads_->StopCollectingActivity();
next_retry_start_timer_in_ = 0;
next_catalog_check_timestamp_in_seconds = 0;
ResetCatalog();
}
void AdsServe::UpdateNextCatalogCheck() {
next_retry_start_timer_in_ = 0;
uint64_t ping;
if (_is_debug) {
ping = kDebugCatalogPing;
} else {
ping = bundle_->GetCatalogPing();
}
// Choose a geometrically distributed number of seconds so that from
// the server's perspective, at any given time, every client has the
// same distribution on when its next check will be, so knowing
// which client made the last one when reveals nothing about which
// client will make the next one.
auto rand_delay = brave_base::random::Geometric(ping);
next_catalog_check_timestamp_in_seconds = rand_delay;
ads_->StartCollectingActivity(next_catalog_check_timestamp_in_seconds);
}
//////////////////////////////////////////////////////////////////////////////
bool AdsServe::ProcessCatalog(const std::string& json) {
// TODO(Terry Mancey): Refactor function to use callbacks
Catalog catalog(ads_client_);
BLOG(INFO) << "Parsing catalog";
if (!catalog.FromJson(json)) {
return false;
}
BLOG(INFO) << "Catalog parsed";
// TODO(Terry Mancey): Implement Log (#44)
// 'Catalog parsed', underscore.extend(underscore.clone(header),
// { status: 'processed', campaigns: underscore.keys(campaigns).length,
// creativeSets: underscore.keys(creativeSets).length
if (!catalog.HasChanged(bundle_->GetCatalogId())) {
BLOG(WARNING) << "Catalog id " << catalog.GetId() <<
" matches current catalog id " << bundle_->GetCatalogId();
UpdateNextCatalogCheck();
return true;
}
BLOG(INFO) << "Generating bundle";
if (!bundle_->UpdateFromCatalog(catalog)) {
BLOG(ERROR) << "Failed to generate bundle";
return false;
}
auto callback = std::bind(&AdsServe::OnCatalogSaved, this, _1);
catalog.Save(json, callback);
auto issuers_info = std::make_unique<IssuersInfo>(catalog.GetIssuers());
ads_client_->SetCatalogIssuers(std::move(issuers_info));
return true;
}
void AdsServe::OnCatalogSaved(const Result result) {
if (result != SUCCESS) {
// If the catalog fails to save, we will retry the next time we collect
// activity
BLOG(ERROR) << "Failed to save catalog";
return;
}
BLOG(INFO) << "Successfully saved catalog";
}
void AdsServe::RetryDownloadingCatalog() {
BLOG(INFO) << "Retry downloading catalog";
if (next_retry_start_timer_in_ == 0) {
if (ads_->IsMobile()) {
next_retry_start_timer_in_ = 2 * base::Time::kSecondsPerMinute;
} else {
next_retry_start_timer_in_ = base::Time::kSecondsPerMinute;
}
} else {
next_retry_start_timer_in_ *= 2;
}
auto rand_delay = brave_base::random::Geometric(next_retry_start_timer_in_);
next_retry_start_timer_in_ = rand_delay;
ads_->StartCollectingActivity(next_retry_start_timer_in_);
}
void AdsServe::ResetCatalog() {
BLOG(INFO) << "Resetting catalog to default state";
Catalog catalog(ads_client_);
auto callback = std::bind(&AdsServe::OnCatalogReset, this, _1);
catalog.Reset(callback);
}
void AdsServe::OnCatalogReset(const Result result) {
if (result != SUCCESS) {
BLOG(ERROR) << "Failed to reset catalog";
return;
}
BLOG(INFO) << "Successfully reset catalog";
}
} // namespace ads