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

SQL Modal Changes #413

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions src/pages/Stream/Views/Explore/useLogsFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { useLogsStore, logsStoreReducers } from '../../providers/LogsProvider';
import { useQueryLogs } from '@/hooks/useQueryLogs';
import { useFetchCount } from '@/hooks/useQueryResult';
import { useStreamStore } from '../../providers/StreamProvider';
import useParamsController from '@/pages/Stream/hooks/useParamsController';
import _ from 'lodash';

const { setCleanStoreForStreamChange } = logsStoreReducers;
const { syncTimeRange } = appStoreReducers;

const useLogsFetcher = (props: { schemaLoading: boolean; infoLoading: boolean }) => {
const { isStoreSynced } = useParamsController();
const { schemaLoading, infoLoading } = props;
const [currentStream] = useAppStore((store) => store.currentStream);
const [{ timeRange }, setAppStore] = useAppStore((store) => store);
Expand All @@ -25,6 +27,7 @@ const useLogsFetcher = (props: { schemaLoading: boolean; infoLoading: boolean })
const showTable = hasContentLoaded && !hasNoData && !queryLogsError;

useEffect(() => {
if (!isStoreSynced) return;
setAppStore(syncTimeRange);
setLogsStore(setCleanStoreForStreamChange);
}, [currentStream]);
Expand Down
12 changes: 11 additions & 1 deletion src/pages/Stream/components/Querier/FilterQueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
RuleTypeOverride,
Combinator,
} from '../../providers/FilterProvider';
import { useAppStore } from '@/layouts/MainLayout/providers/AppProvider';

type RuleSetProps = {
ruleSet: RuleGroupTypeOverride;
Expand All @@ -50,6 +51,7 @@ const {
updateGroupCombinator,
updateParentCombinator,
updateRule,
parseQuery,
} = filterStoreReducers;

type RuleViewType = {
Expand Down Expand Up @@ -284,17 +286,25 @@ export const QueryPills = () => {
);
};

export const FilterQueryBuilder = (props: { onClear: () => void; onApply: () => void }) => {
export const FilterQueryBuilder = (props: {
onClear: () => void;
onApply: () => void;
filterBuilderQuery: (query: string) => void;
}) => {
const [{ query, isSumbitDisabled, fields }, setFilterStore] = useFilterStore((store) => store);
const [{ isQuerySearchActive, viewMode }] = useLogsStore((store) => store.custQuerySearchState);
const isFiltersApplied = isQuerySearchActive && viewMode === 'filters';
const { combinator, rules: ruleSets } = query;
const [currentStream] = useAppStore((store) => store.currentStream);

useEffect(() => {
// init first rule
if (query.rules.length === 0 && fields.length !== 0) {
setFilterStore((store) => createRuleGroup(store));
}

const { parsedQuery } = parseQuery(query, currentStream || '');
props.filterBuilderQuery(parsedQuery);
}, [query.rules, fields]);

return (
Expand Down
47 changes: 31 additions & 16 deletions src/pages/Stream/components/Querier/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import classes from '../../styles/Querier.module.css';
import { Text } from '@mantine/core';
import { FilterQueryBuilder, QueryPills } from './FilterQueryBuilder';
import { AppliedSQLQuery } from './QueryEditor';
import QueryCodeEditor, { defaultCustSQLQuery } from './QueryCodeEditor';
import QueryCodeEditor from './QueryCodeEditor';
import { useLogsStore, logsStoreReducers } from '../../providers/LogsProvider';
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { filterStoreReducers, noValueOperators, useFilterStore } from '../../providers/FilterProvider';
import { appStoreReducers, useAppStore } from '@/layouts/MainLayout/providers/AppProvider';
import { useStreamStore } from '../../providers/StreamProvider';
import SaveFilterModal from './SaveFilterModal';
import SavedFiltersModal from './SavedFiltersModal';
import _ from 'lodash';
import useParamsController from '@/pages/Stream/hooks/useParamsController';

const { setFields, parseQuery, storeAppliedQuery, resetFilters, toggleSubmitBtn, toggleSaveFiltersModal } =
filterStoreReducers;
Expand Down Expand Up @@ -74,24 +75,34 @@ const QuerierModal = (props: {
onSqlSearchApply: (query: string) => void;
onFiltersApply: () => void;
}) => {
const [currentStream] = useAppStore((store) => store.currentStream);
const [{ showQueryBuilder, viewMode }, setLogsStore] = useLogsStore((store) => store.custQuerySearchState);
const [streamInfo] = useStreamStore((store) => store.info);
const [timeRange] = useAppStore((store) => store.timeRange);
const timePartitionColumn = _.get(streamInfo, 'time_partition', 'p_timestamp');
const { isStoreSynced } = useParamsController();
const [{ showQueryBuilder, viewMode, custSearchQuery, activeMode }, setLogsStore] = useLogsStore(
(store) => store.custQuerySearchState,
);
const [parsedFilterQuery, setParsedFilterQuery] = useState('');

const getParsedFilterQuery = useCallback((query: string) => {
setParsedFilterQuery(query);
}, []);
const onClose = useCallback(() => {
setLogsStore((store) => toggleQueryBuilder(store, false));
}, []);
setParsedFilterQuery('');
setLogsStore((store) => toggleCustQuerySearchViewMode(store, activeMode !== null ? activeMode : 'filters'));
}, [activeMode]);

const queryCodeEditorRef = useRef<any>(''); // to store input value even after the editor unmounts

useEffect(() => {
queryCodeEditorRef.current = defaultCustSQLQuery(
currentStream,
timeRange.startTime,
timeRange.endTime,
timePartitionColumn,
);
}, [currentStream, timeRange.endTime, timeRange.startTime, timePartitionColumn]);
if (!isStoreSynced) return;
if (!_.isEmpty(parsedFilterQuery)) {
queryCodeEditorRef.current = parsedFilterQuery;
return;
}
if (!_.isEmpty(custSearchQuery)) {
queryCodeEditorRef.current = custSearchQuery;
return;
}
}, [isStoreSynced, parsedFilterQuery, custSearchQuery]);

return (
<Modal
Expand All @@ -106,7 +117,11 @@ const QuerierModal = (props: {
title={<ModalTitle title={getLabel(viewMode)} />}>
<Stack style={{ padding: '1rem 0.5rem', height: '100%' }} gap={2}>
{viewMode === 'filters' ? (
<FilterQueryBuilder onClear={props.onClear} onApply={props.onFiltersApply} />
<FilterQueryBuilder
onClear={props.onClear}
onApply={props.onFiltersApply}
filterBuilderQuery={getParsedFilterQuery}
/>
) : (
<QueryCodeEditor
queryCodeEditorRef={queryCodeEditorRef}
Expand Down
Loading