Commit 1d290271 by Torkel Ödegaard

wip: progress on adding query types

parent a69f79ca
import moment from 'moment';
import LokiDatasource from './datasource'; import LokiDatasource from './datasource';
describe('LokiDatasource', () => { describe('LokiDatasource', () => {
...@@ -13,12 +14,19 @@ describe('LokiDatasource', () => { ...@@ -13,12 +14,19 @@ describe('LokiDatasource', () => {
replace: a => a, replace: a => a,
}; };
const range = { from: 'now-6h', to: 'now' }; const range = {
from: moment(),
to: moment(),
raw: {
from: 'now-6h',
to: 'now'
}
};
test('should use default max lines when no limit given', () => { test('should use default max lines when no limit given', () => {
const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock); const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock);
backendSrvMock.datasourceRequest = jest.fn(); backendSrvMock.datasourceRequest = jest.fn();
ds.query({ range, targets: [{ expr: 'foo' }] }); ds.query({ range, targets: [{ expr: 'foo', refId: 'B' }] });
expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1); expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000'); expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000');
}); });
...@@ -28,7 +36,7 @@ describe('LokiDatasource', () => { ...@@ -28,7 +36,7 @@ describe('LokiDatasource', () => {
const customSettings = { ...instanceSettings, jsonData: customData }; const customSettings = { ...instanceSettings, jsonData: customData };
const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock); const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
backendSrvMock.datasourceRequest = jest.fn(); backendSrvMock.datasourceRequest = jest.fn();
ds.query({ range, targets: [{ expr: 'foo' }] }); ds.query({ range, targets: [{ expr: 'foo', refId: 'A' }] });
expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1); expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=20'); expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=20');
}); });
......
...@@ -11,7 +11,7 @@ import { makeSeriesForLogs } from 'app/core/logs_model'; ...@@ -11,7 +11,7 @@ import { makeSeriesForLogs } from 'app/core/logs_model';
// Types // Types
import { LogsStream, LogsModel } from 'app/core/logs_model'; import { LogsStream, LogsModel } from 'app/core/logs_model';
import { PluginMeta, DataQueryOptions, DataSourceApi } from '@grafana/ui/src/types'; import { PluginMeta, DataQueryOptions } from '@grafana/ui/src/types';
import { LokiQuery } from './types'; import { LokiQuery } from './types';
export const DEFAULT_MAX_LINES = 1000; export const DEFAULT_MAX_LINES = 1000;
...@@ -32,7 +32,7 @@ function serializeParams(data: any) { ...@@ -32,7 +32,7 @@ function serializeParams(data: any) {
.join('&'); .join('&');
} }
export default class LokiDatasource implements DataSourceApi<LokiQuery> { export default class LokiDatasource {
languageProvider: LanguageProvider; languageProvider: LanguageProvider;
maxLines: number; maxLines: number;
...@@ -101,7 +101,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> { ...@@ -101,7 +101,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> {
}); });
} }
async importQueries(queries: DataQuery[], originMeta: PluginMeta): Promise<DataQuery[]> { async importQueries(queries: LokiQuery[], originMeta: PluginMeta): Promise<LokiQuery[]> {
return this.languageProvider.importQueries(queries, originMeta.id); return this.languageProvider.importQueries(queries, originMeta.id);
} }
...@@ -114,7 +114,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> { ...@@ -114,7 +114,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> {
}); });
} }
modifyQuery(query: DataQuery, action: any): DataQuery { modifyQuery(query: LokiQuery, action: any): LokiQuery {
const parsed = parseQuery(query.expr || ''); const parsed = parseQuery(query.expr || '');
let selector = parsed.query; let selector = parsed.query;
switch (action.type) { switch (action.type) {
...@@ -129,7 +129,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> { ...@@ -129,7 +129,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> {
return { ...query, expr: expression }; return { ...query, expr: expression };
} }
getHighlighterExpression(query: DataQuery): string { getHighlighterExpression(query: LokiQuery): string {
return parseQuery(query.expr).regexp; return parseQuery(query.expr).regexp;
} }
......
// Libraries
import _ from 'lodash'; import _ from 'lodash';
import moment from 'moment'; import moment from 'moment';
// Services & Utils
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils';
import syntax from './syntax';
// Types
import { import {
CompletionItem, CompletionItem,
CompletionItemGroup, CompletionItemGroup,
...@@ -9,9 +15,7 @@ import { ...@@ -9,9 +15,7 @@ import {
TypeaheadOutput, TypeaheadOutput,
HistoryItem, HistoryItem,
} from 'app/types/explore'; } from 'app/types/explore';
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils'; import { LokiQuery } from './types';
import syntax from './syntax';
import { DataQuery } from '@grafana/ui/src/types';
const DEFAULT_KEYS = ['job', 'namespace']; const DEFAULT_KEYS = ['job', 'namespace'];
const EMPTY_SELECTOR = '{}'; const EMPTY_SELECTOR = '{}';
...@@ -20,7 +24,9 @@ const HISTORY_COUNT_CUTOFF = 1000 * 60 * 60 * 24; // 24h ...@@ -20,7 +24,9 @@ const HISTORY_COUNT_CUTOFF = 1000 * 60 * 60 * 24; // 24h
const wrapLabel = (label: string) => ({ label }); const wrapLabel = (label: string) => ({ label });
export function addHistoryMetadata(item: CompletionItem, history: HistoryItem[]): CompletionItem { type LokiHistoryItem = HistoryItem<LokiQuery>;
export function addHistoryMetadata(item: CompletionItem, history: LokiHistoryItem[]): CompletionItem {
const cutoffTs = Date.now() - HISTORY_COUNT_CUTOFF; const cutoffTs = Date.now() - HISTORY_COUNT_CUTOFF;
const historyForItem = history.filter(h => h.ts > cutoffTs && (h.query.expr as string) === item.label); const historyForItem = history.filter(h => h.ts > cutoffTs && (h.query.expr as string) === item.label);
const count = historyForItem.length; const count = historyForItem.length;
...@@ -155,7 +161,7 @@ export default class LokiLanguageProvider extends LanguageProvider { ...@@ -155,7 +161,7 @@ export default class LokiLanguageProvider extends LanguageProvider {
return { context, refresher, suggestions }; return { context, refresher, suggestions };
} }
async importQueries(queries: DataQuery[], datasourceType: string): Promise<DataQuery[]> { async importQueries(queries: LokiQuery[], datasourceType: string): Promise<LokiQuery[]> {
if (datasourceType === 'prometheus') { if (datasourceType === 'prometheus') {
return Promise.all( return Promise.all(
queries.map(async query => { queries.map(async query => {
......
...@@ -243,9 +243,9 @@ export interface ExploreUrlState { ...@@ -243,9 +243,9 @@ export interface ExploreUrlState {
range: RawTimeRange; range: RawTimeRange;
} }
export interface HistoryItem { export interface HistoryItem<TQuery extends DataQuery = DataQuery> {
ts: number; ts: number;
query: DataQuery; query: TQuery;
} }
export abstract class LanguageProvider { export abstract class LanguageProvider {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment