garf-executors 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of garf-executors might be problematic. Click here for more details.
- garf_executors/__init__.py +26 -0
- garf_executors/api_executor.py +98 -0
- garf_executors/bq_executor.py +123 -0
- garf_executors/entrypoints/__init__.py +0 -0
- garf_executors/entrypoints/cli/__init__.py +0 -0
- garf_executors/entrypoints/cli/api.py +213 -0
- garf_executors/entrypoints/cli/bq.py +112 -0
- garf_executors/entrypoints/cli/gaarf.py +213 -0
- garf_executors/entrypoints/cli/sql.py +109 -0
- garf_executors/entrypoints/utils.py +470 -0
- garf_executors/sql_executor.py +79 -0
- garf_executors-0.0.1.dist-info/METADATA +30 -0
- garf_executors-0.0.1.dist-info/RECORD +16 -0
- garf_executors-0.0.1.dist-info/WHEEL +5 -0
- garf_executors-0.0.1.dist-info/entry_points.txt +3 -0
- garf_executors-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Copyright 2022 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
"""Module for defing `garf` CLI utility.
|
|
14
|
+
|
|
15
|
+
`garf` allows to execute GAQL queries and store results in local/remote
|
|
16
|
+
storage.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import argparse
|
|
22
|
+
import functools
|
|
23
|
+
import sys
|
|
24
|
+
from collections.abc import MutableSequence
|
|
25
|
+
from concurrent import futures
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
|
|
28
|
+
import smart_open
|
|
29
|
+
import yaml
|
|
30
|
+
from garf import api_clients, exceptions, query_executor
|
|
31
|
+
from garf.cli import utils
|
|
32
|
+
from garf.io import reader, writer
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main():
|
|
36
|
+
parser = argparse.ArgumentParser()
|
|
37
|
+
parser.add_argument('query', nargs='*')
|
|
38
|
+
parser.add_argument('-c', '--config', dest='garf_config', default=None)
|
|
39
|
+
parser.add_argument('--account', dest='account', default=None)
|
|
40
|
+
parser.add_argument('--output', dest='output', default=None)
|
|
41
|
+
parser.add_argument('--input', dest='input', default='file')
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
'--ads-config', dest='config', default=str(Path.home() / 'google-ads.yaml')
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument('--api-version', dest='api_version', default=None)
|
|
46
|
+
parser.add_argument('--log', '--loglevel', dest='loglevel', default='info')
|
|
47
|
+
parser.add_argument('--logger', dest='logger', default='local')
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
'--customer-ids-query', dest='customer_ids_query', default=None
|
|
50
|
+
)
|
|
51
|
+
parser.add_argument(
|
|
52
|
+
'--customer-ids-query-file', dest='customer_ids_query_file', default=None
|
|
53
|
+
)
|
|
54
|
+
parser.add_argument('--save-config', dest='save_config', action='store_true')
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
'--no-save-config', dest='save_config', action='store_false'
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
'--config-destination', dest='save_config_dest', default='config.yaml'
|
|
60
|
+
)
|
|
61
|
+
parser.add_argument(
|
|
62
|
+
'--parallel-queries', dest='parallel_queries', action='store_true'
|
|
63
|
+
)
|
|
64
|
+
parser.add_argument(
|
|
65
|
+
'--no-parallel-queries', dest='parallel_queries', action='store_false'
|
|
66
|
+
)
|
|
67
|
+
parser.add_argument(
|
|
68
|
+
'--optimize-performance', dest='optimize_performance', default='NONE'
|
|
69
|
+
)
|
|
70
|
+
parser.add_argument('--dry-run', dest='dry_run', action='store_true')
|
|
71
|
+
parser.add_argument(
|
|
72
|
+
'--disable-account-expansion',
|
|
73
|
+
dest='disable_account_expansion',
|
|
74
|
+
action='store_true',
|
|
75
|
+
)
|
|
76
|
+
parser.add_argument('-v', '--version', dest='version', action='store_true')
|
|
77
|
+
parser.add_argument(
|
|
78
|
+
'--parallel-threshold', dest='parallel_threshold', default=None, type=int
|
|
79
|
+
)
|
|
80
|
+
parser.set_defaults(save_config=False)
|
|
81
|
+
parser.set_defaults(parallel_queries=True)
|
|
82
|
+
parser.set_defaults(dry_run=False)
|
|
83
|
+
parser.set_defaults(disable_account_expansion=False)
|
|
84
|
+
args = parser.parse_known_args()
|
|
85
|
+
main_args = args[0]
|
|
86
|
+
|
|
87
|
+
if main_args.version:
|
|
88
|
+
import pkg_resources
|
|
89
|
+
|
|
90
|
+
version = pkg_resources.require('google-ads-api-report-fetcher')[0].version
|
|
91
|
+
print(f'garf version {version}')
|
|
92
|
+
sys.exit()
|
|
93
|
+
|
|
94
|
+
logger = utils.init_logging(
|
|
95
|
+
loglevel=main_args.loglevel.upper(), logger_type=main_args.logger
|
|
96
|
+
)
|
|
97
|
+
if not main_args.query:
|
|
98
|
+
logger.error('Please provide one or more queries to run')
|
|
99
|
+
raise exceptions.GarfMissingQueryException(
|
|
100
|
+
'Please provide one or more queries to run'
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
with smart_open.open(main_args.config, 'r', encoding='utf-8') as f:
|
|
104
|
+
google_ads_config_dict = yaml.safe_load(f)
|
|
105
|
+
|
|
106
|
+
config = utils.ConfigBuilder('garf').build(vars(main_args), args[1])
|
|
107
|
+
if not config.account:
|
|
108
|
+
if mcc := google_ads_config_dict.get('login_customer_id'):
|
|
109
|
+
config.account = str(mcc)
|
|
110
|
+
else:
|
|
111
|
+
raise exceptions.GarfMissingAccountException(
|
|
112
|
+
'No account found, please specify via --account CLI flag'
|
|
113
|
+
'or add as login_customer_id in google-ads.yaml'
|
|
114
|
+
)
|
|
115
|
+
logger.debug('config: %s', config)
|
|
116
|
+
|
|
117
|
+
if main_args.save_config and not main_args.garf_config:
|
|
118
|
+
utils.ConfigSaver(main_args.save_config_dest).save(config)
|
|
119
|
+
if main_args.dry_run:
|
|
120
|
+
sys.exit()
|
|
121
|
+
|
|
122
|
+
if config.params:
|
|
123
|
+
config = utils.initialize_runtime_parameters(config)
|
|
124
|
+
logger.debug('initialized config: %s', config)
|
|
125
|
+
|
|
126
|
+
ads_client = api_clients.GoogleAdsApiClient(
|
|
127
|
+
config_dict=google_ads_config_dict,
|
|
128
|
+
version=config.api_version,
|
|
129
|
+
use_proto_plus=main_args.optimize_performance
|
|
130
|
+
not in ('PROTOBUF', 'BATCH_PROTOBUF'),
|
|
131
|
+
)
|
|
132
|
+
ads_query_executor = query_executor.AdsQueryExecutor(ads_client)
|
|
133
|
+
reader_factory = reader.ReaderFactory()
|
|
134
|
+
reader_client = reader_factory.create_reader(main_args.input)
|
|
135
|
+
|
|
136
|
+
if config.customer_ids_query:
|
|
137
|
+
customer_ids_query = config.customer_ids_query
|
|
138
|
+
elif config.customer_ids_query_file:
|
|
139
|
+
file_reader = reader_factory.create_reader('file')
|
|
140
|
+
customer_ids_query = file_reader.read(config.customer_ids_query_file)
|
|
141
|
+
else:
|
|
142
|
+
customer_ids_query = None
|
|
143
|
+
|
|
144
|
+
if main_args.disable_account_expansion:
|
|
145
|
+
logger.info(
|
|
146
|
+
'Skipping account expansion because of ' 'disable_account_expansion flag'
|
|
147
|
+
)
|
|
148
|
+
customer_ids = (
|
|
149
|
+
config.account
|
|
150
|
+
if isinstance(config.account, MutableSequence)
|
|
151
|
+
else [config.account]
|
|
152
|
+
)
|
|
153
|
+
else:
|
|
154
|
+
customer_ids = ads_query_executor.expand_mcc(
|
|
155
|
+
config.account, customer_ids_query
|
|
156
|
+
)
|
|
157
|
+
if not customer_ids:
|
|
158
|
+
logger.warning(
|
|
159
|
+
'Not a single under MCC %s is found that satisfies '
|
|
160
|
+
'the following customer_id query: "%s"',
|
|
161
|
+
config.account,
|
|
162
|
+
customer_ids_query,
|
|
163
|
+
)
|
|
164
|
+
sys.exit()
|
|
165
|
+
writer_client = writer.WriterFactory().create_writer(
|
|
166
|
+
config.output, **config.writer_params
|
|
167
|
+
)
|
|
168
|
+
if config.output == 'bq':
|
|
169
|
+
_ = writer_client.create_or_get_dataset()
|
|
170
|
+
if config.output == 'sheet':
|
|
171
|
+
writer_client.init_client()
|
|
172
|
+
|
|
173
|
+
logger.info(
|
|
174
|
+
'Total number of customer_ids is %d, accounts=[%s]',
|
|
175
|
+
len(customer_ids),
|
|
176
|
+
','.join(map(str, customer_ids)),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if main_args.parallel_queries:
|
|
180
|
+
logger.info('Running queries in parallel')
|
|
181
|
+
with futures.ThreadPoolExecutor(main_args.parallel_threshold) as executor:
|
|
182
|
+
future_to_query = {
|
|
183
|
+
executor.submit(
|
|
184
|
+
ads_query_executor.execute,
|
|
185
|
+
reader_client.read(query),
|
|
186
|
+
query,
|
|
187
|
+
customer_ids,
|
|
188
|
+
writer_client,
|
|
189
|
+
config.params,
|
|
190
|
+
main_args.optimize_performance,
|
|
191
|
+
): query
|
|
192
|
+
for query in main_args.query
|
|
193
|
+
}
|
|
194
|
+
for future in futures.as_completed(future_to_query):
|
|
195
|
+
query = future_to_query[future]
|
|
196
|
+
utils.garf_runner(query, future.result, logger)
|
|
197
|
+
else:
|
|
198
|
+
logger.info('Running queries sequentially')
|
|
199
|
+
for query in main_args.query:
|
|
200
|
+
callback = functools.partial(
|
|
201
|
+
ads_query_executor.execute,
|
|
202
|
+
reader_client.read(query),
|
|
203
|
+
query,
|
|
204
|
+
customer_ids,
|
|
205
|
+
writer_client,
|
|
206
|
+
config.params,
|
|
207
|
+
main_args.optimize_performance,
|
|
208
|
+
)
|
|
209
|
+
utils.garf_runner(query, callback, logger)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
if __name__ == '__main__':
|
|
213
|
+
main()
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Copyright 2023 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Module for defing `garf` CLI utility.
|
|
15
|
+
|
|
16
|
+
`garf-sql` allows to execute SQL queries in various Databases via SqlAlchemy.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import argparse
|
|
22
|
+
import functools
|
|
23
|
+
import sys
|
|
24
|
+
from concurrent import futures
|
|
25
|
+
|
|
26
|
+
import sqlalchemy
|
|
27
|
+
from garf_writers import reader # type: ignore
|
|
28
|
+
|
|
29
|
+
from garf_executors import sql_executor
|
|
30
|
+
from garf_executors.entrypoints import utils
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def main():
|
|
34
|
+
parser = argparse.ArgumentParser()
|
|
35
|
+
parser.add_argument('query', nargs='+')
|
|
36
|
+
parser.add_argument('-c', '--config', dest='garf_config', default=None)
|
|
37
|
+
parser.add_argument('--conn', '--connection-string', dest='connection_string')
|
|
38
|
+
parser.add_argument('--save-config', dest='save_config', action='store_true')
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
'--no-save-config', dest='save_config', action='store_false'
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
'--config-destination', dest='save_config_dest', default='config.yaml'
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument('--log', '--loglevel', dest='loglevel', default='info')
|
|
46
|
+
parser.add_argument('--logger', dest='logger', default='local')
|
|
47
|
+
parser.add_argument('--dry-run', dest='dry_run', action='store_true')
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
'--parallel-queries', dest='parallel_queries', action='store_true'
|
|
50
|
+
)
|
|
51
|
+
parser.add_argument(
|
|
52
|
+
'--no-parallel-queries', dest='parallel_queries', action='store_false'
|
|
53
|
+
)
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
'--parallel-threshold', dest='parallel_threshold', default=None, type=int
|
|
56
|
+
)
|
|
57
|
+
parser.set_defaults(save_config=False)
|
|
58
|
+
parser.set_defaults(dry_run=False)
|
|
59
|
+
parser.set_defaults(parallel_queries=True)
|
|
60
|
+
args = parser.parse_known_args()
|
|
61
|
+
main_args = args[0]
|
|
62
|
+
|
|
63
|
+
logger = utils.init_logging(
|
|
64
|
+
loglevel=main_args.loglevel.upper(), logger_type=main_args.logger
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
config = utils.ConfigBuilder('garf-sql').build(vars(main_args), args[1])
|
|
68
|
+
logger.debug('config: %s', config)
|
|
69
|
+
if main_args.save_config and not main_args.garf_config:
|
|
70
|
+
utils.ConfigSaver(main_args.save_config_dest).save(config)
|
|
71
|
+
if main_args.dry_run:
|
|
72
|
+
sys.exit()
|
|
73
|
+
|
|
74
|
+
config = utils.initialize_runtime_parameters(config)
|
|
75
|
+
logger.debug('initialized config: %s', config)
|
|
76
|
+
|
|
77
|
+
engine = sqlalchemy.create_engine(config.connection_string)
|
|
78
|
+
sqlalchemy_query_executor = sql_executor.SqlAlchemyQueryExecutor(engine)
|
|
79
|
+
|
|
80
|
+
reader_client = reader.FileReader()
|
|
81
|
+
|
|
82
|
+
if main_args.parallel_queries:
|
|
83
|
+
logger.info('Running queries in parallel')
|
|
84
|
+
with futures.ThreadPoolExecutor(
|
|
85
|
+
max_workers=main_args.parallel_threshold
|
|
86
|
+
) as executor:
|
|
87
|
+
future_to_query = {
|
|
88
|
+
executor.submit(
|
|
89
|
+
sqlalchemy_query_executor.execute,
|
|
90
|
+
query,
|
|
91
|
+
reader_client.read(query),
|
|
92
|
+
config.params,
|
|
93
|
+
): query
|
|
94
|
+
for query in sorted(main_args.query)
|
|
95
|
+
}
|
|
96
|
+
for future in futures.as_completed(future_to_query):
|
|
97
|
+
query = future_to_query[future]
|
|
98
|
+
utils.postprocessor_runner(query, future.result, logger)
|
|
99
|
+
else:
|
|
100
|
+
logger.info('Running queries sequentially')
|
|
101
|
+
for query in sorted(main_args.query):
|
|
102
|
+
callback = functools.partial(
|
|
103
|
+
executor.execute, query, reader_client.read(query), config.params
|
|
104
|
+
)
|
|
105
|
+
utils.postprocessor_runner(query, callback, logger)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == '__main__':
|
|
109
|
+
main()
|