Commit 1929490d by Torkel Ödegaard

Renamed filterSrv to timeSrv and made it a service again

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