forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 9
/
kbnIndex.js
113 lines (103 loc) · 3.45 KB
/
kbnIndex.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
define([
'angular',
'lodash',
'config',
'moment'
],
function (angular, _, config, moment) {
'use strict';
var module = angular.module('kibana.services');
module.service('kbnIndex', function($http, alertSrv) {
// returns a promise containing an array of all indices matching the index
// pattern that exist in a given range
this.indices = function(from,to,pattern,interval) {
var possible = [];
_.each(expand_range(fake_utc(from),fake_utc(to),interval),function(d){
possible.push(d.format(pattern));
});
return resolve_indices(possible).then(function(p) {
// an extra intersection
var indices = _.uniq(_.flatten(_.map(possible,function(possibleIndex) {
return _.intersection(possibleIndex.split(','),p);
})));
indices.reverse();
console.log(indices);
return indices;
});
};
// returns a promise containing an array of all indices in an elasticsearch
// cluster
function resolve_indices(indices) {
var something;
indices = _.map(indices, encodeURIComponent);
something = $http({
url: config.elasticsearch + "/" + indices.join(",") + "/_aliases?ignore_missing=true",
method: "GET"
});
return something.then(function(p) {
var indices = [];
_.each(p.data, function(v,k) {
indices.push(k);
// Also add the aliases. Could be expensive on systems with a lot of them
_.each(v.aliases, function(v, k) {
indices.push(k);
});
});
return indices;
}, function (p) {
if (p.status === 404) {
return [];
}
else if(p.status === 0) {
alertSrv.set('Error',"Could not contact Elasticsearch at "+config.elasticsearch+
". Please ensure that Elasticsearch is reachable from your system." ,'error');
} else {
alertSrv.set('Error',"Could not reach "+config.elasticsearch+"/_aliases. If you"+
" are using a proxy, ensure it is configured correctly",'error');
}
return [];
});
}
// this is stupid, but there is otherwise no good way to ensure that when
// I extract the date from an object that I get the UTC date. Stupid js.
// I die a little inside every time I call this function.
// Update: I just read this again. I died a little more inside.
// Update2: More death.
function fake_utc(date) {
date = moment(date).clone().toDate();
return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000));
}
// Create an array of date objects by a given interval
function expand_range(start, end, interval) {
if(_.contains(['hour','day','week','month','year'],interval)) {
var range;
start = moment(start).clone();
range = [];
while (start.isBefore(end)) {
range.push(start.clone());
switch (interval) {
case 'hour':
start.add('hours',1);
break;
case 'day':
start.add('days',1);
break;
case 'week':
start.add('weeks',1);
break;
case 'month':
start.add('months',1);
break;
case 'year':
start.add('years',1);
break;
}
}
range.push(moment(end).clone());
return range;
} else {
return false;
}
}
});
});