diff --git a/caravel/assets/javascripts/datasource.js b/caravel/assets/javascripts/datasource.js new file mode 100644 index 0000000000000..7b067cc9d037f --- /dev/null +++ b/caravel/assets/javascripts/datasource.js @@ -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 + }); +}); diff --git a/caravel/assets/javascripts/modules/json_generator.js b/caravel/assets/javascripts/modules/json_generator.js new file mode 100644 index 0000000000000..c081d91f438ed --- /dev/null +++ b/caravel/assets/javascripts/modules/json_generator.js @@ -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 +}; \ No newline at end of file diff --git a/caravel/assets/package.json b/caravel/assets/package.json index ccebcdb792d85..9085a8613e233 100644 --- a/caravel/assets/package.json +++ b/caravel/assets/package.json @@ -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", diff --git a/caravel/assets/webpack.config.js b/caravel/assets/webpack.config.js index d9253f3d5c413..2696f6e273029 100644 --- a/caravel/assets/webpack.config.js +++ b/caravel/assets/webpack.config.js @@ -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, diff --git a/caravel/templates/caravel/models/datasource/edit.html b/caravel/templates/caravel/models/datasource/edit.html new file mode 100644 index 0000000000000..751ecc14d5239 --- /dev/null +++ b/caravel/templates/caravel/models/datasource/edit.html @@ -0,0 +1,32 @@ +{% extends "appbuilder/general/model/edit.html" %} + +{% block content %} + {{ super() }} + +{% endblock %} + +{% block tail_js %} + {{ super() }} + +{% endblock %} diff --git a/caravel/views.py b/caravel/views.py index aea86169c7b93..92c30f30bd7b9 100644 --- a/caravel/views.py +++ b/caravel/views.py @@ -181,6 +181,10 @@ class DruidMetricInlineView(CompactCRUDMixin, CaravelModelView): # noqa "[Druid Post Aggregation]" "(http://druid.io/docs/latest/querying/post-aggregations.html)", True), + # 'json': Markup("Json Generator") + 'json': utils.markdown( + "You can also use the [generator](javascript:showJsonGenModal()) to generate json", + True), } appbuilder.add_view_no_menu(DruidMetricInlineView) @@ -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')