-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
265 lines (228 loc) · 9.54 KB
/
app.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
from dotenv import load_dotenv
from flask import Flask, request, send_file
from sqlalchemy import create_engine
import pandas as pd
import os
import json
from event_log_generator.event_reader import get_db_connection
from event_log_generator.event_reader import generate_eventlog
import logging
import requests
from flask_apscheduler import APScheduler
import shutil
from tasks import clean_directory
try:
import psutil
parent_pid = os.getpid()
parent_name = str(psutil.Process(parent_pid).name())
except psutil.NoSuchProcess:
print("No such process")
parent_name = "unknown"
import pm4py
# current directory
current_dir = os.path.dirname(os.path.realpath(__file__))
# load environment variables
load_dotenv(dotenv_path=current_dir+'/.env')
mysql_user = os.environ['MYSQL_USER']
mysql_password = os.environ['MYSQL_PASSWORD']
mysql_host = os.environ['MYSQL_HOST']
mysql_db = os.environ['MYSQL_DB']
mysql_port = os.environ['MYSQL_PORT']
cleanup_interval = os.environ.get('CLEANUP_INTERVAL', 60)
db_connection = get_db_connection(
mysql_host, mysql_port, mysql_user, mysql_password, mysql_db)
# port
port = os.environ['PORT']
# set port to 8086 if not set
if port is None:
port = 8087
class Config(object):
JOBS = [
{
'id': 'cleaning_job',
'func': 'tasks:clean_directory',
'args': (os.path.join(current_dir, 'event_logs'),),
'trigger': 'interval',
'seconds': int(cleanup_interval)
}
]
SCHEDULER_API_ENABLED = True
app = Flask(__name__)
app.config.from_object(Config())
# Define a logger
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.DEBUG, format=log_format, filename='app.log')
logger = logging.getLogger(__name__)
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
@app.route('/resource/<resource_id>', methods=['GET'])
def send_xml_file_for_resource(resource_id):
logger.info('Request received for resource ' + resource_id)
start_date = request.args.get('start_date', None)
end_date = request.args.get('end_date', None)
include_bot_messages = request.args.get('include_bot_messages', False)
include_life_cycle_start = request.args.get(
'include_life_cycle_start', False)
use_cache = request.args.get('use_cache', False)
file_name = get_filename(start_date, end_date, [resource_id],
include_bot_messages, include_life_cycle_start)
if use_cache is not None and os.path.exists(os.path.join(current_dir, 'event_logs', file_name)):
return send_file(os.path.join(current_dir, 'event_logs', file_name), as_attachment=True)
else:
try:
filepath = generateXESfile(
db_connection, start_date, end_date, [resource_id], include_bot_messages, include_life_cycle_start)
if file_name is None:
return 'No events found for resource', 204
return send_file(filepath, as_attachment=True)
except ValueError as e:
return str(e), 400
except Exception as e:
return str(e), 500
@app.route('/resources', methods=['POST'])
def send_xml_files_for_resources():
body = request.get_json()
resource_ids = body.get('resource_ids', [])
start_date = request.args.get('start_date', None)
end_date = request.args.get('end_date', None)
include_bot_messages = request.args.get('include_bot_messages', False)
include_life_cycle_start = request.args.get(
'include_life_cycle_start', False)
use_cache = request.args.get('use_cache', False)
file_name = get_filename(start_date, end_date, resource_ids,
include_bot_messages, include_life_cycle_start)
if use_cache is not None and os.path.exists(os.path.join(current_dir, 'event_logs', file_name)):
return send_file(os.path.join(current_dir, 'event_logs', file_name), as_attachment=True)
else:
try:
file_path = generateXESfile(
db_connection, start_date, end_date, resource_ids, include_bot_messages, include_life_cycle_start)
if file_name is None:
return 'No events found for resource', 204
return send_file(file_path, as_attachment=True)
except ValueError as e:
return str(e), 400
except Exception as e:
return str(e), 500
@app.route('/bot/<botName>', methods=['GET'])
def send_xml_file_for_bot(botName):
logger.info('Request received for bot ' + botName)
if request.method == 'GET':
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
include_bot_messages = request.args.get('include_bot_messages', False)
include_life_cycle_start = request.args.get(
'include_life_cycle_start', False)
use_cache = request.args.get('use_cache', False)
if 'bot-manager-url' not in request.args:
return {
"error": "bot-manager-url parameter is missing"
}, 400
bot_manager_url = request.args.get('bot-manager-url')
try:
resource_ids = get_resource_ids_from_bot_manager(
bot_manager_url, botName)
if len(resource_ids) == 0:
return 'No resource ids found for bot', 500
file_name = get_filename(start_date, end_date, resource_ids,
include_bot_messages, include_life_cycle_start)
if use_cache and os.path.exists(os.path.join(current_dir, file_name)):
return send_file(file_name, as_attachment=True)
else:
filepath = generateXESfile(db_connection, start_date, end_date, resource_ids,
include_bot_messages=include_bot_messages, include_life_cycle_start=include_life_cycle_start)
if filepath is None:
return 'No events found for resource', 204
return send_file(filepath, as_attachment=True)
except ValueError as e:
print(e)
logger.error(e)
return str(e), 400
except Exception as e:
print(e)
logger.error(e)
return str(e), 500
else:
return 'Method not allowed', 405
if __name__ == '__main__':
file_handler = logging.FileHandler('app.log')
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)
app.run(port=port, debug=True)
def generateXESfile(db_connection, start_date=None, end_date=None, resource_ids=None, include_bot_messages=False, include_life_cycle_start=False,deserialize_remarks=False):
"""
This function generates an XES file from the events in the database
Parameters
----------
db_connection : sqlalchemy.engine.base.Connection
Connection to the database
start_date : string
Start date of the events
end_date : string
End date of the events
resource_ids : list
List of resource ids
include_bot_messages : bool
Include bot message events in the event log
include_life_cycle_start : bool
Include life cycle start events in the event log
Returns
-------
file_path : string
Path to the generated XES file
"""
logger.info('Reading events from database')
if not os.path.exists(os.path.join(current_dir, 'event_logs')):
os.makedirs(os.path.join(current_dir, 'event_logs'))
event_log = generate_eventlog(db_connection, start_date, end_date, resource_ids,
include_bot_messages=include_bot_messages, include_life_cycle_start=include_life_cycle_start,deserialize_remarks=deserialize_remarks)
if event_log is None or event_log.empty:
logger.info('No events found for resource ids: '+str(resource_ids))
return None
logger.info('Events read from database')
file_name = get_filename(start_date, end_date, resource_ids,
include_bot_messages, include_life_cycle_start)
pm4py.write_xes(event_log, 'event_logs/'+file_name,
case_id_key='case:concept:name')
return os.path.join(current_dir, 'event_logs', file_name)
def get_filename(start_date=None, end_date=None, resource_ids=None, include_bot_messages=False, include_life_cycle_start=False):
filename = f"{''.join(resource_ids)}"
if start_date is not None:
filename += f"-{start_date}"
if end_date is not None:
filename += f"_{end_date}"
if include_bot_messages:
filename += "_bot_messages"
if include_life_cycle_start:
filename += "_life_cycle_start"
return filename + '.xes'
def get_resource_ids_from_bot_manager(bot_manager_url, botName):
"""
This function returns the resource ids of the bot
Parameters
----------
bot_manager_url : string
URL of the bot manager
Returns
-------
resource_ids : list
List of resource ids
"""
if bot_manager_url is None:
raise ValueError('bot_manager_url must be set')
logger.info(
f"Getting resource ids from bot manager {bot_manager_url}/bots")
response = requests.get(bot_manager_url + '/bots')
try:
data = response.json()
keys = []
for key, value in data.items():
if isinstance(value, dict) and "name" in value and value["name"] == botName:
keys.append(key)
if len(keys) == 0:
logger.error(f"No resource ids found for bot {botName}")
return keys
except json.JSONDecodeError as e:
print("Invalid JSON format:", e)
return []