Skip to content

Commit 8c2aa37

Browse files
seunghun1eetechnowhizz
authored andcommitted
Add options to attach pre/postfix to rate value
Adds optional Horizon settings variable ``OPENSTACK_CLOUDKITTY_RATE_PREFIX`` and ``OPENSTACK_CLOUDKITTY_RATE_POSTFIX``. These allow users to set pre/postfix to rate vaules shown at the dashboard such as currency. Change-Id: Ib663d29be9cee48aec94934a170ea80554538e0d
1 parent 6b3736c commit 8c2aa37

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

cloudkittydashboard/dashboards/admin/summary/views.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from django.conf import settings
1516
from django.utils.translation import gettext_lazy as _
1617
from horizon import tables
1718

1819
from openstack_dashboard.api import keystone as api_keystone
1920

2021
from cloudkittydashboard.api import cloudkitty as api
2122
from cloudkittydashboard.dashboards.admin.summary import tables as sum_tables
23+
from cloudkittydashboard import utils
24+
25+
rate_prefix = getattr(settings,
26+
'OPENSTACK_CLOUDKITTY_RATE_PREFIX', None)
27+
rate_postfix = getattr(settings,
28+
'OPENSTACK_CLOUDKITTY_RATE_POSTFIX', None)
2229

2330

2431
class IndexView(tables.DataTableView):
@@ -39,6 +46,9 @@ def get_data(self):
3946
for tenant in summary:
4047
tenant['name'] = tenants.get(tenant.id, '-')
4148
summary[-1]['name'] = 'Cloud Total'
49+
for tenant in summary:
50+
tenant['rate'] = utils.formatRate(tenant['rate'],
51+
rate_prefix, rate_postfix)
4252
return summary
4353

4454

@@ -65,4 +75,7 @@ def get_data(self):
6575
'rate': sum([float(item['rate']) for item in summary]),
6676
})
6777
summary = api.identify(summary, key='res_type', name=True)
78+
for item in summary:
79+
item['rate'] = utils.formatRate(item['rate'],
80+
rate_prefix, rate_postfix)
6881
return summary

cloudkittydashboard/dashboards/project/rating/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
from cloudkittydashboard.api import cloudkitty as api
2323
from cloudkittydashboard.dashboards.project.rating \
2424
import tables as rating_tables
25+
from cloudkittydashboard import utils
26+
27+
rate_prefix = getattr(settings,
28+
'OPENSTACK_CLOUDKITTY_RATE_PREFIX', None)
29+
rate_postfix = getattr(settings,
30+
'OPENSTACK_CLOUDKITTY_RATE_POSTFIX', None)
2531

2632

2733
class IndexView(tables.DataTableView):
@@ -38,6 +44,9 @@ def get_data(self):
3844
total = sum([r.get('rate') for r in data])
3945

4046
data.append({'type': 'TOTAL', 'rate': total})
47+
for item in data:
48+
item['rate'] = utils.formatRate(item['rate'],
49+
rate_prefix, rate_postfix)
4150
return data
4251

4352

cloudkittydashboard/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ def __getattr__(self, key):
2525

2626
def __setattr__(self, key, val):
2727
self[key] = val
28+
29+
30+
def formatRate(rate: float, prefix: str, postfix: str) -> str:
31+
rate = str(rate)
32+
if prefix:
33+
rate = prefix + rate
34+
if postfix:
35+
rate = rate + postfix
36+
return rate

doc/source/installation.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,37 @@ For more detailed information about CloudKitty installation check out the
4040

4141

4242
.. _installation section: https://cloudkitty.readthedocs.org/en/latest/installation.html
43+
44+
45+
Configuration
46+
=============
47+
48+
To configure CloudKitty dashboard, add variables to your Horizon settings
49+
file.
50+
For more details about how to add variables to Horizon settings checkout the
51+
`Horizon Settings Reference documentation`_.
52+
53+
54+
.. _Horizon Settings Reference documentation: https://docs.openstack.org/horizon/latest/configuration/settings.html
55+
56+
Rate Pre/Postfix
57+
----------------
58+
59+
You can configure pre/postfix to rate vaules shown at the dashboard.
60+
61+
Here's example of setting rate currency to US Dollar.
62+
63+
.. code-block:: python
64+
65+
# You can choose to have prefix or postfix or both.
66+
# Prefix and postfix are not mutally exclusive.
67+
OPENSTACK_CLOUDKITTY_RATE_PREFIX = '$'
68+
OPENSTACK_CLOUDKITTY_RATE_POSTFIX = 'USD'
69+
70+
Some symbols (Such as Non-ASCII) might require to use unicode value directly.
71+
72+
.. code-block:: python
73+
74+
# British Pound
75+
OPENSTACK_CLOUDKITTY_RATE_PREFIX = u'\xA3'
76+
OPENSTACK_CLOUDKITTY_RATE_POSTFIX = 'GBP'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
features:
3+
- |
4+
Adds optional Horizon settings variable OPENSTACK_CLOUDKITTY_RATE_PREFIX
5+
and OPENSTACK_CLOUDKITTY_RATE_POSTFIX. These allow users to attach
6+
pre/postfix to their rate vaules shown at the dashboard such as currency.
7+
These values can be set in ``.py`` settings snippets under
8+
``openstack_dashboard/local/local_settings.d`` directory. Follow
9+
https://docs.openstack.org/horizon/latest/configuration/settings.html
10+
for more details.

0 commit comments

Comments
 (0)