forked from openstack/cloudkitty-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
81 lines (68 loc) · 3.14 KB
/
views.py
File metadata and controls
81 lines (68 loc) · 3.14 KB
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
# Copyright 2018 Objectif Libre
#
# 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 the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from horizon import tables
from openstack_dashboard.api import keystone as api_keystone
from cloudkittydashboard.api import cloudkitty as api
from cloudkittydashboard.dashboards.admin.summary import tables as sum_tables
from cloudkittydashboard import utils
rate_prefix = getattr(settings,
'OPENSTACK_CLOUDKITTY_RATE_PREFIX', None)
rate_postfix = getattr(settings,
'OPENSTACK_CLOUDKITTY_RATE_POSTFIX', None)
class IndexView(tables.DataTableView):
template_name = 'admin/rating_summary/index.html'
table_class = sum_tables.SummaryTable
def get_data(self):
summary = api.cloudkittyclient(self.request).report.get_summary(
groupby=['tenant_id'], all_tenants=True)['summary']
tenants, _ = api_keystone.tenant_list(self.request)
tenants = {tenant.id: tenant.name for tenant in tenants}
summary.append({
'tenant_id': 'ALL',
'rate': sum([float(item['rate']) for item in summary]),
})
summary = api.identify(summary, key='tenant_id')
for tenant in summary:
tenant['name'] = tenants.get(tenant.id, '-')
summary[-1]['name'] = 'Cloud Total'
for tenant in summary:
tenant['rate'] = utils.formatRate(tenant['rate'],
rate_prefix, rate_postfix)
return summary
class TenantDetailsView(tables.DataTableView):
template_name = 'admin/rating_summary/details.html'
table_class = sum_tables.TenantSummaryTable
page_title = _("Script details: {{ table.project_id }}")
def _get_cloud_total_summary(self):
return api.cloudkittyclient(self.request).report.get_summary(
groupby=['res_type'], all_tenants=True)['summary']
def get_data(self):
tenant_id = self.kwargs['project_id']
if tenant_id == 'ALL':
summary = self._get_cloud_total_summary()
else:
summary = api.cloudkittyclient(self.request).report.get_summary(
groupby=['res_type'], tenant_id=tenant_id)['summary']
summary.append({
'tenant_id': tenant_id,
'res_type': 'TOTAL',
'rate': sum([float(item['rate']) for item in summary]),
})
summary = api.identify(summary, key='res_type', name=True)
for item in summary:
item['rate'] = utils.formatRate(item['rate'],
rate_prefix, rate_postfix)
return summary