# Path: cloudkittydashboard/api/cloudkitty.py
# def cloudkittyclient(request):
# def identify(what, name=False, key=None):
# 
# Path: cloudkittydashboard/dashboards/admin/summary/tables.py
# def get_details_link(datum):
# class SummaryTable(tables.DataTable):
#     class Meta(object):
# class TenantSummaryTable(tables.DataTable):
#     class Meta(object):


from django.utils.translation import ugettext_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
#    License for the specific language governing permissions and limitations
#    under the License.





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'
        return summary


class TenantDetailsView(tables.DataTableView):
    template_name = 'admin/rating_summary/details.html'

# Path: cloudkittydashboard/api/cloudkitty.py
# def cloudkittyclient(request):
# def identify(what, name=False, key=None):
# 
# Path: cloudkittydashboard/dashboards/admin/summary/tables.py
# def get_details_link(datum):
# class SummaryTable(tables.DataTable):
#     class Meta(object):
# class TenantSummaryTable(tables.DataTable):
#     class Meta(object):


# Path: cloudkittydashboard/dashboards/admin/summary/views.py
from django.utils.translation import ugettext_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

#    License for the specific language governing permissions and limitations
#    under the License.





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'
        return summary


class TenantDetailsView(tables.DataTableView):
    template_name = 'admin/rating_summary/details.html'

    table_class = sum_tables.TenantSummaryTable