-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Charlot
authored
Apr 24, 2019
1 parent
a354db0
commit ada7cdb
Showing
16 changed files
with
321 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,56 @@ | ||
import { capitalize, map } from 'lodash'; | ||
import * as magprov from 'ui/agg_types/metrics/metric_agg_type'; | ||
import * as ffprov from 'ui/registry/field_formats'; | ||
import { MetricAggType } from 'ui/agg_types/metrics/metric_agg_type'; | ||
import { fieldFormats } from 'ui/registry/field_formats'; | ||
import formulaEditor from './formula.html'; | ||
import formatterEditor from './formatter.html'; | ||
|
||
export function AggTypesMetricsFormulaProvider(Private) { | ||
const MetricAggType = magprov.MetricAggType || Private(magprov.AggTypesMetricsMetricAggTypeProvider); | ||
const fieldFormats = ffprov.fieldFormats || Private(ffprov.RegistryFieldFormatsProvider); | ||
const defaultValue = null; | ||
const formatters = map(['number', 'percent', 'boolean', 'bytes', 'numeral'], f => { | ||
return { id: f, title: capitalize(f) }; | ||
}); | ||
const formatters = map(['number', 'percent', 'boolean', 'bytes', 'numeral'], f => { | ||
return { id: f, title: capitalize(f) }; | ||
}); | ||
|
||
return new MetricAggType({ | ||
name: 'datasweet_formula', | ||
title: 'Datasweet Formula', | ||
subtype: 'Calculated Metrics', | ||
hasNoDsl: true, | ||
makeLabel: function (aggConfig) { | ||
return 'Formula ' + aggConfig.id; | ||
export const formulaMetricAgg = new MetricAggType({ | ||
name: 'datasweet_formula', | ||
title: 'Datasweet Formula', | ||
subtype: 'Calculated Metrics', | ||
hasNoDsl: true, | ||
makeLabel: function (aggConfig) { | ||
return 'Formula ' + aggConfig.id; | ||
}, | ||
params: [ | ||
{ | ||
name: 'formula', | ||
editor: formulaEditor, | ||
}, | ||
params: [ | ||
{ | ||
name: 'formula', | ||
editor: formulaEditor, | ||
}, | ||
{ | ||
name: 'formatter', | ||
editor: formatterEditor, | ||
getFormatters: function () { | ||
return formatters; | ||
} | ||
}, | ||
{ | ||
name: 'numeralFormat' | ||
{ | ||
name: 'formatter', | ||
editor: formatterEditor, | ||
getFormatters: function () { | ||
return formatters; | ||
} | ||
], | ||
getFormat: function (agg) { | ||
const formatterId = agg.params.formatter; | ||
if (!formatterId) { | ||
return fieldFormats.getDefaultInstance('number'); | ||
} | ||
|
||
if (formatterId === 'numeral') { | ||
const format = agg.params.numeralFormat; | ||
if (!format) { | ||
return fieldFormats.getDefaultInstance('number'); | ||
} | ||
}, | ||
{ | ||
name: 'numeralFormat' | ||
} | ||
], | ||
getFormat: function (agg) { | ||
const formatterId = agg.params.formatter; | ||
if (!formatterId) { | ||
return fieldFormats.getDefaultInstance('number'); | ||
} | ||
|
||
const FieldFormat = fieldFormats.getType('number'); | ||
const f = new FieldFormat({ pattern: format }); | ||
return f; | ||
if (formatterId === 'numeral') { | ||
const format = agg.params.numeralFormat; | ||
if (!format) { | ||
return fieldFormats.getDefaultInstance('number'); | ||
} | ||
|
||
return fieldFormats.getInstance(formatterId); | ||
}, | ||
getValue: function () { | ||
return defaultValue; | ||
const FieldFormat = fieldFormats.getType('number'); | ||
const f = new FieldFormat({ pattern: format }); | ||
return f; | ||
} | ||
}); | ||
} | ||
return fieldFormats.getInstance(formatterId); | ||
}, | ||
getValue: function () { | ||
return null; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import * as prov from 'ui/agg_types'; | ||
import { AggTypesMetricsFormulaProvider } from 'plugins/datasweet_formula/agg_types/formula'; | ||
import { aggTypes } from 'ui/agg_types'; | ||
import { formulaMetricAgg } from 'plugins/datasweet_formula/agg_types/formula'; | ||
|
||
export function decorateAggTypes(Private) { | ||
const AggTypes = prov.aggTypes || Private(prov.AggTypesIndexProvider); | ||
const AggFormula = Private(AggTypesMetricsFormulaProvider); | ||
AggFormula.type = 'metrics'; | ||
AggTypes.push(AggFormula); | ||
export function decorateAggTypes() { | ||
formulaMetricAgg.type = 'metrics'; | ||
aggTypes.push(formulaMetricAgg); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,29 @@ | ||
import expect from 'expect.js'; | ||
import ngMock from 'ng_mock'; | ||
import 'ui/private'; | ||
import { FormulaParserProvider } from '../formula_parser'; | ||
import { formulaParser } from '../formula_parser'; | ||
|
||
describe('Formula Parser', () => { | ||
let FormulaParser; | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
beforeEach(ngMock.inject(function (Private) { | ||
FormulaParser = Private(FormulaParserProvider); | ||
})); | ||
|
||
|
||
it('unary operators must be wrapped', () => { | ||
const parser = new FormulaParser(); | ||
expect(parser.evaluate('sqrt(9)')).to.equal(3); | ||
expect(parser.evaluate('sqrt(agg1)', { 'agg1': [4,9,16]})).to.eql([2,3,4]); | ||
expect(formulaParser.evaluate('sqrt(9)')).to.equal(3); | ||
expect(formulaParser.evaluate('sqrt(agg1)', { 'agg1': [4,9,16]})).to.eql([2,3,4]); | ||
}); | ||
|
||
it('binary operators must be wrapped', () => { | ||
const parser = new FormulaParser(); | ||
const agg1 = [3,4,6]; | ||
const agg2 = [10,2,7]; | ||
const agg3 = [8,2]; | ||
|
||
expect(parser.evaluate('10 - 2')).to.equal(8); | ||
expect(parser.evaluate('agg1 - 2', { agg1 })).to.eql([1,2,4]); | ||
expect(parser.evaluate('10 - agg1', { agg1 })).to.eql([7,6,4]); | ||
expect(parser.evaluate('agg1 - agg2', { agg1, agg2 })).to.eql([-7,2,-1]); | ||
expect(parser.evaluate('agg1 - agg3', { agg1, agg3 })).to.eql([-5,2,6]); | ||
expect(parser.evaluate('agg3 - agg1', { agg1, agg3 })).to.eql([5,-2,-6]); | ||
expect(formulaParser.evaluate('10 - 2')).to.equal(8); | ||
expect(formulaParser.evaluate('agg1 - 2', { agg1 })).to.eql([1,2,4]); | ||
expect(formulaParser.evaluate('10 - agg1', { agg1 })).to.eql([7,6,4]); | ||
expect(formulaParser.evaluate('agg1 - agg2', { agg1, agg2 })).to.eql([-7,2,-1]); | ||
expect(formulaParser.evaluate('agg1 - agg3', { agg1, agg3 })).to.eql([-5,2,6]); | ||
expect(formulaParser.evaluate('agg3 - agg1', { agg1, agg3 })).to.eql([5,-2,-6]); | ||
}); | ||
|
||
it('ternary ops condition must be deactivated', () => { | ||
const parser = new FormulaParser(); | ||
expect(function () { | ||
parser.evaluate('true ? 1 : 0'); | ||
formulaParser.evaluate('true ? 1 : 0'); | ||
}).to.throwException(/\?/); | ||
}); | ||
}); |
Oops, something went wrong.