Skip to content

Commit

Permalink
fix sorting for variables (#107)
Browse files Browse the repository at this point in the history
* fix: add sorting for variables

* Update CHANGELOG.md

---------

Co-authored-by: Roman Khavronenko <roman@victoriametrics.com>
  • Loading branch information
Loori-R and hagen1778 authored Nov 20, 2024
1 parent 7d55c66 commit 39b4845
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* FEATURE: add support for the `/select/logsql/stats_query` and `/select/logsql/stats_query_range` API calls. This feature helps to build different panels with statistic data. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/61).

* BUGFIX: fix options sorting in variables for numerical data type. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/97).

## v0.7.0

* FEATURE: add support to display live logs by querying the tail endpoint in the datasource. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/83)
Expand Down
52 changes: 45 additions & 7 deletions src/language_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getDefaultTimeRange, LanguageProvider, TimeRange, } from '@grafana/data
import { BackendSrvRequest } from '@grafana/runtime';

import { VictoriaLogsDatasource } from './datasource';
import { FiledHits, FilterFieldType } from "./types";
import { FieldHits, FilterFieldType } from "./types";

interface FetchFieldsOptions {
type: FilterFieldType;
Expand All @@ -20,18 +20,24 @@ interface FieldsRequestParams {
field?: string;
}

enum HitsValueType {
NUMBER = 'number',
DATE = 'date',
STRING = 'string'
}

export default class LogsQlLanguageProvider extends LanguageProvider {
declare startTask: Promise<any>;
datasource: VictoriaLogsDatasource;
cacheSize: number;
cacheValues: Map<string, FiledHits[]>
cacheValues: Map<string, FieldHits[]>

constructor(datasource: VictoriaLogsDatasource, initialValues?: Partial<LogsQlLanguageProvider>) {
super();

this.datasource = datasource;
this.cacheSize = 100;
this.cacheValues = new Map<string, FiledHits[]>();
this.cacheValues = new Map<string, FieldHits[]>();

Object.assign(this, initialValues);
}
Expand All @@ -51,7 +57,7 @@ export default class LogsQlLanguageProvider extends LanguageProvider {
return Promise.all([]);
};

async getFieldList(options: FetchFieldsOptions): Promise<FiledHits[]> {
async getFieldList(options: FetchFieldsOptions): Promise<FieldHits[]> {
if (options.type === FilterFieldType.FieldValue && !options.field) {
return [];
}
Expand Down Expand Up @@ -80,9 +86,10 @@ export default class LogsQlLanguageProvider extends LanguageProvider {
this.cacheValues.delete(firstKey);
}

const result = await this.request(url, [], params, { method: 'POST' });
this.cacheValues.set(key, result);
return result;
const result = await this.request(url, [], params, { method: 'POST' }) as FieldHits[];
const sortedResult = sortFieldHits(result);
this.cacheValues.set(key, sortedResult);
return sortedResult;
}

getTimeRangeParams(timeRange?: TimeRange) {
Expand All @@ -93,3 +100,34 @@ export default class LogsQlLanguageProvider extends LanguageProvider {
}
}
}

function determineType(value: string): HitsValueType {
if (!isNaN(Number(value))) {
return HitsValueType.NUMBER;
}
if (!isNaN(Date.parse(value))) {
return HitsValueType.DATE;
}
return HitsValueType.STRING;
}

function getArrayType(data: FieldHits[]): HitsValueType {
const types = new Set(data.map(item => determineType(item.value)));
return types.size === 1 ? Array.from(types)[0] : HitsValueType.STRING;
}

function sortFieldHits(data: FieldHits[]): FieldHits[] {
const arrayType = getArrayType(data);

return data.sort((a, b) => {
switch (arrayType) {
case HitsValueType.NUMBER:
return Number(a.value) - Number(b.value);
case HitsValueType.DATE:
return new Date(a.value).getTime() - new Date(b.value).getTime();
case HitsValueType.STRING:
default:
return a.value.localeCompare(b.value);
}
});
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface RequestArguments {
options?: Partial<BackendSrvRequest>;
}

export interface FiledHits {
export interface FieldHits {
value: string;
hits: number;
}
Expand Down

0 comments on commit 39b4845

Please sign in to comment.