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

[Fleet] Fix incorrect count of agents in bulk actions #175318

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const defaultProps = {
shownAgents: 10,
inactiveShownAgents: 0,
totalManagedAgentIds: [],
inactiveManagedAgentIds: [],
selectionMode: 'manual',
currentQuery: '',
selectedAgents: [],
Expand Down Expand Up @@ -126,6 +127,10 @@ describe('AgentBulkActions', () => {
});

describe('When in query mode', () => {
mockedExperimentalFeaturesService.get.mockReturnValue({
diagnosticFileUploadEnabled: true,
} as any);

it('should show correct actions for active agents when no managed policies exist', async () => {
const results = render({
...defaultProps,
Expand Down Expand Up @@ -162,15 +167,57 @@ describe('AgentBulkActions', () => {

expect(results.getByText('Add / remove tags').closest('button')!).toBeEnabled();
expect(results.getByText('Assign to new policy').closest('button')!).toBeEnabled();
expect(results.getByText('Unenroll 8 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Upgrade 8 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Schedule upgrade for 8 agents').closest('button')!).toBeDisabled();
expect(
results.getByText('Request diagnostics for 8 agents').closest('button')!
).toBeEnabled();
expect(results.getByText('Unenroll 8 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Upgrade 8 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Schedule upgrade for 8 agents').closest('button')!).toBeDisabled();
expect(results.getByText('Restart upgrade 8 agents').closest('button')!).toBeEnabled();
});

it('should show correct actions also when there are inactive managed agents', async () => {
const results = render({
...defaultProps,
inactiveManagedAgentIds: ['agentId1', 'agentId2'],
totalManagedAgentIds: ['agentId1', 'agentId2', 'agentId3'],
selectionMode: 'query',
});

const bulkActionsButton = results.getByTestId('agentBulkActionsButton');
await act(async () => {
fireEvent.click(bulkActionsButton);
});

expect(results.getByText('Add / remove tags').closest('button')!).toBeEnabled();
expect(results.getByText('Assign to new policy').closest('button')!).toBeEnabled();
expect(results.getByText('Unenroll 9 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Upgrade 9 agents').closest('button')!).toBeEnabled();
expect(results.getByText('Schedule upgrade for 9 agents').closest('button')!).toBeDisabled();
expect(results.getByText('Restart upgrade 9 agents').closest('button')!).toBeEnabled();
});

it('should show disabled actions when only inactive agents are selected', async () => {
const results = render({
...defaultProps,
inactiveShownAgents: 10,
selectedAgents: [{ id: 'agent1' }, { id: 'agent2' }] as Agent[],
selectionMode: 'query',
});

const bulkActionsButton = results.getByTestId('agentBulkActionsButton');
await act(async () => {
fireEvent.click(bulkActionsButton);
});

expect(results.getByText('Add / remove tags').closest('button')!).toBeDisabled();
expect(results.getByText('Assign to new policy').closest('button')!).toBeDisabled();
expect(results.getByText('Unenroll 0 agents').closest('button')!).toBeDisabled();
expect(results.getByText('Upgrade 0 agents').closest('button')!).toBeDisabled();
expect(results.getByText('Schedule upgrade for 0 agents').closest('button')!).toBeDisabled();
expect(results.getByText('Restart upgrade 0 agents').closest('button')!).toBeDisabled();
});

it('should generate a correct kuery to select agents', async () => {
const results = render({
...defaultProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface Props {
shownAgents: number;
inactiveShownAgents: number;
totalManagedAgentIds: string[];
inactiveManagedAgentIds: string[];
selectionMode: SelectionMode;
currentQuery: string;
selectedAgents: Agent[];
Expand All @@ -51,6 +52,7 @@ export const AgentBulkActions: React.FunctionComponent<Props> = ({
shownAgents,
inactiveShownAgents,
totalManagedAgentIds,
inactiveManagedAgentIds,
selectionMode,
currentQuery,
selectedAgents,
Expand Down Expand Up @@ -91,18 +93,19 @@ export const AgentBulkActions: React.FunctionComponent<Props> = ({
}
}, [currentQuery, totalManagedAgentIds]);

// Check if user is working with only inactive agents
const atLeastOneActiveAgentSelected =
selectionMode === 'manual'
? !!selectedAgents.find((agent) => agent.active)
: shownAgents > inactiveShownAgents;
const totalActiveAgents = shownAgents - inactiveShownAgents;

// exclude inactive agents from the count
const agentCount =
selectionMode === 'manual'
? selectedAgents.length
: totalActiveAgents - totalManagedAgentIds?.length;
: totalActiveAgents - (totalManagedAgentIds?.length - inactiveManagedAgentIds?.length);

// Check if user is working with only inactive agents
const atLeastOneActiveAgentSelected =
selectionMode === 'manual'
? !!selectedAgents.find((agent) => agent.active)
: agentCount > 0 && shownAgents > inactiveShownAgents;
const agents = selectionMode === 'manual' ? selectedAgents : selectionQuery;

const [tagsPopoverButton, setTagsPopoverButton] = useState<HTMLElement>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface SearchAndFilterBarProps {
inactiveShownAgents: number;
totalInactiveAgents: number;
totalManagedAgentIds: string[];
inactiveManagedAgentIds: string[];
selectionMode: SelectionMode;
currentQuery: string;
selectedAgents: Agent[];
Expand Down Expand Up @@ -78,6 +79,7 @@ export const SearchAndFilterBar: React.FunctionComponent<SearchAndFilterBarProps
inactiveShownAgents,
totalInactiveAgents,
totalManagedAgentIds,
inactiveManagedAgentIds,
selectionMode,
currentQuery,
selectedAgents,
Expand Down Expand Up @@ -202,6 +204,7 @@ export const SearchAndFilterBar: React.FunctionComponent<SearchAndFilterBarProps
shownAgents={shownAgents}
inactiveShownAgents={inactiveShownAgents}
totalManagedAgentIds={totalManagedAgentIds}
inactiveManagedAgentIds={inactiveManagedAgentIds}
selectionMode={selectionMode}
currentQuery={currentQuery}
selectedAgents={selectedAgents}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
const [inactiveShownAgents, setInactiveShownAgents] = useState(0);
const [totalInactiveAgents, setTotalInactiveAgents] = useState(0);
const [totalManagedAgentIds, setTotalManagedAgentIds] = useState<string[]>([]);
const [inactiveManagedAgentIds, setinactiveManagedAgentIds] = useState<string[]>([]);
criamico marked this conversation as resolved.
Show resolved Hide resolved
const [managedAgentsOnCurrentPage, setManagedAgentsOnCurrentPage] = useState(0);
const [showAgentActivityTour, setShowAgentActivityTour] = useState({ isOpen: false });
const getSortFieldForAPI = (field: keyof Agent): string => {
Expand Down Expand Up @@ -335,7 +336,11 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
}
const allManagedAgents = response.data?.items ?? [];
const allManagedAgentIds = allManagedAgents?.map((agent) => agent.id);
const inactiveManagedIds = allManagedAgents
?.filter((agent) => agent.status === 'inactive')
.map((agent) => agent.id);
setTotalManagedAgentIds(allManagedAgentIds);
setinactiveManagedAgentIds(inactiveManagedIds);
criamico marked this conversation as resolved.
Show resolved Hide resolved

setManagedAgentsOnCurrentPage(
agentsResponse.data.items
Expand Down Expand Up @@ -606,6 +611,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
inactiveShownAgents={inactiveShownAgents}
totalInactiveAgents={totalInactiveAgents}
totalManagedAgentIds={totalManagedAgentIds}
inactiveManagedAgentIds={inactiveManagedAgentIds}
selectionMode={selectionMode}
currentQuery={kuery}
selectedAgents={selectedAgents}
Expand Down