cr-connect-workflow/crc/scripts/get_logs_for_study.py
Dan 962d05c875 1. Created a TaskLoggingService to encapsulate what we are doing with Task Logging through BPMN Script calls and API calls from the front end.
2. Added two api endpoints that will allow us to get all the logs for a specific workflow or study.
3. Assured that requests for logs are paginated, sortable, and can be limited to a specific code if needed.
4. Assure that we only use logging levels that match the log levels of Python.
2022-01-10 13:16:54 -05:00

39 lines
1.5 KiB
Python

from crc import session
from crc.models.task_log import TaskLogModel, TaskLogModelSchema, TaskLogQuery
from crc.scripts.script import Script
from crc.services.task_logging_service import TaskLoggingService
class GetLogsByWorkflow(Script):
def get_description(self):
return """Script to retrieve logs for the current study.
Accepts an optional `code` argument that is used to filter the DB query.
Accepts an optional 'size' otherwise will return the most recent 10 records.
"""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
log_model = TaskLogModel(level='info',
code='mocked_code',
message='This is my logging message',
study_id=study_id,
workflow_id=workflow_id,
task=task.get_name())
return TaskLogModelSchema(many=True).dump([log_model])
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
code = None
size = 10
if 'code' in kwargs:
code = kwargs['code']
elif len(args) > 0:
code = args[0]
if 'size' in kwargs:
size = kwargs['size']
elif len(args) > 1:
size = args[1]
query = TaskLogQuery(code=code, per_page=size)
log_models = TaskLoggingService.get_logs_for_study(study_id, query).items
return TaskLogModelSchema(many=True).dump(log_models)