Skip to content

Commit

Permalink
update timeline event status constants (#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 25, 2024
1 parent 3100222 commit 56dfd4b
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Changed
- Upgrade to iRODS v4.3.3 in CI (#1815)
- Upgrade to python-irodsclient v2.1.0 (#2007)
- Upgrade minimum supported iRODS version to v4.3.3 (#1815, #2007)
- Use constants for timeline event status types (#2010)
- **Irodsinfo**
- Update REST API versioning (#1936)
- **Landingzones**
Expand Down
7 changes: 5 additions & 2 deletions landingzones/tests/test_views_taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def make_zone_taskflow(
user=user,
event_name='zone_create',
description='create landing zone',
status_type='SUBMIT',
status_type=timeline.TL_STATUS_SUBMIT,
)

flow_data = {
Expand Down Expand Up @@ -396,6 +396,7 @@ class TestZoneMoveView(

def setUp(self):
super().setUp()
self.timeline = get_backend_api('timeline_backend')
# Make project with owner in Taskflow and Django
self.project, self.owner_as = self.make_project_taskflow(
title='TestProject',
Expand Down Expand Up @@ -715,7 +716,9 @@ def test_move_lock_failure(self):
self.assertEqual(len(mail.outbox), 1) # TODO: Should this send email?
tl_event = TimelineEvent.objects.filter(event_name='zone_move').first()
self.assertIsInstance(tl_event, TimelineEvent)
self.assertEqual(tl_event.get_status().status_type, ZONE_STATUS_FAILED)
self.assertEqual(
tl_event.get_status().status_type, self.timeline.TL_STATUS_FAILED
)
# TODO: Create app alerts for async failures (see #1499)
self.assertEqual(
AppAlert.objects.filter(alert_name='zone_move').count(), 0
Expand Down
8 changes: 4 additions & 4 deletions landingzones/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def submit_create(
event_name='zone_{}'.format(tl_action),
description='{} landing zone {{{}}}{} for {{{}}} in '
'{{{}}}'.format(tl_action, 'zone', config_str, 'user', 'assay'),
status_type='SUBMIT',
status_type=timeline.TL_STATUS_SUBMIT,
extra_data=tl_extra,
)
tl_event.add_object(obj=zone, label='zone', name=zone.title)
Expand Down Expand Up @@ -265,7 +265,7 @@ def update_zone(self, zone, request=None):
user=user,
event_name='zone_update',
description=description,
status_type='OK',
status_type=timeline.TL_STATUS_OK,
extra_data=tl_extra,
)
tl_event.add_object(obj=zone, label='zone', name=zone.title)
Expand Down Expand Up @@ -312,7 +312,7 @@ def submit_delete(self, zone):
tl_event.add_object(
obj=zone.user, label='user', name=zone.user.username
)
tl_event.set_status('SUBMIT')
tl_event.set_status(timeline.TL_STATUS_SUBMIT)

# Check zone root collection status
zone_path = irods_backend.get_path(zone)
Expand Down Expand Up @@ -380,7 +380,7 @@ def _submit_validate_move(self, zone, validate_only, request=None):
label='assay',
name=zone.assay.get_display_name(),
)
tl_event.set_status('SUBMIT')
tl_event.set_status(timeline.TL_STATUS_SUBMIT)

flow_data = self.get_flow_data(
zone,
Expand Down
10 changes: 6 additions & 4 deletions samplesheets/management/commands/normalizesheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,19 @@ def handle(self, *args, **options):
investigation, description=COMMAND_DESC
)
ok_count += 1
tl_status = 'OK'
tl_desc = None
if timeline:
tl_status = timeline.TL_STATUS_OK
tl_desc = None
except Exception as ex:
logger.error(
'Error normalizing sheets in project {}: {}'.format(
project.get_log_title(), ex
)
)
err_count += 1
tl_status = 'FAILED'
tl_desc = str(ex)
if timeline:
tl_status = timeline.TL_STATUS_FAILED
tl_desc = str(ex)
if timeline and not check and edit_count > 0:
tl_event = timeline.add_event(
project=project,
Expand Down
26 changes: 15 additions & 11 deletions samplesheets/tasks_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def update_project_cache_task(
user=user,
event_name=CACHE_UPDATE_EVENT,
description='update cache for project sheets',
status_type='SUBMIT',
status_type=timeline.TL_STATUS_SUBMIT,
status_desc='Asynchronous update started',
)

Expand All @@ -68,8 +68,12 @@ def update_project_cache_task(

try:
app_plugin.update_cache(project=project, user=user)
tl_status_type = 'OK'
tl_status_desc = 'Update OK'
if tl_event:
tl_status_type = timeline.TL_STATUS_OK
tl_status_desc = 'Update OK'
tl_event.set_status(
status_type=tl_status_type, status_desc=tl_status_desc
)
app_level = 'INFO'
app_msg = 'Sample sheet iRODS cache updated'
if alert_msg:
Expand All @@ -78,8 +82,12 @@ def update_project_cache_task(
'Cache update OK for project {}'.format(project.get_log_title())
)
except Exception as ex:
tl_status_type = 'FAILED'
tl_status_desc = 'Update failed: {}'.format(ex)
if tl_event:
tl_status_type = timeline.TL_STATUS_FAILED
tl_status_desc = 'Update failed: {}'.format(ex)
tl_event.set_status(
status_type=tl_status_type, status_desc=tl_status_desc
)
app_level = 'DANGER'
app_msg = 'Sample sheet iRODS cache update failed: {}'.format(ex)
logger.error(
Expand All @@ -88,10 +96,6 @@ def update_project_cache_task(
)
)

if tl_event:
tl_event.set_status(
status_type=tl_status_type, status_desc=tl_status_desc
)
if add_alert and user:
app_alerts = get_backend_api('appalerts_backend')
if app_alerts:
Expand All @@ -116,7 +120,7 @@ def sheet_sync_task(_self):

timeline = get_backend_api('timeline_backend')
tl_add = False
tl_status_type = 'OK'
tl_status_type = timeline.TL_STATUS_OK if timeline else 'OK'
tl_status_desc = 'Sync OK'

for project in Project.objects.filter(type=PROJECT_TYPE_PROJECT):
Expand All @@ -135,7 +139,7 @@ def sheet_sync_task(_self):
fail_msg = 'Sync failed: {}'.format(ex)
logger.error(fail_msg)
tl_add = True # Add timeline event
tl_status_type = 'FAILED'
tl_status_type = timeline.TL_STATUS_FAILED if timeline else 'FAILED'
tl_status_desc = fail_msg

if timeline and tl_add:
Expand Down
9 changes: 7 additions & 2 deletions samplesheets/tests/test_views_taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def setUp(self):
self.investigation = self.import_isa_from_file(SHEET_PATH, self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.timeline = get_backend_api('timeline_backend')
self.url = reverse(
'samplesheets:delete',
kwargs={'project': self.project.sodar_uuid},
Expand Down Expand Up @@ -483,7 +484,9 @@ def test_post(self):
# Assert timeline event status
self._assert_tl_event_count(1)
tl_event = TimelineEvent.objects.get(event_name='sheet_delete')
self.assertEqual(tl_event.get_status().status_type, 'OK')
self.assertEqual(
tl_event.get_status().status_type, self.timeline.TL_STATUS_OK
)

def test_post_colls(self):
"""Test POST with collections created"""
Expand All @@ -503,7 +506,9 @@ def test_post_colls(self):
)
self._assert_tl_event_count(1)
tl_event = TimelineEvent.objects.get(event_name='sheet_delete')
self.assertEqual(tl_event.get_status().status_type, 'OK')
self.assertEqual(
tl_event.get_status().status_type, self.timeline.TL_STATUS_OK
)

def test_post_files_owner(self):
"""Test sheet deleting with files as owner"""
Expand Down
56 changes: 35 additions & 21 deletions samplesheets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ class SheetImportMixin:
#: Whether configs should be regenerated on sheet replace
replace_configs = True

#: TimelineAPI
timeline = None

def add_tl_event(self, project, action, tpl_name=None):
"""
Add timeline event for sample sheet import, replace or create.
Expand All @@ -186,8 +189,9 @@ def add_tl_event(self, project, action, tpl_name=None):
"""
if action not in ['create', 'import', 'replace']:
raise ValueError('Invalid action "{}"'.format(action))
timeline = get_backend_api('timeline_backend')
if not timeline:
if not self.timeline:
self.timeline = get_backend_api('timeline_backend')
if not self.timeline:
return None

if action == 'replace':
Expand All @@ -199,7 +203,7 @@ def add_tl_event(self, project, action, tpl_name=None):
if tpl_name:
tl_desc += ' from template "{}"'.format(tpl_name)

return timeline.add_event(
return self.timeline.add_event(
project=project,
app_name=APP_NAME,
user=self.request.user,
Expand Down Expand Up @@ -334,9 +338,11 @@ def _add_crits(legend, warnings, eh):
if ui_mode:
messages.error(self.request, ex_msg)

if tl_event:
if tl_event and self.timeline:
tl_event.set_status(
'FAILED', status_desc=ex_msg, extra_data=extra_data
self.timeline.TL_STATUS_FAILED,
status_desc=ex_msg,
extra_data=extra_data,
)

def finalize_import(
Expand All @@ -354,7 +360,7 @@ def finalize_import(
investigation.save()

# Add investigation data in Timeline
if tl_event:
if tl_event and self.timeline:
extra_data = (
{'warnings': investigation.parser_warnings}
if investigation.parser_warnings
Expand All @@ -363,7 +369,9 @@ def finalize_import(
)
status_desc = WARNING_STATUS_MSG if extra_data else None
tl_event.set_status(
'OK', status_desc=status_desc, extra_data=extra_data
self.timeline.TL_STATUS_OK,
status_desc=status_desc,
extra_data=extra_data,
)

if ui_mode:
Expand Down Expand Up @@ -666,7 +674,7 @@ def create_colls(self, investigation, request=None, sync=False):
event_name='sheet_colls_' + tl_action,
description=tl_action + ' iRODS collection structure for '
'{investigation}',
status_type='SUBMIT',
status_type=timeline.TL_STATUS_SUBMIT,
)
tl_event.add_object(
obj=investigation,
Expand Down Expand Up @@ -694,7 +702,7 @@ def create_colls(self, investigation, request=None, sync=False):
APP_NAME, 'public_access_ticket', ticket_str, project=project
)
if tl_event:
tl_event.set_status('OK')
tl_event.set_status(timeline.TL_STATUS_OK)

if settings.SHEETS_ENABLE_CACHE:
from samplesheets.tasks_celery import update_project_cache_task
Expand Down Expand Up @@ -739,7 +747,7 @@ def add_tl_event(cls, ticket, action):
event_name='irods_ticket_{}'.format(action),
description=tl_desc,
extra_data=extra_data,
status_type='OK',
status_type=timeline.TL_STATUS_OK,
)
tl_event.add_object(ticket, 'ticket', ticket.get_display_name())
tl_event.add_object(
Expand Down Expand Up @@ -813,7 +821,7 @@ def add_tl_event(self, irods_request, action):
user=irods_request.user,
event_name='irods_request_{}'.format(action),
description=action + ' iRODS data request {irods_request}',
status_type='OK',
status_type=self.timeline.TL_STATUS_OK,
extra_data=extra_data,
)
tl_event.add_object(
Expand Down Expand Up @@ -942,7 +950,7 @@ def accept_request(
status = IRODS_REQUEST_STATUS_FAILED
else:
description = 'accept iRODS data request {irods_request}'
status = 'OK'
status = timeline.TL_STATUS_OK if timeline else 'OK'
if timeline:
tl_event = timeline.add_event(
project=project,
Expand Down Expand Up @@ -1738,7 +1746,8 @@ def post(self, request, *args, **kwargs):
'Incorrect host name for confirming sheet '
'deletion: "{}"'.format(host_confirm)
)
tl_event.set_status('FAILED', msg)
if tl_event:
tl_event.set_status(timeline.TL_STATUS_FAILED, msg)
logger.error(msg + ' (correct={})'.format(actual_host))
messages.error(
request, 'Host name input incorrect, deletion cancelled.'
Expand All @@ -1748,25 +1757,30 @@ def post(self, request, *args, **kwargs):
delete_success = True
if taskflow and investigation.irods_status:
if tl_event:
tl_event.set_status('SUBMIT')
tl_event.set_status(timeline.TL_STATUS_SUBMIT)
try:
taskflow.submit(
project=project, flow_name='sheet_delete', flow_data={}
)
tl_event.set_status('OK')
if tl_event:
tl_event.set_status(timeline.TL_STATUS_OK)
except taskflow.FlowSubmitException as ex:
delete_success = False
messages.error(
self.request,
'Failed to delete sample sheets: {}'.format(ex),
)
tl_event.set_status('FAILED', status_info=str(ex))
if tl_event:
tl_event.set_status(
timeline.TL_STATUS_FAILED, status_info=str(ex)
)
else:
# Clear cached study tables (force delete)
for study in investigation.studies.all():
table_builder.clear_study_cache(study, delete=True)
investigation.delete()
tl_event.set_status('OK')
if tl_event:
tl_event.set_status(timeline.TL_STATUS_OK)

if delete_success:
# Delete ISA-Tab versions
Expand Down Expand Up @@ -2130,7 +2144,7 @@ def get_success_url(self):
user=self.request.user,
event_name='version_delete',
description='delete sample sheet version {isatab}',
status_type='OK',
status_type=timeline.TL_STATUS_OK,
)
tl_event.add_object(
obj=self.object,
Expand Down Expand Up @@ -2188,7 +2202,7 @@ def post(self, request, **kwargs):
user=self.request.user,
event_name='version_delete',
description='delete sample sheet version {isatab}',
status_type='OK',
status_type=timeline.TL_STATUS_OK,
)
tl_event.add_object(
obj=sv, label='isatab', name=sv.get_full_name()
Expand Down Expand Up @@ -2914,7 +2928,7 @@ def get(self, request, *args, **kwargs):
project = self.get_project()
timeline = get_backend_api('timeline_backend')
tl_add = False
tl_status_type = 'OK'
tl_status_type = timeline.TL_STATUS_OK if timeline else 'OK'
tl_status_desc = 'Sync OK'
sheet_sync_enable = app_settings.get(
APP_NAME, 'sheet_sync_enable', project=project
Expand All @@ -2936,7 +2950,7 @@ def get(self, request, *args, **kwargs):
request, 'Sample sheet sync skipped, no changes detected.'
)
except Exception as ex:
tl_status_type = 'FAILED'
tl_status_type = timeline.TL_STATUS_FAILED if timeline else 'FAILED'
tl_status_desc = 'Sync failed: {}'.format(ex)
messages.error(request, '{}: {}'.format(SYNC_FAIL_PREFIX, ex))
tl_add = True # Add timeline event
Expand Down
Loading

0 comments on commit 56dfd4b

Please sign in to comment.