mirror of https://github.com/grafana/grafana
Variables: enables cancel for slow query variables queries (#24430)
* Refactor: initial commit * Tests: updates tests * Tests: updates snapshots * Chore: updates after PR comments * Chore: renamed initVariablesBatch * Tests: adds transactionReducer tests * Chore: updates after PR comments * Refactor: renames cancelAllDataSourceRequests * Refactor: reduces cancellation complexity * Tests: adds tests for cancelAllInFlightRequests * Tests: adds initVariablesTransaction tests * Tests: adds tests for cleanUpVariables and cancelVariables * Always cleanup dashboard on unmount, even if init is in progress. Check if init phase has changed after services init is completed * fixed failing tests and added some more to test new scenario. Co-authored-by: Torkel Ödegaard <torkel@grafana.com> Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>pull/19405/head^2
parent
0f5b894256
commit
e65dbcfea1
@ -0,0 +1,61 @@ |
||||
import { reducerTester } from '../../../../test/core/redux/reducerTester'; |
||||
import { |
||||
initialTransactionState, |
||||
transactionReducer, |
||||
TransactionStatus, |
||||
variablesClearTransaction, |
||||
variablesCompleteTransaction, |
||||
variablesInitTransaction, |
||||
} from './transactionReducer'; |
||||
|
||||
describe('transactionReducer', () => { |
||||
describe('when variablesInitTransaction is dispatched', () => { |
||||
it('then state should be correct', () => { |
||||
reducerTester() |
||||
.givenReducer(transactionReducer, { ...initialTransactionState }) |
||||
.whenActionIsDispatched(variablesInitTransaction({ uid: 'a uid' })) |
||||
.thenStateShouldEqual({ ...initialTransactionState, uid: 'a uid', status: TransactionStatus.Fetching }); |
||||
}); |
||||
}); |
||||
|
||||
describe('when variablesCompleteTransaction is dispatched', () => { |
||||
describe('and transaction uid is the same', () => { |
||||
it('then state should be correct', () => { |
||||
reducerTester() |
||||
.givenReducer(transactionReducer, { |
||||
...initialTransactionState, |
||||
uid: 'before', |
||||
status: TransactionStatus.Fetching, |
||||
}) |
||||
.whenActionIsDispatched(variablesCompleteTransaction({ uid: 'before' })) |
||||
.thenStateShouldEqual({ ...initialTransactionState, uid: 'before', status: TransactionStatus.Completed }); |
||||
}); |
||||
}); |
||||
|
||||
describe('and transaction uid is not the same', () => { |
||||
it('then state should be correct', () => { |
||||
reducerTester() |
||||
.givenReducer(transactionReducer, { |
||||
...initialTransactionState, |
||||
uid: 'before', |
||||
status: TransactionStatus.Fetching, |
||||
}) |
||||
.whenActionIsDispatched(variablesCompleteTransaction({ uid: 'after' })) |
||||
.thenStateShouldEqual({ ...initialTransactionState, uid: 'before', status: TransactionStatus.Fetching }); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when variablesClearTransaction is dispatched', () => { |
||||
it('then state should be correct', () => { |
||||
reducerTester() |
||||
.givenReducer(transactionReducer, { |
||||
...initialTransactionState, |
||||
uid: 'before', |
||||
status: TransactionStatus.Completed, |
||||
}) |
||||
.whenActionIsDispatched(variablesClearTransaction()) |
||||
.thenStateShouldEqual({ ...initialTransactionState }); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,45 @@ |
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
||||
|
||||
export enum TransactionStatus { |
||||
NotStarted = 'Not started', |
||||
Fetching = 'Fetching', |
||||
Completed = 'Completed', |
||||
} |
||||
|
||||
export interface TransactionState { |
||||
uid: string | undefined | null; |
||||
status: TransactionStatus; |
||||
} |
||||
|
||||
export const initialTransactionState: TransactionState = { uid: null, status: TransactionStatus.NotStarted }; |
||||
|
||||
const transactionSlice = createSlice({ |
||||
name: 'templating/transaction', |
||||
initialState: initialTransactionState, |
||||
reducers: { |
||||
variablesInitTransaction: (state, action: PayloadAction<{ uid: string | undefined | null }>) => { |
||||
state.uid = action.payload.uid; |
||||
state.status = TransactionStatus.Fetching; |
||||
}, |
||||
variablesCompleteTransaction: (state, action: PayloadAction<{ uid: string | undefined | null }>) => { |
||||
if (state.uid !== action.payload.uid) { |
||||
// this might be an action from a cancelled batch
|
||||
return; |
||||
} |
||||
|
||||
state.status = TransactionStatus.Completed; |
||||
}, |
||||
variablesClearTransaction: (state, action: PayloadAction<undefined>) => { |
||||
state.uid = null; |
||||
state.status = TransactionStatus.NotStarted; |
||||
}, |
||||
}, |
||||
}); |
||||
|
||||
export const { |
||||
variablesInitTransaction, |
||||
variablesClearTransaction, |
||||
variablesCompleteTransaction, |
||||
} = transactionSlice.actions; |
||||
|
||||
export const transactionReducer = transactionSlice.reducer; |
Loading…
Reference in new issue