forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_usage_analysis.py
More file actions
88 lines (62 loc) · 1.92 KB
/
explore_usage_analysis.py
File metadata and controls
88 lines (62 loc) · 1.92 KB
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
from pprint import pprint as pp
from lookerapi import LookerApi
import json
import csv
import requests
import yaml
### ------- HERE ARE PARAMETERS TO CONFIGURE -------
host = 'localhost'
### ------- OPEN THE CONFIG FILE and INSTANTIATE API -------
f = open('config.yml')
params = yaml.load(f)
f.close()
my_host = params['hosts'][host]['host']
my_secret = params['hosts'][host]['secret']
my_token = params['hosts'][host]['token']
looker = LookerApi(host=my_host,
token=my_token,
secret = my_secret)
## --------- csv writing -------
lis = {}
def write_fields(explore,value):
model,exp= explore.split(',')
# ### compile the line - this is possible to combine above, but here to keep things simple
rowout = []
rowout.append(model)
rowout.append(exp)
rowout.append(value)
w.writerow(rowout)
## --------- csv formatting -------------
csvfile= open('explore_analysis.csv', 'wb')
w = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
header = ['model',
'explore',
'count_queries']
w.writerow(header)
## -- Get all models --
models = looker.get_model("")
for model in models:
model_name = model['name']
## -- Get single model --
model_def = looker.get_model(model_name)
# pp(model_def)
## -- Get single explore --
for explore_def in model_def['explores']:
# print(model_name + " " + explore_def['name'])
explore_name = model_name+','+explore_def['name']
lis[explore_name]=0
body = {
"model":"i__looker",
"view":"history",
"fields":["query.view","query.model","history.query_run_count"],
"filters":{"history.most_recent_run_at_date":"90 days"},
# "sorts":["products.count desc 0"],
"limit":"500",
"query_timezone":"America/Los_Angeles"
}
res = looker.run_inline_query(body)
for exp in res:
explore_name = exp['query.model']+','+exp['query.view']
lis[explore_name]=exp['history.query_run_count']
for i in list(lis.keys()):
write_fields(i, lis[i])