Commit 7aa753a2 by Torkel Ödegaard

tech: migrating elasticsearch to typescript

parent 487c475a
...@@ -42,6 +42,7 @@ function (angular, _, coreModule, config) { ...@@ -42,6 +42,7 @@ function (angular, _, coreModule, config) {
var pluginDef = dsConfig.meta; var pluginDef = dsConfig.meta;
System.import(pluginDef.module).then(function(plugin) { System.import(pluginDef.module).then(function(plugin) {
console.log(plugin);
// check if its in cache now // check if its in cache now
if (self.datasources[name]) { if (self.datasources[name]) {
deferred.resolve(self.datasources[name]); deferred.resolve(self.datasources[name]);
......
...@@ -2,7 +2,7 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/co ...@@ -2,7 +2,7 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/co
import _ from 'lodash'; import _ from 'lodash';
import {HistoryListCtrl} from 'app/features/dashboard/history/history'; import {HistoryListCtrl} from 'app/features/dashboard/history/history';
import { versions, compare, restore } from 'test/mocks/history-mocks'; import {versions, compare, restore} from './history_mocks';
describe('HistoryListCtrl', function() { describe('HistoryListCtrl', function() {
var RESTORE_ID = 4; var RESTORE_ID = 4;
......
define([],
function() {
'use strict';
export function versions() {
return [{
id: 4,
dashboardId: 1,
parentVersion: 3,
restoredFrom: 0,
version: 4,
created: '2017-02-22T17:43:01-08:00',
createdBy: 'admin',
message: '',
},
{
id: 3,
dashboardId: 1,
parentVersion: 1,
restoredFrom: 1,
version: 3,
created: '2017-02-22T17:43:01-08:00',
createdBy: 'admin',
message: '',
},
{
id: 2,
dashboardId: 1,
parentVersion: 0,
restoredFrom: -1,
version: 2,
created: '2017-02-22T17:29:52-08:00',
createdBy: 'admin',
message: '',
},
{
id: 1,
dashboardId: 1,
parentVersion: 0,
restoredFrom: -1,
slug: 'history-dashboard',
version: 1,
created: '2017-02-22T17:06:37-08:00',
createdBy: 'admin',
message: '',
}];
}
export function compare(type) {
return type === 'basic' ? '<div></div>' : '<pre><code></code></pre>';
}
export function restore(version, restoredFrom?) {
return { return {
versions: function() { dashboard: {
return [{ meta: {
id: 4, type: 'db',
dashboardId: 1, canSave: true,
parentVersion: 3, canEdit: true,
restoredFrom: 0, canStar: true,
version: 4, slug: 'history-dashboard',
created: '2017-02-22T17:43:01-08:00', expires: '0001-01-01T00:00:00Z',
createdBy: 'admin', created: '2017-02-21T18:40:45-08:00',
message: '', updated: '2017-04-11T21:31:22.59219665-07:00',
}, updatedBy: 'admin',
{
id: 3,
dashboardId: 1,
parentVersion: 1,
restoredFrom: 1,
version: 3,
created: '2017-02-22T17:43:01-08:00',
createdBy: 'admin',
message: '',
},
{
id: 2,
dashboardId: 1,
parentVersion: 0,
restoredFrom: -1,
version: 2,
created: '2017-02-22T17:29:52-08:00',
createdBy: 'admin', createdBy: 'admin',
message: '', version: version,
}, },
{ dashboard: {
annotations: {
list: []
},
description: 'A random dashboard for implementing the history list',
editable: true,
gnetId: null,
graphTooltip: 0,
hideControls: false,
id: 1, id: 1,
dashboardId: 1, links: [],
parentVersion: 0, restoredFrom: restoredFrom,
restoredFrom: -1, rows: [{
slug: 'history-dashboard', collapse: false,
version: 1, height: '250px',
created: '2017-02-22T17:06:37-08:00', panels: [{
createdBy: 'admin', aliasColors: {},
message: '', bars: false,
}]; datasource: null,
}, fill: 1,
compare: function(type) {
return type === 'basic' ? '<div></div>' : '<pre><code></code></pre>';
},
restore: function(version, restoredFrom) {
return {
dashboard: {
meta: {
type: 'db',
canSave: true,
canEdit: true,
canStar: true,
slug: 'history-dashboard',
expires: '0001-01-01T00:00:00Z',
created: '2017-02-21T18:40:45-08:00',
updated: '2017-04-11T21:31:22.59219665-07:00',
updatedBy: 'admin',
createdBy: 'admin',
version: version,
},
dashboard: {
annotations: {
list: []
},
description: 'A random dashboard for implementing the history list',
editable: true,
gnetId: null,
graphTooltip: 0,
hideControls: false,
id: 1, id: 1,
links: [], legend: {
restoredFrom: restoredFrom, avg: false,
rows: [{ current: false,
collapse: false, max: false,
height: '250px', min: false,
panels: [{ show: true,
aliasColors: {}, total: false,
bars: false, values: false
datasource: null,
fill: 1,
id: 1,
legend: {
avg: false,
current: false,
max: false,
min: false,
show: true,
total: false,
values: false
},
lines: true,
linewidth: 1,
nullPointMode: "null",
percentage: false,
pointradius: 5,
points: false,
renderer: 'flot',
seriesOverrides: [],
span: 12,
stack: false,
steppedLine: false,
targets: [{}],
thresholds: [],
timeFrom: null,
timeShift: null,
title: 'Panel Title',
tooltip: {
shared: true,
sort: 0,
value_type: 'individual'
},
type: 'graph',
xaxis: {
mode: 'time',
name: null,
show: true,
values: []
},
yaxes: [{
format: 'short',
label: null,
logBase: 1,
max: null,
min: null,
show: true
}, {
format: 'short',
label: null,
logBase: 1,
max: null,
min: null,
show: true
}]
}],
repeat: null,
repeatIteration: null,
repeatRowId: null,
showTitle: false,
title: 'Dashboard Row',
titleSize: 'h6'
}
],
schemaVersion: 14,
style: 'dark',
tags: [
'development'
],
templating: {
'list': []
}, },
time: { lines: true,
from: 'now-6h', linewidth: 1,
to: 'now' nullPointMode: "null",
percentage: false,
pointradius: 5,
points: false,
renderer: 'flot',
seriesOverrides: [],
span: 12,
stack: false,
steppedLine: false,
targets: [{}],
thresholds: [],
timeFrom: null,
timeShift: null,
title: 'Panel Title',
tooltip: {
shared: true,
sort: 0,
value_type: 'individual'
}, },
timepicker: { type: 'graph',
refresh_intervals: [ xaxis: {
'5s', mode: 'time',
'10s', name: null,
'30s', show: true,
'1m', values: []
'5m',
'15m',
'30m',
'1h',
'2h',
'1d',
],
time_options: [
'5m',
'15m',
'1h',
'6h',
'12h',
'24h',
'2d',
'7d',
'30d'
]
}, },
timezone: 'utc', yaxes: [{
title: 'History Dashboard', format: 'short',
version: version, label: null,
} logBase: 1,
max: null,
min: null,
show: true
}, {
format: 'short',
label: null,
logBase: 1,
max: null,
min: null,
show: true
}]
}],
repeat: null,
repeatIteration: null,
repeatRowId: null,
showTitle: false,
title: 'Dashboard Row',
titleSize: 'h6'
}
],
schemaVersion: 14,
style: 'dark',
tags: [
'development'
],
templating: {
'list': []
},
time: {
from: 'now-6h',
to: 'now'
},
timepicker: {
refresh_intervals: [
'5s',
'10s',
'30s',
'1m',
'5m',
'15m',
'30m',
'1h',
'2h',
'1d',
],
time_options: [
'5m',
'15m',
'1h',
'6h',
'12h',
'24h',
'2d',
'7d',
'30d'
]
}, },
message: 'Dashboard restored to version ' + version, timezone: 'utc',
version: version title: 'History Dashboard',
}; version: version,
}
}, },
message: 'Dashboard restored to version ' + version,
version: version
}; };
}); }
...@@ -2,7 +2,7 @@ import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common'; ...@@ -2,7 +2,7 @@ import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
import helpers from 'test/specs/helpers'; import helpers from 'test/specs/helpers';
import '../history/history_srv'; import '../history/history_srv';
import {versions, restore} from 'test/mocks/history-mocks'; import {versions, restore} from './history_mocks';
describe('historySrv', function() { describe('historySrv', function() {
var ctx = new helpers.ServiceTestContext(); var ctx = new helpers.ServiceTestContext();
......
declare var ElasticDatasource: any;
export {ElasticDatasource};
define([ ///<reference path="../../../headers/common.d.ts" />
'lodash',
'moment',
],
function (_, moment) {
'use strict';
function IndexPattern(pattern, interval) {
this.pattern = pattern;
this.interval = interval;
}
IndexPattern.intervalMap = { import moment from 'moment';
"Hourly": { startOf: 'hour', amount: 'hours'},
"Daily": { startOf: 'day', amount: 'days'}, const intervalMap = {
"Weekly": { startOf: 'isoWeek', amount: 'weeks'}, "Hourly": { startOf: 'hour', amount: 'hours'},
"Monthly": { startOf: 'month', amount: 'months'}, "Daily": { startOf: 'day', amount: 'days'},
"Yearly": { startOf: 'year', amount: 'years'}, "Weekly": { startOf: 'isoWeek', amount: 'weeks'},
}; "Monthly": { startOf: 'month', amount: 'months'},
"Yearly": { startOf: 'year', amount: 'years'},
};
export class IndexPattern {
IndexPattern.prototype.getIndexForToday = function() { constructor(private pattern, private interval: string | null) { }
getIndexForToday() {
if (this.interval) { if (this.interval) {
return moment.utc().format(this.pattern); return moment.utc().format(this.pattern);
} else { } else {
...@@ -26,12 +22,12 @@ function (_, moment) { ...@@ -26,12 +22,12 @@ function (_, moment) {
} }
}; };
IndexPattern.prototype.getIndexList = function(from, to) { getIndexList(from, to) {
if (!this.interval) { if (!this.interval) {
return this.pattern; return this.pattern;
} }
var intervalInfo = IndexPattern.intervalMap[this.interval]; var intervalInfo = intervalMap[this.interval];
var start = moment(from).utc().startOf(intervalInfo.startOf); var start = moment(from).utc().startOf(intervalInfo.startOf);
var end = moment(to).utc().startOf(intervalInfo.startOf).valueOf(); var end = moment(to).utc().startOf(intervalInfo.startOf).valueOf();
var indexList = []; var indexList = [];
...@@ -42,7 +38,6 @@ function (_, moment) { ...@@ -42,7 +38,6 @@ function (_, moment) {
} }
return indexList; return indexList;
}; }
}
return IndexPattern;
});
define([ import * as queryDef from './query_def';
'./query_def',
],
function (queryDef) {
'use strict';
function ElasticQueryBuilder(options) { export class ElasticQueryBuilder {
timeField: string;
esVersion: number;
constructor(options) {
this.timeField = options.timeField; this.timeField = options.timeField;
this.esVersion = options.esVersion; this.esVersion = options.esVersion;
} }
ElasticQueryBuilder.prototype.getRangeFilter = function() { getRangeFilter() {
var filter = {}; var filter = {};
filter[this.timeField] = { filter[this.timeField] = {
gte: "$timeFrom", gte: "$timeFrom",
...@@ -18,9 +18,9 @@ function (queryDef) { ...@@ -18,9 +18,9 @@ function (queryDef) {
}; };
return filter; return filter;
}; }
ElasticQueryBuilder.prototype.buildTermsAgg = function(aggDef, queryNode, target) { buildTermsAgg(aggDef, queryNode, target) {
var metricRef, metric, y; var metricRef, metric, y;
queryNode.terms = { "field": aggDef.field }; queryNode.terms = { "field": aggDef.field };
...@@ -57,10 +57,10 @@ function (queryDef) { ...@@ -57,10 +57,10 @@ function (queryDef) {
} }
return queryNode; return queryNode;
}; }
ElasticQueryBuilder.prototype.getDateHistogramAgg = function(aggDef) { getDateHistogramAgg(aggDef) {
var esAgg = {}; var esAgg: any = {};
var settings = aggDef.settings || {}; var settings = aggDef.settings || {};
esAgg.interval = settings.interval; esAgg.interval = settings.interval;
esAgg.field = this.timeField; esAgg.field = this.timeField;
...@@ -77,10 +77,10 @@ function (queryDef) { ...@@ -77,10 +77,10 @@ function (queryDef) {
} }
return esAgg; return esAgg;
}; }
ElasticQueryBuilder.prototype.getHistogramAgg = function(aggDef) { getHistogramAgg(aggDef) {
var esAgg = {}; var esAgg: any = {};
var settings = aggDef.settings || {}; var settings = aggDef.settings || {};
esAgg.interval = settings.interval; esAgg.interval = settings.interval;
esAgg.field = aggDef.field; esAgg.field = aggDef.field;
...@@ -90,9 +90,9 @@ function (queryDef) { ...@@ -90,9 +90,9 @@ function (queryDef) {
esAgg.missing = settings.missing; esAgg.missing = settings.missing;
} }
return esAgg; return esAgg;
}; }
ElasticQueryBuilder.prototype.getFiltersAgg = function(aggDef) { getFiltersAgg(aggDef) {
var filterObj = {}; var filterObj = {};
for (var i = 0; i < aggDef.settings.filters.length; i++) { for (var i = 0; i < aggDef.settings.filters.length; i++) {
var query = aggDef.settings.filters[i].query; var query = aggDef.settings.filters[i].query;
...@@ -107,9 +107,9 @@ function (queryDef) { ...@@ -107,9 +107,9 @@ function (queryDef) {
} }
return filterObj; return filterObj;
}; }
ElasticQueryBuilder.prototype.documentQuery = function(query, size) { documentQuery(query, size) {
query.size = size; query.size = size;
query.sort = {}; query.sort = {};
query.sort[this.timeField] = {order: 'desc', unmapped_type: 'boolean'}; query.sort[this.timeField] = {order: 'desc', unmapped_type: 'boolean'};
...@@ -126,9 +126,9 @@ function (queryDef) { ...@@ -126,9 +126,9 @@ function (queryDef) {
query.docvalue_fields = [this.timeField]; query.docvalue_fields = [this.timeField];
} }
return query; return query;
}; }
ElasticQueryBuilder.prototype.addAdhocFilters = function(query, adhocFilters) { addAdhocFilters(query, adhocFilters) {
if (!adhocFilters) { if (!adhocFilters) {
return; return;
} }
...@@ -142,7 +142,7 @@ function (queryDef) { ...@@ -142,7 +142,7 @@ function (queryDef) {
queryCondition = {}; queryCondition = {};
queryCondition[filter.key] = {query: filter.value}; queryCondition[filter.key] = {query: filter.value};
switch(filter.operator){ switch (filter.operator){
case "=": case "=":
if (!query.query.bool.must) { query.query.bool.must = []; } if (!query.query.bool.must) { query.query.bool.must = []; }
query.query.bool.must.push({match_phrase: queryCondition}); query.query.bool.must.push({match_phrase: queryCondition});
...@@ -169,7 +169,7 @@ function (queryDef) { ...@@ -169,7 +169,7 @@ function (queryDef) {
} }
}; };
ElasticQueryBuilder.prototype.build = function(target, adhocFilters, queryString) { build(target, adhocFilters?, queryString?) {
// make sure query has defaults; // make sure query has defaults;
target.metrics = target.metrics || [{ type: 'count', id: '1' }]; target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.dsType = 'elasticsearch'; target.dsType = 'elasticsearch';
...@@ -213,7 +213,7 @@ function (queryDef) { ...@@ -213,7 +213,7 @@ function (queryDef) {
var aggDef = target.bucketAggs[i]; var aggDef = target.bucketAggs[i];
var esAgg = {}; var esAgg = {};
switch(aggDef.type) { switch (aggDef.type) {
case 'date_histogram': { case 'date_histogram': {
esAgg["date_histogram"] = this.getDateHistogramAgg(aggDef); esAgg["date_histogram"] = this.getDateHistogramAgg(aggDef);
break; break;
...@@ -273,10 +273,10 @@ function (queryDef) { ...@@ -273,10 +273,10 @@ function (queryDef) {
} }
return query; return query;
}; }
ElasticQueryBuilder.prototype.getTermsQuery = function(queryDef) { getTermsQuery(queryDef) {
var query = { var query: any = {
"size": 0, "size": 0,
"query": { "query": {
"bool": { "bool": {
...@@ -311,7 +311,5 @@ function (queryDef) { ...@@ -311,7 +311,5 @@ function (queryDef) {
} }
}; };
return query; return query;
}; }
}
return ElasticQueryBuilder;
});
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import {describe, it, expect} from 'test/lib/common'; import {describe, it, expect} from 'test/lib/common';
import moment from 'moment'; import moment from 'moment';
import IndexPattern from '../index_pattern'; import {IndexPattern} from '../index_pattern';
describe('IndexPattern', function() { describe('IndexPattern', function() {
...@@ -19,7 +19,7 @@ describe('IndexPattern', function() { ...@@ -19,7 +19,7 @@ describe('IndexPattern', function() {
describe('no interval', function() { describe('no interval', function() {
it('should return correct index', function() { it('should return correct index', function() {
var pattern = new IndexPattern('my-metrics'); var pattern = new IndexPattern('my-metrics', null);
var from = new Date(2015, 4, 30, 1, 2, 3); var from = new Date(2015, 4, 30, 1, 2, 3);
var to = new Date(2015, 5, 1, 12, 5 , 6); var to = new Date(2015, 5, 1, 12, 5 , 6);
expect(pattern.getIndexList(from, to)).to.eql('my-metrics'); expect(pattern.getIndexList(from, to)).to.eql('my-metrics');
......
import {describe, beforeEach, it, expect} from 'test/lib/common'; import {describe, beforeEach, it, expect} from 'test/lib/common';
import ElasticQueryBuilder from '../query_builder'; import {ElasticQueryBuilder} from '../query_builder';
describe('ElasticQueryBuilder', function() { describe('ElasticQueryBuilder', function() {
var builder; var builder;
......
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