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