Skip to content

[Fleet] Fix bulk action dropdown - #166475

Merged
jillguyonnet merged 12 commits into
elastic:mainfrom
jillguyonnet:fleet/fix-bulk-action-dropdown
Sep 26, 2023
Merged

jillguyonnet merged 12 commits into
elastic:mainfrom
jillguyonnet:fleet/fix-bulk-action-dropdown

Conversation

@jillguyonnet

@jillguyonnet jillguyonnet commented Sep 14, 2023 •

Copy link
Copy Markdown
Member

Summary

Closes #164083
Related to #157844

This PR addresses two current issues affecting agent selection in Fleet UI:

  1. When there are inactive agents that are not listed on the current page and "Select everything on all pages" is clicked, the count of actionable agents is incorrect (cf. this comment for details). This can have two consequences:
    1. Incorrect and sometimes negative agent count in the "Actions" dropdown.
    2. Disabled menu items in the "Actions" dropdown.
  2. The "Select everything on all pages button is incorrectly displayed when there are agents on managed policies on the page and there is no pagination (cf. this comment).

Testing steps

Reproduce the bug (main branch)

  1. Have a fleet server with a managed policy. One way of doing this is to create the usual fleet server policy with fleet-server-policy id and run the following in Dev Tools:
    PUT kbn:/api/fleet/agent_policies/fleet-server-policy
    {
      "name": "Managed agent policy",
      "description": "",
      "namespace": "default",
      "monitoring_enabled": [
        "logs",
        "metrics"
      ],
      "is_managed": true
    }
    
  2. On another agent policy (Agent policy 1), enroll 30 agents, have them all inactive except for one. This can be achieved like so:
    • Enroll 30 agents with horde
    • Set agent policy inactivity timeout to 60 seconds
    • Kill horde agents
    • Enroll another agent (e.g. via a Docker container or a VM)
  3. In Fleet's main page (agents list), show agents for all policies and hide inactive (default filters), so 2 agents are visible (fleet server + active agent).
  4. Select all agents on first page:
    • Actions menu is enabled and correctly shows agent count as 1 (since fleet server is not selectable).
    • "Select everything on all pages" is displayed (incorrect 🐞).
  5. Click "Select everything on all pages": the Actions menu is disabled and agent count is negative (-29) (incorrect 🐞).

Test the fixes (this branch)

With the above setup:

  1. In Fleet's main page (agents list), show agents for all policies and hide inactive (default filters), so 2 agents are visible (fleet server + active agent).
  2. Select all agents on first page. Check that:
    • Actions menu is enabled and correctly shows agent count as 1 (since fleet server is not selectable).
    • "Select everything on all pages" is NOT displayed (fix ✅).
  3. Enroll more agents with horde so that you have enough active agents to trigger pagination (you should still have your 30 inactive agents as well).
  4. Select all agents on first page. The "Select everything on all pages" button should be displayed.
  5. Click "Select everything on all pages": check that the Actions menu is enabled and shows the correct count of agents.

Additional testing

The following scenarios should be checked as they depend on the area of the code that was amended:

Checklist

@ghost

ghost commented Sep 14, 2023

Copy link
Copy Markdown

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • /oblt-deploy-serverless : Deploy a serverless Kibana instance using the Observability test environments.
  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@jillguyonnet

Copy link
Copy Markdown
Member Author

@elasticmachine merge upstream

@jillguyonnet
jillguyonnet marked this pull request as ready for review September 19, 2023 10:57
@jillguyonnet
jillguyonnet requested a review from a team as a code owner September 19, 2023 10:57
@elasticmachine

Copy link
Copy Markdown
Contributor

Pinging @elastic/fleet (Team:Fleet)

@joshdover joshdover left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test here that reproduces the bug and would fail if the code changes weren't present? It wasn't obvious to me which test covers this scenario

showInactive,
}),
]);
const [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This useCallback hook is getting really big - should we extract this into a dedicated hook that can easily be tested in isolation? I worry that we don't have much coverage on this logic simply becuase we have to test the whole component.

const props: any = {
totalAgents: 10,
totalAgentsPaginated: 10,
totalInactiveAgentsPaginated: 0,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do the paginated variables represent? It would be good to add some explaining comments why we need totalInactiveAgents and totalInactiveAgentsPaginated as separate vars.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the naming is somewhat cumbersome, this is because there was a confusion about what "total" meant: in the current implementation, totalAgents meant all agents on the page (paginated API call) while totalInactiveAgents meant all inactive agents as it was obtained from getAgentStatus. This confusion caused the bug.

We need to know how many inactive agents there are on the page (so totalInactiveAgentsPaginated) for the Actions menu logic.

As for totalInactiveAgents, it is actually used by the AgentStatusFilter component as part of logic to determine whether there are some newly inactive agents. I had a look at it, but it looks to me like it does rely on the total number of inactive agents.

WDYT? Could there be a way to make the above simpler or clearer?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, it helps.
I think then we could make the naming clearer, what about renaming totalAgents to totalAgentsOnCurrentPage and totalInactiveAgentsPaginated to inactiveAgentsOnCurrentPage?

@jillguyonnet

Copy link
Copy Markdown
Member Author

Hey @juliaElastic - thanks for your question about paginated variables, as I was refactoring the variable names I found out that my understanding hadn't been completely correct. I've made new changes with hopefully clearer naming. In the agent list root page (agent_list_page/index.tsx), these are the relevant variables:

  • agentsOnCurrentPage: list of agents shown on the current page
  • shownAgents: number of agents listed in the table (not just the current page)
  • inactiveShownAgents: number of inactive agents listed in the table
  • totalInactiveAgents: number of inactive agents in total regardless of filtering (so that inactiveShownAgents is either 0 or equal to totalInactiveAgents depending on whether inactive agents are included in the filter)
  • totalManagedAgentIds: list of agent ids with managed policy in total
  • managedAgentsOnCurrentPage: number of agents with managed policy on current page

I am not completely happy with the data fetching that file in the current state. As Josh pointed out, it is getting very long, plus I am concerned that the additional fetching of managed agents data is not implemented in the most efficient way. I might require a few pointers in order to sort that out.

@jillguyonnet

Copy link
Copy Markdown
Member Author

@elasticmachine merge upstream

@jillguyonnet

Copy link
Copy Markdown
Member Author

@elasticmachine merge upstream

@kpollich
kpollich self-requested a review September 22, 2023 15:20
@jillguyonnet

Copy link
Copy Markdown
Member Author

@elasticmachine merge upstream

@jillguyonnet

jillguyonnet commented Sep 26, 2023 •

Copy link
Copy Markdown
Member Author

@elastic/fleet I was checking that the tests cover what we want and I need to clarify the following question: the logic for the number of agents considered for bulk actions is different between manual and query modes. Is this expected? Note that this is existing logic.

Edit: found a mention that active is only set to false in the index if the agent is unenrolled, so I guess what I'm looking for is confirmation that this the definition of "active" that should be valid in this logic.

Details:

  • Assuming I have 1 fleet server, 1 active agent, 30 inactive agents (by inactive, I mean the Fleet status, not the active field in the agent, which is true).
  • When I select inactive agents manually, they are counted towards bulk action.
  • When I click "Select everything on all pages" and there are selected inactive agents, they are not counted towards bulk actions.

Manual mode:
Screenshot 2023-09-26 at 10 21 33

Query mode:
Screenshot 2023-09-26 at 10 21 46

Inactive Fleet status but active agent:
Screenshot 2023-09-26 at 10 35 12

@jillguyonnet

Copy link
Copy Markdown
Member Author

Per conversation with @jlind23 - we'll address the above question separately.

@juliaElastic

Copy link
Copy Markdown
Contributor

When I click "Select everything on all pages" and there are selected inactive agents, they are not counted towards bulk actions.

Is there an extra check/filter that excludes inactive agents in this case?
I don't recall deliberately excluding inactive agents, we could check the history to see why it was implemented this way.
I think it would be good to have consistency in manual and query actions, to me it sounds simpler to include inactive in both cases.

showInactive ? totalInactiveAgentsResponse.data.results.inactive || 0 : 0
);

const managedAgentPolicies = managedAgentPoliciesResponse.data?.items ?? [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add more unit tests on the logic here?

@juliaElastic juliaElastic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the changes.

@kibana-ci

kibana-ci commented Sep 26, 2023 •

Copy link
Copy Markdown

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #23 / serverless examples UI Field formats example "before all" hook for "renders field formats example 1"

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
fleet 1.2MB 1.2MB +425.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@jillguyonnet

Copy link
Copy Markdown
Member Author

Thanks @juliaElastic for reviewing! I'll open a separate issue for the inactive agents question, I agree it should be consistent.

Regarding unit testing on managed agents, this is tested in children components as it only affects the props the parent is passing down. There is another task on this table coming up (https://github.com/elastic/ingest-dev/issues/1937) which will likely complexify the state, which I think would be a good opportunity to refactor how it is handled.

@joshdover

Copy link
Copy Markdown
Contributor

@jillguyonnet Can we backport this to 8.10.x as well?

@jillguyonnet

Copy link
Copy Markdown
Member Author

@joshdover Ah sorry! Can this be done after merging?

@joshdover joshdover added backport:prev-minor and removed backport:skip This PR does not require backporting labels Sep 26, 2023
@joshdover

Copy link
Copy Markdown
Contributor

No worries, just wanted to make sure there weren't any technical reasons not to. Yes you can by updating the labels, I went ahead and did it. The automation should pick it up soon.

@kibanamachine

Copy link
Copy Markdown
Contributor

💔 All backports failed

Status Branch Result
❌ 8.10 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 166475

Questions ?

Please refer to the Backport tool documentation

@jillguyonnet

Copy link
Copy Markdown
Member Author

💚 All backports created successfully

Status Branch Result
✅ 8.10

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

jillguyonnet added a commit to jillguyonnet/kibana that referenced this pull request Sep 27, 2023
Closes elastic#164083
Related to elastic/sdh-beats#3759
Related to elastic#157844

This PR addresses two current issues affecting agent selection in Fleet
UI:
1. When there are inactive agents that are not listed on the current
page and "Select everything on all pages" is clicked, the count of
actionable agents is incorrect (cf. [this
comment](elastic#164083 (comment))
for details). This can have two consequences:
1. Incorrect and sometimes negative agent count in the "Actions"
dropdown.
   2. Disabled menu items in the "Actions" dropdown.
2. The "Select everything on all pages button is incorrectly displayed
when there are agents on managed policies on the page and there is no
pagination (cf. [this
comment](elastic#164083 (comment))).

(cherry picked from commit 591df70)

# Conflicts:
#	x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/components/bulk_actions.test.tsx
jillguyonnet added a commit that referenced this pull request Sep 27, 2023
# Backport

This will backport the following commits from `main` to `8.10`:
- [[Fleet] Fix bulk action dropdown
(#166475)](#166475)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Jill
Guyonnet","email":"jill.guyonnet@elastic.co"},"sourceCommit":{"committedDate":"2023-09-26T13:15:08Z","message":"[Fleet]
Fix bulk action dropdown (#166475)\n\nCloses
https://github.com/elastic/kibana/issues/164083\r\nRelated to
https://github.com/elastic/sdh-beats/issues/3759\r\nRelated to
https://github.com/elastic/kibana/issues/157844\r\n\r\nThis PR addresses
two current issues affecting agent selection in Fleet\r\nUI:\r\n1. When
there are inactive agents that are not listed on the current\r\npage and
\"Select everything on all pages\" is clicked, the count
of\r\nactionable agents is incorrect (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711780591)\r\nfor
details). This can have two consequences:\r\n1. Incorrect and sometimes
negative agent count in the \"Actions\"\r\ndropdown.\r\n 2. Disabled
menu items in the \"Actions\" dropdown.\r\n2. The \"Select everything on
all pages button is incorrectly displayed\r\nwhen there are agents on
managed policies on the page and there is no\r\npagination (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711781808)).","sha":"591df706da23663c898247f23faa00c015c2d26c","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Fleet","backport:prev-minor","v8.11.0"],"number":166475,"url":"https://github.com/elastic/kibana/pull/166475","mergeCommit":{"message":"[Fleet]
Fix bulk action dropdown (#166475)\n\nCloses
https://github.com/elastic/kibana/issues/164083\r\nRelated to
https://github.com/elastic/sdh-beats/issues/3759\r\nRelated to
https://github.com/elastic/kibana/issues/157844\r\n\r\nThis PR addresses
two current issues affecting agent selection in Fleet\r\nUI:\r\n1. When
there are inactive agents that are not listed on the current\r\npage and
\"Select everything on all pages\" is clicked, the count
of\r\nactionable agents is incorrect (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711780591)\r\nfor
details). This can have two consequences:\r\n1. Incorrect and sometimes
negative agent count in the \"Actions\"\r\ndropdown.\r\n 2. Disabled
menu items in the \"Actions\" dropdown.\r\n2. The \"Select everything on
all pages button is incorrectly displayed\r\nwhen there are agents on
managed policies on the page and there is no\r\npagination (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711781808)).","sha":"591df706da23663c898247f23faa00c015c2d26c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/166475","number":166475,"mergeCommit":{"message":"[Fleet]
Fix bulk action dropdown (#166475)\n\nCloses
https://github.com/elastic/kibana/issues/164083\r\nRelated to
https://github.com/elastic/sdh-beats/issues/3759\r\nRelated to
https://github.com/elastic/kibana/issues/157844\r\n\r\nThis PR addresses
two current issues affecting agent selection in Fleet\r\nUI:\r\n1. When
there are inactive agents that are not listed on the current\r\npage and
\"Select everything on all pages\" is clicked, the count
of\r\nactionable agents is incorrect (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711780591)\r\nfor
details). This can have two consequences:\r\n1. Incorrect and sometimes
negative agent count in the \"Actions\"\r\ndropdown.\r\n 2. Disabled
menu items in the \"Actions\" dropdown.\r\n2. The \"Select everything on
all pages button is incorrectly displayed\r\nwhen there are agents on
managed policies on the page and there is no\r\npagination (cf.
[this\r\ncomment](https://github.com/elastic/kibana/issues/164083#issuecomment-1711781808)).","sha":"591df706da23663c898247f23faa00c015c2d26c"}}]}]
BACKPORT-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Fleet] Bulk action dropdown is disabled and shows negative agent counts

6 participants