Skip to content

Commit

Permalink
updating tagcloud interpreter func arguments (#33773) (#34536)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Apr 4, 2019
1 parent 693f580 commit ea1c8d5
Show file tree
Hide file tree
Showing 27 changed files with 250 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ export const kibanaDatatable = () => ({
name: 'kibana_datatable',
from: {
datatable: context => {
context.columns.forEach(c => c.id = c.name);
return {
type: 'kibana_datatable',
rows: context.rows,
columns: context.columns,
columns: context.columns.map(column => {
return {
id: column.name,
name: column.name,
};
}),
};
},
pointseries: context => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { esaggs } from './esaggs';
import { kibana } from './kibana';
import { kibanaContext } from './kibana_context';
import { visualization } from './visualization';
import { visDimension } from './vis_dimension';

export const functions = [
clog, esaggs, kibana, kibanaContext, visualization
clog, esaggs, kibana, kibanaContext, visualization, visDimension,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';

export const visDimension = () => ({
name: 'visdimension',
help: i18n.translate('interpreter.function.visDimension.help', {
defaultMessage: 'Generates visConfig dimension object'
}),
type: 'vis_dimension',
context: {
types: [
'kibana_datatable'
],
},
args: {
accessor: {
types: ['string', 'number'],
aliases: ['_'],
help: i18n.translate('interpreter.function.visDimension.accessor.help', {
defaultMessage: 'Column in your dataset to use (either column index or column name)'
}),
},
format: {
types: ['string'],
default: 'string'
},
formatParams: {
types: ['string'],
default: '"{}"',
}
},
fn: (context, args) => {
const accessor = Number.isInteger(args.accessor) ?
args.accessor :
context.columns.find(c => c.id === args.accessor);
if (accessor === undefined) {
throw new Error(i18n.translate('interpreter.function.visDimension.error.accessor', {
defaultMessage: 'Column name provided is invalid'
}));
}

return {
type: 'vis_dimension',
accessor: accessor,
format: {
id: args.format,
params: JSON.parse(args.formatParams),
}
};
},
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 52 additions & 4 deletions src/legacy/core_plugins/tagcloud/public/tag_cloud_fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,61 @@ export const tagcloud = () => ({
defaultMessage: 'Tagcloud visualization'
}),
args: {
visConfig: {
types: ['string', 'null'],
default: '"{}"',
scale: {
types: ['string'],
default: 'linear',
options: ['linear', 'log', 'square root'],
help: i18n.translate('tagCloud.function.scale.help', {
defaultMessage: 'Scale to determine font size of a word'
}),
},
orientation: {
types: ['string'],
default: 'single',
options: ['single', 'right angled', 'multiple'],
help: i18n.translate('tagCloud.function.orientation.help', {
defaultMessage: 'Orientation of words inside tagcloud'
}),
},
minFontSize: {
types: ['number'],
default: 18,
},
maxFontSize: {
types: ['number'],
default: 72
},
showLabel: {
types: ['boolean'],
default: true,
},
metric: {
types: ['vis_dimension'],
help: i18n.translate('tagCloud.function.metric.help', {
defaultMessage: 'metric dimension configuration'
}),
required: true,
},
bucket: {
types: ['vis_dimension'],
help: i18n.translate('tagCloud.function.bucket.help', {
defaultMessage: 'bucket dimension configuration'
}),
},
},
fn(context, args) {
const visConfig = JSON.parse(args.visConfig);
const visConfig = {
scale: args.scale,
orientation: args.orientation,
minFontSize: args.minFontSize,
maxFontSize: args.maxFontSize,
showLabel: args.showLabel,
metric: args.metric,
};

if (args.bucket !== undefined) {
visConfig.bucket = args.bucket;
}

return {
type: 'render',
Expand Down
11 changes: 2 additions & 9 deletions src/legacy/core_plugins/tagcloud/public/tag_cloud_fn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,11 @@ describe('interpreter/functions#tagcloud', () => {
minFontSize: 18,
maxFontSize: 72,
showLabel: true,
metric: {
accessor: 0,
format: {
id: 'number',
},
params: {},
aggType: 'count',
},
metric: { accessor: 0, format: { id: 'number' } },
};

it('returns an object with the correct structure', () => {
const actual = fn(context, { visConfig: JSON.stringify(visConfig) });
const actual = fn(context, visConfig);
expect(actual).toMatchSnapshot();
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ describe('visualize loader pipeline helpers: build pipeline', () => {
const params = {};

it('without buckets', () => {
const schemas = { metric: [0] };
const schemas = { metric: [{ accessor: 0 }] };
const actual = buildPipelineVisFunction.tagcloud({ params }, schemas);
expect(actual).toMatchSnapshot();
});

it('with buckets', () => {
const schemas = {
metric: [0],
segment: [1, 2]
metric: [{ accessor: 0 }],
segment: [{ accessor: 1 }]
};
const actual = buildPipelineVisFunction.tagcloud({ params }, schemas);
expect(actual).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,35 @@ export const buildPipelineVisFunction: BuildPipelineVisFunction = {
return `kibana_metric ${prepareJson('visConfig', visConfig)}`;
},
tagcloud: (visState, schemas) => {
const visConfig = {
...visState.params,
...buildVisConfig.tagcloud(schemas),
};
return `tagcloud ${prepareJson('visConfig', visConfig)}`;
const { scale, orientation, minFontSize, maxFontSize, showLabel } = visState.params;
const { metric, bucket } = buildVisConfig.tagcloud(schemas);
let expr = `tagcloud metric={visdimension ${metric.accessor}} `;

if (scale) {
expr += `scale='${scale}' `;
}
if (orientation) {
expr += `orientation='${orientation}' `;
}
if (minFontSize) {
expr += `minFontSize=${minFontSize} `;
}
if (maxFontSize) {
expr += `maxFontSize=${maxFontSize} `;
}
if (showLabel) {
expr += `showLabel=${showLabel} `;
}

if (bucket) {
expr += ` bucket={visdimension ${bucket.accessor} `;
if (bucket.format) {
expr += `format=${bucket.format.id} `;
expr += prepareJson('formatParams', bucket.format.params);
}
expr += '} ';
}
return expr;
},
region_map: (visState, schemas) => {
const visConfig = {
Expand Down
8 changes: 7 additions & 1 deletion test/interpreter_functional/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ To run these tests during development you can use the following commands:
```
# Start the test server (can continue running)
node scripts/functional_tests_server.js --config test/interpreter_functional/config.js
# Start a test run
node scripts/functional_test_runner.js --config test/interpreter_functional/config.js
```

# Writing tests

Look into test_suites/run_pipeline/basic.js for examples
Look into test_suites/run_pipeline/basic.js for examples

to update baseline screenshots and snapshots run with:
```
node scripts/functional_test_runner.js --config test/interpreter_functional/config.js --updateBaselines
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"as":"visualization","type":"render","value":{"params":{"listenOnChange":true},"visConfig":{"bucket":{"accessor":1,"format":{"id":"string","params":{}},"type":"vis_dimension"},"maxFontSize":72,"metric":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"minFontSize":18,"orientation":"single","scale":"linear","showLabel":true},"visData":{"columns":[{"id":"col-0-2","name":"response.raw: Descending"},{"id":"col-1-1","name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"kibana_datatable"},"visType":"tagcloud"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"as":"visualization","type":"render","value":{"params":{"listenOnChange":true},"visConfig":{"bucket":{"accessor":1,"format":{"id":"string","params":{}},"type":"vis_dimension"},"maxFontSize":40,"metric":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"minFontSize":20,"orientation":"single","scale":"linear","showLabel":true},"visData":{"columns":[{"id":"col-0-2","name":"response.raw: Descending"},{"id":"col-1-1","name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"kibana_datatable"},"visType":"tagcloud"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"error":{"message":"[tagcloud] > [vis_dimension] > Objects must have a type property","stack":"Error: [vis_dimension] > Objects must have a type property\n at getType (http://localhost:5620/bundles/commons.bundle.js:15876:27)\n at cast (http://localhost:5620/bundles/commons.bundle.js:15683:46)\n at _callee3$ (http://localhost:5620/bundles/commons.bundle.js:31552:35)\n at tryCatch (webpack://%5Bname%5D/./node_modules/regenerator-runtime/runtime.js?:62:40)\n at Generator.invoke [as _invoke] (webpack://%5Bname%5D/./node_modules/regenerator-runtime/runtime.js?:288:22)\n at Generator.prototype.(anonymous function) [as next] (webpack://%5Bname%5D/./node_modules/regenerator-runtime/runtime.js?:114:21)\n at asyncGeneratorStep (http://localhost:5620/bundles/commons.bundle.js:31397:103)\n at _next (http://localhost:5620/bundles/commons.bundle.js:31399:194)\n at http://localhost:5620/bundles/commons.bundle.js:31399:364\n at new Promise (<anonymous>)"},"type":"error"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"as":"visualization","type":"render","value":{"params":{"listenOnChange":true},"visConfig":{"maxFontSize":72,"metric":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"minFontSize":18,"orientation":"single","scale":"linear","showLabel":true},"visData":{"columns":[{"id":"col-0-2","name":"response.raw: Descending"},{"id":"col-1-1","name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"kibana_datatable"},"visType":"tagcloud"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"as":"visualization","type":"render","value":{"params":{"listenOnChange":true},"visConfig":{"bucket":{"accessor":1,"format":{"id":"string","params":{}},"type":"vis_dimension"},"maxFontSize":72,"metric":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"minFontSize":18,"orientation":"multiple","scale":"log","showLabel":true},"visData":{"columns":[{"id":"col-0-2","name":"response.raw: Descending"},{"id":"col-1-1","name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"kibana_datatable"},"visType":"tagcloud"}}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export default function ({ getService, getPageObjects, loadTestFile }) {
});

loadTestFile(require.resolve('./basic'));
loadTestFile(require.resolve('./tag_cloud'));
});
}
72 changes: 72 additions & 0 deletions test/interpreter_functional/test_suites/run_pipeline/tag_cloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { expectExpressionProvider } from './helpers';

// this file showcases how to use testing utilities defined in helpers.js together with the kbn_tp_run_pipeline
// test plugin to write autmated tests for interprete
export default function ({ getService, updateBaselines }) {

let expectExpression;
describe('tag cloud pipeline expression tests', () => {
before(() => {
expectExpression = expectExpressionProvider({ getService, updateBaselines });
});

// we should not use this for tests like the ones below. this should be unit tested.
// - tests against a single function could easily be written as unit tests (and should be)
describe('correctly renders tagcloud', () => {
let dataContext;
before(async () => {
const expression = `kibana | kibana_context | esaggs index='logstash-*' aggConfigs='[
{"id":"1","enabled":true,"type":"count","schema":"metric","params":{}},
{"id":"2","enabled":true,"type":"terms","schema":"segment","params":
{"field":"response.raw","size":4,"order":"desc","orderBy":"1"}
}]'`;
// we execute the part of expression that fetches the data and store its response
dataContext = await expectExpression('partial_tagcloud_test', expression).getResponse();
});

it.skip('with invalid data', async () => {
const expression = 'tagcloud metric={visdimension 0}';
await (await expectExpression('tagcloud_invalid_data', expression).toMatchSnapshot()).toMatchScreenshot();
});

it('with just metric data', async () => {
const expression = 'tagcloud metric={visdimension 0}';
await (await expectExpression('tagcloud_metric_data', expression, dataContext).toMatchSnapshot()).toMatchScreenshot();
});

it('with metric and bucket data', async () => {
const expression = 'tagcloud metric={visdimension 0} bucket={visdimension 1}';
await (await expectExpression('tagcloud_all_data', expression, dataContext).toMatchSnapshot()).toMatchScreenshot();
});

it('with font size options', async () => {
const expression = 'tagcloud metric={visdimension 0} bucket={visdimension 1} minFontSize=20 maxFontSize=40';
await (await expectExpression('tagcloud_fontsize', expression, dataContext).toMatchSnapshot()).toMatchScreenshot();
});

it('with scale and orientation options', async () => {
const expression = 'tagcloud metric={visdimension 0} bucket={visdimension 1} scale="log" orientation="multiple"';
await (await expectExpression('tagcloud_options', expression, dataContext).toMatchSnapshot()).toMatchScreenshot();
});
});
});
}

0 comments on commit ea1c8d5

Please sign in to comment.