Add basic text/html renderer

Change-Id: I929d3fe0aed37bd3722f6a7dc96210a03668c87f
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2012-10-11 17:57:44 +02:00
parent b937aa509b
commit 22cf0dd36e
14 changed files with 277 additions and 9 deletions

View File

@ -0,0 +1,26 @@
Copyright (c) 2012, Michael Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name Michael Bostock may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,7 @@
Copyright (C) 2011 by Shutterstock Images, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because one or more lines are too long

4
ceilometer/api/static/d3.v2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
ceilometer/api/static/rickshaw.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,175 @@
<!DOCTYPE html>
<html>
<head>
<title>
Metering for {{meter}}
</title>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="/static/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css">
<link href="/static/rickshaw.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<div class="content">
<div>
<ul class="breadcrumb">
{% if source %}
<li class="active">
<a href="/v1/sources/{{source}}">
<i class="icon-share"></i>
Source {{source}}
</a>
<span class="divider">/</span>
</li>
{% endif %}
{% if user %}
<li class="active">
<a href="/v1/users/{{user}}">
<i class="icon-user"></i>
User {{user}}
</a>
<span class="divider">/</span>
</li>
{% endif %}
{% if project %}
<li class="active">
<a href="/v1/projects/{{project}}">
<i class="icon-tasks"></i>
Project <strong>{{project}}</strong>
</a>
<span class="divider">/</span>
</li>
{% endif %}
{% if resource %}
<li>
<a href="/v1/resources/{{resource}}">
<i class="icon-cog"></i>
Resource
{{resource}}
</a>
<span class="divider">/</span>
</li>
{% endif %}
<li class="active">
<a href="#">
<i class="icon-screenshot"></i>
Meter
{{meter}}
</a>
</li>
</ul>
</div>
<div class="row">
<div class="span7">
<h1>Graph</h1>
<div id="chart"></div>
</div>
<div class="span5">
<h1>API</h1>
<div class="btn-group" id="api-buttons">
</div>
<br>
<div id="result"></div>
</div>
</div>
<h1>JSON</h1>
<pre class="pre-scrollable" id="events-json">
</pre>
</div>
</div>
<script src="/static/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/static/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="/static/d3.v2.min.js" type="text/javascript"></script>
<script src="/static/rickshaw.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var events = {{events.data|safe}};
$("pre#events-json").text(JSON.stringify(events, undefined, 2));
var graph = new Rickshaw.Graph( {
element: document.getElementById("chart"),
renderer: 'line',
series: [
{
data: events.events.map(function (event) {
formatDate = d3.time.format.iso;
return {
x: formatDate.parse(event.timestamp).getTime(),
y: event.counter_volume,
}
}),
color: "#c05020",
name: "{{meter}}",
},
]
} );
var y_ticks = new Rickshaw.Graph.Axis.Y( {
graph: graph,
orientation: 'left',
tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
element: document.getElementById('y_axis'),
} );
var axes = new Rickshaw.Graph.Axis.Time({
graph: graph,
ticksTreatment: "glow",
});
var hoverDetail = new Rickshaw.Graph.HoverDetail({
graph: graph
});
graph.render();
})
</script>
<script type="text/javascript">
$(function () {
var meter_api = [["volume/max", "volume-max", "chevron-up"],
["volume/sum", "volume-sum", "plus-sign"],
["duration", "duration", "time"]];
meter_api.forEach(function (data) {
var link = data[0];
var class_name = data[1];
var icon = data[2];
$("#api-buttons")
.append($("<a>")
.attr({
href: "./{{meter}}/" + link,
id: "button-" + class_name,
})
.addClass("btn btn-primary")
.append($("<i>").addClass("icon-white icon-" + icon))
.append(" Get " + link)
.click(function (event) {
event.preventDefault();
$.ajax({
url: "./{{meter}}/" + link,
}).done(function (data) {
$("#result")
.empty()
.append($("<div>")
.addClass("well")
.append($("<strong>")
.append($("<i>").addClass("icon-" + icon))
.append(" " + link))
.append($("<br>"))
.append($("<pre>")
.append(JSON.stringify(data,
undefined, 2))))
})
}))
});
});
</script>
</body>
</html>

View File

@ -91,6 +91,14 @@ LOG = log.getLogger(__name__)
blueprint = flask.Blueprint('v1', __name__)
def request_wants_html():
best = flask.request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'text/html' and \
flask.request.accept_mimetypes[best] > \
flask.request.accept_mimetypes['application/json']
## APIs for working with resources.
@ -242,12 +250,11 @@ def list_projects_by_source(source):
## APIs for working with events.
def _list_events(project=None,
resource=None,
source=None,
user=None,
meter=None,
):
def _list_events(meter,
project=None,
resource=None,
source=None,
user=None):
"""Return a list of raw metering events.
"""
f = storage.EventFilter(user=user,
@ -256,8 +263,17 @@ def _list_events(project=None,
meter=meter,
resource=resource,
)
events = flask.request.storage_conn.get_raw_events(f)
return flask.jsonify(events=list(events))
events = list(flask.request.storage_conn.get_raw_events(f))
jsonified = flask.jsonify(events=events)
if request_wants_html():
return flask.templating.render_template('list_event.html',
user=user,
project=project,
source=source,
meter=meter,
resource=resource,
events=jsonified)
return jsonified
@blueprint.route('/projects/<project>/meters/<meter>')

View File

@ -18,7 +18,7 @@
# under the License.
import textwrap
import os
import setuptools
from ceilometer.openstack.common import setup as common_setup
@ -29,6 +29,12 @@ depend_links = common_setup.parse_dependency_links(['tools/pip-requires'])
version = '0.2'
url_base = 'http://tarballs.openstack.org/ceilometer/ceilometer-%s.tar.gz'
def directories(target_dir):
return [dirpath
for dirpath, dirnames, filenames in os.walk(target_dir)]
setuptools.setup(
name='ceilometer',
@ -58,6 +64,11 @@ setuptools.setup(
packages=setuptools.find_packages(exclude=['bin']),
cmdclass=common_setup.get_cmdclass(),
package_data={
"ceilometer":
directories("ceilomter/api/static")
+ directories("ceilometer/api/templates"),
},
include_package_data=True,
test_suite='nose.collector',