Skip to content

Instantly share code, notes, and snippets.

@mbabineau
Created August 3, 2015 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbabineau/e77a98b8dff40911ec85 to your computer and use it in GitHub Desktop.
Save mbabineau/e77a98b8dff40911ec85 to your computer and use it in GitHub Desktop.
babelchart example
import datetime
import json
import boto
import requests
from flask import Flask, render_template
from babelchart.sources.cloudwatch import CloudWatchSource
from babelchart.sinks.googlecharts import GoogleChartsSink
from babelchart.sources.graphite import GraphiteSource
app = Flask(__name__)
@app.route('/data')
def data():
#'google.visualization.Query.setResponse({"status":"ok","table":{"rows":[{"c":[{"v":"Date(2014,0,22)"},null,null,{"v":40},null]},{"c":[{"v":"Date(2014,0,19)"},{"v":1},null,{"v":10},null]},{"c":[{"v":"Date(2014,0,21)"},null,null,{"v":30},null]},{"c":[{"v":"Date(2014,0,20)"},{"v":2},{"v":"crazy foo event"},null,null]},{"c":[{"v":"Date(2014,0,23)"},{"v":5},null,{"v":50},{"v":"normal bar event"}]}],"cols":[{"type":"date","id":"Date","label":"Date"},{"type":"number","id":"foo","label":"foo"},{"type":"string","id":"foo note","label":"foo note"},{"type":"number","id":"bar","label":"bar"},{"type":"string","id":"bar note","label":"bar note"}]},"reqId":"0","version":"0.6"});'
c = boto.connect_cloudwatch()
cw_result = c.get_metric_statistics(
1800,
datetime.datetime.utcnow() - datetime.timedelta(days=7),
datetime.datetime.utcnow(),
'CPUUtilization',
'AWS/EC2',
['Average', 'Minimum', 'Maximum'],
dimensions={'InstanceId':['i-1bc8ee3a','i-b435179a']}
)
series = CloudWatchSource.to_series(cw_result)
return GoogleChartsSink.from_series(series)
@app.route('/foo')
def example():
return render_template('googlecharts.html')
if __name__ == '__main__':
app.run(debug=True)
<html>
<head>
<title>Example</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="/static/js/json2.js"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['annotationchart']});
</script>
<script type="text/javascript">
var visualization;
function drawVisualization() {
var url = 'http://' + window.location.host + '/data';
var query = new google.visualization.Query(url);
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
visualization = new google.visualization.AnnotationChart(document.getElementById('chart'));
visualization.draw(data, { 'displayAnnotations': true,
'fill': 20,
'legendPosition': 'newRow'})
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body>
<div id="chart" style="width: 800px; height: 400px;"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment