]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | # -*- coding: utf-8 -*- |
2 | ''' | |
3 | Progress Mgr Module Helper | |
4 | ||
5 | This python module implements helper methods to retrieve the | |
6 | executing and completed tasks tacked by the progress mgr module | |
7 | using the same structure of dashboard tasks | |
8 | ''' | |
9 | ||
9f95a23c | 10 | |
9f95a23c | 11 | import logging |
f67539c2 | 12 | from datetime import datetime |
9f95a23c | 13 | |
9f95a23c | 14 | from .. import mgr |
f67539c2 | 15 | from . import rbd # pylint: disable=no-name-in-module |
9f95a23c TL |
16 | |
17 | logger = logging.getLogger('progress') | |
18 | ||
19 | ||
20 | def _progress_event_to_dashboard_task_common(event, task): | |
21 | if event['refs'] and isinstance(event['refs'], dict): | |
22 | refs = event['refs'] | |
23 | if refs['origin'] == "rbd_support": | |
24 | # rbd mgr module event, we can transform this event into an rbd dashboard task | |
25 | action_map = { | |
26 | 'remove': "delete", | |
27 | 'flatten': "flatten", | |
28 | 'trash remove': "trash/remove" | |
29 | } | |
30 | action = action_map.get(refs['action'], refs['action']) | |
31 | metadata = {} | |
32 | if 'image_name' in refs: | |
33 | metadata['image_spec'] = rbd.get_image_spec(refs['pool_name'], | |
34 | refs['pool_namespace'], | |
35 | refs['image_name']) | |
36 | else: | |
37 | metadata['image_id_spec'] = rbd.get_image_spec(refs['pool_name'], | |
38 | refs['pool_namespace'], | |
39 | refs['image_id']) | |
40 | task.update({ | |
41 | 'name': "rbd/{}".format(action), | |
42 | 'metadata': metadata, | |
43 | 'begin_time': "{}Z".format(datetime.fromtimestamp(event["started_at"]) | |
44 | .isoformat()), | |
45 | }) | |
46 | return | |
47 | ||
48 | task.update({ | |
49 | # we're prepending the "progress/" prefix to tag tasks that come | |
50 | # from the progress module | |
51 | 'name': "progress/{}".format(event['message']), | |
52 | 'metadata': dict(event.get('refs', {})), | |
53 | 'begin_time': "{}Z".format(datetime.fromtimestamp(event["started_at"]) | |
54 | .isoformat()), | |
55 | }) | |
56 | ||
57 | ||
58 | def _progress_event_to_dashboard_task(event, completed=False): | |
59 | task = {} | |
60 | _progress_event_to_dashboard_task_common(event, task) | |
61 | if not completed: | |
62 | task.update({ | |
63 | 'progress': int(100 * event['progress']) | |
64 | }) | |
65 | else: | |
66 | task.update({ | |
67 | 'end_time': "{}Z".format(datetime.fromtimestamp(event['finished_at']) | |
68 | .isoformat()), | |
69 | 'duration': event['finished_at'] - event['started_at'], | |
70 | 'progress': 100, | |
71 | 'success': 'failed' not in event, | |
72 | 'ret_value': None, | |
73 | 'exception': {'detail': event['failure_message']} if 'failed' in event else None | |
74 | }) | |
75 | return task | |
76 | ||
77 | ||
78 | def get_progress_tasks(): | |
79 | executing_t = [] | |
80 | finished_t = [] | |
81 | progress_events = mgr.remote('progress', "_json") | |
82 | ||
83 | for ev in progress_events['events']: | |
84 | logger.debug("event=%s", ev) | |
85 | executing_t.append(_progress_event_to_dashboard_task(ev)) | |
86 | ||
87 | for ev in progress_events['completed']: | |
88 | logger.debug("finished event=%s", ev) | |
89 | finished_t.append(_progress_event_to_dashboard_task(ev, True)) | |
90 | ||
91 | return executing_t, finished_t |