-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathcourier.js
147 lines (122 loc) · 4.07 KB
/
courier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import _ from 'lodash';
import 'ui/es';
import 'ui/promises';
import 'ui/index_patterns';
import { uiModules } from 'ui/modules';
import { Notifier } from 'ui/notify/notifier';
import { DocSourceProvider } from './data_source/doc_source';
import { SearchSourceProvider } from './data_source/search_source';
import { SearchStrategyProvider } from './fetch/strategy/search';
import { RequestQueueProvider } from './_request_queue';
import { FetchProvider } from './fetch';
import { DocDataLooperProvider } from './looper/doc_data';
import { SearchLooperProvider } from './looper/search';
import { RootSearchSourceProvider } from './data_source/_root_search_source';
import { SavedObjectProvider } from './saved_object';
import { RedirectWhenMissingProvider } from './_redirect_when_missing';
uiModules.get('kibana/courier')
.service('courier', function ($rootScope, Private, Promise, indexPatterns) {
function Courier() {
const self = this;
const DocSource = Private(DocSourceProvider);
const SearchSource = Private(SearchSourceProvider);
const searchStrategy = Private(SearchStrategyProvider);
const requestQueue = Private(RequestQueueProvider);
const fetch = Private(FetchProvider);
const docDataLooper = self.docLooper = Private(DocDataLooperProvider);
const searchLooper = self.searchLooper = Private(SearchLooperProvider);
// expose some internal modules
self.setRootSearchSource = Private(RootSearchSourceProvider).set;
self.SavedObject = Private(SavedObjectProvider);
self.indexPatterns = indexPatterns;
self.redirectWhenMissing = Private(RedirectWhenMissingProvider);
self.DocSource = DocSource;
self.SearchSource = SearchSource;
/**
* update the time between automatic search requests
*
* @chainable
*/
self.fetchInterval = function (ms) {
searchLooper.ms(ms);
return this;
};
/**
* Start fetching search requests on an interval
* @chainable
*/
self.start = function () {
searchLooper.start();
docDataLooper.start();
return this;
};
/**
* Process the pending request queue right now, returns
* a promise that resembles the success of the fetch completing,
* individual errors are routed to their respective requests.
*/
self.fetch = function () {
fetch.fetchQueued(searchStrategy).then(function () {
searchLooper.restart();
});
};
/**
* is the currior currently fetching search
* results automatically?
*
* @return {boolean}
*/
self.started = function () {
return searchLooper.started();
};
/**
* stop the courier from fetching more search
* results, does not stop vaidating docs.
*
* @chainable
*/
self.stop = function () {
searchLooper.stop();
return this;
};
/**
* create a source object that is a child of this courier
*
* @param {string} type - the type of Source to create
*/
self.createSource = function (type) {
switch (type) {
case 'doc':
return new DocSource();
case 'search':
return new SearchSource();
}
};
/**
* Abort all pending requests
* @return {[type]} [description]
*/
self.close = function () {
searchLooper.stop();
docDataLooper.stop();
_.invoke(requestQueue, 'abort');
if (requestQueue.length) {
throw new Error('Aborting all pending requests failed.');
}
};
// Listen for refreshInterval changes
$rootScope.$watchCollection('timefilter.refreshInterval', function () {
const refreshValue = _.get($rootScope, 'timefilter.refreshInterval.value');
const refreshPause = _.get($rootScope, 'timefilter.refreshInterval.pause');
if (_.isNumber(refreshValue) && !refreshPause) {
self.fetchInterval(refreshValue);
} else {
self.fetchInterval(0);
}
});
const onFatalDefer = Promise.defer();
onFatalDefer.promise.then(self.close);
Notifier.fatalCallbacks.push(onFatalDefer.resolve);
}
return new Courier();
});