Elasticsearch: Fix using of datetime format for time field (#64676)

* Elasticsearch: Fix using of datetime format for time field

* Use convertFieldType to convert time field for better performance
This commit is contained in:
Ivana Huckova 2023-03-15 10:47:48 +01:00 committed by GitHub
parent d2fa019d78
commit 96fc0b814e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -1301,6 +1301,7 @@ describe('ElasticResponse', () => {
refId: 'A',
metrics: [{ type: 'raw_data', id: '1' }],
bucketAggs: [],
timeField: '@timestamp',
},
];
@ -1317,7 +1318,7 @@ describe('ElasticResponse', () => {
_id: '1',
_type: '_doc',
_index: 'index',
_source: { sourceProp: 'asd' },
_source: { sourceProp: 'asd', '@timestamp': '2019-01-01T00:00:00Z' },
},
],
},
@ -1333,6 +1334,12 @@ describe('ElasticResponse', () => {
expect(field.config.filterable).toBe(true);
}
});
it('should have time field values in DateTime format', () => {
const timeField = result.data[0].fields.find((field) => field.name === '@timestamp');
expect(timeField).toBeDefined();
expect(timeField?.values.get(0)).toBe(1546300800000);
});
});
describe('simple logs query and count', () => {

View File

@ -8,6 +8,7 @@ import {
MutableDataFrame,
PreferredVisualisationType,
} from '@grafana/data';
import { convertFieldType } from '@grafana/data/src/transformations/transformers/convertFieldType';
import TableModel from 'app/core/TableModel';
import flatten from 'app/core/utils/flatten';
@ -601,6 +602,14 @@ export class ElasticResponse {
}
}
for (let frame of dataFrame) {
for (let field of frame.fields) {
if (field.type === FieldType.time && typeof field.values.get(0) !== 'number') {
field.values = convertFieldType(field, { destinationType: FieldType.time }).values;
}
}
}
return { data: dataFrame };
}