Commit 1d290271 by Torkel Ödegaard

wip: progress on adding query types

parent a69f79ca
import moment from 'moment';
import LokiDatasource from './datasource';
describe('LokiDatasource', () => {
......@@ -13,12 +14,19 @@ describe('LokiDatasource', () => {
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', () => {
const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock);
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[0][0].url).toContain('limit=1000');
});
......@@ -28,7 +36,7 @@ describe('LokiDatasource', () => {
const customSettings = { ...instanceSettings, jsonData: customData };
const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
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[0][0].url).toContain('limit=20');
});
......
......@@ -11,7 +11,7 @@ import { makeSeriesForLogs } from 'app/core/logs_model';
// Types
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';
export const DEFAULT_MAX_LINES = 1000;
......@@ -32,7 +32,7 @@ function serializeParams(data: any) {
.join('&');
}
export default class LokiDatasource implements DataSourceApi<LokiQuery> {
export default class LokiDatasource {
languageProvider: LanguageProvider;
maxLines: number;
......@@ -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);
}
......@@ -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 || '');
let selector = parsed.query;
switch (action.type) {
......@@ -129,7 +129,7 @@ export default class LokiDatasource implements DataSourceApi<LokiQuery> {
return { ...query, expr: expression };
}
getHighlighterExpression(query: DataQuery): string {
getHighlighterExpression(query: LokiQuery): string {
return parseQuery(query.expr).regexp;
}
......
// Libraries
import _ from 'lodash';
import moment from 'moment';
// Services & Utils
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils';
import syntax from './syntax';
// Types
import {
CompletionItem,
CompletionItemGroup,
......@@ -9,9 +15,7 @@ import {
TypeaheadOutput,
HistoryItem,
} from 'app/types/explore';
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils';
import syntax from './syntax';
import { DataQuery } from '@grafana/ui/src/types';
import { LokiQuery } from './types';
const DEFAULT_KEYS = ['job', 'namespace'];
const EMPTY_SELECTOR = '{}';
......@@ -20,7 +24,9 @@ const HISTORY_COUNT_CUTOFF = 1000 * 60 * 60 * 24; // 24h
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 historyForItem = history.filter(h => h.ts > cutoffTs && (h.query.expr as string) === item.label);
const count = historyForItem.length;
......@@ -155,7 +161,7 @@ export default class LokiLanguageProvider extends LanguageProvider {
return { context, refresher, suggestions };
}
async importQueries(queries: DataQuery[], datasourceType: string): Promise<DataQuery[]> {
async importQueries(queries: LokiQuery[], datasourceType: string): Promise<LokiQuery[]> {
if (datasourceType === 'prometheus') {
return Promise.all(
queries.map(async query => {
......
......@@ -243,9 +243,9 @@ export interface ExploreUrlState {
range: RawTimeRange;
}
export interface HistoryItem {
export interface HistoryItem<TQuery extends DataQuery = DataQuery> {
ts: number;
query: DataQuery;
query: TQuery;
}
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