Skip to content

Commit

Permalink
Initial work on support for query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
arikfr committed Jan 27, 2015
1 parent 3b9d9ac commit c0c1022
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
1 change: 1 addition & 0 deletions rd_ui/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<script src="/scripts/ui-bootstrap-tpls-0.5.0.min.js"></script>
<script src="/bower_components/bucky/bucky.js"></script>
<script src="/bower_components/pace/pace.js"></script>
<script src="/bower_components/mustache/mustache.js"></script>
<!-- endbuild -->

<!-- build:js({.tmp,app}) /scripts/scripts.js -->
Expand Down
40 changes: 17 additions & 23 deletions rd_ui/app/scripts/controllers/query_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,7 @@
var isNewQuery = !$scope.query.id,
queryText = $scope.query.query,
// ref to QueryViewCtrl.saveQuery
saveQuery = $scope.saveQuery,
shortcuts = {
'meta+s': function () {
if ($scope.canEdit) {
$scope.saveQuery();
}
},
'ctrl+s': function () {
if ($scope.canEdit) {
$scope.saveQuery();
}
},
// Cmd+Enter for Mac
'meta+enter': function () {
$scope.executeQuery();
},
// Ctrl+Enter for PC
'ctrl+enter': function () {
$scope.executeQuery();
}
};
saveQuery = $scope.saveQuery;

$scope.sourceMode = true;
$scope.canEdit = currentUser.canEdit($scope.query);
Expand All @@ -49,8 +29,22 @@
}
});


KeyboardShortcuts.bind(shortcuts);
KeyboardShortcuts.bind({
'meta+s': function () {
if ($scope.canEdit) {
$scope.saveQuery();
}
},
'ctrl+s': function () {
if ($scope.canEdit) {
$scope.saveQuery();
}
},
// Cmd+Enter for Mac
'meta+enter': $scope.executeQuery,
// Ctrl+Enter for PC
'ctrl+enter': $scope.executeQuery
});

// @override
$scope.saveQuery = function(options, data) {
Expand Down
19 changes: 17 additions & 2 deletions rd_ui/app/scripts/controllers/query_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@
function QueryViewCtrl($scope, Events, $route, $location, notifications, growl, Query, DataSource) {
var DEFAULT_TAB = 'table';

var getQueryResult = function(ttl) {
// Collect params, and getQueryResult with params; getQueryResult merges it into the query
var parameterNames = $scope.query.getParameters();
var parameters = {};

var queryString = $location.search();
_.each(parameterNames, function(param, i) {
var qsName = "p_" + param;
if (qsName in queryString) {
parameters[param] = queryString[qsName];
}
});
$scope.queryResult = $scope.query.getQueryResult(ttl, parameters);
}

$scope.query = $route.current.locals.query;
Events.record(currentUser, 'view', 'query', $scope.query.id);
$scope.queryResult = $scope.query.getQueryResult();
getQueryResult();
$scope.queryExecuting = false;

$scope.isQueryOwner = currentUser.id === $scope.query.user.id;
Expand Down Expand Up @@ -57,7 +72,7 @@
};

$scope.executeQuery = function() {
$scope.queryResult = $scope.query.getQueryResult(0);
getQueryResult(0);
$scope.lockButton(true);
$scope.cancelling = false;
Events.record(currentUser, 'execute', 'query', $scope.query.id);
Expand Down
40 changes: 38 additions & 2 deletions rd_ui/app/scripts/services/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,35 @@
return '/queries/' + this.id + '/source';
};

Query.prototype.getQueryResult = function (ttl) {
Query.prototype.getQueryResult = function (ttl, parameters) {
if (ttl == undefined) {
ttl = this.ttl;
}

var queryText = this.query;

var queryParameters = this.getParameters();
var paramsRequired = !_.isEmpty(queryParameters);

var missingParams = parameters === undefined ? queryParameters : _.difference(queryParameters, _.keys(parameters));

if (paramsRequired && missingParams.length > 0) {
var paramsWord = "parameter";
if (missingParams.length > 1) {
paramsWord = "parameters";
}

return new QueryResult({job: {error: "Missing values for " + missingParams.join(', ') + " "+paramsWord+".", status: 4}});
}

if (parameters !== undefined) {
queryText = Mustache.render(queryText, parameters);

// Need to clear latest results, to make sure we don't used results for different params.
this.latest_query_data = null;
this.latest_query_data_id = null;
}

if (this.latest_query_data && ttl != 0) {
if (!this.queryResult) {
this.queryResult = new QueryResult({'query_result': this.latest_query_data});
Expand All @@ -417,14 +441,26 @@
this.queryResult = QueryResult.getById(this.latest_query_data_id);
}
} else if (this.data_source_id) {
this.queryResult = QueryResult.get(this.data_source_id, this.query, ttl);
this.queryResult = QueryResult.get(this.data_source_id, queryText, ttl);
}

return this.queryResult;
};

Query.prototype.getQueryResultPromise = function() {
return this.getQueryResult().toPromise();
};

Query.prototype.getParameters = function() {
var parts = Mustache.parse(this.query);
var parameters = [];
_.each(parts, function(part) {
if (part[0] == 'name') {
parameters.push(part[1]);
}
});

return parameters;
}

return Query;
Expand Down
3 changes: 2 additions & 1 deletion rd_ui/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"bucky": "~0.2.6",
"pace": "~0.5.1",
"angular-ui-select": "0.8.2",
"font-awesome": "~4.2.0"
"font-awesome": "~4.2.0",
"mustache": "~1.0.0"
},
"devDependencies": {
"angular-mocks": "1.2.18",
Expand Down

0 comments on commit c0c1022

Please sign in to comment.