loki: logrowcontext: explicitly name fields (#48043)

This commit is contained in:
Gábor Farkas 2022-04-22 08:43:33 +02:00 committed by GitHub
parent 87dc1bfc88
commit f2814046d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -557,17 +557,46 @@ export class LokiDatasource
const limit = (options && options.limit) || 10;
const { query, range } = this.prepareLogRowContextQueryTarget(row, limit, direction);
const sortResults = (result: DataQueryResponse): DataQueryResponse => {
return {
...result,
data: result.data.map((frame: DataFrame) => {
const timestampFieldIndex = frame.fields.findIndex((field) => field.type === FieldType.time);
if (timestampFieldIndex === -1) {
return frame;
const processDataFrame = (frame: DataFrame): DataFrame => {
// log-row-context requires specific field-names to work, so we set them here: "ts", "line", "id"
const cache = new FieldCache(frame);
const timestampField = cache.getFirstFieldOfType(FieldType.time);
const lineField = cache.getFirstFieldOfType(FieldType.string);
const idField = cache.getFieldByName('id');
if (timestampField === undefined || lineField === undefined || idField === undefined) {
// this should never really happen, but i want to keep typescript happy
return { ...frame, fields: [] };
}
return sortDataFrameByTime(frame, 'DESCENDING');
}),
return {
...frame,
fields: [
{
...timestampField,
name: 'ts',
},
{
...lineField,
name: 'line',
},
{
...idField,
name: 'id',
},
],
};
};
const processResults = (result: DataQueryResponse): DataQueryResponse => {
const frames: DataFrame[] = result.data;
const processedFrames = frames
.map((frame) => sortDataFrameByTime(frame, 'DESCENDING'))
.map((frame) => processDataFrame(frame)); // rename fields if needed
return {
...result,
data: processedFrames,
};
};
@ -584,7 +613,7 @@ export class LokiDatasource
};
throw error;
}),
switchMap((res) => of(sortResults(res)))
switchMap((res) => of(processResults(res)))
)
);
};