Loki: Remove unused methods (#75106)

* Loki: Remove unused imports in dataosurce.ts

* Extract range from props, fix lint

* Fix naming

* Remove unused types
This commit is contained in:
Ivana Huckova 2023-09-22 12:41:54 +02:00 committed by GitHub
parent 15f6e8a500
commit 28b4d7fa0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7643 additions and 8571 deletions

View File

@ -29,7 +29,7 @@ export const testIds = {
};
export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
const { onChange, onRunQuery, onAddQuery, data, app, queries, datasource } = props;
const { onChange, onRunQuery, onAddQuery, data, app, queries, datasource, range: timeRange } = props;
const [parseModalOpen, setParseModalOpen] = useState(false);
const [queryPatternsModalOpen, setQueryPatternsModalOpen] = useState(false);
const [dataIsStale, setDataIsStale] = useState(false);
@ -37,9 +37,8 @@ export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
const [queryStats, setQueryStats] = useState<QueryStats | null>(null);
const { flag: explain, setFlag: setExplain } = useFlag(lokiQueryEditorExplainKey);
const timerange = datasource.getTimeRange();
const predefinedOperations = datasource.predefinedOperations;
const previousTimerange = usePrevious(timerange);
const previousTimeRange = usePrevious(timeRange);
const query = getQueryWithDefaults(props.query);
if (config.featureToggles.lokiPredefinedOperations && !query.expr && predefinedOperations) {
@ -100,8 +99,8 @@ export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
const update = shouldUpdateStats(
query.expr,
previousQueryExpr,
timerange,
previousTimerange,
timeRange,
previousTimeRange,
query.queryType,
previousQueryType
);
@ -112,7 +111,7 @@ export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
};
makeAsyncRequest();
}
}, [datasource, timerange, previousTimerange, query, previousQueryExpr, previousQueryType, setQueryStats]);
}, [datasource, timeRange, previousTimeRange, query, previousQueryExpr, previousQueryType, setQueryStats]);
return (
<>

View File

@ -8,7 +8,11 @@ import { LokiQueryType } from '../types';
* @param {(DateTime | string)} time1
* @param {(DateTime | string | undefined)} time2
*/
function compareTime(time1: DateTime | string, time2: DateTime | string | undefined) {
function compareTime(time1: DateTime | string | undefined, time2: DateTime | string | undefined) {
if (!time1 || !time2) {
return false;
}
const isAbsolute = isDateTime(time1);
if (isAbsolute) {
@ -21,8 +25,8 @@ function compareTime(time1: DateTime | string, time2: DateTime | string | undefi
export function shouldUpdateStats(
query: string,
prevQuery: string | undefined,
timerange: TimeRange,
prevTimerange: TimeRange | undefined,
timeRange: TimeRange | undefined,
prevTimeRange: TimeRange | undefined,
queryType: LokiQueryType | undefined,
prevQueryType: LokiQueryType | undefined
): boolean {
@ -31,8 +35,8 @@ export function shouldUpdateStats(
}
if (
compareTime(timerange.raw.from, prevTimerange?.raw.from) &&
compareTime(timerange.raw.to, prevTimerange?.raw.to)
compareTime(timeRange?.raw.from, prevTimeRange?.raw.from) &&
compareTime(timeRange?.raw.to, prevTimeRange?.raw.to)
) {
return false;
}

View File

@ -9,7 +9,6 @@ import {
CoreApp,
DataFrame,
DataFrameView,
DataQueryError,
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
@ -18,8 +17,6 @@ import {
SupplementaryQueryType,
DataSourceWithQueryExportSupport,
DataSourceWithQueryImportSupport,
dateMath,
DateTime,
FieldCache,
FieldType,
Labels,
@ -40,7 +37,7 @@ import {
} from '@grafana/data';
import { intervalToMs } from '@grafana/data/src/datetime/rangeutil';
import { Duration } from '@grafana/lezer-logql';
import { BackendSrvRequest, config, DataSourceWithBackend, FetchError } from '@grafana/runtime';
import { BackendSrvRequest, config, DataSourceWithBackend } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { convertToWebSocketUrl } from 'app/core/utils/explore';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
@ -141,8 +138,8 @@ export class LokiDatasource
DataSourceWithToggleableQueryFiltersSupport<LokiQuery>
{
private streams = new LiveStreams();
private logContextProvider: LogContextProvider;
languageProvider: LanguageProvider;
logContextProvider: LogContextProvider;
maxLines: number;
predefinedOperations: string;
@ -229,7 +226,7 @@ export class LokiDatasource
}
}
getLogsVolumeDataProvider(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> | undefined {
private getLogsVolumeDataProvider(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> | undefined {
const logsVolumeRequest = cloneDeep(request);
const targets = logsVolumeRequest.targets
.map((query) => this.getSupplementaryQuery({ type: SupplementaryQueryType.LogsVolume }, query))
@ -250,7 +247,7 @@ export class LokiDatasource
);
}
getLogsSampleDataProvider(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> | undefined {
private getLogsSampleDataProvider(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> | undefined {
const logsSampleRequest = cloneDeep(request);
const targets = logsSampleRequest.targets
.map((query) => this.getSupplementaryQuery({ type: SupplementaryQueryType.LogsSample, limit: 100 }, query))
@ -321,7 +318,7 @@ export class LokiDatasource
);
}
runLiveQueryThroughBackend(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> {
private runLiveQueryThroughBackend(request: DataQueryRequest<LokiQuery>): Observable<DataQueryResponse> {
// this only works in explore-mode, so variables don't need to be handled,
// and only for logs-queries, not metric queries
const logsQueries = request.targets.filter((query) => query.expr !== '' && isLogsQuery(query.expr));
@ -342,7 +339,7 @@ export class LokiDatasource
return merge(...subQueries);
}
createLiveTarget(target: LokiQuery, maxDataPoints: number): LokiLiveTarget {
private createLiveTarget(target: LokiQuery, maxDataPoints: number): LokiLiveTarget {
const query = target.expr;
const baseUrl = this.instanceSettings.url;
const params = serializeParams({ query });
@ -361,7 +358,7 @@ export class LokiDatasource
* Loki streams, sets only common labels on dataframe.labels and has additional dataframe.fields.labels for unique
* labels per row.
*/
runLiveQuery = (target: LokiQuery, maxDataPoints: number): Observable<DataQueryResponse> => {
private runLiveQuery = (target: LokiQuery, maxDataPoints: number): Observable<DataQueryResponse> => {
const liveTarget = this.createLiveTarget(target, maxDataPoints);
return this.streams.getStream(liveTarget).pipe(
@ -376,16 +373,6 @@ export class LokiDatasource
);
};
getRangeScopedVars(range: TimeRange = this.getTimeRange()) {
const msRange = range.to.diff(range.from);
const sRange = Math.round(msRange / 1000);
return {
__range_ms: { text: msRange, value: msRange },
__range_s: { text: sRange, value: sRange },
__range: { text: sRange + 's', value: sRange + 's' },
};
}
interpolateVariablesInQueries(queries: LokiQuery[], scopedVars: ScopedVars): LokiQuery[] {
let expandedQueries = queries;
if (queries && queries.length) {
@ -571,7 +558,7 @@ export class LokiDatasource
return await this.processMetricFindQuery(interpolatedQuery);
}
async processMetricFindQuery(query: LokiVariableQuery) {
private async processMetricFindQuery(query: LokiVariableQuery) {
if (query.type === LokiVariableQueryType.LabelNames) {
return this.labelNamesQuery();
}
@ -613,14 +600,14 @@ export class LokiDatasource
return result.map((value: string) => ({ text: value }));
}
async labelValuesQuery(label: string) {
private async labelValuesQuery(label: string) {
const params = this.getTimeRangeParams();
const url = `label/${label}/values`;
const result = await this.metadataRequest(url, params);
return result.map((value: string) => ({ text: value }));
}
async labelValuesSeriesQuery(expr: string, label: string) {
private async labelValuesSeriesQuery(expr: string, label: string) {
const timeParams = this.getTimeRangeParams();
const params = {
...timeParams,
@ -798,14 +785,6 @@ export class LokiDatasource
return { ...query, expr: expression };
}
getTime(date: string | DateTime, roundUp: boolean) {
if (typeof date === 'string') {
date = dateMath.parse(date, roundUp)!;
}
return Math.ceil(date.valueOf() * 1e6);
}
getLogRowContext = async (
row: LogRowModel,
options?: LogRowContextOptions,
@ -892,17 +871,6 @@ export class LokiDatasource
return true;
}
processError(err: FetchError, target: LokiQuery) {
let error: DataQueryError = cloneDeep(err);
error.refId = target.refId;
if (error.data && err.data.message.includes('escape') && target.expr.includes('\\')) {
error.data.message = `Error: ${err.data.message}. Make sure that all special characters are escaped with \\. For more information on escaping of special characters visit LogQL documentation at https://grafana.com/docs/loki/latest/logql/.`;
}
return error;
}
addAdHocFilters(queryExpr: string) {
const adhocFilters = this.templateSrv.getAdhocFilters(this.name);
let expr = replaceVariables(queryExpr);

View File

@ -1,25 +1,9 @@
import { DataQuery, DataQueryRequest, DataSourceJsonData, QueryResultMeta, ScopedVars, TimeRange } from '@grafana/data';
import { DataQuery, DataQueryRequest, DataSourceJsonData, TimeRange } from '@grafana/data';
import { Loki as LokiQueryFromSchema, LokiQueryType, SupportingQueryType, LokiQueryDirection } from './dataquery.gen';
export { LokiQueryDirection, LokiQueryType, SupportingQueryType };
export interface LokiInstantQueryRequest {
query: string;
limit?: number;
time?: string;
direction?: LokiQueryDirection;
}
export interface LokiRangeQueryRequest {
query: string;
limit?: number;
start?: number;
end?: number;
step?: number;
direction?: LokiQueryDirection;
}
export enum LokiResultType {
Stream = 'streams',
Vector = 'vector',
@ -51,54 +35,11 @@ export interface LokiOptions extends DataSourceJsonData {
predefinedOperations?: string;
}
export interface LokiStats {
[component: string]: {
[label: string]: number;
};
}
export interface LokiVectorResult {
metric: { [label: string]: string };
value: [number, string];
}
export interface LokiVectorResponse {
status: string;
data: {
resultType: LokiResultType.Vector;
result: LokiVectorResult[];
stats?: LokiStats;
};
}
export interface LokiMatrixResult {
metric: Record<string, string>;
values: Array<[number, string]>;
}
export interface LokiMatrixResponse {
status: string;
data: {
resultType: LokiResultType.Matrix;
result: LokiMatrixResult[];
stats?: LokiStats;
};
}
export interface LokiStreamResult {
stream: Record<string, string>;
values: Array<[string, string]>;
}
export interface LokiStreamResponse {
status: string;
data: {
resultType: LokiResultType.Stream;
result: LokiStreamResult[];
stats?: LokiStats;
};
}
export interface LokiTailResponse {
streams: LokiStreamResult[];
dropped_entries?: Array<{
@ -107,19 +48,6 @@ export interface LokiTailResponse {
}> | null;
}
export type LokiResult = LokiVectorResult | LokiMatrixResult | LokiStreamResult;
export type LokiResponse = LokiVectorResponse | LokiMatrixResponse | LokiStreamResponse;
export interface LokiLogsStreamEntry {
line: string;
ts: string;
}
export interface LokiExpression {
regexp: string;
query: string;
}
export type DerivedFieldConfig = {
matcherRegex: string;
name: string;
@ -128,14 +56,6 @@ export type DerivedFieldConfig = {
datasourceUid?: string;
};
export interface TransformerOptions {
legendFormat?: string;
query: string;
refId: string;
scopedVars: ScopedVars;
meta?: QueryResultMeta;
}
export enum LokiVariableQueryType {
LabelNames,
LabelValues,