-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
427 lines (317 loc) · 16.4 KB
/
main.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from flask import Flask, render_template, request, make_response, jsonify, redirect, url_for, send_file
import base64
import json
from sapextractor import factory
import tempfile
from flask_cors import CORS
from sapextractor.database_connection import factory as database_factory
import pm4py
from pm4py.objects.dfg.filtering import dfg_filtering
import uuid
app = Flask(__name__)
CORS(app, expose_headers=["x-suggested-filename"])
def __process_parameters(parameters):
try:
parameters = json.loads(base64.b64decode(parameters))
except:
import traceback
traceback.print_exc()
parameters = {}
return parameters
def __get_log_type_from_ext_type(ext_type):
if "obj_centr" in ext_type or "ocel" in ext_type:
return 0
elif "dataframe" in ext_type or "csv" in ext_type:
return 1
elif "log" in ext_type or "xes" in ext_type:
return 2
def __prepare_event_log(parameters):
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
process = parameters["process"] if "process" in parameters else "ap_ar"
ext_type = parameters["ext_type"] if "ext_type" in parameters else "document_flow_log"
ext_arg = parameters["ext_args"] if "ext_args" in parameters else {}
log = factory.apply(db_type, db_con_args, process, ext_type, ext_arg)
return log
@app.route('/')
def empty_path():
return redirect(url_for('welcome'))
@app.route('/index.html')
def index():
return redirect(url_for('welcome'))
@app.route("/welcome")
def welcome():
response = make_response(render_template('extraction.html'))
return response
@app.route('/new_extractor.html')
def new_extractor():
response = make_response(render_template('new_extractor.html'))
return response
@app.route("/newExtractorGetTableCount")
def getTableCount():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
table = parameters["table"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.table_count import get
return {"count": get.apply(c, table)}
@app.route("/newExtractorGetMainObjectClasses")
def getMainObjectClasses():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
mandt = parameters["mandt"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.objclass_to_tables import get
return get.apply(c, mandt)
@app.route("/newExtractorGetMainTablesPerObjectClass")
def getMainTablesPerObjectClass():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
mandt = parameters["mandt"]
objectclass = parameters["objectclass"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.objclass_to_tables import convert
return {"obj_class_tables": list(convert.apply(c, objectclass, mandt))}
@app.route("/newExtractorGetPrimaryKeys")
def getPrimaryKeys():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
tabnames = parameters["tabnames"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.preprocessing_fields import get_fields
return {"primary_keys": get_fields.apply(c, tabnames)}
@app.route("/newExtractorGetPrimaryKeyValue")
def getPrimaryKeyValue():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
tabnames = parameters["tabnames"]
fname = parameters["fname"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.preprocessing_fields import get_field_values
return {"values": get_field_values.apply(c, tabnames, fname)}
@app.route("/newExtractorExpandTable")
def expandTable():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
table = parameters["table"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.table_expansion import expand
return {"expanded_tables": sorted(list(expand.expand(c, table)))}
@app.route("/newExtractorExpandTables")
def expandTables():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
tabnames = parameters["tabnames"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.table_expansion import expand
from sapextractor.utils.table_fields import extract_fields
from sapextractor.utils.dbstattora import extract_count
dbstattora = extract_count.apply_static(c)
tables = expand.expand_set(c, tabnames)
tables_count = {}
for x in tables:
extract_fields.apply_static(c, x)
tables_count[x] = dbstattora[x] if x in dbstattora else 1
edges = expand.extract_expansion_graph(c, tables)
for x in tables:
extract_fields.apply_static(c, x)
tables_count[x] = dbstattora[x] if x in dbstattora else 1
ret = {"expanded_tables": sorted(list(tables)), "types": {x: extract_fields.classify_table(c, x, tables) for x in tables}, "tables_count": tables_count, "initial_tabnames": tabnames, "edges": edges}
print(ret)
return ret
@app.route("/newExtractorPerformExtraction")
def newExtractorPerformExtraction():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
tabnames = parameters["tabnames"]
key_spec = parameters["key_spec"]
mandt = parameters["mandt"]
c = database_factory.apply(db_type, db_con_args)
from sapextractor.utils.generic_extractors import extract_table
file_name = str(uuid.uuid4())+".parquet"
df = extract_table.apply_set_tables(c, tabnames, mandt=mandt)
from pm4pymdl.objects.mdl.exporter import exporter
exporter.apply(df, file_name)
obj_types = [x for x in df.columns if not x.startswith("event_")]
return {"file_name": file_name, "obj_types": obj_types}
@app.route("/newExtractorDownloadLog")
def newExtractorDownloadLog():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
file_name = parameters["file_name"]
resp = send_file(file_name,
mimetype="text/plain", # use appropriate type based on file
as_attachment=True,
conditional=False)
resp.headers["x-suggested-filename"] = file_name
return resp
@app.route("/newExtractorDownloadSvg")
def newExtractorDownloadSvg():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
file_name = parameters["file_name"]
from pm4pymdl.objects.mdl.importer import importer as mdl_importer
df = mdl_importer.apply(file_name)
from pm4pymdl.algo.mvp.gen_framework3 import discovery as mvp_discovery
model = mvp_discovery.apply(df)
from pm4pymdl.visualization.mvp.gen_framework3 import visualizer as mvp_visualizer
gviz = mvp_visualizer.apply(model, parameters={"format": "svg"})
ser = pm4py.visualization.dfg.visualizer.serialize(gviz).decode("utf-8")
return ser
@app.route("/newExtractorCheckConnection")
def checkConnection():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
return "yes"
@app.route("/o2cClientTable")
def o2cClientTable():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.o2c import freq_client
return {"res": freq_client.apply(c)}
@app.route("/aparClientTable")
def aparClientTable():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.ap_ar import freq_doc_types
return {"res": freq_doc_types.apply(c)}
@app.route("/p2pClientTable")
def p2pClientTable():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.p2p import freq_doc_types
return {"res": freq_doc_types.apply(c)}
@app.route("/vbfaGetDfg")
def vbfaGetDfg():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.o2c import graph_retrieval_util
dfg, act_count, sa, ea = graph_retrieval_util.extract_dfg(c)
dfg, sa, ea, act_count = dfg_filtering.filter_dfg_on_paths_percentage(dfg, sa, ea, act_count, 0.2, keep_all_activities=False)
gviz = pm4py.visualization.dfg.visualizer.apply(dfg, activities_count=act_count, parameters={"format": "svg", "start_activities": sa, "end_activities": ea})
ser = pm4py.visualization.dfg.visualizer.serialize(gviz).decode("utf-8")
dfg = sorted([[x[0], x[1], y] for x, y in dfg.items()], key=lambda x: x[1], reverse=True)
act_count = sorted([(x, y) for x, y in act_count.items()], key=lambda x: x[1], reverse=True)
return jsonify({"dfg": dfg, "act_count": act_count, "ser": ser})
@app.route("/bkpfGetDfg")
def bkpfGetDfg():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.ap_ar import graph_retrieval_util
dfg, act_count, sa, ea = graph_retrieval_util.extract_dfg_apar(c)
dfg, sa, ea, act_count = dfg_filtering.filter_dfg_on_paths_percentage(dfg, sa, ea, act_count, 0.2, keep_all_activities=False)
gviz = pm4py.visualization.dfg.visualizer.apply(dfg, activities_count=act_count, parameters={"format": "svg", "start_activities": sa, "end_activities": ea})
ser = pm4py.visualization.dfg.visualizer.serialize(gviz).decode("utf-8")
dfg = sorted([[x[0], x[1], y] for x, y in dfg.items()], key=lambda x: x[1], reverse=True)
act_count = sorted([(x, y) for x, y in act_count.items()], key=lambda x: x[1], reverse=True)
return jsonify({"dfg": dfg, "act_count": act_count, "ser": ser})
@app.route("/vbfaChangeActivityUtil")
def vbfaChangeActivityUtil():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
db_type = parameters["db_type"] if "db_type" in parameters else "sqlite"
db_con_args = parameters["db_con_args"] if "db_con_args" in parameters else {"path": "sap.sqlite"}
c = database_factory.apply(db_type, db_con_args)
from sapextractor.algo.o2c import change_activities_util
changes_count = change_activities_util.extract(c)
return jsonify({"changes_count": changes_count})
@app.route("/downloadLog")
def download_event_log():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
log = __prepare_event_log(parameters)
ext_type = parameters["ext_type"] if "ext_type" in parameters else "document_flow_log"
log_type = __get_log_type_from_ext_type(ext_type)
if log_type == 0:
extension = ".jsonocel"
temp_file = tempfile.NamedTemporaryFile(suffix=extension)
temp_file.close()
from pm4pymdl.objects.ocel.exporter import exporter as ocel_exporter
ocel_exporter.apply(log, temp_file.name)
elif log_type == 1:
extension = ".csv"
temp_file = tempfile.NamedTemporaryFile(suffix=extension)
temp_file.close()
log.to_csv(temp_file.name, index=False)
elif log_type == 2:
extension = ".xes"
temp_file = tempfile.NamedTemporaryFile(suffix=extension)
temp_file.close()
from pm4py.objects.log.exporter.xes import exporter as xes_exporter
xes_exporter.apply(log, temp_file.name)
resp = send_file(temp_file.name,
mimetype="text/plain", # use appropriate type based on file
as_attachment=True,
conditional=False)
resp.headers["x-suggested-filename"] = "log" + extension
return resp
@app.route("/getProcessSvg")
def get_process_svg():
parameters = request.args.get("parameters")
parameters = __process_parameters(parameters)
log = __prepare_event_log(parameters)
ext_type = parameters["ext_type"] if "ext_type" in parameters else "document_flow_log"
log_type = __get_log_type_from_ext_type(ext_type)
if log_type == 0:
log.type = "succint"
from pm4pymdl.algo.mvp.gen_framework import algorithm as discovery
from pm4pymdl.visualization.mvp.gen_framework import visualizer as vis_factory
model = discovery.apply(log, model_type_variant="model3", node_freq_variant="type31", edge_freq_variant="type11")
gviz = vis_factory.apply(model, parameters={"format": "svg"})
elif log_type == 1 or log_type == 2:
import pandas as pd
if type(log) is pd.DataFrame:
from pm4py.objects.dfg.retrieval.pandas import get_dfg_graph
dfg = get_dfg_graph(log)
from pm4py.statistics.start_activities.pandas import get as pd_sa_get
from pm4py.statistics.end_activities.pandas import get as pd_ea_get
sa = pd_sa_get.get_start_activities(log)
ea = pd_ea_get.get_end_activities(log)
else:
dfg, sa, ea = pm4py.discover_dfg(log)
act_count = pm4py.get_attribute_values(log, "concept:name")
dfg, sa, ea, act_count = dfg_filtering.filter_dfg_on_paths_percentage(dfg, sa, ea, act_count, 0.2,
keep_all_activities=True)
gviz = pm4py.visualization.dfg.visualizer.apply(dfg, activities_count=act_count,
parameters={"format": "svg", "start_activities": sa,
"end_activities": ea})
ser = pm4py.visualization.dfg.visualizer.serialize(gviz).decode("utf-8")
return ser
def main():
app.run(host='0.0.0.0')
if __name__ == "__main__":
main()