From db8c8ace6d48e0cc471cd21e114155063a20bb76 Mon Sep 17 00:00:00 2001 From: criamico Date: Thu, 7 Apr 2022 17:53:38 +0200 Subject: [PATCH] Address code review comments; add retry logic to incoming data component --- .../agent_enrollment_flyout.test.mocks.ts | 4 -- .../agent_enrollment_flyout.test.tsx | 39 ----------- .../confirm_agent_enrollment.tsx | 2 +- .../confirm_incoming_data.tsx | 18 +++-- .../steps/compute_steps.tsx | 2 +- .../steps/incoming_data_confirmation_step.tsx | 8 +-- .../agent_enrollment_flyout/types.ts | 3 +- .../use_get_agent_incoming_data.tsx | 65 +++++++++++++------ .../standalone/index.tsx | 15 +++-- x-pack/plugins/fleet/public/hooks/index.ts | 1 - .../fleet/server/services/agents/status.ts | 2 +- 11 files changed, 74 insertions(+), 85 deletions(-) rename x-pack/plugins/fleet/public/{hooks => components/agent_enrollment_flyout}/use_get_agent_incoming_data.tsx (65%) diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts index c06f2e3bb532ba..408f884116e260 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts @@ -76,10 +76,6 @@ jest.mock('./steps', () => { 'data-test-subj': 'agent-enrollment-key-selection-step', title: 'agent-enrollment-key-selection-step', }), - ViewDataStep: jest.fn().mockReturnValue({ - 'data-test-subj': 'view-data-step', - title: 'view-data-step', - }), DownloadStep: jest .fn() .mockReturnValue({ 'data-test-subj': 'download-step', title: 'download-step' }), diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx index 757df51366681e..7a4d1ed44c406e 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx @@ -189,44 +189,6 @@ describe('', () => { expect(exists('agent-enrollment-key-selection-step')).toBe(true); }); }); - - // Skipped due to implementation details in the step components. See https://github.com/elastic/kibana/issues/103894 - describe.skip('"View data" extension point', () => { - it('shows the "View data" step when UI extension is provided', async () => { - jest.clearAllMocks(); - await act(async () => { - testBed = await setup({ - onClose: jest.fn(), - }); - testBed.component.update(); - }); - const { exists, actions } = testBed; - expect(exists('agentEnrollmentFlyout')).toBe(true); - expect(exists('view-data-step')).toBe(true); - - jest.clearAllMocks(); - actions.goToStandaloneTab(); - expect(exists('agentEnrollmentFlyout')).toBe(true); - expect(exists('view-data-step')).toBe(false); - }); - - it('does not call the "View data" step when UI extension is not provided', async () => { - jest.clearAllMocks(); - await act(async () => { - testBed = await setup({ - onClose: jest.fn(), - }); - testBed.component.update(); - }); - const { exists, actions } = testBed; - expect(exists('agentEnrollmentFlyout')).toBe(true); - expect(exists('view-data-step')).toBe(false); - - jest.clearAllMocks(); - actions.goToStandaloneTab(); - expect(exists('view-data-step')).toBe(false); - }); - }); }); // Skipped due to UI changing in https://github.com/elastic/kibana/issues/125534. These tests should be rethought overall @@ -234,7 +196,6 @@ describe('', () => { describe.skip('standalone instructions', () => { it('uses the agent policy selection step', async () => { const { exists, actions } = testBed; - actions.goToStandaloneTab(); expect(exists('agentEnrollmentFlyout')).toBe(true); diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_agent_enrollment.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_agent_enrollment.tsx index 98b25b346b105a..1b27ad37f5c168 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_agent_enrollment.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_agent_enrollment.tsx @@ -25,7 +25,7 @@ const POLLING_INTERVAL_MS = 5 * 1000; // 5 sec * Hook for finding agents enrolled since component was rendered. Should be * used by parent component to power rendering * @param policyId - * @returns + * @returns agentIds */ export const usePollingAgentCount = (policyId: string) => { const [agentIds, setAgentIds] = useState([]); diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_incoming_data.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_incoming_data.tsx index 46d2be2a08aff7..b74129e443e458 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_incoming_data.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/confirm_incoming_data.tsx @@ -9,23 +9,29 @@ import React from 'react'; import { EuiCallOut, EuiText, EuiSpacer, EuiButton } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import type { InstalledIntegrationPolicy } from '../../hooks'; -import { useGetAgentIncomingData } from '../../hooks'; +import type { InstalledIntegrationPolicy } from './use_get_agent_incoming_data'; +import { useGetAgentIncomingData, usePollingIncomingData } from './use_get_agent_incoming_data'; + interface Props { - agentsIds: string[]; + agentIds: string[]; installedPolicy?: InstalledIntegrationPolicy; agentDataConfirmed: boolean; setAgentDataConfirmed: (v: boolean) => void; } export const ConfirmIncomingData: React.FunctionComponent = ({ - agentsIds, + agentIds, installedPolicy, agentDataConfirmed, setAgentDataConfirmed, }) => { - const { enrolledAgents, numAgentsWithData, isLoading, linkButton, message } = - useGetAgentIncomingData(agentsIds, installedPolicy); + const { incomingData, isLoading } = usePollingIncomingData(agentIds); + + const { enrolledAgents, numAgentsWithData, linkButton, message } = useGetAgentIncomingData( + incomingData, + installedPolicy + ); + if (!isLoading && enrolledAgents > 0 && numAgentsWithData > 0) { setAgentDataConfirmed(true); } diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx index ec267ebe82224c..cae508299f259f 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/compute_steps.tsx @@ -260,7 +260,7 @@ export const ManagedSteps: React.FunctionComponent = ({ if (selectedPolicy && enrolledAgentIds.length) { steps.push( IncomingDataConfirmationStep({ - agentsIds: enrolledAgentIds, + agentIds: enrolledAgentIds, agentDataConfirmed, setAgentDataConfirmed, installedPolicy: installedPackagePolicy, diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/incoming_data_confirmation_step.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/incoming_data_confirmation_step.tsx index 9a4071727bcc04..76cd1c07f46300 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/incoming_data_confirmation_step.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/steps/incoming_data_confirmation_step.tsx @@ -11,17 +11,17 @@ import { i18n } from '@kbn/i18n'; import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/steps'; -import type { InstalledIntegrationPolicy } from '../../../hooks'; +import type { InstalledIntegrationPolicy } from '../use_get_agent_incoming_data'; import { ConfirmIncomingData } from '../confirm_incoming_data'; export const IncomingDataConfirmationStep = ({ - agentsIds, + agentIds, installedPolicy, agentDataConfirmed, setAgentDataConfirmed, }: { - agentsIds: string[]; + agentIds: string[]; installedPolicy?: InstalledIntegrationPolicy; agentDataConfirmed: boolean; setAgentDataConfirmed: (v: boolean) => void; @@ -36,7 +36,7 @@ export const IncomingDataConfirmationStep = ({ }), children: ( { - const [isLoading, setIsLoading] = useState(true); - const [incomingData, setIncomingData] = useState([]); - - useEffect(() => { - const getIncomingData = async () => { - const { data } = await sendGetAgentIncomingData({ agentsIds }); - if (data?.items) { - setIncomingData(data?.items); - setIsLoading(false); - } - }; - if (agentsIds) { - getIncomingData(); - } - }, [agentsIds]); - const enrolledAgents = useMemo(() => incomingData.length, [incomingData.length]); const numAgentsWithData = useMemo( () => @@ -83,8 +67,47 @@ export const useGetAgentIncomingData = ( return { enrolledAgents, numAgentsWithData, - isLoading, linkButton, message, }; }; + +/** + * Hook for polling incoming data for the selected agent policy. + * @param agentIds + * @returns incomingData, isLoading + */ +const POLLING_INTERVAL_MS = 5 * 1000; // 5 sec + +export const usePollingIncomingData = (agentsIds: string[]) => { + const timeout = useRef(undefined); + const [incomingData, setIncomingData] = useState([]); + const [isLoading, setIsLoading] = useState(true); + + useEffect(() => { + let isAborted = false; + + const poll = () => { + timeout.current = window.setTimeout(async () => { + const { data } = await sendGetAgentIncomingData({ agentsIds }); + + if (data?.items) { + setIncomingData(data?.items); + setIsLoading(false); + } + if (!isAborted) { + poll(); + } + }, POLLING_INTERVAL_MS); + }; + + poll(); + if (isAborted || incomingData.length > 0) clearTimeout(timeout.current); + + return () => { + isAborted = true; + }; + }, [agentsIds, incomingData]); + + return { incomingData, isLoading }; +}; diff --git a/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx b/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx index dc3a821e300e53..db36c16934e4b0 100644 --- a/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx +++ b/x-pack/plugins/fleet/public/components/enrollment_instructions/standalone/index.tsx @@ -14,16 +14,19 @@ export const StandaloneInstructions = ( const KUBERNETES_RUN_INSTRUCTIONS = 'kubectl apply -f elastic-agent-standalone-kubernetes.yaml'; const STANDALONE_RUN_INSTRUCTIONS_LINUX = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz - tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz - sudo ./elastic-agent install`; +tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz +cd elastic-agent-${kibanaVersion}-linux-x86_64 +sudo ./elastic-agent install`; const STANDALONE_RUN_INSTRUCTIONS_MAC = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz - tar xzvf elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz - sudo ./elastic-agent install`; +tar xzvf elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz +cd elastic-agent-${kibanaVersion}-darwin-x86_64 +sudo ./elastic-agent install`; const STANDALONE_RUN_INSTRUCTIONS_WINDOWS = `wget https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip - Expand-Archive .\elastic-agent-${kibanaVersion}-windows-x86_64.zip - .\\elastic-agent.exe install`; +Expand-Archive .\elastic-agent-${kibanaVersion}-windows-x86_64.zip +cd elastic-agent-${kibanaVersion}-windows-x86_64 +.\\elastic-agent.exe install`; const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-amd64.deb sudo dpkg -i elastic-agent-${kibanaVersion}-amd64.deb \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`; diff --git a/x-pack/plugins/fleet/public/hooks/index.ts b/x-pack/plugins/fleet/public/hooks/index.ts index c5dcdd78b9bb9d..5c995131396b40 100644 --- a/x-pack/plugins/fleet/public/hooks/index.ts +++ b/x-pack/plugins/fleet/public/hooks/index.ts @@ -27,4 +27,3 @@ export * from './use_platform'; export * from './use_agent_policy_refresh'; export * from './use_package_installations'; export * from './use_agent_enrollment_flyout_data'; -export * from './use_get_agent_incoming_data'; diff --git a/x-pack/plugins/fleet/server/services/agents/status.ts b/x-pack/plugins/fleet/server/services/agents/status.ts index 828a3b1622cef8..90113d1d177efc 100644 --- a/x-pack/plugins/fleet/server/services/agents/status.ts +++ b/x-pack/plugins/fleet/server/services/agents/status.ts @@ -144,7 +144,7 @@ export async function getIncomingDataByAgentsId( agent_ids: { terms: { field: 'agent.id', - size: 10, + size: agentsIds.length, }, }, },