Merge pull request #78 from cloudbuilders/usage_csv
Ability to download usage reports in csv format
This commit is contained in:
commit
d2876ad516
@ -195,13 +195,24 @@ def usage(request, tenant_id=None):
|
||||
if show_terminated:
|
||||
instances += terminated_instances
|
||||
|
||||
return shortcuts.render_to_response('dash_usage.html', {
|
||||
if request.GET.get('format', 'html') == 'csv':
|
||||
template_name = 'dash_usage.csv'
|
||||
mimetype = "text/csv"
|
||||
else:
|
||||
template_name = 'dash_usage.html'
|
||||
mimetype = "text/html"
|
||||
|
||||
return shortcuts.render_to_response(template_name, {
|
||||
'usage': usage,
|
||||
'ram_unit': ram_unit,
|
||||
'total_ram': total_ram,
|
||||
# there are no date selection caps yet so keeping csv_link simple
|
||||
'csv_link': '?format=csv',
|
||||
'show_terminated': show_terminated,
|
||||
'datetime_start': datetime_start,
|
||||
'datetime_end': datetime_end,
|
||||
'instances': instances
|
||||
}, context_instance=template.RequestContext(request))
|
||||
}, context_instance = template.RequestContext(request), mimetype=mimetype)
|
||||
|
||||
|
||||
@login_required
|
||||
|
@ -71,6 +71,10 @@ def _get_start_and_end_date(request):
|
||||
return (date_start, date_end, datetime_start, datetime_end)
|
||||
|
||||
|
||||
def _csv_usage_link(date_start):
|
||||
return "?date_month=%s&date_year=%s&format=csv" % (date_start.month, date_start.year)
|
||||
|
||||
|
||||
@login_required
|
||||
def usage(request):
|
||||
(date_start, date_end, datetime_start, datetime_end) = _get_start_and_end_date(request)
|
||||
@ -91,13 +95,21 @@ def usage(request):
|
||||
global_summary.human_readable('disk_size')
|
||||
global_summary.human_readable('ram_size')
|
||||
|
||||
if request.GET.get('format', 'html') == 'csv':
|
||||
template_name = 'syspanel_usage.csv'
|
||||
mimetype = "text/csv"
|
||||
else:
|
||||
template_name = 'syspanel_usage.html'
|
||||
mimetype = "text/html"
|
||||
|
||||
return render_to_response(
|
||||
'syspanel_usage.html',{
|
||||
template_name, {
|
||||
'dateform': dateform,
|
||||
'usage_list': global_summary.usage_list,
|
||||
'csv_link': _csv_usage_link(date_start),
|
||||
'global_summary': global_summary.summary,
|
||||
'external_links': settings.EXTERNAL_MONITORING,
|
||||
}, context_instance = template.RequestContext(request))
|
||||
}, context_instance = template.RequestContext(request), mimetype=mimetype)
|
||||
|
||||
|
||||
@login_required
|
||||
@ -134,12 +146,20 @@ def tenant_usage(request, tenant_id):
|
||||
else:
|
||||
running_instances.append(i)
|
||||
|
||||
return render_to_response('syspanel_tenant_usage.html', {
|
||||
if request.GET.get('format', 'html') == 'csv':
|
||||
template_name = 'syspanel_tenant_usage.csv'
|
||||
mimetype = "text/csv"
|
||||
else:
|
||||
template_name = 'syspanel_tenant_usage.html'
|
||||
mimetype = "text/html"
|
||||
|
||||
return render_to_response(template_name, {
|
||||
'dateform': dateform,
|
||||
'usage': usage,
|
||||
'csv_link': _csv_usage_link(date_start),
|
||||
'instances': running_instances + terminated_instances,
|
||||
'tenant_id': tenant_id,
|
||||
}, context_instance = template.RequestContext(request))
|
||||
}, context_instance = template.RequestContext(request), mimetype=mimetype)
|
||||
|
||||
|
||||
@login_required
|
||||
|
@ -182,6 +182,30 @@ class InstanceViewTests(base.BaseViewTests):
|
||||
|
||||
self.reset_times()
|
||||
|
||||
def test_instance_csv_usage(self):
|
||||
TEST_RETURN = 'testReturn'
|
||||
|
||||
now = self.override_times()
|
||||
|
||||
self.mox.StubOutWithMock(api, 'usage_get')
|
||||
api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT,
|
||||
datetime.datetime(now.year, now.month, 1,
|
||||
now.hour, now.minute, now.second),
|
||||
now).AndReturn(TEST_RETURN)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
res = self.client.get(reverse('dash_usage', args=[self.TEST_TENANT]) +
|
||||
"?format=csv")
|
||||
|
||||
self.assertTemplateUsed(res, 'dash_usage.csv')
|
||||
|
||||
self.assertEqual(res.context['usage'], TEST_RETURN)
|
||||
|
||||
self.mox.VerifyAll()
|
||||
|
||||
self.reset_times()
|
||||
|
||||
def test_instance_usage_exception(self):
|
||||
now = self.override_times()
|
||||
|
||||
|
10
openstack-dashboard/dashboard/templates/dash_usage.csv
Normal file
10
openstack-dashboard/dashboard/templates/dash_usage.csv
Normal file
@ -0,0 +1,10 @@
|
||||
Usage Report For Period: {{datetime_start}} - {{datetime_end}}
|
||||
Tenant ID: {{usage.tenant_id}}
|
||||
Total Active VCPUs: {{usage.total_active_vcpus}}
|
||||
CPU-HRs Used: {{usage.total_cpu_usage}}
|
||||
Total Active Ram (MB): {{usage.total_active_ram_size}}
|
||||
Total Disk Size: {{usage.total_active_disk_size}}
|
||||
Total Disk Usage: {{usage.total_disk_usage}}
|
||||
|
||||
ID,Name,UserId,VCPUs,RamMB,DiskGB,Flavor,Usage(Hours),Uptime(Seconds),State
|
||||
{% for instance in usage.instances %}{{instance.id}},{{instance.name|addslashes}},{{instance.user_id|addslashes}},{{instance.vcpus|addslashes}},{{instance.ram_size|addslashes}},{{instance.disk_size|addslashes}},{{instance.flavor|addslashes}},{{instance.hours}},{{instance.uptime}},{{instance.state|capfirst|addslashes}}{% endfor %}
|
|
@ -42,6 +42,7 @@
|
||||
</div>
|
||||
|
||||
<div class='table_title wide'>
|
||||
<a class="csv_download_link" href="{{csv_link}}">Download CSV »</a>
|
||||
<h3>Server Usage Summary
|
||||
<small>
|
||||
{% if show_terminated %}
|
||||
|
@ -0,0 +1,10 @@
|
||||
Usage Report For Period: {{datetime_start}} - {{datetime_end}}
|
||||
Tenant ID: {{usage.tenant_id}}
|
||||
Total Active VCPUs: {{usage.total_active_vcpus}}
|
||||
CPU-HRs Used: {{usage.total_cpu_usage}}
|
||||
Total Active Ram (MB): {{usage.total_active_ram_size}}
|
||||
Total Disk Size: {{usage.total_active_disk_size}}
|
||||
Total Disk Usage: {{usage.total_disk_usage}}
|
||||
|
||||
ID,Name,UserId,VCPUs,RamMB,DiskGB,Flavor,Usage(Hours),Uptime(Seconds),State
|
||||
{% for instance in usage.instances %}{{instance.id}},{{instance.name|addslashes}},{{instance.user_id|addslashes}},{{instance.vcpus|addslashes}},{{instance.ram_size|addslashes}},{{instance.disk_size|addslashes}},{{instance.flavor|addslashes}},{{instance.hours}},{{instance.uptime}},{{instance.state|capfirst|addslashes}}{% endfor %}
|
|
@ -56,11 +56,13 @@
|
||||
|
||||
{% if usage.instances %}
|
||||
<div class='table_title wide'>
|
||||
<a class="csv_download_link" href="{{csv_link}}">Download CSV »</a>
|
||||
<h3>Tenant Usage: {{tenant_id}}</h3>
|
||||
</div>
|
||||
|
||||
<table class="wide">
|
||||
<tr id='headings'>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>User</th>
|
||||
<th>VCPUs</th>
|
||||
@ -77,6 +79,7 @@
|
||||
{% else %}
|
||||
<tr class="{% cycle 'odd' 'even' %}">
|
||||
{% endif %}
|
||||
<td>{{instance.id}}</td>
|
||||
<td>{{instance.name}}</td>
|
||||
<td>{{instance.user_id}}</td>
|
||||
<td>{{instance.vcpus}}</td>
|
||||
|
@ -0,0 +1,7 @@
|
||||
Usage Report For Period: {{datetime_start}} - {{datetime_end}}
|
||||
Active Instances: {{global_summary.total_active_instances|default:'-'}}
|
||||
This month's VCPU-Hours: {{global_summary.total_cpu_usage|floatformat|default:'-'}}
|
||||
This month's GB-Hours: {{global_summary.total_disk_usage|floatformat|default:'-'}}
|
||||
|
||||
Name,UserId,VCPUs,RamMB,DiskGB,Flavor,Usage(Hours),Uptime(Seconds),State
|
||||
{% for usage in usage_list %}{% for instance in usage.instances %}{{instance.name|addslashes}},{{instance.user_id|addslashes}},{{instance.vcpus|addslashes}},{{instance.ram_size|addslashes}},{{instance.disk_size|addslashes}},{{instance.flavor|addslashes}},{{instance.hours}},{{instance.uptime}},{{instance.state|capfirst|addslashes}}{% endfor %}{% endfor %}
|
|
@ -66,6 +66,7 @@
|
||||
{% if usage_list %}
|
||||
<div id="usage_table">
|
||||
<div class='table_title wide'>
|
||||
<a class="csv_download_link" href="{{csv_link}}">Download CSV »</a>
|
||||
<h3>Server Usage Summary</h3>
|
||||
</div>
|
||||
|
||||
|
@ -696,6 +696,10 @@ td#actions input[type="submit"]:hover { text-decoration: underline; }
|
||||
padding: 0 0 0 1px;
|
||||
}
|
||||
|
||||
.csv_download_link {
|
||||
float: right;
|
||||
}
|
||||
|
||||
|
||||
/* Forms */
|
||||
|
||||
@ -889,4 +893,4 @@ h3 span, .left h3 { color: #6a6a6a; }
|
||||
.left h3 a {
|
||||
margin: 25px 0 0 0;
|
||||
float: left;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user