From d48e095b0e4eee244d177898ce40d9d6302c487a Mon Sep 17 00:00:00 2001 From: Evgeniy L Date: Thu, 24 Dec 2015 14:55:19 +0300 Subject: [PATCH] Add formatters, which is required for easier debugging --- bareon_dynamic_allocator/allocators.py | 2 +- bareon_dynamic_allocator/cmd.py | 6 +- bareon_dynamic_allocator/viewer.py | 86 ++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 bareon_dynamic_allocator/viewer.py diff --git a/bareon_dynamic_allocator/allocators.py b/bareon_dynamic_allocator/allocators.py index 07a500b..338e442 100644 --- a/bareon_dynamic_allocator/allocators.py +++ b/bareon_dynamic_allocator/allocators.py @@ -129,7 +129,7 @@ class DynamicAllocationLinearProgram(object): spaces_grouped_by_disk = list(grouper(solution.x, len(self.spaces))) for disk_i in range(len(self.disks)): disk_id = self.disks[disk_i].id - disk = {'disk_id': disk_id, 'spaces': []} + disk = {'disk_id': disk_id, 'size': self.disks[disk_i].size, 'spaces': []} spaces_for_disk = spaces_grouped_by_disk[disk_i] for space_i, space_size in enumerate(spaces_for_disk): diff --git a/bareon_dynamic_allocator/cmd.py b/bareon_dynamic_allocator/cmd.py index 3d4f6b1..21f7095 100644 --- a/bareon_dynamic_allocator/cmd.py +++ b/bareon_dynamic_allocator/cmd.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from pprint import pprint - import sys from oslo_config import cfg @@ -21,6 +19,7 @@ from oslo_log import log from bareon_dynamic_allocator import utils from bareon_dynamic_allocator.allocators import DynamicAllocator +from bareon_dynamic_allocator import viewer cli_opts = [ @@ -75,7 +74,8 @@ def parse_configs(conf): def save_result(data, output_file): - pprint(data) + viewer.StdoutViewer(data).show_me() + viewer.SVGViewer(data).show_me() def validate_schema(schema): diff --git a/bareon_dynamic_allocator/viewer.py b/bareon_dynamic_allocator/viewer.py new file mode 100644 index 0000000..0a8f0aa --- /dev/null +++ b/bareon_dynamic_allocator/viewer.py @@ -0,0 +1,86 @@ +# Copyright 2015 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See then +# License for the specific language governing permissions and limitations +# under the License. + +from pprint import pprint +import svgwrite +from svgwrite import cm, mm + + +class StdoutViewer(object): + + def __init__(self, disks_spaces_mapping): + self.disks_spaces_mapping = disks_spaces_mapping + + def show_me(self): + pprint(self.disks_spaces_mapping) + + +class SVGViewer(object): + + PALETTE = ['#666547', '#fb2e01', '#6fcb9f', '#ffe28a', '#fffeb3'] + DISK_HEIGHT = 3 * cm + SPACE_HEIGHT = 3 * cm + WIDTH_MULTIPLIER = 10 + DISKS_INTERVAL = 150 + SPACES_X_INTERVAL = 2 + STYLE = "fill:{color};stroke:black;stroke-width:5;" + + def __init__(self, disks_spaces_mapping, file_path='/tmp/bareon.svg'): + self.disks_spaces_mapping = disks_spaces_mapping + self.dwg = svgwrite.Drawing(filename=file_path, debug=True) + + def show_me(self): + self._add_disk_with_spaces() + self.dwg.save() + + def _add_disk_with_spaces(self): + disk_g = self.dwg.add(self.dwg.g(id='disks-group', transform="translate({0}, {1})".format(30, 30))) + + for disk_idx, disk_w_spaces in enumerate(self.disks_spaces_mapping): + disk_id = disk_w_spaces['disk_id'] + size = disk_w_spaces['size'] + + disk = disk_g.add(self.dwg.g(id=disk_id, transform="translate(0, {0})".format(disk_idx * self.DISKS_INTERVAL))) + disk.add(self.dwg.text(text='{0} size={1}'.format(disk_id, size), fill="black")) + + disk_rect = disk.add(self.dwg.g(transform="translate({0}, {1})".format(0, 10), id='in-{0}'.format(disk_id))) + disk_rect.add(self.dwg.rect( + style=self.STYLE.format(color='#f5f5f5'), + ry=5, + rx=5, + size=(self.WIDTH_MULTIPLIER * disk_w_spaces['size'], + self.DISK_HEIGHT))) + + last_insert = [0, 0] + for space_idx, space in enumerate(disk_w_spaces['spaces']): + palette = self.PALETTE[space_idx % len(self.PALETTE)] + disk_rect.add(self.dwg.rect( + style=self.STYLE.format(color=palette), + ry=5, + rx=5, + id=space['space_id'], + insert=last_insert, + size=(self.WIDTH_MULTIPLIER * space['size'], self.SPACE_HEIGHT))) + + last_insert[0] += self.WIDTH_MULTIPLIER * space['size'] + + spaces_lines = ['{0} size={1}'.format(space['space_id'], space['size']) for space in disk_w_spaces['spaces']] + + last_insert[0] = self.WIDTH_MULTIPLIER * disk_w_spaces['size'] + last_insert[0] += 10 + last_insert[1] += 20 + for space_idx, space_line in enumerate(spaces_lines): + palette = self.PALETTE[space_idx % len(self.PALETTE)] + disk_rect.add(self.dwg.text(insert=last_insert, text=space_line, fill=palette)) + last_insert[1] += 20 diff --git a/requirements.txt b/requirements.txt index 8628ec5..6cc5b3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ PyYAML==3.10 six>=1.5.2 scipy numpy +svgwrite