mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
loki: logrowcontext: explicitly name fields (#48043)
This commit is contained in:
parent
87dc1bfc88
commit
f2814046d1
@ -557,17 +557,46 @@ export class LokiDatasource
|
|||||||
const limit = (options && options.limit) || 10;
|
const limit = (options && options.limit) || 10;
|
||||||
const { query, range } = this.prepareLogRowContextQueryTarget(row, limit, direction);
|
const { query, range } = this.prepareLogRowContextQueryTarget(row, limit, direction);
|
||||||
|
|
||||||
const sortResults = (result: DataQueryResponse): DataQueryResponse => {
|
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 {
|
||||||
|
...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 {
|
return {
|
||||||
...result,
|
...result,
|
||||||
data: result.data.map((frame: DataFrame) => {
|
data: processedFrames,
|
||||||
const timestampFieldIndex = frame.fields.findIndex((field) => field.type === FieldType.time);
|
|
||||||
if (timestampFieldIndex === -1) {
|
|
||||||
return frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sortDataFrameByTime(frame, 'DESCENDING');
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -584,7 +613,7 @@ export class LokiDatasource
|
|||||||
};
|
};
|
||||||
throw error;
|
throw error;
|
||||||
}),
|
}),
|
||||||
switchMap((res) => of(sortResults(res)))
|
switchMap((res) => of(processResults(res)))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user