Skip to content

Implement Asset Inventory Bar Chart#210938

Merged
albertoblaz merged 9 commits intoelastic:mainfrom
albertoblaz:asset-inv-chart
Feb 27, 2025
Merged

Implement Asset Inventory Bar Chart#210938
albertoblaz merged 9 commits intoelastic:mainfrom
albertoblaz:asset-inv-chart

Conversation

@albertoblaz
Copy link
Contributor

@albertoblaz albertoblaz commented Feb 12, 2025

Summary

Closes #201711.

Implement "Top 10 Asset Types" bar chart.

  • The X-axis shows all assets grouped by category (entity.category field), one category per bar
  • Each bar shows stacked subgroups of assets by source (entity.type field)
  • The Y-axis shows the counts of assets

Depends on

Screenshots

Loading state (animated spinner from Elastic Charts) Screenshot 2025-02-25 at 18 14 39
Fetching state (animated progress bar) Screenshot 2025-02-25 at 18 14 58
Chart with fetched data Screenshot 2025-02-24 at 13 11 03
Chart with filtered, fetched data (by type: "Identity") Screenshot 2025-02-24 at 13 11 17
Empty chart - no data Screenshot 2025-02-13 at 09 47 08

Definition of done

  • Add a bar chart titled "Top 10 Asset Types" to the "Asset Inventory" page.
  • Use the @elastic/charts library to implement the visualization.
  • Configure the chart with:
    • X-axis: Asset type categories
    • Y-axis: Count of assets
    • Legend: A color-coded key linking each bar to a specific category.
  • Ensure the chart is responsive when resizing the screen and adheres to the visual spec.
  • Integrate the chart so that it updates based on the filters section and the Unified Header component.

How to test

Follow the instructions from this PR to prepare the local env with data.

Alternatively, open the asset_inventory/components/top_assets_bar_chart.tsx file and edit yourself the data prop that we pass into <BarSeries> with mocked data. The data must have the following shape:

[
  { category: 'cloud-compute', source: 'gcp-compute', count: 500, },
  { category: 'cloud-compute', source: 'aws-security', count: 300, },
  { category: 'cloud-storage', source: 'gcp-compute', count: 221, },
  { category: 'cloud-storage', source: 'aws-security', count: 117, },
];

Checklist

  • This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The release_note:breaking label should be applied in these situations.
  • The PR description includes the appropriate Release Notes section, and the correct release_note:* label is applied per the guidelines

Identify risks

No risks whatsoever.

@albertoblaz albertoblaz added release_note:skip Skip the PR/issue when compiling release notes backport:skip This PR does not require backporting Team:Cloud Security Cloud Security team related labels Feb 12, 2025
@albertoblaz albertoblaz self-assigned this Feb 12, 2025
@albertoblaz albertoblaz force-pushed the asset-inv-chart branch 2 times, most recently from 2e98414 to 9c697ef Compare February 18, 2025 12:30
@albertoblaz albertoblaz marked this pull request as ready for review February 18, 2025 13:01
@albertoblaz albertoblaz requested review from a team as code owners February 18, 2025 13:01
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-cloud-security-posture (Team:Cloud Security)

Copy link
Contributor

@JordanSh JordanSh left a comment

Choose a reason for hiding this comment

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

im requesting changes because the aggregation logic should be using es query instead of our own. also, rows could be limited in some way. for example in csp we have limited the amount of items to 500, but we probably want to show the chart for the entire data that we have.

if those assumptions are wrong lmk and so i can remove this block

Copy link
Contributor

@opauloh opauloh left a comment

Choose a reason for hiding this comment

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

as per the discussions I suggest we create a new fetcher function, with aggregated data by entity.type

@albertoblaz albertoblaz force-pushed the asset-inv-chart branch 4 times, most recently from 3e4497c to 913f8e4 Compare February 24, 2025 14:14
notifications: { toasts },
} = useKibana().services;
return useQuery(
['asset_inventory_top_assets_chart', { params: options }],
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Consider extracting the React Query key into a constant to improve maintainability and avoid potential duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll tackle this and other minor changes in a refactor PR. But I totally agree with you 💯

import type * as estypes from '@elastic/elasticsearch/lib/api/types';
import { showErrorToast } from '@kbn/cloud-security-posture';
import type { IKibanaSearchResponse, IKibanaSearchRequest } from '@kbn/search-types';
import type { FindingsBaseEsQuery } from '@kbn/cloud-security-posture';
Copy link
Contributor

Choose a reason for hiding this comment

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

Todo: I think we should rename FindingsBaseEsQuery to not be related to Findings, i.e something like BaseEsQuery (in a separate PR)

Copy link
Contributor

Choose a reason for hiding this comment

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

(can be done in this PR as well if its just a simple rename)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@opauloh @JordanSh I raised a separate PR, even if minor, since it touches a bunch of unrelated files. Still in draft since it depends on this PR:

@albertoblaz
Copy link
Contributor Author

/ci

size: 0,
index: ASSET_INVENTORY_INDEX_PATTERN,
aggs: {
'0': {
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure i follow the 0/1/2 patterns that are repeating here, is this a part of ES? if its just a convenient naming pattern lets find a meaningful name for those

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's part of ES. I used Lens to build a bar chart that splits bars by type and the aggregation payload used these numbers. I tried simplifying the payload and removing the value_count but the aggregation didn't work and returned errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

lens? im pretty sure we used eui charts and not lens. unless you mean in another PR we also have a lens graph?
regardless, what im asking is can 0 be renamed to something like "entityCategories", 1 to "entityTypes", 2 to "entityIds"

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually instructed Alberto on how to use Lens to build a chart and retrieve the ES Query, but I forgot to mention that we should rename the aggregations it generates to a more readable value, good call @JordanSh

rawResponse: { aggregations },
} = await lastValueFrom(
data.search.search<TopAssetsRequest, TopAssetsResponse>({
params: getTopAssetsQuery(options) as TopAssetsRequest['params'],
Copy link
Contributor

Choose a reason for hiding this comment

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

do we actually need type casting here? would be nice if we could add a return type to getTopAssetsQuery

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it's needed. Found the same type casting in several files:

  • packages/kbn-cloud-security-posture/public/src/hooks/use_misconfiguration_findings.ts
  • packages/kbn-cloud-security-posture/public/src/hooks/use_misconfiguration_preview.ts
  • packages/kbn-cloud-security-posture/public/src/hooks/use_vulnerabilities_findings.ts
  • packages/kbn-cloud-security-posture/public/src/hooks/use_vulnerabilities_preview.ts
  • plugins/cloud_security_posture/public/pages/vulnerabilities/hooks/use_latest_vulnerabilities.tsx


const {
data: chartData,
// error: fetchChartDataError,
Copy link
Contributor

Choose a reason for hiding this comment

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

remove if not needed

Copy link
Contributor

@opauloh opauloh left a comment

Choose a reason for hiding this comment

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

LGTM

@elasticmachine
Copy link
Contributor

elasticmachine commented Feb 26, 2025

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 7094 7097 +3

Async chunks

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

id before after diff
securitySolution 8.9MB 8.9MB +3.1KB

History

cc @albertoblaz

@albertoblaz albertoblaz merged commit f0584d9 into elastic:main Feb 27, 2025
9 checks passed
@albertoblaz albertoblaz deleted the asset-inv-chart branch February 27, 2025 08:01
JoseLuisGJ pushed a commit to JoseLuisGJ/kibana that referenced this pull request Feb 27, 2025
## Summary

Closes elastic#201711.

Implement "Top 10 Asset Types" bar chart. 
- The X-axis shows all assets grouped by category (`entity.category`
field), one category per bar
- Each bar shows stacked subgroups of assets by source (`entity.type`
field)
- The Y-axis shows the counts of assets

### Depends on

- elastic#208417 so that the chart
renders data fetched dynamically. When it gets merged, this one will be
rebased and will only contain the last commit as changes.

### Screenshots

<details><summary>Loading state (animated spinner from <a
href="https://eui.elastic.co/#/display/loading#chart"
target="_blank">Elastic Charts</a>)</summary>
<img width="1378" alt="Screenshot 2025-02-25 at 18 14 39"
src="https://github.com/user-attachments/assets/553294e2-aaee-40c0-b1bb-de3e85f64d78"
/>
</details> 

<details><summary>Fetching state (animated progress bar)</summary>
<img width="1376" alt="Screenshot 2025-02-25 at 18 14 58"
src="https://github.com/user-attachments/assets/accdbc0e-40a2-4b30-9f4e-808a466be4d5"
/>
</details>

<details><summary>Chart with fetched data</summary>
<img width="1428" alt="Screenshot 2025-02-24 at 13 11 03"
src="https://github.com/user-attachments/assets/3c455bc8-5bdd-4ea2-a946-53e138ae081b"
/>
</details>

<details><summary>Chart with filtered, fetched data (by type:
"Identity")</summary>
<img width="1429" alt="Screenshot 2025-02-24 at 13 11 17"
src="https://github.com/user-attachments/assets/a1e75210-757e-42d1-b852-945de5f3f44b"
/>
</details>

<details><summary>Empty chart - no data</summary>
<img width="1258" alt="Screenshot 2025-02-13 at 09 47 08"
src="https://github.com/user-attachments/assets/c239a5a6-337e-41c9-a9a3-7cdc2c9b1e01"
/>
</details>

### Definition of done

- [x] Add a bar chart titled "Top 10 Asset Types" to the "Asset
Inventory" page.
- [x] Use the `@elastic/charts` library to implement the visualization.
- [x] Configure the chart with:
  - **X-axis:** Asset type categories
  - **Y-axis:** Count of assets
- **Legend:** A color-coded key linking each bar to a specific category.
- [x] Ensure the chart is responsive when resizing the screen and
adheres to the [visual
spec](https://www.figma.com/design/9zUqAhhglT1EGYG4LOl1X6/Asset-Management?node-id=2946-19648&t=FuD3BEY4FyxAKV38-4).
- [x] Integrate the chart so that it updates based on the filters
section and the Unified Header component.

### How to test

Follow the instructions from [this
PR](elastic#208417) to prepare the local
env with data.

Alternatively, open the
`asset_inventory/components/top_assets_bar_chart.tsx` file and edit
yourself the `data` prop that we pass into `<BarSeries>` with mocked
data. The data must have the following shape:

```js
[
  { category: 'cloud-compute', source: 'gcp-compute', count: 500, },
  { category: 'cloud-compute', source: 'aws-security', count: 300, },
  { category: 'cloud-storage', source: 'gcp-compute', count: 221, },
  { category: 'cloud-storage', source: 'aws-security', count: 117, },
];
```

### Checklist

- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

No risks whatsoever.
albertoblaz added a commit that referenced this pull request Feb 27, 2025
## Summary

Rename the `FindingsBaseEsQuery` interface exposed by the
`@kibana/cloud-security-posture` package as well as all references where
it's imported.

Separating this renaming into its own PR also lets us tag it with
`backport:prev-minor` and avoid potential merge conflicts in the future.

### Depends on

- #210938

### Checklist

- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

No risk whatsoever.
albertoblaz added a commit to albertoblaz/kibana that referenced this pull request Feb 27, 2025
Rename the `FindingsBaseEsQuery` interface exposed by the
`@kibana/cloud-security-posture` package as well as all references where
it's imported.

Separating this renaming into its own PR also lets us tag it with
`backport:prev-minor` and avoid potential merge conflicts in the future.

- elastic#210938

- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

No risk whatsoever.

(cherry picked from commit aac8413)
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Mar 22, 2025
## Summary

Closes elastic#201711.

Implement "Top 10 Asset Types" bar chart. 
- The X-axis shows all assets grouped by category (`entity.category`
field), one category per bar
- Each bar shows stacked subgroups of assets by source (`entity.type`
field)
- The Y-axis shows the counts of assets

### Depends on

- elastic#208417 so that the chart
renders data fetched dynamically. When it gets merged, this one will be
rebased and will only contain the last commit as changes.

### Screenshots

<details><summary>Loading state (animated spinner from <a
href="https://eui.elastic.co/#/display/loading#chart"
target="_blank">Elastic Charts</a>)</summary>
<img width="1378" alt="Screenshot 2025-02-25 at 18 14 39"
src="https://github.com/user-attachments/assets/553294e2-aaee-40c0-b1bb-de3e85f64d78"
/>
</details> 

<details><summary>Fetching state (animated progress bar)</summary>
<img width="1376" alt="Screenshot 2025-02-25 at 18 14 58"
src="https://github.com/user-attachments/assets/accdbc0e-40a2-4b30-9f4e-808a466be4d5"
/>
</details>

<details><summary>Chart with fetched data</summary>
<img width="1428" alt="Screenshot 2025-02-24 at 13 11 03"
src="https://github.com/user-attachments/assets/3c455bc8-5bdd-4ea2-a946-53e138ae081b"
/>
</details>

<details><summary>Chart with filtered, fetched data (by type:
"Identity")</summary>
<img width="1429" alt="Screenshot 2025-02-24 at 13 11 17"
src="https://github.com/user-attachments/assets/a1e75210-757e-42d1-b852-945de5f3f44b"
/>
</details>

<details><summary>Empty chart - no data</summary>
<img width="1258" alt="Screenshot 2025-02-13 at 09 47 08"
src="https://github.com/user-attachments/assets/c239a5a6-337e-41c9-a9a3-7cdc2c9b1e01"
/>
</details>

### Definition of done

- [x] Add a bar chart titled "Top 10 Asset Types" to the "Asset
Inventory" page.
- [x] Use the `@elastic/charts` library to implement the visualization.
- [x] Configure the chart with:
  - **X-axis:** Asset type categories
  - **Y-axis:** Count of assets
- **Legend:** A color-coded key linking each bar to a specific category.
- [x] Ensure the chart is responsive when resizing the screen and
adheres to the [visual
spec](https://www.figma.com/design/9zUqAhhglT1EGYG4LOl1X6/Asset-Management?node-id=2946-19648&t=FuD3BEY4FyxAKV38-4).
- [x] Integrate the chart so that it updates based on the filters
section and the Unified Header component.

### How to test

Follow the instructions from [this
PR](elastic#208417) to prepare the local
env with data.

Alternatively, open the
`asset_inventory/components/top_assets_bar_chart.tsx` file and edit
yourself the `data` prop that we pass into `<BarSeries>` with mocked
data. The data must have the following shape:

```js
[
  { category: 'cloud-compute', source: 'gcp-compute', count: 500, },
  { category: 'cloud-compute', source: 'aws-security', count: 300, },
  { category: 'cloud-storage', source: 'gcp-compute', count: 221, },
  { category: 'cloud-storage', source: 'aws-security', count: 117, },
];
```

### Checklist

- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

No risks whatsoever.
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Mar 22, 2025
## Summary

Rename the `FindingsBaseEsQuery` interface exposed by the
`@kibana/cloud-security-posture` package as well as all references where
it's imported.

Separating this renaming into its own PR also lets us tag it with
`backport:prev-minor` and avoid potential merge conflicts in the future.

### Depends on

- elastic#210938

### Checklist

- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

No risk whatsoever.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:Cloud Security Cloud Security team related v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement "Top 10 Asset Types" Bar Chart Visualization

6 participants