Commit 1929490d by Torkel Ödegaard

Renamed filterSrv to timeSrv and made it a service again

parent bb3b3182
......@@ -14,7 +14,7 @@ function (angular, $, config, _) {
$scope,
$rootScope,
dashboardKeybindings,
filterSrv,
timeSrv,
templateSrv,
dashboardSrv,
dashboardViewStateSrv,
......@@ -52,8 +52,7 @@ function (angular, $, config, _) {
$scope.grafana.style = $scope.dashboard.style;
$scope.filter = filterSrv;
$scope.filter.init($scope.dashboard);
timeSrv.init($scope.dashboard);
templateSrv.init($scope.dashboard);
$scope.submenuEnabled = $scope.dashboard.templating.enable || $scope.dashboard.annotations.enable;
......
......@@ -11,7 +11,7 @@ function (angular, _, moment, config, store) {
var module = angular.module('grafana.controllers');
module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, datasourceSrv) {
module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, datasourceSrv, timeSrv) {
$scope.init = function() {
$scope.db = datasourceSrv.getGrafanaDB();
......@@ -114,7 +114,7 @@ function (angular, _, moment, config, store) {
// function $scope.zoom
// factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
$scope.zoom = function(factor) {
var _range = $scope.filter.timeRange();
var _range = timeSrv.timeRange();
var _timespan = (_range.to.valueOf() - _range.from.valueOf());
var _center = _range.to.valueOf() - _timespan/2;
......@@ -128,7 +128,7 @@ function (angular, _, moment, config, store) {
_to = Date.now();
}
$scope.filter.setTime({
timeSrv.setTime({
from:moment.utc(_from).toDate(),
to:moment.utc(_to).toDate(),
});
......
......@@ -110,7 +110,7 @@ function (angular, _, config, gfunc, Parser) {
}
var path = getSegmentPathUpTo(fromIndex + 1);
return $scope.datasource.metricFindQuery($scope.filter, path)
return $scope.datasource.metricFindQuery(path)
.then(function(segments) {
if (segments.length === 0) {
$scope.segments = $scope.segments.splice(0, fromIndex);
......@@ -147,7 +147,7 @@ function (angular, _, config, gfunc, Parser) {
var query = index === 0 ?
'*' : getSegmentPathUpTo(index) + '.*';
return $scope.datasource.metricFindQuery($scope.filter, query)
return $scope.datasource.metricFindQuery(query)
.then(function(segments) {
$scope.altSegments = _.map(segments, function(segment) {
return new MetricSegment({ value: segment.text, expandable: segment.expandable });
......
......@@ -10,7 +10,7 @@ function (angular, $, kbn, moment, _) {
var module = angular.module('grafana.directives');
module.directive('grafanaGraph', function($rootScope) {
module.directive('grafanaGraph', function($rootScope, timeSrv) {
return {
restrict: 'A',
template: '<div> </div>',
......@@ -416,7 +416,7 @@ function (angular, $, kbn, moment, _) {
elem.bind("plotselected", function (event, ranges) {
scope.$apply(function() {
scope.filter.setTime({
timeSrv.setTime({
from : moment.utc(ranges.xaxis.from).toDate(),
to : moment.utc(ranges.xaxis.to).toDate(),
});
......
......@@ -23,7 +23,7 @@ function (angular, app, $, _, kbn, moment, TimeSeries) {
var module = angular.module('grafana.panels.graph');
app.useModule(module);
module.controller('GraphCtrl', function($scope, $rootScope, $timeout, panelSrv, annotationsSrv) {
module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, timeSrv) {
$scope.panelMeta = {
modals : [],
......@@ -179,8 +179,8 @@ function (angular, app, $, _, kbn, moment, TimeSeries) {
$scope.hiddenSeries = {};
$scope.updateTimeRange = function () {
$scope.range = $scope.filter.timeRange();
$scope.rangeUnparsed = $scope.filter.timeRange(false);
$scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false);
$scope.resolution = Math.ceil($(window).width() * ($scope.panel.span / 12));
$scope.interval = '10m';
......@@ -203,9 +203,9 @@ function (angular, app, $, _, kbn, moment, TimeSeries) {
cacheTimeout: $scope.panel.cacheTimeout
};
$scope.annotationsPromise = annotationsSrv.getAnnotations($scope.filter, $scope.rangeUnparsed, $scope.dashboard);
$scope.annotationsPromise = annotationsSrv.getAnnotations($scope.rangeUnparsed, $scope.dashboard);
return $scope.datasource.query($scope.filter, metricsQuery)
return $scope.datasource.query(metricsQuery)
.then($scope.dataHandler)
.then(null, function(err) {
$scope.panelMeta.loading = false;
......
......@@ -3,7 +3,6 @@ define([
'app',
'lodash',
'require',
'services/filterSrv'
],
function (angular, app, _, require) {
'use strict';
......
......@@ -16,8 +16,7 @@
<li class="dropdown">
<a class="dropdown-toggle timepicker-dropdown" data-toggle="dropdown" href="" bs-tooltip="time.tooltip" data-placement="bottom" ng-click="dismiss();">
<span ng-show="filter.time" ng-bind="time.rangeString"></span>
<span ng-hide="filter.time">Time filter</span>
<span ng-bind="time.rangeString"></span>
<span ng-show="dashboard.refresh" class="text-warning">refreshed every {{dashboard.refresh}} </span>
<i class="icon-caret-down"></i>
</a>
......
......@@ -25,11 +25,11 @@ function (angular, app, _, moment, kbn) {
var module = angular.module('grafana.panels.timepicker', []);
app.useModule(module);
module.controller('timepicker', function($scope) {
module.controller('timepicker', function($scope, timeSrv) {
$scope.panelMeta = {
status : "Stable",
description : "A panel for controlling the time range filters. If you have time based data, "+
" or if you're using time stamped indices, you need one of these"
description : ""
};
// Set and populate defaults
......@@ -55,9 +55,9 @@ function (angular, app, _, moment, kbn) {
});
$scope.init = function() {
var time = this.filter.timeRange(true);
var time = timeSrv.timeRange(true);
if(time) {
$scope.panel.now = this.filter.timeRange(false).to === "now" ? true : false;
$scope.panel.now = timeSrv.timeRange(false).to === "now" ? true : false;
$scope.time = getScopeTimeObj(time.from,time.to);
}
};
......@@ -126,7 +126,7 @@ function (angular, app, _, moment, kbn) {
}
// Set the filter
$scope.panel.filter_id = $scope.filter.setTime(_filter);
$scope.panel.filter_id = timeSrv.setTime(_filter);
// Update our representation
$scope.time = getScopeTimeObj(time.from,time.to);
......@@ -140,7 +140,7 @@ function (angular, app, _, moment, kbn) {
to: "now"
};
this.filter.setTime(_filter);
timeSrv.setTime(_filter);
$scope.time = getScopeTimeObj(kbn.parseDate(_filter.from),new Date());
};
......@@ -173,7 +173,7 @@ function (angular, app, _, moment, kbn) {
model.tooltip = 'Click to set time filter';
}
if ($scope.filter.time) {
if (timeSrv.time) {
if ($scope.panel.now) {
model.rangeString = moment(model.from.date).fromNow() + ' to ' +
moment(model.to.date).fromNow();
......
define([
'./alertSrv',
'./datasourceSrv',
'./filterSrv',
'./timeSrv',
'./templateSrv',
'./panelSrv',
'./timer',
......
......@@ -21,7 +21,7 @@ define([
list = [];
};
this.getAnnotations = function(filterSrv, rangeUnparsed, dashboard) {
this.getAnnotations = function(rangeUnparsed, dashboard) {
if (!dashboard.annotations.enable) {
return $q.when(null);
}
......@@ -36,7 +36,7 @@ define([
var promises = _.map(annotations, function(annotation) {
var datasource = datasourceSrv.get(annotation.datasource);
return datasource.annotationQuery(annotation, filterSrv, rangeUnparsed)
return datasource.annotationQuery(annotation, rangeUnparsed)
.then(this.receiveAnnotationResults)
.then(null, errorHandler);
}, this);
......
......@@ -11,7 +11,7 @@ function (angular, _, $, config, kbn, moment) {
var module = angular.module('grafana.services');
module.factory('GraphiteDatasource', function($q, $http) {
module.factory('GraphiteDatasource', function($q, $http, timeSrv) {
function GraphiteDatasource(datasource) {
this.type = 'graphite';
......@@ -26,7 +26,7 @@ function (angular, _, $, config, kbn, moment) {
this.cacheTimeout = datasource.cacheTimeout;
}
GraphiteDatasource.prototype.query = function(filterSrv, options) {
GraphiteDatasource.prototype.query = function(options) {
try {
var graphOptions = {
from: this.translateTime(options.range.from, 'round-down'),
......@@ -37,7 +37,7 @@ function (angular, _, $, config, kbn, moment) {
maxDataPoints: options.maxDataPoints,
};
var params = this.buildGraphiteParams(filterSrv, graphOptions);
var params = this.buildGraphiteParams(graphOptions);
if (options.format === 'png') {
return $q.when(this.url + '/render' + '?' + params.join('&'));
......@@ -60,10 +60,10 @@ function (angular, _, $, config, kbn, moment) {
}
};
GraphiteDatasource.prototype.annotationQuery = function(annotation, filterSrv, rangeUnparsed) {
GraphiteDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
// Graphite metric as annotation
if (annotation.target) {
var target = filterSrv.applyTemplateToTarget(annotation.target);
var target = timeSrv.applyTemplateToTarget(annotation.target);
var graphiteQuery = {
range: rangeUnparsed,
targets: [{ target: target }],
......@@ -71,7 +71,7 @@ function (angular, _, $, config, kbn, moment) {
maxDataPoints: 100
};
return this.query(filterSrv, graphiteQuery)
return this.query(timeSrv, graphiteQuery)
.then(function(result) {
var list = [];
......@@ -95,7 +95,7 @@ function (angular, _, $, config, kbn, moment) {
}
// Graphite event as annotation
else {
var tags = filterSrv.applyTemplateToTarget(annotation.tags);
var tags = timeSrv.applyTemplateToTarget(annotation.tags);
return this.events({ range: rangeUnparsed, tags: tags })
.then(function(results) {
var list = [];
......@@ -166,10 +166,10 @@ function (angular, _, $, config, kbn, moment) {
return date.unix();
};
GraphiteDatasource.prototype.metricFindQuery = function(filterSrv, query) {
GraphiteDatasource.prototype.metricFindQuery = function(query) {
var interpolated;
try {
interpolated = encodeURIComponent(filterSrv.applyTemplateToTarget(query));
interpolated = encodeURIComponent(timeSrv.applyTemplateToTarget(query));
}
catch(err) {
return $q.reject(err);
......@@ -210,7 +210,7 @@ function (angular, _, $, config, kbn, moment) {
return $http(options);
};
GraphiteDatasource.prototype.buildGraphiteParams = function(filterSrv, options) {
GraphiteDatasource.prototype.buildGraphiteParams = function(options) {
var clean_options = [];
var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
......@@ -226,7 +226,7 @@ function (angular, _, $, config, kbn, moment) {
if (key === "targets") {
_.each(value, function (value) {
if (value.target && !value.hide) {
var targetValue = filterSrv.applyTemplateToTarget(value.target);
var targetValue = timeSrv.applyTemplateToTarget(value.target);
clean_options.push("target=" + encodeURIComponent(targetValue));
}
}, this);
......
......@@ -9,7 +9,7 @@ function (angular, _, kbn, InfluxSeries) {
var module = angular.module('grafana.services');
module.factory('InfluxDatasource', function($q, $http) {
module.factory('InfluxDatasource', function($q, $http, timeSrv) {
function InfluxDatasource(datasource) {
this.type = 'influxDB';
......@@ -31,7 +31,7 @@ function (angular, _, kbn, InfluxSeries) {
this.annotationEditorSrc = 'app/partials/influxdb/annotation_editor.html';
}
InfluxDatasource.prototype.query = function(filterSrv, options) {
InfluxDatasource.prototype.query = function(options) {
var promises = _.map(options.targets, function(target) {
var query;
var alias = '';
......@@ -73,7 +73,7 @@ function (angular, _, kbn, InfluxSeries) {
}
query = queryElements.join(" ");
query = filterSrv.applyTemplateToTarget(query);
query = timeSrv.applyTemplateToTarget(query);
}
else {
......@@ -100,7 +100,7 @@ function (angular, _, kbn, InfluxSeries) {
}
query = _.template(template, templateData, this.templateSettings);
query = filterSrv.applyTemplateToTarget(query);
query = timeSrv.applyTemplateToTarget(query);
if (target.groupby_field_add) {
groupByField = target.groupby_field;
......
......@@ -8,10 +8,17 @@ define([
var module = angular.module('grafana.services');
module.factory('filterSrv', function($rootScope, $timeout, $routeParams) {
var result = {
module.service('timeSrv', function($rootScope, $timeout, $routeParams) {
updateTemplateData: function(initial) {
this.init = function(dashboard) {
this.dashboard = dashboard;
this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
this.time = dashboard.time;
this.templateParameters = dashboard.templating.list;
this.updateTemplateData(true);
};
this.updateTemplateData = function(initial) {
var _templateData = {};
_.each(this.templateParameters, function(templateParameter) {
if (initial) {
......@@ -26,22 +33,22 @@ define([
_templateData[templateParameter.name] = templateParameter.current.value;
});
this._templateData = _templateData;
},
};
addTemplateParameter: function(templateParameter) {
this.addTemplateParameter = function(templateParameter) {
this.templateParameters.push(templateParameter);
this.updateTemplateData();
},
};
applyTemplateToTarget: function(target) {
this.applyTemplateToTarget = function(target) {
if (!target || target.indexOf('[[') === -1) {
return target;
}
return _.template(target, this._templateData, this.templateSettings);
},
};
setTime: function(time) {
this.setTime = function(time) {
_.extend(this.time, time);
// disable refresh if we have an absolute time
......@@ -55,9 +62,9 @@ define([
}
$timeout(this.dashboard.emit_refresh, 0);
},
};
timeRange: function(parse) {
this.timeRange = function(parse) {
var _t = this.time;
if(_.isUndefined(_t) || _.isUndefined(_t.from)) {
return false;
......@@ -76,18 +83,8 @@ define([
to : kbn.parseDate(_to)
};
}
},
init: function(dashboard) {
this.dashboard = dashboard;
this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
this.time = dashboard.time;
this.templateParameters = dashboard.templating.list;
this.updateTemplateData(true);
}
};
};
return result;
});
});
......@@ -15,6 +15,9 @@ define([
describe(desc, function() {
var ctx = {};
ctx.setup = function (setupFunc) {
beforeEach(module(function($provide) {
$provide.value("timeSrv", new helpers.TimeSrvStub());
}));
beforeEach(inject(function($rootScope, $compile) {
var scope = $rootScope.$new();
var element = angular.element("<div style='width:500px' grafana-graph><div>");
......
......@@ -28,7 +28,7 @@ define([
});
it('should validate metric key exists', function() {
expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[1]).to.be('test.prod.*');
expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
});
it('should delete last segment if no metrics are found', function() {
......
......@@ -8,6 +8,7 @@ define([
this.datasource = {};
this.annotationsSrv = {};
this.timeSrv = new TimeSrvStub();
this.datasourceSrv = {
getMetricSources: function() {},
get: function() { return self.datasource; }
......@@ -17,6 +18,7 @@ define([
return module(function($provide) {
$provide.value('datasourceSrv', self.datasourceSrv);
$provide.value('annotationsSrv', self.annotationsSrv);
$provide.value('timeSrv', self.timeSrv);
});
};
......@@ -25,7 +27,6 @@ define([
self.scope = $rootScope.$new();
self.scope.panel = {};
self.scope.row = { panels:[] };
self.scope.filter = new FilterSrvStub();
self.scope.dashboard = {};
self.scope.dashboardViewState = new DashboardViewStateStub();
......@@ -50,7 +51,6 @@ define([
self.service = InfluxDatasource;
self.$q = $q;
self.$rootScope = $rootScope;
self.filterSrv = new FilterSrvStub();
self.$httpBackend = $httpBackend;
}]);
};
......@@ -61,7 +61,7 @@ define([
};
}
function FilterSrvStub() {
function TimeSrvStub() {
this.time = { from:'now-1h', to: 'now'};
this.timeRange = function(parse) {
if (parse === false) {
......@@ -81,7 +81,7 @@ define([
return {
ControllerTestContext: ControllerTestContext,
FilterSrvStub: FilterSrvStub,
TimeSrvStub: TimeSrvStub,
ServiceTestContext: ServiceTestContext
};
......
......@@ -29,7 +29,7 @@ define([
var ds = new ctx.service({ urls: [''], user: 'test', password: 'mupp' });
ctx.$httpBackend.expect('GET', urlExpected).respond(response);
ds.query(ctx.filterSrv, query).then(function(data) { results = data; });
ds.query(query).then(function(data) { results = data; });
ctx.$httpBackend.flush();
});
......@@ -59,7 +59,7 @@ define([
var ds = new ctx.service({ urls: [''], user: 'test', password: 'mupp' });
ctx.$httpBackend.expect('GET', urlExpected).respond(response);
ds.query(ctx.filterSrv, query).then(function(data) { results = data; });
ds.query(query).then(function(data) { results = data; });
ctx.$httpBackend.flush();
});
......
define([
'mocks/dashboard-mock',
'lodash',
'services/filterSrv'
'services/timeSrv'
], function(dashboardMock, _) {
'use strict';
describe('filterSrv', function() {
var _filterSrv;
describe('timeSrv', function() {
var _timeSrv;
var _dashboard;
beforeEach(module('grafana.services'));
......@@ -14,52 +14,52 @@ define([
_dashboard = dashboardMock.create();
}));
beforeEach(inject(function(filterSrv) {
_filterSrv = filterSrv;
beforeEach(inject(function(timeSrv) {
_timeSrv = timeSrv;
}));
beforeEach(function() {
_filterSrv.init(_dashboard);
_timeSrv.init(_dashboard);
});
describe('init', function() {
beforeEach(function() {
_filterSrv.addTemplateParameter({ name: 'test', current: { value: 'oogle' } });
_timeSrv.addTemplateParameter({ name: 'test', current: { value: 'oogle' } });
});
it('should initialize template data', function() {
var target = _filterSrv.applyTemplateToTarget('this.[[test]].filters');
var target = _timeSrv.applyTemplateToTarget('this.[[test]].filters');
expect(target).to.be('this.oogle.filters');
});
});
describe('updateTemplateData', function() {
beforeEach(function() {
_filterSrv.addTemplateParameter({
_timeSrv.addTemplateParameter({
name: 'test',
value: 'muuu',
current: { value: 'muuuu' }
});
_filterSrv.updateTemplateData();
_timeSrv.updateTemplateData();
});
it('should set current value and update template data', function() {
var target = _filterSrv.applyTemplateToTarget('this.[[test]].filters');
var target = _timeSrv.applyTemplateToTarget('this.[[test]].filters');
expect(target).to.be('this.muuuu.filters');
});
});
describe('timeRange', function() {
it('should return unparsed when parse is false', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(false);
_timeSrv.setTime({from: 'now', to: 'now-1h' });
var time = _timeSrv.timeRange(false);
expect(time.from).to.be('now');
expect(time.to).to.be('now-1h');
});
it('should return parsed when parse is true', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(true);
_timeSrv.setTime({from: 'now', to: 'now-1h' });
var time = _timeSrv.timeRange(true);
expect(_.isDate(time.from)).to.be(true);
expect(_.isDate(time.to)).to.be(true);
});
......@@ -69,15 +69,15 @@ define([
it('should return disable refresh for absolute times', function() {
_dashboard.refresh = true;
_filterSrv.setTime({from: '2011-01-01', to: '2015-01-01' });
_timeSrv.setTime({from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).to.be(false);
});
it('should restore refresh after relative time range is set', function() {
_dashboard.refresh = true;
_filterSrv.setTime({from: '2011-01-01', to: '2015-01-01' });
_timeSrv.setTime({from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).to.be(false);
_filterSrv.setTime({from: '2011-01-01', to: 'now' });
_timeSrv.setTime({from: '2011-01-01', to: 'now' });
expect(_dashboard.refresh).to.be(true);
});
});
......
......@@ -125,7 +125,7 @@ require([
'specs/graph-ctrl-specs',
'specs/grafanaGraph-specs',
'specs/seriesOverridesCtrl-specs',
'specs/filterSrv-specs',
'specs/timeSrv-specs',
'specs/kbn-format-specs',
'specs/dashboardSrv-specs',
'specs/dashboardViewStateSrv-specs',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment