Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context window to default values #670

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/datasource/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class CHDataSource
defaultDateDate32: instanceSettings.jsonData.defaultDateDate32,
},
defaultDateTimeType: instanceSettings.jsonData.defaultDateTimeType,
contextWindowSize: instanceSettings.jsonData.contextWindowSize,
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface CHQuery extends DataQuery {
intervalFactor?: number;
interval?: string;
formattedQuery?: string;
contextWindowSize?: number;
contextWindowSize?: string;
adHocValuesQuery?: string;
}

Expand All @@ -66,6 +66,7 @@ export interface CHDataSourceOptions extends DataSourceJsonData {
defaultDateDate32?: string;
defaultDateTimeType?: string;
adHocValuesQuery: string;
contextWindowSize?: string;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/views/ConfigEditor/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import _ from 'lodash';
import { DefaultValues } from './FormParts/DefaultValues/DefaultValues';
import { LANGUAGE_ID } from '../QueryEditor/components/QueryTextEditor/editor/initiateEditor';
import { MONACO_EDITOR_OPTIONS } from '../constants';
import {COMPRESSION_TYPE_OPTIONS} from "./constants";
import {DEFAULT_VALUES_QUERY} from "../../datasource/adhoc";
import { COMPRESSION_TYPE_OPTIONS } from './constants';
import { DEFAULT_VALUES_QUERY } from '../../datasource/adhoc';

export interface CHSecureJsonData {
password?: string;
Expand Down
19 changes: 13 additions & 6 deletions src/views/ConfigEditor/FormParts/DefaultValues/DefaultValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ interface DefaultValuesInterface {
onFieldChange: any;
}

export const DefaultValues = ({
jsonData,
newOptions,
onSwitchToggle,
onFieldChange,
}: DefaultValuesInterface) => {
export const DefaultValues = ({ jsonData, newOptions, onSwitchToggle, onFieldChange }: DefaultValuesInterface) => {
const [defaultDateTime64Options, setDefaultDateTime64Options] = useState<any[]>([]);
const [defaultDateTimeOptions, setDefaultDateTimeOptions] = useState<any[]>([]);
const [defaultUint32Options, setDefaultUint32Options] = useState<any[]>([]);
Expand Down Expand Up @@ -242,6 +237,18 @@ export const DefaultValues = ({
options={defaultDateDate32Options}
/>
</InlineField>
<h6>Logs settings</h6>
<InlineField label="Context window" labelWidth={32} style={{ marginLeft: '30px' }}>
<Select
width={24}
data-testid="context-window-size-select"
onChange={(changeEvent) => {
onFieldChange({ value: changeEvent ? changeEvent.value : undefined }, 'contextWindowSize');
}}
options={['10', '20', '50', '100'].map((value) => ({ label: value + ' entries', value }))}
value={jsonData.contextWindowSize}
/>
</InlineField>
</>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/views/ConfigEditor/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export const COMPRESSION_TYPE_OPTIONS = [
{ label: 'br', value: 'br' },
{ label: 'deflate', value: 'deflate' },
{ label: 'zstd', value: 'zstd' },
]
];
8 changes: 4 additions & 4 deletions src/views/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useQueryState } from './hooks/useQueryState';
import { useFormattedData } from './hooks/useFormattedData';
import { initializeQueryDefaults } from './helpers/initializeQueryDefaults';
import './QueryEditor.css';
import {getAdhocFilters} from "./helpers/getAdHocFilters";
import { getAdhocFilters } from './helpers/getAdHocFilters';

export function QueryEditor(props: QueryEditorProps<CHDataSource, CHQuery, CHDataSourceOptions>) {
const { datasource, query, onChange, onRunQuery } = props;
Expand All @@ -23,11 +23,11 @@ export function QueryEditor(props: QueryEditorProps<CHDataSource, CHQuery, CHDat
const onSqlChange = (sql: string) => onChange({ ...initializedQuery, query: sql });
const onFieldChange = (field: any) => onChange({ ...initializedQuery, [field.fieldName]: field.value });
const onTriggerQuery = () => onRunQuery();

// @ts-ignore
const adHocFilters = getAdhocFilters(datasource?.name, query.datasource?.uid)
const adHocFilters = getAdhocFilters(datasource?.name, query.datasource?.uid);
// @ts-ignore
const adHocFiltersKey = adHocFilters.map(({key,operator,value }) => `${key}${operator}${value}`).join(',');
const adHocFiltersKey = adHocFilters.map(({ key, operator, value }) => `${key}${operator}${value}`).join(',');
const areAdHocFiltersAvailable = !!adHocFilters.length;

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SelectableValue} from "@grafana/data";
import {EditorMode} from "../../../../types/types";
import {E2ESelectors} from "@grafana/e2e-selectors";
import { SelectableValue } from '@grafana/data';
import { EditorMode } from '../../../../types/types';
import { E2ESelectors } from '@grafana/e2e-selectors';

const Components = {
QueryEditor: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CHQuery, EditorMode} from "../../../../types/types";
import {CHDataSource} from "../../../../datasource/datasource";
import { CHQuery, EditorMode } from '../../../../types/types';
import { CHDataSource } from '../../../../datasource/datasource';

export interface QueryHeaderProps {
isAnnotationView: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CHQuery, TimestampFormat} from "../../../../../types/types";
import {CHDataSource} from "../../../../../datasource/datasource";
import { CHQuery, TimestampFormat } from '../../../../../types/types';
import { CHDataSource } from '../../../../../datasource/datasource';

export function findDifferences(query: CHQuery, datasource: CHDataSource) {
const { defaultValues } = datasource;
Expand Down Expand Up @@ -74,6 +74,15 @@ export function findDifferences(query: CHQuery, datasource: CHDataSource) {
fieldName: 'dateColDataType',
});
}

if (query.contextWindowSize !== defaultValues.contextWindowSize) {
differences.push({
key: 'Logs context window size',
original: checkValue(query.contextWindowSize),
updated: defaultValues.contextWindowSize,
fieldName: 'contextWindowSize',
});
}
}

return differences;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ export const QueryTextEditor = ({

return (
<>
<SQLCodeEditor
datasource={datasource}
onSqlChange={onSqlChange}
query={query}
onRunQuery={onRunQuery}
/>
<SQLCodeEditor datasource={datasource} onSqlChange={onSqlChange} query={query} onRunQuery={onRunQuery} />
{!areAdHocFiltersAvailable && adhocFilters.length > 0 && (
<TagsInput
className={'adhoc-filters-tags'}
Expand Down Expand Up @@ -154,7 +149,7 @@ export const QueryTextEditor = ({
width={'auto'}
data-testid="context-window-size-select"
onChange={(e) => handleContextWindowChange(e.value)}
options={[10, 20, 50, 100].map((value) => ({ label: value + ' entries', value }))}
options={['10', '20', '50', '100'].map((value) => ({ label: value + ' entries', value }))}
value={query.contextWindowSize}
/>
</InlineField>
Expand Down
14 changes: 8 additions & 6 deletions src/views/QueryEditor/helpers/getAdHocFilters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {getTemplateSrv} from "@grafana/runtime";
import {AdHocVariableModel} from "@grafana/data";
import { getTemplateSrv } from '@grafana/runtime';
import { AdHocVariableModel } from '@grafana/data';

export const getAdhocFilters = (datasourceName, datasourceUid: any) => {
const templateSrv= getTemplateSrv()
const adhocVariables: any[] = templateSrv.getVariables().filter(variable => variable.type === 'adhoc') as AdHocVariableModel[];
const templateSrv = getTemplateSrv();
const adhocVariables: any[] = templateSrv
.getVariables()
.filter((variable) => variable.type === 'adhoc') as AdHocVariableModel[];
let filters: AdHocVariableModel[] = [];
// ts-ignore
for (const variable of adhocVariables) {
Expand All @@ -14,5 +16,5 @@ export const getAdhocFilters = (datasourceName, datasourceUid: any) => {
}
}

return filters
}
return filters;
};
6 changes: 5 additions & 1 deletion src/views/QueryEditor/helpers/initializeQueryDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const initializeQueryDefaults = (
query: query.query || defaultQuery,
formattedQuery: query.formattedQuery || query.query,
editorMode: query.database && query.table ? EditorMode.SQL : EditorMode.Builder,
contextWindowSize: query.contextWindowSize || 10,
contextWindowSize: query.contextWindowSize || '10',
adHocValuesQuery: query.adHocValuesQuery || '',
};

Expand Down Expand Up @@ -58,6 +58,10 @@ export const initializeQueryDefaults = (
initializedQuery.dateTimeColDataType = datasource.defaultValues.dateTime.defaultUint32;
}

if (datasource.defaultValues.contextWindowSize && !query.contextWindowSize) {
initializedQuery.contextWindowSize = datasource.defaultValues.contextWindowSize;
}

onChange({ ...query, ...initializedQuery, initialized: true });
}

Expand Down
1 change: 0 additions & 1 deletion src/views/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const DEFAULT_DATE_TIME_TYPE = TimestampFormat.DateTime;
export const DEFAULT_ROUND = '0s';
export const DEFAULT_INTERVAL_FACTOR = 1;


export const MONACO_EDITOR_OPTIONS: any = {
scrollBeyondLastLine: false,
wordWrap: 'on',
Expand Down