/
streams.py
408 lines (320 loc) · 16.5 KB
/
streams.py
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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import logging
from abc import ABC
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional
import pendulum
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources.streams import IncrementalMixin, Stream
from airbyte_cdk.sources.utils.transform import TransformConfig, TypeTransformer
from google.ads.googleads.errors import GoogleAdsException
from google.ads.googleads.v11.errors.types.authorization_error import AuthorizationErrorEnum
from google.ads.googleads.v11.errors.types.request_error import RequestErrorEnum
from google.ads.googleads.v11.services.services.google_ads_service.pagers import SearchPager
from .google_ads import GoogleAds
from .models import Customer
class cyclic_sieve:
def __init__(self, logger: logging.Logger, fraction: int = 10):
self._logger = logger
self._cycle_counter = 0
self._fraction = fraction
def __getattr__(self, item):
if self._cycle_counter % self._fraction == 0:
return getattr(self._logger, item)
return self.stub
def stub(self, *args, **kwargs):
pass
def bump(self):
self._cycle_counter += 1
def parse_dates(stream_slice):
start_date = pendulum.parse(stream_slice["start_date"])
end_date = pendulum.parse(stream_slice["end_date"])
return start_date, end_date
def chunk_date_range(
start_date: str,
conversion_window: int,
end_date: str = None,
days_of_data_storage: int = None,
range_days: int = None,
time_zone=None,
) -> Iterable[Optional[MutableMapping[str, any]]]:
"""
Returns `start_date` and `end_date` for the given stream_slice.
If (end_date - start_date) is a big date range (>= 1 month), it can take more than 2 hours to process all the records from the given slice.
After 2 hours next page tokens will be expired, finally resulting in page token expired error
Currently this method returns `start_date` and `end_date` with `range_days` difference which is 15 days in most cases.
"""
yesterday = pendulum.yesterday(tz=time_zone)
end_date = min(pendulum.parse(end_date), yesterday) if end_date else yesterday
start_date = pendulum.parse(start_date)
# For some metrics we can only get data not older than N days, it is Google Ads policy
if days_of_data_storage:
start_date = max(start_date, pendulum.now().subtract(days=days_of_data_storage - conversion_window))
# As in to return some state when state in abnormal
if start_date > end_date:
return [None]
# applying conversion window
start_date = start_date.subtract(days=conversion_window)
slice_start = start_date
while slice_start.date() <= end_date.date():
slice_end = min(end_date, slice_start.add(days=range_days - 1))
yield {
"start_date": slice_start.to_date_string(),
"end_date": slice_end.to_date_string(),
}
slice_start = slice_end.add(days=1)
class GoogleAdsStream(Stream, ABC):
CATCH_API_ERRORS = True
def __init__(self, api: GoogleAds, customers: List[Customer]):
self.google_ads_client = api
self.customers = customers
self.base_sieve_logger = cyclic_sieve(self.logger, 10)
def get_query(self, stream_slice: Mapping[str, Any]) -> str:
query = GoogleAds.convert_schema_into_query(schema=self.get_json_schema(), report_name=self.name)
return query
def parse_response(self, response: SearchPager) -> Iterable[Mapping]:
for result in response:
yield self.google_ads_client.parse_single_result(self.get_json_schema(), result)
def stream_slices(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Optional[Mapping[str, any]]]:
for customer in self.customers:
yield {"customer_id": customer.id}
def read_records(self, sync_mode, stream_slice: Optional[Mapping[str, Any]] = None, **kwargs) -> Iterable[Mapping[str, Any]]:
self.base_sieve_logger.bump()
self.base_sieve_logger.info(f"Read records using g-ads client. Stream slice is {stream_slice}")
if stream_slice is None:
return []
customer_id = stream_slice["customer_id"]
try:
response_records = self.google_ads_client.send_request(self.get_query(stream_slice), customer_id=customer_id)
for response in response_records:
yield from self.parse_response(response)
except GoogleAdsException as exc:
exc.customer_id = customer_id
if not self.CATCH_API_ERRORS:
raise
for error in exc.failure.errors:
if error.error_code.authorization_error == AuthorizationErrorEnum.AuthorizationError.CUSTOMER_NOT_ENABLED:
self.base_sieve_logger.error(error.message)
continue
# log and ignore only CUSTOMER_NOT_ENABLED error, otherwise - raise further
raise
class IncrementalGoogleAdsStream(GoogleAdsStream, IncrementalMixin, ABC):
days_of_data_storage = None
cursor_field = "segments.date"
primary_key = None
# Date range is set to 15 days, because for conversion_window_days default value is 14.
# Range less than 15 days will break the integration tests.
range_days = 15
def __init__(self, start_date: str, conversion_window_days: int, end_date: str = None, **kwargs):
self.conversion_window_days = conversion_window_days
self._start_date = start_date
self._end_date = end_date
self._state = {}
super().__init__(**kwargs)
self.incremental_sieve_logger = cyclic_sieve(self.logger, 10)
@property
def state(self) -> MutableMapping[str, Any]:
return self._state
@state.setter
def state(self, value: MutableMapping[str, Any]):
self._state.update(value)
def current_state(self, customer_id, default=None):
default = default or self.state.get(self.cursor_field)
return self.state.get(customer_id, {}).get(self.cursor_field) or default
def stream_slices(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Optional[MutableMapping[str, any]]]:
for customer in self.customers:
logger = cyclic_sieve(self.logger, 10)
stream_state = stream_state or {}
if stream_state.get(customer.id):
start_date = stream_state[customer.id].get(self.cursor_field) or self._start_date
# We should keep backward compatibility with the previous version
elif stream_state.get(self.cursor_field) and len(self.customers) == 1:
start_date = stream_state.get(self.cursor_field) or self._start_date
else:
start_date = self._start_date
end_date = self._end_date
logger.info(f"Generating slices for customer {customer.id}. Start date is {start_date}, end date is {end_date}")
for chunk in chunk_date_range(
start_date=start_date,
end_date=end_date,
conversion_window=self.conversion_window_days,
days_of_data_storage=self.days_of_data_storage,
range_days=self.range_days,
time_zone=customer.time_zone,
):
if chunk:
chunk["customer_id"] = customer.id
logger.info(f"Next slice is {chunk}")
logger.bump()
yield chunk
def read_records(
self, sync_mode: SyncMode, cursor_field: List[str] = None, stream_slice: MutableMapping[str, Any] = None, **kwargs
) -> Iterable[Mapping[str, Any]]:
"""
This method is overridden to handle GoogleAdsException with EXPIRED_PAGE_TOKEN error code,
and update `start_date` key in the `stream_slice` with the latest read record's cursor value, then retry the sync.
"""
self.incremental_sieve_logger.bump()
while True:
self.incremental_sieve_logger.info("Starting a while loop iteration")
customer_id = stream_slice and stream_slice["customer_id"]
try:
records = super().read_records(sync_mode, stream_slice=stream_slice)
for record in records:
current_state = self.current_state(customer_id)
if current_state:
date_in_current_stream = pendulum.parse(current_state)
date_in_latest_record = pendulum.parse(record[self.cursor_field])
cursor_value = (max(date_in_current_stream, date_in_latest_record)).to_date_string()
self.state = {customer_id: {self.cursor_field: cursor_value}}
self.incremental_sieve_logger.info(f"Updated state for customer {customer_id}. Full state is {self.state}.")
yield record
continue
self.state = {customer_id: {self.cursor_field: record[self.cursor_field]}}
self.incremental_sieve_logger.info(f"Initialized state for customer {customer_id}. Full state is {self.state}.")
yield record
continue
except GoogleAdsException as exception:
self.incremental_sieve_logger.info(f"Caught a GoogleAdsException: {str(exception)}")
error = next(iter(exception.failure.errors))
if error.error_code.request_error == RequestErrorEnum.RequestError.EXPIRED_PAGE_TOKEN:
start_date, end_date = parse_dates(stream_slice)
current_state = self.current_state(customer_id)
self.incremental_sieve_logger.info(
f"Start date is {start_date}. End date is {end_date}. Current state is {current_state}"
)
if (end_date - start_date).days == 1:
# If range days is 1, no need in retry, because it's the minimum date range
self.incremental_sieve_logger.error("Page token has expired.")
raise exception
elif current_state == stream_slice["start_date"]:
# It couldn't read all the records within one day, it will enter an infinite loop,
# so raise the error
self.incremental_sieve_logger.error("Page token has expired.")
raise exception
# Retry reading records from where it crushed
stream_slice["start_date"] = self.current_state(customer_id, default=stream_slice["start_date"])
self.incremental_sieve_logger.info(f"Retry reading records from where it crushed with a modified slice: {stream_slice}")
else:
# raise caught error for other error statuses
raise exception
else:
# return the control if no exception is raised
self.incremental_sieve_logger.info("Current slice has been read. Exiting read_records()")
return
def get_query(self, stream_slice: Mapping[str, Any] = None) -> str:
query = GoogleAds.convert_schema_into_query(
schema=self.get_json_schema(),
report_name=self.name,
from_date=stream_slice.get("start_date"),
to_date=stream_slice.get("end_date"),
cursor_field=self.cursor_field,
)
return query
class Accounts(IncrementalGoogleAdsStream):
"""
Accounts stream: https://developers.google.com/google-ads/api/fields/v11/customer
"""
primary_key = ["customer.id", "segments.date"]
class ServiceAccounts(GoogleAdsStream):
"""
This stream is intended to be used as a service class, not exposed to a user
"""
CATCH_API_ERRORS = False
primary_key = ["customer.id"]
class Campaigns(IncrementalGoogleAdsStream):
"""
Campaigns stream: https://developers.google.com/google-ads/api/fields/v11/campaign
"""
transformer = TypeTransformer(TransformConfig.DefaultSchemaNormalization)
primary_key = ["campaign.id", "segments.date", "segments.hour"]
class CampaignBudget(IncrementalGoogleAdsStream):
"""
Campaigns stream: https://developers.google.com/google-ads/api/fields/v13/campaign_budget
"""
transformer = TypeTransformer(TransformConfig.DefaultSchemaNormalization)
primary_key = ["campaign_budget.id", "segments.date"]
class CampaignLabels(GoogleAdsStream):
"""
Campaign labels stream: https://developers.google.com/google-ads/api/fields/v11/campaign_label
"""
# Note that this is a string type. Google doesn't return a more convenient identifier.
primary_key = ["campaign_label.resource_name"]
class AdGroups(IncrementalGoogleAdsStream):
"""
AdGroups stream: https://developers.google.com/google-ads/api/fields/v11/ad_group
"""
primary_key = ["ad_group.id", "segments.date"]
class AdGroupLabels(GoogleAdsStream):
"""
Ad Group Labels stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_label
"""
# Note that this is a string type. Google doesn't return a more convenient identifier.
primary_key = ["ad_group_label.resource_name"]
class AdGroupAds(IncrementalGoogleAdsStream):
"""
AdGroups stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_ad
"""
primary_key = ["ad_group_ad.ad.id", "segments.date"]
class AdGroupAdLabels(GoogleAdsStream):
"""
Ad Group Ad Labels stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_ad_label
"""
# Note that this is a string type. Google doesn't return a more convenient identifier.
primary_key = ["ad_group_ad_label.resource_name"]
class AccountPerformanceReport(IncrementalGoogleAdsStream):
"""
AccountPerformanceReport stream: https://developers.google.com/google-ads/api/fields/v11/customer
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#account_performance
"""
class AdGroupAdReport(IncrementalGoogleAdsStream):
"""
AdGroupAdReport stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_ad
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#ad_performance
"""
class DisplayKeywordPerformanceReport(IncrementalGoogleAdsStream):
"""
DisplayKeywordPerformanceReport stream: https://developers.google.com/google-ads/api/fields/v11/display_keyword_view
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#display_keyword_performance
"""
class DisplayTopicsPerformanceReport(IncrementalGoogleAdsStream):
"""
DisplayTopicsPerformanceReport stream: https://developers.google.com/google-ads/api/fields/v11/topic_view
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#display_topics_performance
"""
class ShoppingPerformanceReport(IncrementalGoogleAdsStream):
"""
ShoppingPerformanceReport stream: https://developers.google.com/google-ads/api/fields/v11/shopping_performance_view
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#shopping_performance
"""
class UserLocationReport(IncrementalGoogleAdsStream):
"""
UserLocationReport stream: https://developers.google.com/google-ads/api/fields/v11/user_location_view
Google Ads API field mapping: https://developers.google.com/google-ads/api/docs/migration/mapping#geo_performance
"""
class GeographicReport(IncrementalGoogleAdsStream):
"""
UserLocationReport stream: https://developers.google.com/google-ads/api/fields/v11/geographic_view
"""
class KeywordReport(IncrementalGoogleAdsStream):
"""
UserLocationReport stream: https://developers.google.com/google-ads/api/fields/v11/keyword_view
"""
class ClickView(IncrementalGoogleAdsStream):
"""
ClickView stream: https://developers.google.com/google-ads/api/reference/rpc/v11/ClickView
"""
primary_key = ["click_view.gclid", "segments.date", "segments.ad_network_type"]
days_of_data_storage = 90
range_days = 1
class UserInterest(GoogleAdsStream):
"""
Ad Group Ad Labels stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_ad_label
"""
primary_key = ["user_interest.user_interest_id"]
class Audience(GoogleAdsStream):
"""
Ad Group Ad Labels stream: https://developers.google.com/google-ads/api/fields/v11/ad_group_ad_label
"""
primary_key = ["audience.id"]