Skip to content

Commit

Permalink
Fix time range for getTagKeys and getTagValues function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmassot committed Mar 26, 2024
1 parent 7a328e7 commit 7bf9a4a
Show file tree
Hide file tree
Showing 7 changed files with 1,293 additions and 2,171 deletions.
3,412 changes: 1,266 additions & 2,146 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"license": "AGPL-3.0",
"devDependencies": {
"@babel/core": "^7.21.4",
"@grafana/e2e": "10.0.3",
"@grafana/e2e-selectors": "10.0.3",
"@grafana/e2e": "10.1.9",
"@grafana/e2e-selectors": "10.1.9",
"@grafana/eslint-config": "^6.0.0",
"@grafana/tsconfig": "^1.2.0-rc1",
"@storybook/addon-essentials": "^7.6.17",
Expand Down Expand Up @@ -74,10 +74,10 @@
},
"dependencies": {
"@emotion/css": "^11.11",
"@grafana/data": "10.0.3",
"@grafana/experimental": "^1.7.6",
"@grafana/runtime": "10.0.3",
"@grafana/ui": "10.0.3",
"@grafana/data": "10.1.9",
"@grafana/experimental": "^1.7.10",
"@grafana/runtime": "10.1.9",
"@grafana/ui": "10.1.9",
"@reduxjs/toolkit": "^1.9.5",
"@uiw/react-codemirror": "^4.21.21",
"lucene": "^2.1.1",
Expand Down
6 changes: 3 additions & 3 deletions src/LogContext/components/LogContextUI.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useEffect, useState, useCallback, useMemo } from "react";
import { LogRowModel, Field as GrafanaField } from '@grafana/data';
import { LogRowModel, Field as GrafanaField, getDefaultTimeRange } from '@grafana/data';
import { ElasticsearchQuery as DataQuery } from '../../types';
import { LuceneQueryEditor } from "../../components/LuceneQueryEditor";

import { css } from "@emotion/css";
import { Button } from "@grafana/ui";
import { useQueryBuilder } from '@/QueryBuilder/lucene';
import { LogContextQueryBuilderSidebar } from "./LogContextQueryBuilderSidebar";
import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryContext";
import { DatasourceContext, useRange } from "@/components/QueryEditor/ElasticsearchQueryContext";
import { BaseQuickwitDataSource } from "@/datasource/base";
import { useDatasourceFields } from "@/datasource/utils";
import { Field, FieldContingency, Filter } from "../types";
Expand Down Expand Up @@ -51,7 +51,7 @@ export function LogContextUI(props: LogContextUIProps ){
const {query, parsedQuery, setQuery, setParsedQuery} = builder;
const [canRunQuery, setCanRunQuery] = useState<boolean>(false);
const {row, origQuery, updateQuery, runContextQuery } = props;
const {fields, getSuggestions} = useDatasourceFields(props.datasource);
const {fields, getSuggestions} = useDatasourceFields(props.datasource, getDefaultTimeRange());

useEffect(()=>{
setQuery(origQuery?.query || '')
Expand Down
5 changes: 3 additions & 2 deletions src/components/QueryEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useDispatch } from '@/hooks/useStatelessReducer';
import { ElasticsearchQuery } from '@/types';

import { BucketAggregationsEditor } from './BucketAggregationsEditor';
import { ElasticsearchProvider, useDatasource } from './ElasticsearchQueryContext';
import { ElasticsearchProvider, useDatasource, useRange } from './ElasticsearchQueryContext';
import { MetricAggregationsEditor } from './MetricAggregationsEditor';
import { metricAggregationConfig } from './MetricAggregationsEditor/utils';
import { changeQuery } from './state';
Expand Down Expand Up @@ -67,7 +67,8 @@ type ElasticSearchQueryFieldProps = {
export const ElasticSearchQueryField = ({ value, onChange, onSubmit }: ElasticSearchQueryFieldProps) => {
const styles = useStyles2(getStyles);
const datasource = useDatasource()
const { getSuggestions } = useDatasourceFields(datasource);
const range = useRange();
const { getSuggestions } = useDatasourceFields(datasource, range);

return (
<div className={styles.queryItem}>
Expand Down
18 changes: 10 additions & 8 deletions src/datasource/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
DataFrame,
DataQueryRequest,
DataQueryResponse,
DataSourceGetTagKeysOptions,
DataSourceGetTagValuesOptions,
DataSourceInstanceSettings,
DataSourceWithQueryImportSupport,
getDefaultTimeRange,
Expand Down Expand Up @@ -46,7 +48,7 @@ type FieldCapsSpec = {
aggregatable?: boolean,
searchable?: boolean,
type?: string[],
_range?: TimeRange
range?: TimeRange
}

export class BaseQuickwitDataSource
Expand Down Expand Up @@ -163,7 +165,8 @@ export class BaseQuickwitDataSource
)
}

getFields(spec: FieldCapsSpec={}, range = getDefaultTimeRange()): Observable<MetricFindValue[]> {
getFields(spec: FieldCapsSpec={}): Observable<MetricFindValue[]> {
const range = spec.range || getDefaultTimeRange();
return from(this.getResource('_elastic/' + this.index + '/_field_caps', {
start_timestamp: Math.floor(range.from.valueOf()/SECOND),
end_timestamp: Math.ceil(range.to.valueOf()/SECOND),
Expand Down Expand Up @@ -209,17 +212,16 @@ export class BaseQuickwitDataSource
/**
* Get tag keys for adhoc filters
*/
getTagKeys(spec?: FieldCapsSpec) {
const fields = this.getFields(spec)
getTagKeys(options: DataSourceGetTagKeysOptions) {
const fields = this.getFields({aggregatable:true, range: options.timeRange})
return lastValueFrom(fields, {defaultValue:[]});
}

/**
* Get tag values for adhoc filters
*/
getTagValues(options: any) {
const range = getDefaultTimeRange();
const terms = this.getTerms({ field: options.key }, range)
getTagValues(options: DataSourceGetTagValuesOptions) {
const terms = this.getTerms({ field: options.key }, options.timeRange)
return lastValueFrom(terms, {defaultValue:[]});
}

Expand Down Expand Up @@ -292,7 +294,7 @@ export class BaseQuickwitDataSource
if (query) {
if (parsedQuery.find === 'fields') {
parsedQuery.type = this.interpolateLuceneQuery(parsedQuery.type);
return lastValueFrom(this.getFields({aggregatable:true, type:parsedQuery.type, _range:range}), {defaultValue:[]});
return lastValueFrom(this.getFields({aggregatable:true, type:parsedQuery.type, range:range}), {defaultValue:[]});
}
if (parsedQuery.find === 'terms') {
parsedQuery.field = this.interpolateLuceneQuery(parsedQuery.field);
Expand Down
9 changes: 4 additions & 5 deletions src/datasource/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseQuickwitDataSource } from "./base";
import { useState, useEffect, useCallback } from "react";
import{ MetricFindValue } from '@grafana/data';
import{ MetricFindValue, TimeRange } from '@grafana/data';

/**
* Provide suggestions based on datasource fields
Expand All @@ -15,14 +15,13 @@ export type Suggestion = {
}>
}

export function useDatasourceFields(datasource: BaseQuickwitDataSource) {
export function useDatasourceFields(datasource: BaseQuickwitDataSource, range: TimeRange) {
const [fields, setFields] = useState<MetricFindValue[]>([]);

useEffect(() => {
if (datasource.getTagKeys) {
datasource.getTagKeys({ searchable: true }).then(setFields);
datasource.getTagKeys({ searchable: true, range: range}).then(setFields);
}
}, [datasource, setFields]);
}, [datasource, range, setFields]);

const getSuggestions = useCallback(async (word: string): Promise<Suggestion> => {
let suggestions: Suggestion = { from: 0, options: [] };
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const useFields = (type: AggregationType | string[]) => {
return async (q?: string) => {
// _mapping doesn't support filtering, we avoid sending a request everytime q changes
if (!rawFields) {
rawFields = await lastValueFrom(datasource.getFields({aggregatable:true, type:filter, _range:range}));
rawFields = await lastValueFrom(datasource.getFields({aggregatable:true, type:filter, range:range}));
}

return rawFields.filter(({ text }) => q === undefined || text.includes(q)).map(toSelectableValue);
Expand Down

0 comments on commit 7bf9a4a

Please sign in to comment.