diff --git a/public/app/plugins/datasource/loki/result_transformer.test.ts b/public/app/plugins/datasource/loki/result_transformer.test.ts index d4f0d557cd4..127e9c3fceb 100644 --- a/public/app/plugins/datasource/loki/result_transformer.test.ts +++ b/public/app/plugins/datasource/loki/result_transformer.test.ts @@ -1,5 +1,5 @@ -import { FieldType, MutableDataFrame } from '@grafana/data'; -import { LokiLegacyStreamResult, LokiStreamResult } from './types'; +import { FieldType, MutableDataFrame, CircularDataFrame } from '@grafana/data'; +import { LokiLegacyStreamResult, LokiStreamResult, LokiTailResponse } from './types'; import * as ResultTransformer from './result_transformer'; const legacyStreamResult: LokiLegacyStreamResult[] = [ @@ -127,7 +127,7 @@ describe('loki result transformer', () => { }); describe('appendResponseToBufferedData', () => { - it('appends response', () => { + it('should append response', () => { const data = new MutableDataFrame(); data.addField({ name: 'ts', type: FieldType.time, config: { title: 'Time' } }); data.addField({ name: 'line', type: FieldType.string }); @@ -142,5 +142,41 @@ describe('loki result transformer', () => { id: '2764544e18dbc3fcbeee21a573e8cd1b', }); }); + + it('should return a dataframe with ts in iso format', () => { + const tailResponse: LokiTailResponse = { + streams: [ + { + stream: { + filename: '/var/log/grafana/grafana.log', + job: 'grafana', + }, + values: [ + [ + '1581519914265798400', + 't=2020-02-12T15:04:51+0000 lvl=info msg="Starting Grafana" logger=server version=6.7.0-pre commit=6f09bc9fb4 branch=issue-21929 compiled=2020-02-11T20:43:28+0000', + ], + ], + }, + ], + }; + + const data = new CircularDataFrame({ capacity: 1 }); + data.addField({ name: 'ts', type: FieldType.time, config: { title: 'Time' } }); + data.addField({ name: 'tsNs', type: FieldType.time, config: { title: 'Time ns' } }); + data.addField({ name: 'line', type: FieldType.string }).labels = { job: 'grafana' }; + data.addField({ name: 'labels', type: FieldType.other }); + data.addField({ name: 'id', type: FieldType.string }); + + ResultTransformer.appendResponseToBufferedData(tailResponse, data); + expect(data.get(0)).toEqual({ + ts: '2020-02-12T15:05:14.265Z', + tsNs: '1581519914265798400', + line: + 't=2020-02-12T15:04:51+0000 lvl=info msg="Starting Grafana" logger=server version=6.7.0-pre commit=6f09bc9fb4 branch=issue-21929 compiled=2020-02-11T20:43:28+0000', + labels: { filename: '/var/log/grafana/grafana.log' }, + id: '19e8e093d70122b3b53cb6e24efd6e2d', + }); + }); }); }); diff --git a/public/app/plugins/datasource/loki/result_transformer.ts b/public/app/plugins/datasource/loki/result_transformer.ts index 246fe8d3247..d02ad135da5 100644 --- a/public/app/plugins/datasource/loki/result_transformer.ts +++ b/public/app/plugins/datasource/loki/result_transformer.ts @@ -193,7 +193,7 @@ export function appendResponseToBufferedData(response: LokiTailResponse, data: M // Add each line for (const [ts, line] of stream.values) { - data.values.ts.add(ts.substr(0, ts.length - 6)); + data.values.ts.add(new Date(parseInt(ts.substr(0, ts.length - 6), 10)).toISOString()); data.values.tsNs.add(ts); data.values.line.add(line); data.values.labels.add(unique);