Skip to content

Commit

Permalink
Add the json generator for basic aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
x4base committed May 4, 2016
1 parent 0bedaed commit 48d78c5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
22 changes: 22 additions & 0 deletions caravel/assets/javascripts/datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var jsonGenerator = require("./modules/json_generator");

$(document).ready(function () {
$('#convertButton').click(function () {
var expression = $('#expression').val().trim();
var parse_tree = jsonGenerator.parse(expression);
console.log(parse_tree);
$('#json').val(JSON.stringify(parse_tree));
$(this).closest('.modal').modal('hide');
});
$('#json_gen_modal').on('shown.bs.modal', function () {
$('#expression').focus();
});

var showJsonGenModal = function () {
$('#json_gen_modal').modal('show');
};

$.extend(window, {
showJsonGenModal: showJsonGenModal
});
});
41 changes: 41 additions & 0 deletions caravel/assets/javascripts/modules/json_generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var jsep = require("jsep");

var typeMap = {
sum: 'doubleSum',
doubleSum: 'doubleSum',
longSum: 'longSum',
};

var capitalize = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};

var convertFuncCall = function (node) {
var funcName = node.callee.name;
var fieldName = node.arguments[0].name;
if (!funcName) return;
var m = funcName.toLowerCase().match(/^(double|long)?(sum|min|max)$/);
if (!m) return;

var varType = m[1] || 'double'; // default to double
var funcType = m[2];

var type = varType + capitalize(funcType);

return {
type: type,
name: fieldName,
fieldName: fieldName,
};
};

var parse = function (expression) {
expression = expression.trim();
var parse_tree = jsep(expression);
parse_tree = convertFuncCall(parse_tree);
return parse_tree;
};

module.exports = {
parse: parse
};
1 change: 1 addition & 0 deletions caravel/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"imports-loader": "^0.6.5",
"jquery": "^2.2.1",
"jquery-ui": "^1.10.5",
"jsep": "^0.3.0",
"less": "^2.6.1",
"less-loader": "^2.2.2",
"nvd3": "1.8.2",
Expand Down
3 changes: 2 additions & 1 deletion caravel/assets/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var config = {
explore: APP_DIR + '/javascripts/explore.js',
welcome: APP_DIR + '/javascripts/welcome.js',
sql: APP_DIR + '/javascripts/sql.js',
standalone: APP_DIR + '/javascripts/standalone.js'
standalone: APP_DIR + '/javascripts/standalone.js',
datasource: APP_DIR + '/javascripts/datasource.js'
},
output: {
path: BUILD_DIR,
Expand Down
32 changes: 32 additions & 0 deletions caravel/templates/caravel/models/datasource/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "appbuilder/general/model/edit.html" %}

{% block content %}
{{ super() }}
<div class="modal fade" id="json_gen_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content css">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">JSON Generator</h4>
<h6><strong>Enter the algebraic expression to convert. E.g. longSum(price), doubleMin(price)</strong></h6>
</div>
<div class="modal-body">
<textarea id="expression" rows="5" cols="60"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-default" id="convertButton">
Convert to Json
</button>
</div>
</div>
</div>
</div>
{% endblock %}

{% block tail_js %}
{{ super() }}
<script src="/static/assets/javascripts/dist/datasource.entry.js"></script>
{% endblock %}
5 changes: 5 additions & 0 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ class DruidMetricInlineView(CompactCRUDMixin, CaravelModelView): # noqa
"[Druid Post Aggregation]"
"(http://druid.io/docs/latest/querying/post-aggregations.html)",
True),
# 'json': Markup("<a href='javascript:showJsonGenModal()'>Json Generator</a>")
'json': utils.markdown(
"You can also use the [generator](javascript:showJsonGenModal()) to generate json",
True),
}
appbuilder.add_view_no_menu(DruidMetricInlineView)

Expand Down Expand Up @@ -415,6 +419,7 @@ class DruidDatasourceModelView(CaravelModelView, DeleteMixin): # noqa
'datasource_name', 'cluster', 'description', 'owner',
'is_featured', 'is_hidden', 'default_endpoint', 'offset',
'cache_timeout']
edit_template = "caravel/models/datasource/edit.html"
add_columns = edit_columns
page_size = 500
base_order = ('datasource_name', 'asc')
Expand Down

0 comments on commit 48d78c5

Please sign in to comment.