-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLumberSnake (HyperAPI).py
337 lines (265 loc) · 12.4 KB
/
LumberSnake (HyperAPI).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
import os
import os.path
import shutil
import zipfile
import time
from tkinter import Tk
from tkinter.filedialog import askopenfilename, askdirectory
from tableauhyperapi import HyperProcess, Telemetry, \
Connection, CreateMode, escape_string_literal
from pathlib import Path
#############################
# UNZIP FILES (VIZQL / HTTP)#
#############################
def ExtractLogs(OutputFilepath, ZipLogsFile):
print('Extracting VizQL Logs From ' + ZipLogsFile + "...")
zf = zipfile.ZipFile(ZipLogsFile, 'r')
try:
for info in zf.infolist():
if "/nativeapi_vizqlserver" in info.filename:
print(info.filename)
if info.filename[-1] == '/':
continue
info.filename = os.path.basename(info.filename)
zf.extract(info, OutputFilepath+"vizql\\")
elif "/access" in info.filename:
print(info.filename)
if info.filename[-1] == '/':
continue
info.filename = os.path.basename(info.filename)
zf.extract(info, OutputFilepath+"http\\")
except Exception as e:
print (e)
pass
##############################
# Clean any files that clash #
##############################
def cleanFilepath(directory):
if os.path.exists(directory):
print("WARNING - the 'Log Dump' folder exists. Press 'Y' to delete.")
check = input("Okay to delete? Press 'Y' to confirm | Any other key will exit. >> ")
if check.lower() == "y":
shutil.rmtree(directory)
print("Deleted continuing...")
else:
print("Exiting...")
time.sleep(5)
quit()
if os.path.exists(hyperfile):
print("WARNING - an existing LumberSnake.hyper file exists.")
check = input("How would you like to proceed?\n >> Press 'Y' to delete. \n >> Press 'A' to append \n >> Press any other key to exit.\n >> ")
if check.lower() == "y":
try:
os.remove(hyperfile)
except Exception as e:
print(e)
pass
print("Deleted continuing...")
elif check.lower() == "a":
print("Will append to file. Warning, data may be duplicated.")
pass
else:
print("Exiting...")
time.sleep(5)
quit()
##############################
# Create the Hyper DB #
##############################
def HyperCreate():
print(">>> Creating Hyper File <<<")
path_to_database = Path(hyperfile)
with HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper:
with Connection(endpoint=hyper.endpoint,database=path_to_database,create_mode=CreateMode.CREATE_IF_NOT_EXISTS) as connection:
affected_rows = connection.execute_command(command=
f'''create table if not exists http (
serving_host text,
client_host text,
username text,
ts text,
timezone text,
port text,
request_body text,
xforward_for text,
status_code text,
response_size text,
content_length text,
request_time_ms text,
request_id text
);''')
connection.execute_command(command=
f'''create table if not exists dump_table (
dump text
);''')
print(affected_rows)
##############################
# Convert VizQL files to Hyper
# Dumps files in to Hyper
# Validates JSON lines
# Flattens JSON in Hyper + builds structure
# Iterates across files
##############################
def HyperSnake(vizqlfile):
path_to_database = Path(hyperfile)
print(">>> Ingesting " + vizqlfile)
with HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper:
with Connection(endpoint=hyper.endpoint,database=path_to_database) as connection:
affected_rows = connection.execute_command(command=
f'''
CREATE TABLE IF NOT EXISTS dump_table AS (
SELECT * from {escape_string_literal(vizqlfile)}
(SCHEMA(dump json) WITH (FORMAT JSON)));''')
print('>>> Ingested string literals to dump_table or tracebacks: ',affected_rows)
affected_rows = connection.execute_command(command=
f'''
CREATE TABLE IF NOT EXISTS raw_log AS (
SELECT
CAST(dump AS json OR NULL) AS log_entry
FROM dump_table
);''')
print('>>> Ingested raw_log lines or tracebacks: ',affected_rows)
print(">>> Logs ingested!")
print(">>> Cleaning Hyper...")
affected_rows = connection.execute_command(command=
f'''
TRUNCATE TABLE dump_table;
''')
print('>>> Truncated rows in dump_table or tracebacks: ',affected_rows)
affected_rows = connection.execute_command(command=
f'''
DROP TABLE dump_table;
''')
print('>>> Dumped rows in dump_table or tracebacks: ',affected_rows)
affected_rows = connection.execute_command(command=
f'''
DELETE
FROM raw_log
WHERE
log_entry IS NULL
;''')
print('>>> Deleted lines in raw_log or tracebacks: ',affected_rows)
print(">>> Converting structure now...")
affected_rows = connection.execute_command(
command=
f'''
CREATE TABLE IF NOT EXISTS qplog AS (
SELECT
(log_entry->>'ts')::TIMESTAMP AS ts,
(log_entry->>'pid') AS pid,
(log_entry->>'tid') AS tid,
(log_entry->>'req') AS req,
(log_entry->>'sev') AS sev,
(log_entry->>'sess') AS sess,
(log_entry->>'site') AS site,
(log_entry->>'user') AS user,
(log_entry->'v'->>'elapsed')::DOUBLE PRECISION AS elapsed,
(log_entry->'v'->>'elapsed-sum')::DOUBLE PRECISION AS elapsed_sum,
(log_entry->'v'->>'job-count')::INT AS job_count,
(log_entry->'v'->>'query-errors') AS query_errors,
(job_entry->>'elapsed')::DOUBLE PRECISION AS elapsed_jobs,
(job_entry->>'fusion-parent') AS fusion_parent,
(job_entry->>'owner-component') AS owner_component,
(job_entry->>'owner-dashboard') AS owner_dashboard,
(job_entry->>'owner-worksheet') AS owner_worksheet,
(job_entry->>'query-abstract') AS query_abstract,
(job_entry->>'query-id') AS query_id,
(queries_entry->>'cache-hit') AS cache_hit,
(queries_entry->>'native-query-elapsed')::DOUBLE PRECISION AS native_query_elapsed,
(queries_entry->>'protocol-id') AS protocol_id,
(queries_entry->>'query-category') AS query_category,
(queries_entry->>'query-compiled') AS query_compiled
FROM raw_log
CROSS JOIN json_array_elements(log_entry->'v'->'jobs') as e1(job_entry)
CROSS JOIN json_array_elements(job_entry->'queries') as e2(queries_entry)
WHERE
log_entry->>'k' = 'qp-batch-summary'
);''')
print('>>> Converted log entries or tracebacks: ',affected_rows)
affected_rows = connection.execute_command(command=f'''
CREATE TABLE IF NOT EXISTS excplog AS (
SELECT
(log_entry->>'ts')::TIMESTAMP AS ts,
(log_entry->>'pid') AS pid,
(log_entry->>'tid') AS tid,
(log_entry->>'req') AS req,
(log_entry->>'sev') AS sev,
(log_entry->>'sess') AS sess,
(log_entry->>'site') AS site,
(log_entry->>'user') AS username,
(log_entry->'v'->>'excp-msg') AS excp_msg,
(log_entry->'v'->>'excp-type') AS excp_type,
(log_entry->'v'->>'msg') AS msg
FROM raw_log
WHERE log_entry->>'k' = 'excp'
);''')
print('>>> Converted exceptions or tracebacks: ',affected_rows)
print(">>> Dropping the dump table...")
connection.execute_command(
command=f"DROP TABLE raw_log;")
print(">>> Hyper step complete...")
##############################
# Access to Hyper
# Imports the 'Access' file directly in to Hyper
##############################
def HTTPtoHyper(accessfile):
print (">>> Importing Access File " + accessfile + " to Hyper...")
path_to_database = Path(hyperfile)
with HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper:
with Connection(endpoint=hyper.endpoint,
database=path_to_database) as connection:
connection.execute_command(
command=f"COPY http from {escape_string_literal(accessfile)} with "
f"(format CSV, delimiter ' ', NULL '-', QUOTE '\"', ESCAPE '\\')")
print("HTTP file imported in to Hyper...")
##############################
start = time.time()
if __name__ == '__main__':
hyperfile = "LumberSnake.hyper"
directory = '.\\Log Dump\\'
print("Cleaning current directories and files.")
cleanFilepath(directory)
# Select if you want to extract from zip file or point at directory.
print(">>>> DO YOU WANT TO EXTRACT FROM ZIP - OR DIRECTLY FROM A FOLDER? <<<<")
program = input("Select from the following options: \n >> Z for zip file. \n >> F for folder directory. \n >> Any other key to quit. \n >>")
if program.lower() == "z":
print("Select your zip logs.")
Tk().withdraw()
ZipLogsFile = askopenfilename(initialdir="./", title="Select zip logs")
ExtractLogs(directory, ZipLogsFile)
print ("Logs Extracted from Zip!")
elif program.lower() == "f":
print("Pick your log folder. For example: C:/ProgramData/Tableau/Tableau Server/data/tabsvc/logs/")
Tk().withdraw()
if os.path.exists('C:/ProgramData/Tableau/Tableau Server/data/'):
directory = askdirectory(initialdir="C:/ProgramData/Tableau/Tableau Server/data/", title="Select log folder")
print (directory + " selected.")
else:
directory = askdirectory(initialdir="./", title="Select log folder")
print(directory + " selected.")
else:
print("Exiting...")
time.sleep(5)
quit()
HyperCreate()
print(">>> Begin Access File Import <<<")
for dirpath, dirnames, filenames in os.walk(directory):
for filename in [f for f in filenames if f.startswith("access")]:
try:
accessfile = os.path.join(dirpath, filename)
HTTPtoHyper(accessfile)
except Exception as e:
print (e)
pass
print(">>> Begin VizQL File Import <<<")
for dirpath, dirnames, filenames in os.walk(directory):
for filename in [f for f in filenames if f.startswith("nativeapi_vizql")]:
try:
vizqlfile = os.path.join(dirpath, filename)
HyperSnake(vizqlfile)
except Exception as e:
print (e)
pass
if program.lower() == "z":
shutil.rmtree(directory)
end = time.time()
print ("Processed in " + str((end - start)/60)[:6] + "mins.")
time.sleep(5)