From 619f9362ba3e64e6307b01e8985ef49fff8efe19 Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Tue, 12 Sep 2023 15:04:40 -0500 Subject: [PATCH] feat: add fe polling and disable websocket (#3151) --- web/src/gateways/WebSocket.gateway.ts | 3 ++- web/src/models/TestSuiteRun.model.ts | 4 +++ .../providers/TestRun/TestRun.provider.tsx | 2 +- .../TestSuiteRun/TestSuite.provider.tsx | 14 ++++++++--- .../Tracetest/endpoints/Setting.endpoint.ts | 16 ++---------- .../Tracetest/endpoints/TestRun.endpoint.ts | 25 ------------------- .../endpoints/TestSuiteRun.endpoint.ts | 13 ---------- 7 files changed, 20 insertions(+), 57 deletions(-) diff --git a/web/src/gateways/WebSocket.gateway.ts b/web/src/gateways/WebSocket.gateway.ts index 192513146f..0e31906d37 100644 --- a/web/src/gateways/WebSocket.gateway.ts +++ b/web/src/gateways/WebSocket.gateway.ts @@ -172,6 +172,7 @@ function getWebSocketURL() { } const webSocketGateway = WebSocketGateway({url: getWebSocketURL()}); -webSocketGateway.connect(); +// Disable websocket connection for now +// webSocketGateway.connect(); export default webSocketGateway; diff --git a/web/src/models/TestSuiteRun.model.ts b/web/src/models/TestSuiteRun.model.ts index 379a55f776..61d8ca4865 100644 --- a/web/src/models/TestSuiteRun.model.ts +++ b/web/src/models/TestSuiteRun.model.ts @@ -12,6 +12,10 @@ type TestSuiteRun = Model< } >; +export function isRunStateFinished(state: string) { + return ['FINISHED', 'FAILED'].includes(state); +} + const TestSuiteRun = ({ id = 0, createdAt = '', diff --git a/web/src/providers/TestRun/TestRun.provider.tsx b/web/src/providers/TestRun/TestRun.provider.tsx index 0a8814d597..2bc37e78b7 100644 --- a/web/src/providers/TestRun/TestRun.provider.tsx +++ b/web/src/providers/TestRun/TestRun.provider.tsx @@ -37,7 +37,7 @@ const POLLING_INTERVAL = 5000; const TestRunProvider = ({children, testId, runId = 0}: IProps) => { const [pollingInterval, setPollingInterval] = useState(POLLING_INTERVAL); const {data: run, isError} = useGetRunByIdQuery({testId, runId}, {skip: !runId, pollingInterval}); - const {data: runEvents = []} = useGetRunEventsQuery({testId, runId}, {skip: !runId}); + const {data: runEvents = []} = useGetRunEventsQuery({testId, runId}, {skip: !runId, pollingInterval}); const [stopRunAction, {isLoading: isLoadingStop}] = useStopRunMutation(); const stopRun = useCallback(async () => { diff --git a/web/src/providers/TestSuiteRun/TestSuite.provider.tsx b/web/src/providers/TestSuiteRun/TestSuite.provider.tsx index 1175c4d728..be3db7d692 100644 --- a/web/src/providers/TestSuiteRun/TestSuite.provider.tsx +++ b/web/src/providers/TestSuiteRun/TestSuite.provider.tsx @@ -1,6 +1,6 @@ -import {createContext, useContext, useMemo} from 'react'; +import {createContext, useContext, useEffect, useMemo, useState} from 'react'; import TracetestAPI from 'redux/apis/Tracetest'; -import TestSuiteRun from 'models/TestSuiteRun.model'; +import TestSuiteRun, {isRunStateFinished} from 'models/TestSuiteRun.model'; import TestSuiteProvider from '../TestSuite/TestSuite.provider'; const {useGetTestSuiteRunByIdQuery} = TracetestAPI.instance; @@ -21,10 +21,18 @@ interface IProps { export const useTestSuiteRun = () => useContext(Context); +const POLLING_INTERVAL = 5000; + const TestSuiteRunProvider = ({children, testSuiteId, runId}: IProps) => { - const {data: run} = useGetTestSuiteRunByIdQuery({testSuiteId, runId}); + const [pollingInterval, setPollingInterval] = useState(POLLING_INTERVAL); + const {data: run} = useGetTestSuiteRunByIdQuery({testSuiteId, runId}, {pollingInterval}); const value = useMemo(() => ({run: run!}), [run]); + useEffect(() => { + const shouldStopPolling = run?.state && isRunStateFinished(run.state); + setPollingInterval(shouldStopPolling ? undefined : POLLING_INTERVAL); + }, [run?.state]); + return run ? ( {children} diff --git a/web/src/redux/apis/Tracetest/endpoints/Setting.endpoint.ts b/web/src/redux/apis/Tracetest/endpoints/Setting.endpoint.ts index ca44a3eeb4..16a2fa8754 100644 --- a/web/src/redux/apis/Tracetest/endpoints/Setting.endpoint.ts +++ b/web/src/redux/apis/Tracetest/endpoints/Setting.endpoint.ts @@ -1,13 +1,12 @@ import {HTTP_METHOD} from 'constants/Common.constants'; import {TracetestApiTags} from 'constants/Test.constants'; -import Config, {TRawConfig, TRawLiveConfig} from 'models/Config.model'; +import Config, {TRawConfig} from 'models/Config.model'; import Demo, {TRawDemo} from 'models/Demo.model'; import Linter, {TRawLinter} from 'models/Linter.model'; import Polling, {TRawPolling} from 'models/Polling.model'; import TestRunner, {TRawTestRunnerResource} from 'models/TestRunner.model'; -import WebSocketService, {IListenerFunction} from 'services/WebSocket.service'; import {ResourceType, TDraftResource, TListResponse} from 'types/Settings.types'; -import { TTestApiEndpointBuilder } from '../Tracetest.api'; +import {TTestApiEndpointBuilder} from '../Tracetest.api'; export const settingsEndpoints = (builder: TTestApiEndpointBuilder) => ({ getConfig: builder.query({ @@ -20,17 +19,6 @@ export const settingsEndpoints = (builder: TTestApiEndpointBuilder) => ({ }), providesTags: () => [{type: TracetestApiTags.SETTING, id: ResourceType.ConfigType}], transformResponse: (rawConfig: TRawConfig) => Config(rawConfig), - async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) { - const listener: IListenerFunction = data => { - updateCachedData(() => Config.FromLiveUpdate(data.event)); - }; - await WebSocketService.initWebSocketSubscription({ - listener, - resource: '/app/config/update', - waitToCleanSubscription: cacheEntryRemoved, - waitToInitSubscription: cacheDataLoaded, - }); - }, }), getPolling: builder.query({ query: () => ({ diff --git a/web/src/redux/apis/Tracetest/endpoints/TestRun.endpoint.ts b/web/src/redux/apis/Tracetest/endpoints/TestRun.endpoint.ts index 3dc27784b3..4731373516 100644 --- a/web/src/redux/apis/Tracetest/endpoints/TestRun.endpoint.ts +++ b/web/src/redux/apis/Tracetest/endpoints/TestRun.endpoint.ts @@ -9,7 +9,6 @@ import Test from 'models/Test.model'; import TestRun, {TRawTestRun} from 'models/TestRun.model'; import TestRunEvent, {TRawTestRunEvent} from 'models/TestRunEvent.model'; import {TRawTestSpecs} from 'models/TestSpecs.model'; -import WebSocketService, {IListenerFunction} from 'services/WebSocket.service'; import {TTestApiEndpointBuilder} from '../Tracetest.api'; function getTotalCountFromHeaders(meta: any) { @@ -46,17 +45,6 @@ export const testRunEndpoints = (builder: TTestApiEndpointBuilder) => ({ query: ({testId, runId}) => `/tests/${testId}/run/${runId}`, providesTags: result => (result ? [{type: TracetestApiTags.TEST_RUN, id: result?.id}] : []), transformResponse: (rawTestResult: TRawTestRun) => TestRun(rawTestResult), - async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) { - const listener: IListenerFunction = data => { - updateCachedData(() => TestRun(data.event)); - }; - await WebSocketService.initWebSocketSubscription({ - listener, - resource: `test/${arg.testId}/run/${arg.runId}`, - waitToCleanSubscription: cacheEntryRemoved, - waitToInitSubscription: cacheDataLoaded, - }); - }, }), reRun: builder.mutation({ query: ({testId, runId}) => ({ @@ -108,18 +96,5 @@ export const testRunEndpoints = (builder: TTestApiEndpointBuilder) => ({ query: ({runId, testId}) => `/tests/${testId}/run/${runId}/events`, providesTags: [{type: TracetestApiTags.TEST_RUN, id: 'EVENTS'}], transformResponse: (rawTestRunEvent: TRawTestRunEvent[]) => rawTestRunEvent.map(event => TestRunEvent(event)), - async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) { - const listener: IListenerFunction = data => { - updateCachedData(draft => { - draft.push(TestRunEvent(data.event)); - }); - }; - await WebSocketService.initWebSocketSubscription({ - listener, - resource: `test/${arg.testId}/run/${arg.runId}/event`, - waitToCleanSubscription: cacheEntryRemoved, - waitToInitSubscription: cacheDataLoaded, - }); - }, }), }); diff --git a/web/src/redux/apis/Tracetest/endpoints/TestSuiteRun.endpoint.ts b/web/src/redux/apis/Tracetest/endpoints/TestSuiteRun.endpoint.ts index b5371842ac..e2a1bac6b2 100644 --- a/web/src/redux/apis/Tracetest/endpoints/TestSuiteRun.endpoint.ts +++ b/web/src/redux/apis/Tracetest/endpoints/TestSuiteRun.endpoint.ts @@ -4,7 +4,6 @@ import {PaginationResponse} from 'hooks/usePagination'; import {TVariableSetValue} from 'models/VariableSet.model'; import RunError from 'models/RunError.model'; import TestSuiteRun, {TRawTestSuiteRunResourceRun} from 'models/TestSuiteRun.model'; -import WebSocketService, {IListenerFunction} from 'services/WebSocket.service'; import {getTotalCountFromHeaders} from 'utils/Common'; import {TTestApiEndpointBuilder} from '../Tracetest.api'; @@ -45,18 +44,6 @@ export const testSuiteRunEndpoints = (builder: TTestApiEndpointBuilder) => ({ query: ({testSuiteId, runId}) => `/testsuites/${testSuiteId}/run/${runId}`, providesTags: result => [{type: TracetestApiTags.TESTSUITE_RUN, id: result?.id}], transformResponse: (raw: TRawTestSuiteRunResourceRun) => TestSuiteRun(raw), - async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) { - const listener: IListenerFunction = data => { - updateCachedData(() => TestSuiteRun(data.event)); - }; - - await WebSocketService.initWebSocketSubscription({ - listener, - resource: `testsuites/${arg.testSuiteId}/run/${arg.runId}`, - waitToCleanSubscription: cacheEntryRemoved, - waitToInitSubscription: cacheDataLoaded, - }); - }, }), deleteTestSuiteRunById: builder.mutation({