[Maps] fix date labels (#63909)

* [Maps] fix date labels

* review feedback

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Nathan Reese 2020-04-20 10:50:54 -06:00 committed by GitHub
parent 7e3de56303
commit 635be640d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 8 deletions

View file

@ -64,7 +64,7 @@ function ensureGeometryType(type, expectedTypes) {
* @param {string} geoFieldType Geometry field type ["geo_point", "geo_shape"]
* @returns {number}
*/
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType, epochMillisFields) {
const features = [];
const tmpGeometriesAccumulator = [];
@ -80,6 +80,16 @@ export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
geoShapeToGeometry(properties[geoFieldName], tmpGeometriesAccumulator);
}
// There is a bug in Elasticsearch API where epoch_millis are returned as a string instead of a number
// https://github.com/elastic/elasticsearch/issues/50622
// Convert these field values to integers.
for (let i = 0; i < epochMillisFields.length; i++) {
const fieldName = epochMillisFields[i];
if (typeof properties[fieldName] === 'string') {
properties[fieldName] = parseInt(properties[fieldName]);
}
}
// don't include geometry field value in properties
delete properties[geoFieldName];

View file

@ -66,7 +66,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(2);
expect(geojson.features[0]).toEqual({
@ -94,7 +94,7 @@ describe('hitsToGeoJson', () => {
_source: {},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(1);
});
@ -111,7 +111,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.features.length).toBe(1);
const feature = geojson.features[0];
expect(feature.properties.myField).toBe(8);
@ -128,7 +128,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(2);
expect(geojson.features[0]).toEqual({
@ -159,6 +159,23 @@ describe('hitsToGeoJson', () => {
});
});
it('Should convert epoch_millis value from string to integer', () => {
const hits = [
{
_id: 'doc1',
_index: 'index1',
_source: {
[geoFieldName]: '20,100',
myDateField: '1587156257081',
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', ['myDateField']);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(1);
expect(geojson.features[0].properties.myDateField).toBe(1587156257081);
});
describe('dot in geoFieldName', () => {
const indexPatternMock = {
fields: {
@ -184,7 +201,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point', []);
expect(geojson.features[0].geometry).toEqual({
coordinates: [100, 20],
type: 'Point',
@ -199,7 +216,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point', []);
expect(geojson.features[0].geometry).toEqual({
coordinates: [100, 20],
type: 'Point',

View file

@ -387,11 +387,21 @@ export class ESSearchSource extends AbstractESSource {
});
return properties;
};
const epochMillisFields = searchFilters.fieldNames.filter(fieldName => {
const field = getField(indexPattern, fieldName);
return field.readFromDocValues && field.type === 'date';
});
let featureCollection;
try {
const geoField = await this._getGeoField();
featureCollection = hitsToGeoJson(hits, flattenHit, geoField.name, geoField.type);
featureCollection = hitsToGeoJson(
hits,
flattenHit,
geoField.name,
geoField.type,
epochMillisFields
);
} catch (error) {
throw new Error(
i18n.translate('xpack.maps.source.esSearch.convertToGeoJsonErrorMsg', {

View file

@ -279,6 +279,10 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}
getNumericalMbFeatureStateValue(value) {
if (typeof value === 'number') {
return value;
}
const valueAsFloat = parseFloat(value);
return isNaN(valueAsFloat) ? null : valueAsFloat;
}