Skip to content

Commit

Permalink
chore(Queries): store [YTFRONT-4612]
Browse files Browse the repository at this point in the history
  • Loading branch information
SimbiozizV committed Feb 4, 2025
1 parent ced7ff9 commit 533f16c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 40 deletions.
2 changes: 2 additions & 0 deletions packages/ui/src/ui/pages/query-tracker/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {queryFilesFormReducer} from './queryFilesForm/queryFilesFormSlice';
import {vcsReducer} from './vcs/vcsSlice';
import {queryNavigationReducer} from './queryNavigation/queryNavigationSlice';
import {queryChartReducer} from './queryChart/queryChartSlice';
import {neuralNetworkReducer} from './neuralNetwork/neuralNetworkSlice';

export const queryTracker = combineReducers({
list: listReducer,
Expand All @@ -17,4 +18,5 @@ export const queryTracker = combineReducers({
vcs: vcsReducer,
queryNavigation: queryNavigationReducer,
queryChart: queryChartReducer,
neuralNetwork: neuralNetworkReducer,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {ThunkAction} from 'redux-thunk';
import {RootState} from '../../../../store/reducers';
import {wrapApiPromiseByToaster} from '../../../../utils/utils';
import {YTApiId, ytApiV4Id} from '../../../../rum/rum-wrap-api';
import {setContextId} from './neuralNetworkSlice';

export const createContextId =
(queryId: string): ThunkAction<void, RootState, unknown, any> =>
async (dispatch) => {
const newContextId = crypto.randomUUID();

await wrapApiPromiseByToaster(
ytApiV4Id.alterQuery(YTApiId.alterQuery, {
parameters: {
query_id: queryId,
annotations: {
contextId: newContextId,
},
},
}),
{
toasterName: `context_id`,
skipSuccessToast: true,
errorTitle: `Failed to set query contect id`,
},
);
dispatch(setContextId(newContextId));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {PayloadAction, createSlice} from '@reduxjs/toolkit';

type NeuralNetworkState = {
suggestions: string[];
requestId: string;
contextId: string;
};

const initialState: NeuralNetworkState = {
suggestions: [],
requestId: '',
contextId: '',
};

const neuralNetworkSlice = createSlice({
initialState,
name: 'neuralNetwork',
reducers: {
setSuggestions: (state, {payload}: PayloadAction<string[]>) => {
state.suggestions = payload;
},
setRequestId: (state, {payload}: PayloadAction<string>) => {
state.requestId = payload;
},
setContextId: (state, {payload}: PayloadAction<string>) => {
state.contextId = payload;
},
},
});

export const {setSuggestions, setRequestId, setContextId} = neuralNetworkSlice.actions;

export const neuralNetworkReducer = neuralNetworkSlice.reducer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {RootState} from '../../../../store/reducers';

export const selectNeuralNetworkContextId = (state: RootState) =>
state.queryTracker.neuralNetwork.contextId;

export const selectNeuralNetworkRequestId = (state: RootState) =>
state.queryTracker.neuralNetwork.requestId;

export const selectNeuralNetworkSuggestions = (state: RootState) =>
state.queryTracker.neuralNetwork.suggestions;
51 changes: 11 additions & 40 deletions packages/ui/src/ui/pages/query-tracker/module/query/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {chytApiAction, spytApiAction} from '../../../../utils/strawberryControll
import guid from '../../../../common/hammer/guid';
import {getSettingQueryTrackerStage} from '../../../../store/selectors/settings/settings-ts';
import {getDefaultQueryACO, selectIsMultipleAco} from '../query_aco/selectors';
import {createContextId} from '../neuralNetwork/actions';
import {setContextId} from '../neuralNetwork/neuralNetworkSlice';

import {
REQUEST_QUERY,
Expand All @@ -50,7 +52,7 @@ import {
UPDATE_QUERY,
} from '../query-tracker-contants';
import {loadVisualization} from '../queryChart/actions';
import {YTApiId, ytApiV4Id} from '../../../../rum/rum-wrap-api';
import {selectNeuralNetworkContextId} from '../neuralNetwork/selectors';

export const setCurrentClusterToQuery =
(): ThunkAction<void, RootState, unknown, any> => (dispatch, getState) => {
Expand Down Expand Up @@ -109,42 +111,10 @@ export const loadCliqueByCluster =
});
};

export const setContextId =
(queryId: string): ThunkAction<void, RootState, unknown, any> =>
async (dispatch, getState) => {
const draft = getQueryDraft(getState());
const newContextId = crypto.randomUUID();

await wrapApiPromiseByToaster(
ytApiV4Id.alterQuery(YTApiId.alterQuery, {
parameters: {
query_id: queryId,
annotations: {
contextId: newContextId,
},
},
}),
{
toasterName: `context_id`,
skipSuccessToast: true,
errorTitle: `Failed to set query contect id`,
},
);
dispatch(
updateQueryDraft({
...draft,
annotations: {
...draft.annotations,
contextId: newContextId,
},
}),
);
};

export function loadQuery(
queryId: string,
config?: {dontReplaceQueryText?: boolean},
): ThunkAction<any, RootState, any, SetQueryAction | RequestQueryAction | SetQueryErrorLoadAction> {
): ThunkAction<any, RootState, any, any> {
return async (dispatch, getState) => {
const state = getState();
const stage = getSettingQueryTrackerStage(state);
Expand All @@ -169,7 +139,9 @@ export function loadQuery(
}

if (!queryItem.annotations?.contextId) {
dispatch(setContextId(queryId));
dispatch(createContextId(queryId));
} else {
dispatch(setContextId(queryItem.annotations?.contextId));
}

dispatch({
Expand Down Expand Up @@ -248,18 +220,16 @@ export function createEmptyQuery(
engine = QueryEngine.YQL,
query?: string,
settings?: Record<string, string>,
): ThunkAction<any, RootState, any, SetQueryAction> {
): ThunkAction<any, RootState, any, any> {
return (dispatch, getState) => {
const state = getState();
const defaultQueryACO = getDefaultQueryACO(state);

dispatch(setContextId(crypto.randomUUID()));
dispatch({
type: SET_QUERY,
data: {
initialQuery: {
annotations: {
contextId: crypto.randomUUID(),
},
access_control_object: defaultQueryACO,
access_control_objects: [defaultQueryACO],
query: query || '',
Expand All @@ -279,8 +249,9 @@ export function runQuery(
return async (dispatch, getState) => {
const state = getState();
const query = getQueryDraft(state);
const contextId = selectNeuralNetworkContextId(state);

const newQuery = {...query};
const newQuery = {...query, annotations: {...query.annotations, contextId}};
if ('access_control_objects' in newQuery) {
newQuery.access_control_objects = newQuery.access_control_objects?.filter(
(i) => i !== SHARED_QUERY_ACO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DEFAULT_QUERY_ACO = 'nobody';
export const SHARED_QUERY_ACO = 'everyone-share';

export const getQuery = (state: RootState) => getState(state).queryItem;
export const getQueryId = (state: RootState) => getState(state).queryItem?.id;
export const getQueryGetParams = (state: RootState) => getState(state).params;

export const getQueryDraft = (state: RootState) => getState(state).draft;
Expand Down

0 comments on commit 533f16c

Please sign in to comment.