forked from mode/mode_python_sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
97 lines (74 loc) · 2.7 KB
/
demo.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
import json, requests, datetime, ConfigParser, argparse, sys, csv, yaml, os
from requests.auth import HTTPBasicAuth
#############
# Constants #
#############
MODE_URL = 'https://modeanalytics.com'
CONFIG_FILE = 'mode.yml'
####################
# Helper Functions #
####################
def get_api_url(organisation, report_token):
return '/api/' + organisation + '/reports/' + report_token
def get_auth(whichAuth, token, password):
auth = (token, password)
return auth
def get_response_json(url, auth):
response = requests.get(url, auth=auth)
return response.json()
#################
# Main Function #
#################
def main(args):
# Read command line arguments
output_directory = args.output_dir
# Read config file
with open(CONFIG_FILE, 'r') as f:
config = yaml.load(f)
org_name = config['organisation']['name']
reports = config['organisation']['reports']
token = config['mode']['token']
password = config['mode']['password']
report_directory = os.path.join(output_directory, 'mode_reports')
if not os.path.isdir(report_directory):
os.makedirs(report_directory)
if not reports:
print("No reports listed in {}, exiting".format(
CONFIG_FILE
))
for reporttoken in reports:
# Assemble request information
auth = get_auth('mode', token, password)
api_url = get_api_url(org_name, reporttoken)
url = MODE_URL + api_url
print("Dumping queries from report @ {}".format(url))
# Retrieve and parse data
data = get_response_json(url, auth)
links = data['_links']
last_run = links['last_successful_run']
run_url = last_run['href']
url = MODE_URL + run_url + '/query_runs/'
data = get_response_json(url, auth)
embedded = data['_embedded']
query_runs = embedded['query_runs']
# Write data to output directory
query_directory = os.path.join(report_directory, reporttoken, 'queries')
if not os.path.isdir(query_directory):
os.makedirs(query_directory)
files_written = 0
for query_run in query_runs:
output_file = os.path.join(
query_directory,
'mode_query_{}.sql'.format(query_run['query_token'])
)
with open(output_file, 'w') as output:
output.write(query_run['raw_source'])
files_written += 1
print("Wrote {} files to {}".format(
files_written, query_directory
))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output-dir', default='./')
args = parser.parse_args()
main(args)