/
ParallelReportDownload.cs
executable file
·433 lines (377 loc) · 16.7 KB
/
ParallelReportDownload.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.Util.Reports;
using Google.Api.Ads.AdWords.Util.Reports.v201809;
using Google.Api.Ads.AdWords.v201809;
using Google.Api.Ads.Common.Util.Reports;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
/// <summary>
/// This code example runs a report for every advertiser account under a
/// given manager account, using multiple parallel threads. This code example
/// needs to be run against an AdWords manager account.
/// </summary>
public class ParallelReportDownload : ExampleBase
{
/// <summary>
/// The maximum number of reports to download in parallel. This number should
/// be less than or equal to <see cref="MAX_NUMBER_OF_THREADS"/>.
/// </summary>
private const int MAX_REPORT_DOWNLOADS_IN_PARALLEL = 3;
/// <summary>
/// The maximum number of threads to initialize for report downloads.
/// Normally, you would set this to <see cref="MAX_REPORT_DOWNLOADS_IN_PARALLEL"/>.
/// However, a more dynamic strategy involves changing
/// MAX_REPORT_DOWNLOADS_IN_PARALLEL at runtime depending on the AdWords
/// API server loads.
/// </summary>
private const int MAX_NUMBER_OF_THREADS = 10;
/// <summary>
/// Represents a report that was successfully downloaded.
/// </summary>
public class SuccessfulReportDownload
{
/// <summary>
/// Gets or sets the customer ID for the report.
/// </summary>
public long CustomerId { get; set; }
/// <summary>
/// Gets or sets the path to which report was downloaded.
/// </summary>
public string Path { get; set; }
}
/// <summary>
/// Represents a report download that failed.
/// </summary>
public class FailedReportDownload
{
/// <summary>
/// Gets or sets the customer ID for the report.
/// </summary>
public long CustomerId { get; set; }
/// <summary>
/// Gets or sets the exception that was thrown..
/// </summary>
public AdWordsReportsException Exception { get; set; }
}
/// <summary>
/// A data structure to hold data specific for a particular report download
/// thread.
/// </summary>
public class ReportDownloadData
{
/// <summary>
/// Gets or sets the application configuration.
/// </summary>
public AdWordsAppConfig Config { get; set; }
/// <summary>
/// Gets or sets the index of the thread that identifies it.
/// </summary>
public int ThreadIndex { get; set; }
/// <summary>
/// Gets or sets the folder to which reports are downloaded.
/// </summary>
public string DownloadFolder { get; set; }
/// <summary>
/// Gets or sets the event that signals the main thread that this thread
/// is finished with its job.
/// </summary>
public ManualResetEvent SignalEvent { get; set; }
/// <summary>
/// Gets or sets the queue that holds the list of all customerIDs to be
/// processed.
/// </summary>
public IProducerConsumerCollection<long> CustomerIdQueue { get; set; }
/// <summary>
/// Gets or sets the queue that holds the list of successful report
/// downloads.
/// </summary>
public IProducerConsumerCollection<SuccessfulReportDownload> SuccessfulReports
{
get;
set;
}
/// <summary>
/// Gets or sets the queue that holds the list of failed report downloads.
/// </summary>
public IProducerConsumerCollection<FailedReportDownload> FailedReports { get; set; }
/// <summary>
/// Gets or sets the lock that ensures only a fixed number of report
/// downloads happen simultaneously.
/// </summary>
public Semaphore QuotaLock { get; set; }
/// <summary>
/// The callback method for the report download thread.
/// </summary>
public void ThreadCallback(object arg)
{
string query = (string) arg;
AdWordsUser user = new AdWordsUser(this.Config);
while (true)
{
// Wait to acquire a lock on the quota lock.
QuotaLock.WaitOne();
// Try to get a customer ID from the queue.
long customerId = 0;
bool hasMoreCustomers = CustomerIdQueue.TryTake(out customerId);
if (!hasMoreCustomers)
{
// Nothing more to do, break the loop.
QuotaLock.Release();
break;
}
try
{
ProcessCustomer(user, customerId, query);
}
finally
{
// Release the quota lock once we have downloaded the report for the
// customer ID.
QuotaLock.Release();
}
}
// Mark the download as finished.
this.SignalEvent.Set();
}
/// <summary>
/// Processes the customer.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="customerId">The customer ID.</param>
/// <param name="query">The report query.</param>
private void ProcessCustomer(AdWordsUser user, long customerId, string query)
{
// Set the customer ID to the current customer.
this.Config.ClientCustomerId = customerId.ToString();
string downloadFile = string.Format("{0}{1}adgroup_{2:D10}.gz", this.DownloadFolder,
Path.DirectorySeparatorChar, customerId);
// Download the report.
Console.WriteLine("[Thread #{0}]: Downloading report for customer: {1} into {2}...",
this.ThreadIndex, customerId, downloadFile);
try
{
ReportUtilities utilities = new ReportUtilities(user, "v201809", query,
DownloadFormat.GZIPPED_CSV.ToString());
using (ReportResponse response = utilities.GetResponse())
{
response.Save(downloadFile);
}
// Mark this report download as success.
SuccessfulReportDownload success = new SuccessfulReportDownload
{
CustomerId = customerId,
Path = downloadFile
};
SuccessfulReports.TryAdd(success);
Console.WriteLine("Report was downloaded to '{0}'.", downloadFile);
}
catch (AdWordsReportsException e)
{
// Mark this report download as failure.
FailedReportDownload failure = new FailedReportDownload
{
CustomerId = customerId,
Exception = e
};
FailedReports.TryAdd(failure);
Console.WriteLine(
"Failed to download report for customer: {0}. Exception says {1}",
customerId, e.Message);
}
}
}
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
ParallelReportDownload codeExample = new ParallelReportDownload();
Console.WriteLine(codeExample.Description);
try
{
string fileName = "INSERT_FOLDER_NAME_HERE";
codeExample.Run(new AdWordsUser(), fileName);
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while running this code example. {0}",
ExampleUtilities.FormatException(e));
}
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description
{
get
{
return "This code example runs a report for every advertiser account under a " +
"given manager account, using multiple parallel threads. This code example " +
"needs to be run against an AdWords manager account.";
}
}
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="downloadFolder">The file to which the report is downloaded.
/// </param>
public void Run(AdWordsUser user, string downloadFolder)
{
// Increase the number of HTTP connections we can do in parallel.
System.Net.ServicePointManager.DefaultConnectionLimit = 100;
try
{
// Start the rate limiter with an initial value of zero, so that all
// threads block immediately.
Semaphore rateLimiter = new Semaphore(0, MAX_REPORT_DOWNLOADS_IN_PARALLEL);
// Get all the advertiser accounts under this manager account.
List<long> allCustomerIds = GetDescendantAdvertiserAccounts(user);
// Create a concurrent queue of customers so that all threads can work
// on the collection in parallel.
ConcurrentQueue<long> customerQueue = new ConcurrentQueue<long>(allCustomerIds);
// Create queues to keep track of successful and failed report downloads.
ConcurrentQueue<SuccessfulReportDownload> reportsSucceeeded =
new ConcurrentQueue<SuccessfulReportDownload>();
ConcurrentQueue<FailedReportDownload> reportsFailed =
new ConcurrentQueue<FailedReportDownload>();
// Keep an array of events. This is used by the main thread to wait for
// all worker threads to join.
ManualResetEvent[] doneEvents = new ManualResetEvent[MAX_NUMBER_OF_THREADS];
// The list of threads to download reports.
Thread[] threads = new Thread[MAX_NUMBER_OF_THREADS];
// The data for each thread.
ReportDownloadData[] threadData = new ReportDownloadData[MAX_NUMBER_OF_THREADS];
// The query to be run on each account.
ReportQuery query = new ReportQueryBuilder()
.Select("CampaignId", "AdGroupId", "Impressions", "Clicks", "Cost")
.From(ReportDefinitionReportType.ADGROUP_PERFORMANCE_REPORT)
.Where("AdGroupStatus").In("ENABLED", "PAUSED")
.During(ReportDefinitionDateRangeType.LAST_7_DAYS).Build();
// Initialize the threads and their data.
for (int i = 0; i < MAX_NUMBER_OF_THREADS; i++)
{
doneEvents[i] = new ManualResetEvent(false);
threadData[i] = new ReportDownloadData()
{
Config = (AdWordsAppConfig) (user.Config.Clone()),
DownloadFolder = downloadFolder,
SignalEvent = doneEvents[i],
ThreadIndex = i,
QuotaLock = rateLimiter,
CustomerIdQueue = customerQueue,
SuccessfulReports = reportsSucceeeded,
FailedReports = reportsFailed
};
threads[i] = new Thread(threadData[i].ThreadCallback);
}
// Start the threads. Since the initial value of rate limiter is zero,
// all threads will block immediately.
for (int i = 0; i < threads.Length; i++)
{
threads[i].Start(query);
}
// Now reset the rate limiter so all threads can start downloading reports.
rateLimiter.Release(MAX_REPORT_DOWNLOADS_IN_PARALLEL);
// Wait for all threads in pool to complete.
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("Download completed, results:");
Console.WriteLine("Successful reports:");
while (!reportsSucceeeded.IsEmpty)
{
SuccessfulReportDownload success = null;
if (reportsSucceeeded.TryDequeue(out success))
{
Console.WriteLine("Client ID: {0}, Path: {1}", success.CustomerId,
success.Path);
}
}
Console.WriteLine("Failed reports:");
while (!reportsFailed.IsEmpty)
{
FailedReportDownload failure = null;
if (reportsFailed.TryDequeue(out failure))
{
Console.WriteLine("Client ID: {0}, Cause: {1}", failure.CustomerId,
failure.Exception.Message);
}
}
Console.WriteLine("All reports are downloaded.");
}
catch (Exception e)
{
throw new System.ApplicationException("Failed to download reports.", e);
}
}
/// <summary>
/// Gets the list of all descendant advertiser accounts under the manager
/// account.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <returns>A list of customer IDs for descendant advertiser accounts.</returns>
public static List<long> GetDescendantAdvertiserAccounts(AdWordsUser user)
{
List<long> retval = new List<long>();
// Get the ManagedCustomerService.
ManagedCustomerService managedCustomerService =
(ManagedCustomerService) user.GetService(AdWordsService.v201809
.ManagedCustomerService);
// Create selector.
Selector selector = new Selector()
{
fields = new string[]
{
ManagedCustomer.Fields.CustomerId
},
predicates = new Predicate[]
{
// Select only advertiser accounts.
Predicate.Equals(ManagedCustomer.Fields.CanManageClients, false.ToString())
},
paging = Paging.Default
};
ManagedCustomerPage page = null;
try
{
do
{
page = managedCustomerService.get(selector);
if (page.entries != null)
{
foreach (ManagedCustomer customer in page.entries)
{
retval.Add(customer.customerId);
}
}
selector.paging.IncreaseOffset();
} while (selector.paging.startIndex < page.totalNumEntries);
}
catch (Exception)
{
Console.WriteLine(
"Failed to retrieve advertiser accounts under the manager account.");
throw;
}
return retval;
}
}
}