mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
Move timefilter to component, break out watcher reactors, add tests
This commit is contained in:
parent
295cda0edf
commit
f34d90678c
6 changed files with 166 additions and 23 deletions
21
src/kibana/components/timefilter/lib/diff_interval.js
Normal file
21
src/kibana/components/timefilter/lib/diff_interval.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
define(function (require) {
|
||||
var _ = require('lodash');
|
||||
return function diffTimeProvider(Private) {
|
||||
var diff = Private(require('utils/diff_time_picker_vals'));
|
||||
|
||||
return function (self) {
|
||||
var oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
|
||||
return function () {
|
||||
if (diff(self.refreshInterval, oldRefreshInterval)) {
|
||||
self.emit('update');
|
||||
if (!self.refreshInterval.pause && self.refreshInterval.value !== 0) {
|
||||
self.emit('fetch');
|
||||
}
|
||||
}
|
||||
|
||||
oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
};
|
||||
};
|
||||
};
|
||||
});
|
17
src/kibana/components/timefilter/lib/diff_time.js
Normal file
17
src/kibana/components/timefilter/lib/diff_time.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
define(function (require) {
|
||||
var _ = require('lodash');
|
||||
return function diffTimeProvider(Private) {
|
||||
var diff = Private(require('utils/diff_time_picker_vals'));
|
||||
|
||||
return function (self) {
|
||||
var oldTime = _.clone(self.time);
|
||||
return function () {
|
||||
if (diff(self.time, oldTime)) {
|
||||
self.emit('update');
|
||||
self.emit('fetch');
|
||||
}
|
||||
oldTime = _.clone(self.time);
|
||||
};
|
||||
};
|
||||
};
|
||||
});
|
|
@ -1,4 +1,4 @@
|
|||
define(function (require) {
|
||||
define(function (require) {
|
||||
require('modules')
|
||||
.get('kibana')
|
||||
.service('timefilter', function (Private, globalState, $rootScope) {
|
||||
|
@ -22,6 +22,8 @@ define(function (require) {
|
|||
Timefilter.Super.call(this);
|
||||
|
||||
var self = this;
|
||||
var diffTime = Private(require('components/timefilter/lib/diff_time'))(self);
|
||||
var diffInterval = Private(require('components/timefilter/lib/diff_interval'))(self);
|
||||
|
||||
self.enabled = false;
|
||||
|
||||
|
@ -56,33 +58,19 @@ define(function (require) {
|
|||
});
|
||||
|
||||
$rootScope.$$timefilter = self;
|
||||
|
||||
$rootScope.$watchMulti([
|
||||
'$$timefilter.time',
|
||||
'$$timefilter.time.from',
|
||||
'$$timefilter.time.to',
|
||||
'$$timefilter.time.mode',
|
||||
'$$timefilter.time',
|
||||
'$$timefilter.time.mode'
|
||||
], diffTime);
|
||||
|
||||
$rootScope.$watchMulti([
|
||||
'$$timefilter.refreshInterval',
|
||||
'$$timefilter.refreshInterval.pause',
|
||||
'$$timefilter.refreshInterval.value'
|
||||
], (function () {
|
||||
var oldTime;
|
||||
var oldRefreshInterval;
|
||||
|
||||
return function () {
|
||||
if (diff(self.time, oldTime)) {
|
||||
self.emit('update');
|
||||
self.emit('fetch');
|
||||
} else if (diff(self.refreshInterval, oldRefreshInterval)) {
|
||||
self.emit('update');
|
||||
if (!self.refreshInterval.pause && self.refreshInterval.value !== 0) {
|
||||
self.emit('fetch');
|
||||
}
|
||||
}
|
||||
|
||||
oldTime = _.clone(self.time);
|
||||
oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
};
|
||||
}()));
|
||||
], diffInterval);
|
||||
}
|
||||
|
||||
Timefilter.prototype.get = function (indexPattern) {
|
|
@ -17,7 +17,7 @@ define(function (require) {
|
|||
require('components/courier/courier');
|
||||
require('components/index_patterns/index_patterns');
|
||||
require('components/state_management/app_state');
|
||||
require('services/timefilter');
|
||||
require('components/timefilter/timefilter');
|
||||
require('components/highlight/highlight_tags');
|
||||
|
||||
var app = require('modules').get('apps/discover', [
|
||||
|
|
75
test/unit/specs/components/timefilter/diff_interval.js
Normal file
75
test/unit/specs/components/timefilter/diff_interval.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
define(function (require) {
|
||||
var sinon = require('test_utils/auto_release_sinon');
|
||||
describe('Timefilter service', function () {
|
||||
describe('Refresh interval diff watcher', function () {
|
||||
|
||||
var fn, update, fetch, timefilter;
|
||||
beforeEach(module('kibana'));
|
||||
|
||||
beforeEach(inject(function (Private) {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
refreshInterval: {
|
||||
pause: false,
|
||||
value: 0
|
||||
},
|
||||
emit: function (eventType) {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
fn = Private(require('components/timefilter/lib/diff_interval'))(timefilter);
|
||||
}));
|
||||
|
||||
it('not emit anything if nothing has changed', function () {
|
||||
timefilter.refreshInterval = {pause: false, value: 0};
|
||||
fn();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit only an update when paused', function () {
|
||||
timefilter.refreshInterval = {pause: true, value: 5000};
|
||||
fn();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit update, not fetch, when switching to value: 0', function () {
|
||||
timefilter.refreshInterval = {pause: false, value: 5000};
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = {pause: false, value: 0};
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
it('should emit update, not fetch, when moving from unpaused to paused', function () {
|
||||
timefilter.refreshInterval = {pause: false, value: 5000};
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = {pause: true, value: 5000};
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
it('should emit update and fetch when unpaused', function () {
|
||||
timefilter.refreshInterval = {pause: true, value: 5000};
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(false);
|
||||
timefilter.refreshInterval = {pause: false, value: 5000};
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
42
test/unit/specs/components/timefilter/diff_time.js
Normal file
42
test/unit/specs/components/timefilter/diff_time.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
define(function (require) {
|
||||
var sinon = require('test_utils/auto_release_sinon');
|
||||
describe('Timefilter service', function () {
|
||||
describe('time diff watcher', function () {
|
||||
|
||||
var fn, update, fetch, timefilter;
|
||||
beforeEach(module('kibana'));
|
||||
|
||||
beforeEach(inject(function (Private) {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
time: {
|
||||
from: 0,
|
||||
to: 1
|
||||
},
|
||||
emit: function (eventType) {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
fn = Private(require('components/timefilter/lib/diff_time'))(timefilter);
|
||||
}));
|
||||
|
||||
it('not emit anything if the time has not changed', function () {
|
||||
timefilter.time = {from: 0, to: 1};
|
||||
fn();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit update and fetch if the time has changed', function () {
|
||||
timefilter.time = {from: 5, to: 10};
|
||||
fn();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue