From: Patrick Nawracay Date: Fri, 8 Mar 2019 11:30:42 +0000 (+0000) Subject: mgr/dashboard: Clean up of Osd::list() X-Git-Tag: v15.0.0~179^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=f7cd7b025fab5cc4cd22d01918d0cf82e1fb9bf4;p=ceph.git mgr/dashboard: Clean up of Osd::list() Signed-off-by: Patrick Nawracay --- diff --git a/src/pybind/mgr/dashboard/controllers/osd.py b/src/pybind/mgr/dashboard/controllers/osd.py index 24de3b4193d5..b01bd4411949 100644 --- a/src/pybind/mgr/dashboard/controllers/osd.py +++ b/src/pybind/mgr/dashboard/controllers/osd.py @@ -6,6 +6,10 @@ from ..security import Scope from ..services.ceph_service import CephService, SendCommandError from ..services.exception import handle_send_command_error from ..tools import str_to_bool +try: + from typing import Dict, List, Any, Union # pylint: disable=unused-import +except ImportError: + pass # For typing only @ApiController('/osd', Scope.OSD) @@ -14,44 +18,49 @@ class Osd(RESTController): osds = self.get_osd_map() # Extending by osd stats information - for s in mgr.get('osd_stats')['osd_stats']: - osds[str(s['osd'])].update({'osd_stats': s}) + for stat in mgr.get('osd_stats')['osd_stats']: + if stat['osd'] in osds: + osds[stat['osd']]['osd_stats'] = stat # Extending by osd node information nodes = mgr.get('osd_map_tree')['nodes'] - osd_tree = [(str(o['id']), o) for o in nodes if o['id'] >= 0] - for o in osd_tree: - osds[o[0]].update({'tree': o[1]}) + for node in nodes: + if node['type'] == 'osd': + osds[node['id']]['tree'] = node # Extending by osd parent node information - hosts = [(h['name'], h) for h in nodes if h['id'] < 0] - for h in hosts: - for o_id in h[1]['children']: - if o_id >= 0: - osds[str(o_id)]['host'] = h[1] + for host in [n for n in nodes if n['type'] == 'host']: + for osd_id in host['children']: + if osd_id >= 0: + osds[osd_id]['host'] = host # Extending by osd histogram data - for o_id in osds: - o = osds[o_id] - o['stats'] = {} - o['stats_history'] = {} - osd_spec = str(o['osd']) - for s in ['osd.op_w', 'osd.op_in_bytes', 'osd.op_r', 'osd.op_out_bytes']: - prop = s.split('.')[1] - o['stats'][prop] = CephService.get_rate('osd', osd_spec, s) - o['stats_history'][prop] = CephService.get_rates('osd', osd_spec, s) + for osd_id, osd in osds.items(): + osd['stats'] = {} + osd['stats_history'] = {} + osd_spec = str(osd_id) + for stat in ['osd.op_w', 'osd.op_in_bytes', 'osd.op_r', 'osd.op_out_bytes']: + prop = stat.split('.')[1] + osd['stats'][prop] = CephService.get_rate('osd', osd_spec, stat) + osd['stats_history'][prop] = CephService.get_rates('osd', osd_spec, stat) # Gauge stats - for s in ['osd.numpg', 'osd.stat_bytes', 'osd.stat_bytes_used']: - o['stats'][s.split('.')[1]] = mgr.get_latest('osd', osd_spec, s) + for stat in ['osd.numpg', 'osd.stat_bytes', 'osd.stat_bytes_used']: + osd['stats'][stat.split('.')[1]] = mgr.get_latest('osd', osd_spec, stat) return list(osds.values()) - def get_osd_map(self): - osds = {} - for osd in mgr.get('osd_map')['osds']: + @staticmethod + def get_osd_map(svc_id=None): + # type: (Union[int, None]) -> Dict[int, Union[Dict[str, Any], Any]] + def add_id(osd): osd['id'] = osd['osd'] - osds[str(osd['id'])] = osd - return osds + return osd + resp = { + osd['osd']: add_id(osd) + for osd in mgr.get('osd_map')['osds'] + if svc_id is None or (svc_id is not None and osd['osd'] == int(svc_id)) + } + return resp if svc_id is None else resp[int(svc_id)] @handle_send_command_error('osd') def get(self, svc_id): @@ -71,7 +80,7 @@ class Osd(RESTController): raise return { - 'osd_map': self.get_osd_map()[svc_id], + 'osd_map': self.get_osd_map(svc_id), 'osd_metadata': mgr.get_metadata('osd', svc_id), 'histogram': histogram, }