/usr/share/grafana/public/app/plugins/datasource/loki
import { DataFrame, Field, FieldType } from '@grafana/data'; export enum SortDirection { Ascending, Descending, } // creates the `index` for the sorting. // this is needed by the `SortedVector`. // the index is an array of numbers, and it defines an order. // at every slot in the index the values is the position of // the sorted item. // for example, an index of [3,1,2] means that // in the dataframe, that has 3 rows, after sorting: // - the third row will become the first // - the first row will become the second // - the second row will become the third function makeIndex(field: Field<number>, dir: SortDirection): number[] { const fieldValues: number[] = field.values; const { nanos } = field; // we first build an array which is [0,1,2,3....] const index = Array(fieldValues.length); for (let i = 0; i < index.length; i++) { index[i] = i; } const isAsc = dir === SortDirection.Ascending; index.sort((a: number, b: number): number => { // we need to answer this question: // in the field-used-for-sorting, how would we compare value-at-index-a to value-at-index-b? const valA = fieldValues[a]; const valB = fieldValues[b]; if (valA < valB) { return isAsc ? -1 : 1; } if (valA > valB) { return isAsc ? 1 : -1; } // the millisecond timestamps are equal, // compare the nanosecond part, if available if (nanos === undefined) { return 0; } const nanoA = nanos[a]; const nanoB = nanos[b]; if (nanoA < nanoB) { return isAsc ? -1 : 1; } if (nanoA > nanoB) { return isAsc ? 1 : -1; } return 0; }); return index; } // sort a dataframe that is in the Loki dataframe format ascending or desceding based on time, // with nanosecond precision. export function sortDataFrameByTime(frame: DataFrame, dir: SortDirection): DataFrame { const { fields, ...rest } = frame; // we use the approach used in @grafana/data/sortDataframe. // we cannot use it directly, because it does not take `.nanos` into account // (see https://github.com/grafana/grafana/issues/72351). // we can switch to to @grafana/data/sortDataframe when the issue is fixed. const timeField = fields.find((field) => field.type === FieldType.time); if (timeField === undefined) { throw new Error('missing timestamp field. should never happen'); } const index = makeIndex(timeField, dir); return { ...rest, fields: fields.map((field) => ({ ...field, values: sorted(field.values, index), nanos: field.nanos === undefined ? undefined : sorted(field.nanos, index), })), }; } function sorted<T>(vals: T[], index: number[]): T[] { return vals.map((_, idx) => vals[index[idx]]); }
.
Edit
..
Edit
CHANGELOG.md
Edit
LanguageProvider.test.ts
Edit
LanguageProvider.ts
Edit
LiveStreams.test.ts
Edit
LiveStreams.ts
Edit
LogContextProvider.test.ts
Edit
LogContextProvider.ts
Edit
LokiVariableSupport.test.ts
Edit
LokiVariableSupport.ts
Edit
README.md
Edit
backendResultTransformer.test.ts
Edit
backendResultTransformer.ts
Edit
components
Edit
configuration
Edit
dataquery.cue
Edit
dataquery.gen.ts
Edit
datasource.test.ts
Edit
datasource.ts
Edit
dist
Edit
docs
Edit
getDerivedFields.test.ts
Edit
getDerivedFields.ts
Edit
img
Edit
jest-setup.js
Edit
jest.config.js
Edit
languageUtils.test.ts
Edit
languageUtils.ts
Edit
language_utils.test.ts
Edit
lineParser.test.ts
Edit
lineParser.ts
Edit
liveStreamsResultTransformer.test.ts
Edit
liveStreamsResultTransformer.ts
Edit
logsTimeSplitting.test.ts
Edit
logsTimeSplitting.ts
Edit
makeTableFrames.test.ts
Edit
makeTableFrames.ts
Edit
mergeResponses.test.ts
Edit
mergeResponses.ts
Edit
metricTimeSplitting.test.ts
Edit
metricTimeSplitting.ts
Edit
migrations
Edit
mocks
Edit
modifyQuery.test.ts
Edit
modifyQuery.ts
Edit
module.test.ts
Edit
module.ts
Edit
package.json
Edit
plugin.json
Edit
project.json
Edit
queryHints.test.ts
Edit
queryHints.ts
Edit
querySplitting.test.ts
Edit
querySplitting.ts
Edit
queryUtils.test.ts
Edit
queryUtils.ts
Edit
querybuilder
Edit
responseUtils.test.ts
Edit
responseUtils.ts
Edit
shardQuerySplitting.test.ts
Edit
shardQuerySplitting.ts
Edit
sortDataFrame.test.ts
Edit
sortDataFrame.ts
Edit
streaming.test.ts
Edit
streaming.ts
Edit
syntax.test.ts
Edit
syntax.ts
Edit
tracking.test.ts
Edit
tracking.ts
Edit
tsconfig.json
Edit
types.ts
Edit
webpack.config.ts
Edit