summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/progress.py
blob: 96272beff7c451ce7ee2002b49cc0b204e00220e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
'''
Progress Mgr Module Helper

This python module implements helper methods to retrieve the
executing and completed tasks tacked by the progress mgr module
using the same structure of dashboard tasks
'''

from __future__ import absolute_import

import logging
from datetime import datetime

from .. import mgr
from . import rbd  # pylint: disable=no-name-in-module

logger = logging.getLogger('progress')


def _progress_event_to_dashboard_task_common(event, task):
    if event['refs'] and isinstance(event['refs'], dict):
        refs = event['refs']
        if refs['origin'] == "rbd_support":
            # rbd mgr module event, we can transform this event into an rbd dashboard task
            action_map = {
                'remove': "delete",
                'flatten': "flatten",
                'trash remove': "trash/remove"
            }
            action = action_map.get(refs['action'], refs['action'])
            metadata = {}
            if 'image_name' in refs:
                metadata['image_spec'] = rbd.get_image_spec(refs['pool_name'],
                                                            refs['pool_namespace'],
                                                            refs['image_name'])
            else:
                metadata['image_id_spec'] = rbd.get_image_spec(refs['pool_name'],
                                                               refs['pool_namespace'],
                                                               refs['image_id'])
            task.update({
                'name': "rbd/{}".format(action),
                'metadata': metadata,
                'begin_time': "{}Z".format(datetime.fromtimestamp(event["started_at"])
                                           .isoformat()),
            })
            return

    task.update({
        # we're prepending the "progress/" prefix to tag tasks that come
        # from the progress module
        'name': "progress/{}".format(event['message']),
        'metadata': dict(event.get('refs', {})),
        'begin_time': "{}Z".format(datetime.fromtimestamp(event["started_at"])
                                   .isoformat()),
    })


def _progress_event_to_dashboard_task(event, completed=False):
    task = {}
    _progress_event_to_dashboard_task_common(event, task)
    if not completed:
        task.update({
            'progress': int(100 * event['progress'])
        })
    else:
        task.update({
            'end_time': "{}Z".format(datetime.fromtimestamp(event['finished_at'])
                                     .isoformat()),
            'duration': event['finished_at'] - event['started_at'],
            'progress': 100,
            'success': 'failed' not in event,
            'ret_value': None,
            'exception': {'detail': event['failure_message']} if 'failed' in event else None
        })
    return task


def get_progress_tasks():
    executing_t = []
    finished_t = []
    progress_events = mgr.remote('progress', "_json")

    for ev in progress_events['events']:
        logger.debug("event=%s", ev)
        executing_t.append(_progress_event_to_dashboard_task(ev))

    for ev in progress_events['completed']:
        logger.debug("finished event=%s", ev)
        finished_t.append(_progress_event_to_dashboard_task(ev, True))

    return executing_t, finished_t