Skip to content

Commit

Permalink
get resourceids from sbmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhoune committed Oct 23, 2023
1 parent a98d8af commit f77aca8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ cython_debug/
#.idea/
test.xes
.vscode/launch.json
.vscode/settings.json
50 changes: 48 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import json
from event_log_generator import read_events_into_df
from event_log_generator import get_db_connection
from event_log_generator import get_resource_ids
from event_log_generator import get_resource_ids_from_db
import logging
import requests

try:
import psutil
Expand Down Expand Up @@ -116,15 +117,25 @@ def send_xml_file_for_bot(botName):
end_date = request.args.get('end_date')
include_bot_messages = request.args.get('include_bot_messages') if request.args.get('include_bot_messages') is not None else False
include_life_cycle_start = request.args.get('include_life_cycle_start') if request.args.get('include_life_cycle_start') is not None else 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(db_connection, botName)
# resource_ids = get_resource_ids_from_bot_manager(bot_manager_url,botName)
resource_ids += get_resource_ids_from_db(db_connection, botName)
if len(resource_ids) == 0:
return 'No resource ids found for bot', 400
file_name = generateEventLog(db_connection,start_date, end_date, resource_ids, include_bot_messages=include_bot_messages, include_life_cycle_start=include_life_cycle_start)
return send_file(file_name, as_attachment=True), os.remove(file_name)
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
Expand All @@ -135,3 +146,38 @@ def send_xml_file_for_bot(botName):
file_handler = logging.FileHandler('app.log')
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)


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')
print("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)

return keys

except json.JSONDecodeError as e:
print("Invalid JSON format:", e)
return []


2 changes: 1 addition & 1 deletion event_log_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import pandas as pd
from event_log_generator.db_utils import read_events_into_df
from event_log_generator.db_utils import get_db_connection
from event_log_generator.db_utils import get_resource_ids
from event_log_generator.db_utils import get_resource_ids_from_db
4 changes: 2 additions & 2 deletions event_log_generator/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def read_events_into_df(db_connection,start_date = None, end_date =None, resourc
raise ValueError('db_connection must be set')
print(f'Reading events from database from {start_date} until {end_date}')
if resource_ids is None:
resource_ids = get_resource_ids(db_connection, botName)
resource_ids = get_resource_ids_from_db(db_connection, botName)
if start_date is None or end_date is None:
statement = 'SELECT EVENT_TYPE,CASE_ID,ACTIVITY_NAME, TIME_STAMP, LIFECYCLE_PHASE, RESOURCE, RESOURCE_TYPE, REMARKS FROM LAS2PEERMON.EVENTLOG WHERE CASE_ID IS NOT NULL AND RESOURCE IN %s'
df = pd.read_sql(statement, con=db_connection, params=(resource_ids,))
Expand All @@ -34,7 +34,7 @@ def get_db_connection(host,port, user, password, db = 'LAS2PEERMON'):
raise ValueError('Could not connect to database')
return db_connection

def get_resource_ids(db_connection,botName):
def get_resource_ids_from_db(db_connection,botName):
"""
This function returns the resource ids of the bot
Expand Down

0 comments on commit f77aca8

Please sign in to comment.