Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripted_metric in Visualize #8808

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ui/public/agg_types/agg_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AggTypesParamTypesRegexProvider from 'ui/agg_types/param_types/regex';
import AggTypesParamTypesStringProvider from 'ui/agg_types/param_types/string';
import AggTypesParamTypesRawJsonProvider from 'ui/agg_types/param_types/raw_json';
import AggTypesParamTypesBaseProvider from 'ui/agg_types/param_types/base';
import AggTypesParamTypesScriptingLangProvider from 'ui/agg_types/param_types/script_lang';
export default function AggParamsFactory(Private) {


Expand All @@ -16,6 +17,7 @@ export default function AggParamsFactory(Private) {
regex: Private(AggTypesParamTypesRegexProvider),
string: Private(AggTypesParamTypesStringProvider),
json: Private(AggTypesParamTypesRawJsonProvider),
script_lang: Private(AggTypesParamTypesScriptingLangProvider),
_default: Private(AggTypesParamTypesBaseProvider)
};

Expand Down
23 changes: 23 additions & 0 deletions src/ui/public/agg_types/controls/script_lang.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="form-group" ng-controller="aggParam.controller as controller">
<label>{{ aggParam.name | label }}</label>

<!-- placeholder so that auto-select works right -->
<select
class="form-control"
ng-if="controller.loading"
disabled
required
ng-model="agg.params[aggParam.name]"
></select>

<!-- only render if the controller is ready or auto-select won't work -->
<select
class="form-control"
ng-if="!controller.loading"
required
ng-model="agg.params[aggParam.name]"
auto-select-if-only-one="controller.scriptingLangs"
ng-options="scriptingLang as scriptingLang for scriptingLang in controller.scriptingLangs"
></select>

</div>
6 changes: 6 additions & 0 deletions src/ui/public/agg_types/controls/text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="form-group">
<label>{{ aggParam.name | label }}</label>
<div>
<textarea type="text" ng-model="agg.params[aggParam.name]" class="form-control">
</div>
</div>
4 changes: 3 additions & 1 deletion src/ui/public/agg_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import AggTypesMetricsStdDeviationProvider from 'ui/agg_types/metrics/std_deviat
import AggTypesMetricsCardinalityProvider from 'ui/agg_types/metrics/cardinality';
import AggTypesMetricsPercentilesProvider from 'ui/agg_types/metrics/percentiles';
import AggTypesMetricsPercentileRanksProvider from 'ui/agg_types/metrics/percentile_ranks';
import AggTypesMetricsScriptedMetricProvider from 'ui/agg_types/metrics/scripted_metric';
import AggTypesBucketsDateHistogramProvider from 'ui/agg_types/buckets/date_histogram';
import AggTypesBucketsHistogramProvider from 'ui/agg_types/buckets/histogram';
import AggTypesBucketsRangeProvider from 'ui/agg_types/buckets/range';
Expand All @@ -32,7 +33,8 @@ export default function AggTypeService(Private) {
Private(AggTypesMetricsStdDeviationProvider),
Private(AggTypesMetricsCardinalityProvider),
Private(AggTypesMetricsPercentilesProvider),
Private(AggTypesMetricsPercentileRanksProvider)
Private(AggTypesMetricsPercentileRanksProvider),
Private(AggTypesMetricsScriptedMetricProvider)
],
buckets: [
Private(AggTypesBucketsDateHistogramProvider),
Expand Down
49 changes: 49 additions & 0 deletions src/ui/public/agg_types/metrics/scripted_metric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type';
import RegistryFieldFormatsProvider from 'ui/registry/field_formats';
import textHtml from 'ui/agg_types/controls/text.html';

export default function AggTypesMetricsScriptedMetricProvider(Private) {
let MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider);
let fieldFormats = Private(RegistryFieldFormatsProvider);

const buildScriptParam = function (scriptName) {
return {
name: scriptName,
type: 'string',
editor: textHtml,
write:function (aggConfig, output) {
const inlineScript = aggConfig.params[scriptName];
if (!inlineScript) {
return;
}

output.params[scriptName] = {
lang: aggConfig.params.lang,
inline: inlineScript
};
}
};
};

return new MetricAggType({
name: 'scripted_metric',
title: 'Scripted Metric',
makeLabel: function () {
return 'Scripted Metric';
},
getFormat: function () {
return fieldFormats.getDefaultInstance('number');
},
params: [
{
name: 'lang',
type: 'script_lang',
write: function () {}
},
buildScriptParam('init_script'),
buildScriptParam('map_script'),
buildScriptParam('combine_script'),
buildScriptParam('reduce_script')
]
});
};
31 changes: 31 additions & 0 deletions src/ui/public/agg_types/param_types/script_lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import _ from 'lodash';
import editorHtml from 'ui/agg_types/controls/script_lang.html';
import StringParamTypesBaseProvider from 'ui/agg_types/param_types/string';
import { GetScriptingLangsProvider } from 'ui/scripting_langs';

export default function ScriptingLangAggParamFactory(Private) {
const StringAggParam = Private(StringParamTypesBaseProvider);
const getScriptingLangs = Private(GetScriptingLangsProvider);

class ScriptingLangAggParam extends StringAggParam {
constructor(config) {
super(config);

this.default = 'painless';
this.editor = editorHtml;
this.controller = class ScriptingLangParamController {
constructor($scope) {
this.loading = true;

getScriptingLangs()
.then(scriptingLangs => {
this.loading = false;
this.scriptingLangs = scriptingLangs;
});
};
};
}
}

return ScriptingLangAggParam;
};
14 changes: 3 additions & 11 deletions src/ui/public/field_editor/field_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import RegistryFieldFormatsProvider from 'ui/registry/field_formats';
import IndexPatternsFieldProvider from 'ui/index_patterns/_field';
import uiModules from 'ui/modules';
import fieldEditorTemplate from 'ui/field_editor/field_editor.html';
import chrome from 'ui/chrome';
import IndexPatternsCastMappingTypeProvider from 'ui/index_patterns/_cast_mapping_type';
import { scriptedFields as docLinks } from '../documentation_links/documentation_links';
import './field_editor.less';
import { GetScriptingLangsProvider } from 'ui/scripting_langs';

uiModules
.get('kibana', ['colorpicker.module'])
.directive('fieldEditor', function (Private, $sce) {
let fieldFormats = Private(RegistryFieldFormatsProvider);
let Field = Private(IndexPatternsFieldProvider);
const getScriptingLangs = Private(GetScriptingLangsProvider);

const fieldTypesByLang = {
painless: ['number', 'string', 'date', 'boolean'],
Expand All @@ -31,7 +32,7 @@ uiModules
getField: '&field'
},
controllerAs: 'editor',
controller: function ($scope, Notifier, kbnUrl, $http, $q) {
controller: function ($scope, Notifier, kbnUrl, $http) {
let self = this;
let notify = new Notifier({ location: 'Field Editor' });

Expand Down Expand Up @@ -158,15 +159,6 @@ uiModules
else return fieldFormats.getDefaultType(self.field.type);
}

function getScriptingLangs() {
return $http.get(chrome.addBasePath('/api/kibana/scripts/languages'))
.then((res) => res.data)
.catch(() => {
notify.error('Error getting available scripting languages from Elasticsearch');
return [];
});
}

function initDefaultFormat() {
let def = Object.create(fieldFormats.getDefaultType(self.field.type));

Expand Down
16 changes: 16 additions & 0 deletions src/ui/public/scripting_langs/scripting_langs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import uiModules from 'ui/modules';
import chrome from 'ui/chrome';
import Notifier from 'ui/notify/notifier';

const notify = new Notifier({ location: 'Scripting Lang Service' });

export function GetScriptingLangsProvider($http) {
return () => {
return $http.get(chrome.addBasePath('/api/kibana/scripts/languages'))
.then((res) => res.data)
.catch(() => {
notify.error('Error getting available scripting languages from Elasticsearch');
return [];
});
};
}