Commit 7aa753a2 by Torkel Ödegaard

tech: migrating elasticsearch to typescript

parent 487c475a
......@@ -42,6 +42,7 @@ function (angular, _, coreModule, config) {
var pluginDef = dsConfig.meta;
System.import(pluginDef.module).then(function(plugin) {
console.log(plugin);
// check if its in cache now
if (self.datasources[name]) {
deferred.resolve(self.datasources[name]);
......
......@@ -2,7 +2,7 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/co
import _ from 'lodash';
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() {
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 {
versions: function() {
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',
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',
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,
dashboardId: 1,
parentVersion: 0,
restoredFrom: -1,
slug: 'history-dashboard',
version: 1,
created: '2017-02-22T17:06:37-08:00',
createdBy: 'admin',
message: '',
}];
},
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,
links: [],
restoredFrom: restoredFrom,
rows: [{
collapse: false,
height: '250px',
panels: [{
aliasColors: {},
bars: false,
datasource: null,
fill: 1,
id: 1,
links: [],
restoredFrom: restoredFrom,
rows: [{
collapse: false,
height: '250px',
panels: [{
aliasColors: {},
bars: 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': []
legend: {
avg: false,
current: false,
max: false,
min: false,
show: true,
total: false,
values: false
},
time: {
from: 'now-6h',
to: 'now'
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'
},
timepicker: {
refresh_intervals: [
'5s',
'10s',
'30s',
'1m',
'5m',
'15m',
'30m',
'1h',
'2h',
'1d',
],
time_options: [
'5m',
'15m',
'1h',
'6h',
'12h',
'24h',
'2d',
'7d',
'30d'
]
type: 'graph',
xaxis: {
mode: 'time',
name: null,
show: true,
values: []
},
timezone: 'utc',
title: 'History Dashboard',
version: version,
}
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: {
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,
version: version
};
timezone: 'utc',
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';
import helpers from 'test/specs/helpers';
import '../history/history_srv';
import {versions, restore} from 'test/mocks/history-mocks';
import {versions, restore} from './history_mocks';
describe('historySrv', function() {
var ctx = new helpers.ServiceTestContext();
......
declare var ElasticDatasource: any;
export {ElasticDatasource};
define([
'lodash',
'moment',
],
function (_, moment) {
'use strict';
function IndexPattern(pattern, interval) {
this.pattern = pattern;
this.interval = interval;
}
///<reference path="../../../headers/common.d.ts" />
IndexPattern.intervalMap = {
"Hourly": { startOf: 'hour', amount: 'hours'},
"Daily": { startOf: 'day', amount: 'days'},
"Weekly": { startOf: 'isoWeek', amount: 'weeks'},
"Monthly": { startOf: 'month', amount: 'months'},
"Yearly": { startOf: 'year', amount: 'years'},
};
import moment from 'moment';
const intervalMap = {
"Hourly": { startOf: 'hour', amount: 'hours'},
"Daily": { startOf: 'day', amount: 'days'},
"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) {
return moment.utc().format(this.pattern);
} else {
......@@ -26,12 +22,12 @@ function (_, moment) {
}
};
IndexPattern.prototype.getIndexList = function(from, to) {
getIndexList(from, to) {
if (!this.interval) {
return this.pattern;
}
var intervalInfo = IndexPattern.intervalMap[this.interval];
var intervalInfo = intervalMap[this.interval];
var start = moment(from).utc().startOf(intervalInfo.startOf);
var end = moment(to).utc().startOf(intervalInfo.startOf).valueOf();
var indexList = [];
......@@ -42,7 +38,6 @@ function (_, moment) {
}
return indexList;
};
}
}
return IndexPattern;
});
define([
'./query_def',
],
function (queryDef) {
'use strict';
import * as queryDef from './query_def';
function ElasticQueryBuilder(options) {
export class ElasticQueryBuilder {
timeField: string;
esVersion: number;
constructor(options) {
this.timeField = options.timeField;
this.esVersion = options.esVersion;
}
ElasticQueryBuilder.prototype.getRangeFilter = function() {
getRangeFilter() {
var filter = {};
filter[this.timeField] = {
gte: "$timeFrom",
......@@ -18,9 +18,9 @@ function (queryDef) {
};
return filter;
};
}
ElasticQueryBuilder.prototype.buildTermsAgg = function(aggDef, queryNode, target) {
buildTermsAgg(aggDef, queryNode, target) {
var metricRef, metric, y;
queryNode.terms = { "field": aggDef.field };
......@@ -57,10 +57,10 @@ function (queryDef) {
}
return queryNode;
};
}
ElasticQueryBuilder.prototype.getDateHistogramAgg = function(aggDef) {
var esAgg = {};
getDateHistogramAgg(aggDef) {
var esAgg: any = {};
var settings = aggDef.settings || {};
esAgg.interval = settings.interval;
esAgg.field = this.timeField;
......@@ -77,10 +77,10 @@ function (queryDef) {
}
return esAgg;
};
}
ElasticQueryBuilder.prototype.getHistogramAgg = function(aggDef) {
var esAgg = {};
getHistogramAgg(aggDef) {
var esAgg: any = {};
var settings = aggDef.settings || {};
esAgg.interval = settings.interval;
esAgg.field = aggDef.field;
......@@ -90,9 +90,9 @@ function (queryDef) {
esAgg.missing = settings.missing;
}
return esAgg;
};
}
ElasticQueryBuilder.prototype.getFiltersAgg = function(aggDef) {
getFiltersAgg(aggDef) {
var filterObj = {};
for (var i = 0; i < aggDef.settings.filters.length; i++) {
var query = aggDef.settings.filters[i].query;
......@@ -107,9 +107,9 @@ function (queryDef) {
}
return filterObj;
};
}
ElasticQueryBuilder.prototype.documentQuery = function(query, size) {
documentQuery(query, size) {
query.size = size;
query.sort = {};
query.sort[this.timeField] = {order: 'desc', unmapped_type: 'boolean'};
......@@ -126,9 +126,9 @@ function (queryDef) {
query.docvalue_fields = [this.timeField];
}
return query;
};
}
ElasticQueryBuilder.prototype.addAdhocFilters = function(query, adhocFilters) {
addAdhocFilters(query, adhocFilters) {
if (!adhocFilters) {
return;
}
......@@ -142,7 +142,7 @@ function (queryDef) {
queryCondition = {};
queryCondition[filter.key] = {query: filter.value};
switch(filter.operator){
switch (filter.operator){
case "=":
if (!query.query.bool.must) { query.query.bool.must = []; }
query.query.bool.must.push({match_phrase: queryCondition});
......@@ -169,7 +169,7 @@ function (queryDef) {
}
};
ElasticQueryBuilder.prototype.build = function(target, adhocFilters, queryString) {
build(target, adhocFilters?, queryString?) {
// make sure query has defaults;
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.dsType = 'elasticsearch';
......@@ -213,7 +213,7 @@ function (queryDef) {
var aggDef = target.bucketAggs[i];
var esAgg = {};
switch(aggDef.type) {
switch (aggDef.type) {
case 'date_histogram': {
esAgg["date_histogram"] = this.getDateHistogramAgg(aggDef);
break;
......@@ -273,10 +273,10 @@ function (queryDef) {
}
return query;
};
}
ElasticQueryBuilder.prototype.getTermsQuery = function(queryDef) {
var query = {
getTermsQuery(queryDef) {
var query: any = {
"size": 0,
"query": {
"bool": {
......@@ -311,7 +311,5 @@ function (queryDef) {
}
};
return query;
};
return ElasticQueryBuilder;
});
}
}
......@@ -2,7 +2,7 @@
import {describe, it, expect} from 'test/lib/common';
import moment from 'moment';
import IndexPattern from '../index_pattern';
import {IndexPattern} from '../index_pattern';
describe('IndexPattern', function() {
......@@ -19,7 +19,7 @@ describe('IndexPattern', function() {
describe('no interval', 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 to = new Date(2015, 5, 1, 12, 5 , 6);
expect(pattern.getIndexList(from, to)).to.eql('my-metrics');
......
import {describe, beforeEach, it, expect} from 'test/lib/common';
import ElasticQueryBuilder from '../query_builder';
import {ElasticQueryBuilder} from '../query_builder';
describe('ElasticQueryBuilder', function() {
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