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

[Maps] Add fields (test) #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import { AbstractField } from './field';
import { TooltipProperty } from '../tooltips/tooltip_property';

//todo: rename to ESMFileField
export class EMSRegionLayerField extends AbstractField {
static type = 'EMS_REGION_LAYER';

async getLabel() {
const emsFileLayer = await this._source.getEMSFileLayer();
const emsFields = emsFileLayer.getFieldsInLanguage();
// Map EMS field name to language specific label
const emsField = emsFields.find(field => field.name === this.getName());
return emsField ? emsField.description : this.getName();
}

async createTooltipProperty(value) {
const label = await this.getLabel();
return new TooltipProperty(this.getName(), label, value);
}
}
64 changes: 64 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import { AbstractField } from './field';
import { METRIC_TYPE } from '../../../common/constants';

export class ESAggMetricField extends AbstractField {

static type = 'ES_AGG';

constructor({ label, source, aggType, esDocField }) {
super({ source });
this._label = label;
this._aggType = aggType;
this._esDocField = esDocField;
}

getName() {
return this._source.formatMetricKey(this.getAggType(), this.getESDocFieldName());
}

async getLabel() {
return this.getPropertyLabel();
}

getAggType() {
return this._aggType;
}

isValid() {
return (this.getAggType() === METRIC_TYPE.COUNT) ? true : !!this._esDocField;
}

getPropertyLabel() {
return this._label ? this._label : this._source.formatMetricLabel(this.getAggType(), this.getESDocFieldName());
}

getESDocFieldName() {
return this._esDocField ? this._esDocField.getName() : '';
}

getRequestDescription() {
return this.getAggType() !== METRIC_TYPE.COUNT ? `${this.getAggType()} ${this.getESDocFieldName()}` : METRIC_TYPE.COUNT;
}

makeMetricAggConfig() {
const metricAggConfig = {
id: this.getName(),
enabled: true,
type: this.getAggType(),
schema: 'metric',
params: {}
};
if (this.getAggType() !== METRIC_TYPE.COUNT) {
metricAggConfig.params = { field: this.getESDocFieldName() };
}
return metricAggConfig;
}

}
30 changes: 30 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/fields/es_doc_field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import { AbstractField } from './field';
import { ESTooltipProperty } from '../tooltips/es_tooltip_property';

export class ESDocField extends AbstractField {

static type = 'ES_DOC';

async _getField() {
const indexPattern = await this._source.getIndexPattern();
return indexPattern.fields.find((field) => field.name === this._fieldName);
}

async createTooltipProperty(value) {
const indexPattern = await this._source.getIndexPattern();
return new ESTooltipProperty(this.getName(), this.getName(), value, indexPattern);
}

async getType() {
const field = await this._getField();
return field.type;
}

}
52 changes: 52 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/fields/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


export class AbstractField {

//todo consider removing
//double check if we're actually using this consistently
static FIELD_TYPE = {
STRING: 'string',
NUMBER: 'number',
DATE: 'date'
};

constructor({ fieldName, source }) {
this._fieldName = fieldName;
this._source = source;
}

getName() {
return this._fieldName;
}

isValid() {
return !!this._fieldName;
}

async getType() {
return AbstractField.FIELD_TYPE.STRING;
}

async getLabel() {
return this._fieldName;
}

async createTooltipProperty() {
throw new Error('must implement Field#createTooltipProperty');
}

}









Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import { AbstractField } from './field';

//todo: need to be implemented
export class KibanaRegionField extends AbstractField {

static type = 'KIBANA_REGION';

async getType() {
return AbstractField.FIELD_TYPE.STRING;
}

}
10 changes: 6 additions & 4 deletions x-pack/legacy/plugins/maps/public/layers/heatmap_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import _ from 'lodash';
import { AbstractLayer } from './layer';
import { VectorLayer } from './vector_layer';
import { HeatmapStyle } from './styles/heatmap_style';
import { HeatmapStyle } from './styles/heatmap/heatmap_style';
import { EMPTY_FEATURE_COLLECTION, LAYER_TYPE } from '../../common/constants';

const SCALED_PROPERTY_NAME = '__kbn_heatmap_weight__';//unique name to store scaled value for weighting
Expand All @@ -28,12 +27,14 @@ export class HeatmapLayer extends VectorLayer {
if (!style) {
const defaultStyle = HeatmapStyle.createDescriptor();
this._style = new HeatmapStyle(defaultStyle);
} else {
this._style = style;
}
}

_getPropKeyOfSelectedMetric() {
const metricfields = this._source.getMetricFields();
return metricfields[0].propertyKey;
return metricfields[0].getName();
}

_getHeatmapLayerId() {
Expand Down Expand Up @@ -102,7 +103,8 @@ export class HeatmapLayer extends VectorLayer {
}

getLegendDetails() {
const label = _.get(this._source.getMetricFields(), '[0].propertyLabel', '');
const metricFields = this._source.getMetricFields();
const label = metricFields[0].getPropertyLabel();
return this._style.getLegendDetails(label);
}
}
29 changes: 14 additions & 15 deletions x-pack/legacy/plugins/maps/public/layers/joins/inner_join.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@


import { ESTermSource } from '../sources/es_term_source';
import { VectorStyle } from '../styles/vector_style';
import { getComputedFieldNamePrefix } from '../styles/vector/style_util';

export class InnerJoin {

constructor(joinDescriptor, inspectorAdapters) {
constructor(joinDescriptor, leftSource) {
this._descriptor = joinDescriptor;
const inspectorAdapters = leftSource.getInspectorAdapters();
this._rightSource = new ESTermSource(joinDescriptor.right, inspectorAdapters);
this._leftField = this._descriptor.leftField ? leftSource.createField({ fieldName: joinDescriptor.leftField }) : null;
}

destroy() {
this._rightSource.destroy();
}

hasCompleteConfig() {
if (this._descriptor.leftField && this._rightSource) {
if (this._leftField && this._rightSource) {
return this._rightSource.hasCompleteConfig();
}

return false;
}

getRightMetricFields() {
return this._rightSource.getMetricFields();
}

getJoinFields() {
return this.getRightMetricFields().map(({ propertyKey: name, propertyLabel: label }) => {
return { label, name };
return this._rightSource.getMetricFields().map(esAggMetricField => {
return { label: esAggMetricField.getPropertyLabel(), name: esAggMetricField.getName() };
});
}

Expand All @@ -44,18 +42,19 @@ export class InnerJoin {
return `join_source_${this._rightSource.getId()}`;
}

getLeftFieldName() {
return this._descriptor.leftField;
getLeftField() {
return this._leftField;
}

joinPropertiesToFeature(feature, propertiesMap, rightMetricFields) {
joinPropertiesToFeature(feature, propertiesMap) {
const rightMetricFields = this._rightSource.getMetricFields();
// delete feature properties added by previous join
for (let j = 0; j < rightMetricFields.length; j++) {
const { propertyKey: metricPropertyKey } = rightMetricFields[j];
const metricPropertyKey = rightMetricFields[j].getName();
delete feature.properties[metricPropertyKey];

// delete all dynamic properties for metric field
const stylePropertyPrefix = VectorStyle.getComputedFieldNamePrefix(metricPropertyKey);
const stylePropertyPrefix = getComputedFieldNamePrefix(metricPropertyKey);
Object.keys(feature.properties).forEach(featurePropertyKey => {
if (featurePropertyKey.length >= stylePropertyPrefix.length &&
featurePropertyKey.substring(0, stylePropertyPrefix.length) === stylePropertyPrefix) {
Expand All @@ -64,7 +63,7 @@ export class InnerJoin {
});
}

const joinKey = feature.properties[this._descriptor.leftField];
const joinKey = feature.properties[this._leftField.getName()];
const coercedKey = typeof joinKey === 'undefined' || joinKey === null ? null : joinKey.toString();
if (propertiesMap && coercedKey !== null && propertiesMap.has(coercedKey)) {
Object.assign(feature.properties, propertiesMap.get(coercedKey));
Expand Down
19 changes: 16 additions & 3 deletions x-pack/legacy/plugins/maps/public/layers/joins/inner_join.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ const rightSource = {
indexPatternId: '90943e30-9a47-11e8-b64d-95841ca0b247',
indexPatternTitle: 'kibana_sample_data_logs',
term: 'geo.dest',
metrics: [{ type: 'count' }]
};

const mockSource = {
getInspectorAdapters() {
},
createField(name) {
return {
getName() {
return name;
}
};
}
};

const leftJoin = new InnerJoin({
leftField: 'iso2',
right: rightSource
});
}, mockSource);
const COUNT_PROPERTY_NAME = '__kbnjoin__count_groupby_kibana_sample_data_logs.geo.dest';

describe('joinPropertiesToFeature', () => {
Expand Down Expand Up @@ -76,7 +89,7 @@ describe('joinPropertiesToFeature', () => {
const leftJoin = new InnerJoin({
leftField: 'zipcode',
right: rightSource
});
}, mockSource);

const feature = {
properties: {
Expand Down Expand Up @@ -118,7 +131,7 @@ describe('joinPropertiesToFeature', () => {
const leftJoin = new InnerJoin({
leftField: 'code',
right: rightSource
});
}, mockSource);

const feature = {
properties: {
Expand Down
Loading