Skip to content

Commit

Permalink
feat(influxdb): more work on changing the influxdb editor to support …
Browse files Browse the repository at this point in the history
…better aliasing and interval options, grafana#2647, grafana#2599
  • Loading branch information
torkelo committed Sep 9, 2015
1 parent 5b722de commit c4c3f9d
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 266 deletions.
1 change: 0 additions & 1 deletion public/app/plugins/datasource/influxdb/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ define([
'./queryBuilder',
'./directives',
'./queryCtrl',
'./funcEditor',
],
function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
'use strict';
Expand Down
152 changes: 0 additions & 152 deletions public/app/plugins/datasource/influxdb/funcEditor.js

This file was deleted.

41 changes: 35 additions & 6 deletions public/app/plugins/datasource/influxdb/partials/query.editor2.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@
<div class="clearfix"></div>
</div>

<div class="tight-form" ng-repeat="field in target.fields">
<ul class="tight-form-list">
<li class="tight-form-item query-keyword tight-form-align" style="width: 75px;">
<span ng-show="$index === 0">SELECT</span>
</li>
<li>
<metric-segment-model property="field.func" get-options="getFunctions()" on-change="get_data()"></metric-segment>
</li>
<li>
<metric-segment-model property="field.name" get-options="getFields()" on-change="get_data()"></metric-segment>
</li>
<li>
<input type="text" class="tight-form-clear-input text-center" style="width: 70px;" ng-model="field.mathExpr" spellcheck='false' placeholder="math expr" ng-blur="get_data()">
</li>
<li class="tight-form-item query-keyword">
AS
</li>
<li>
<input type="text" class="tight-form-clear-input" style="width: 180px;" ng-model="field.asExpr" spellcheck='false' placeholder="as expr" ng-blur="get_data()">
</li>
</ul>

<ul class="tight-form-list pull-right">
<li class="tight-form-item last" ng-show="$index === 0">
<a class="pointer" ng-click="addSelect()"><i class="fa fa-plus"></i></a>
</li>
<li class="tight-form-item last" ng-show="target.fields.length > 1">
<a class="pointer" ng-click="removeSelect($index)"><i class="fa fa-minus"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>

<div class="tight-form" ng-repeat="tag in target.groupBy">
<ul class="tight-form-list">
<li class="tight-form-item query-keyword tight-form-align" style="width: 75px;">
Expand Down Expand Up @@ -96,14 +129,10 @@
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item query-keyword tight-form-align" style="width: 75px;">
SELECT
</li>
<li class="dropdown" ng-repeat="field in target.fields">
<span influxdb-func-editor field="field" get-fields="getFields()" on-change="fieldChanged(field)" class="tight-form-item">
</span>
ALIAS BY
</li>
<li>
<metric-segment segment="addFieldSegment" get-options="getFieldSegments()" on-change="addField()"></metric-segment>
<input type="text" class="tight-form-clear-input input-xlarge" ng-model="target.alias" spellcheck='false' placeholder="Naming pattern" ng-blur="get_data()">
</li>
</ul>
<div class="clearfix"></div>
Expand Down
20 changes: 18 additions & 2 deletions public/app/plugins/datasource/influxdb/queryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ function (_) {
return query;
};


p._getGroupByTimeInterval = function(interval) {
if (interval === 'auto') {
return '$interval';
}
return interval;
};

p._buildQuery = function() {
var target = this.target;

Expand All @@ -103,6 +111,14 @@ function (_) {
query += ', ';
}
query += field.func + '("' + field.name + '")';
if (field.mathExpr) {
query += field.mathExpr;
}
if (field.asExpr) {
query += ' AS "' + field.asExpr + '"';
} else {
query += ' AS "' + field.name + '"';
}
}

var measurement = target.measurement;
Expand All @@ -122,9 +138,9 @@ function (_) {
for (i = 0; i < target.groupBy.length; i++) {
var group = target.groupBy[i];
if (group.type === 'time') {
query += ' time(10s)';
query += ' time(' + this._getGroupByTimeInterval(group.interval) + ')';
} else {
query += ', ' + group.key;
query += ', "' + group.key + '"';
}
}

Expand Down
44 changes: 17 additions & 27 deletions public/app/plugins/datasource/influxdb/queryCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ function (angular, _, InfluxQueryBuilder) {
$scope.measurementSegment = uiSegmentSrv.newSegment(target.measurement);
}

$scope.addFieldSegment = uiSegmentSrv.newPlusButton();

$scope.tagSegments = [];
_.each(target.tags, function(tag) {
if (!tag.operator) {
Expand Down Expand Up @@ -73,23 +71,13 @@ function (angular, _, InfluxQueryBuilder) {
$scope.get_data();
};

$scope.groupByTagUpdated = function(segment, index) {
if (segment.value === $scope.removeGroupBySegment.value) {
$scope.target.groupByTags.splice(index, 1);
$scope.groupBySegments.splice(index, 1);
$scope.$parent.get_data();
return;
}

if (index === $scope.groupBySegments.length-1) {
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
}

segment.type = 'group-by-key';
segment.fake = false;
$scope.addSelect = function() {
$scope.target.fields.push({name: "select field", func: 'mean'});
};

$scope.target.groupByTags[index] = segment.value;
$scope.$parent.get_data();
$scope.removeSelect = function(index) {
$scope.target.fields.splice(index, 1);
$scope.get_data();
};

$scope.changeFunction = function(func) {
Expand All @@ -105,13 +93,7 @@ function (angular, _, InfluxQueryBuilder) {
$scope.getFields = function() {
var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
return $scope.datasource.metricFindQuery(fieldsQuery)
.then(function(results) {
var values = _.pluck(results, 'text');
if ($scope.target.fields.length > 1) {
values.splice(0, 0, "-- remove from select --");
}
return values;
});
.then($scope.transformToSegments(false), $scope.handleQueryError);
};

$scope.toggleQueryMode = function () {
Expand All @@ -121,8 +103,16 @@ function (angular, _, InfluxQueryBuilder) {
$scope.getMeasurements = function () {
var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
return $scope.datasource.metricFindQuery(query)
.then($scope.transformToSegments(true))
.then(null, $scope.handleQueryError);
.then($scope.transformToSegments(true), $scope.handleQueryError);
};

$scope.getFunctions = function () {
var functionList = ['count', 'mean', 'sum', 'min', 'max', 'mode', 'distinct', 'median',
'derivative', 'non_negative_derivative', 'stddev', 'first', 'last'
];
return $q.when(_.map(functionList, function(func) {
return uiSegmentSrv.newSegment(func);
}));
};

$scope.handleQueryError = function(err) {
Expand Down
Loading

0 comments on commit c4c3f9d

Please sign in to comment.