Skip to content

Comments

[ResponseOps] Cases analytics index creation and backfill#219211

Merged
adcoelho merged 10 commits intoelastic:cases-analytics-indexfrom
adcoelho:cai-index-creation-and-backfill
Jun 11, 2025
Merged

[ResponseOps] Cases analytics index creation and backfill#219211
adcoelho merged 10 commits intoelastic:cases-analytics-indexfrom
adcoelho:cai-index-creation-and-backfill

Conversation

@adcoelho
Copy link
Contributor

@adcoelho adcoelho commented Apr 25, 2025

Closes #221234

Merging into a feature branch

Summary

This PR adds:

  • The analytics index creation when the cases plugin starts.
  • Backfill task registration.
  • Backfill task scheduling.
  • Mapping version 1 for:
    • .internal.cases
    • .internal.cases-comments
    • .internal.cases-attachments
  • All corresponding painless scripts.

How to review

  1. The main functionality is in analytics_index.ts. The class in this file implements the index creation algorithm of the RFC.
  2. Three folders instantiate this class to create the indexes we need:
  • cases_analytics/cases_index/
  • cases_analytics/comments_index/
  • cases_analytics/attachments_index/
  1. The backfill logic can be found in cases_analytics/tasks/backfill_task.
  2. I moved the retry_service previously used by the cases connector to a common folder and started using it in the AnalyticsIndex class.
  3. The call to create the indexes is done in x-pack/platform/plugins/shared/cases/server/plugin.ts.

How to test

A few things are needed.

  1. Create a role that has index creation permission. Below is what works for me; it might be overkill.
Screenshot 2025-04-25 at 11 30 45
  1. Create a user with the role created in step 1. Let's call the user index_creator. The password should be changeme.
Screenshot 2025-04-25 at 11 31 27
  1. Start Kibana with this user as the Elasticsearch user - yarn start with the option --elasticsearch.username=index_creator.

  2. Kibana startup logs should show relevant messages:

[INFO ][plugins.cases] [.internal.cases] Checking if index exists.
[INFO ][plugins.cases] [.internal.cases-attachments] Checking if index exists.
[INFO ][plugins.cases] [.internal.cases-comments] Checking if index exists.
  1. You can check the content of these indexes in the Dev Tools.
GET /.internal.cases/_search
GET /.internal.cases-comments/_search
GET /.internal.cases-attachments/_search
  1. You can check the mapping of each of these indexes in the Dev Tools.
GET /.internal.cases
GET /.internal.cases-comments
GET /.internal.cases-attachments
  1. You can check the painless scripts in the Dev Tools.
GET /_scripts/cai_cases_script_1
GET /_scripts/cai_attachments_script_1
GET /_scripts/cai_comments_script_1

Backfill task implementation.
@adcoelho adcoelho changed the title [ResponseOps][WIP] Cases analytics index [ResponseOps][WIP] Cases analytics index creation and backfill Apr 25, 2025
@adcoelho adcoelho force-pushed the cai-index-creation-and-backfill branch 3 times, most recently from 1747935 to ca4f7ec Compare May 21, 2025 16:43
@adcoelho adcoelho force-pushed the cai-index-creation-and-backfill branch from ca4f7ec to 7d515eb Compare May 22, 2025 14:00
@adcoelho adcoelho changed the title [ResponseOps][WIP] Cases analytics index creation and backfill [ResponseOps] Cases analytics index creation and backfill May 27, 2025
@adcoelho adcoelho marked this pull request as ready for review May 27, 2025 15:02
@adcoelho adcoelho requested a review from a team as a code owner May 27, 2025 15:02
…urce-definitions/scripts/fix-location-collection.ts'
@adcoelho adcoelho added the Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t// label May 28, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

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

Great work! I left some comments 🙂!

@adcoelho adcoelho force-pushed the cai-index-creation-and-backfill branch from 4ed7e23 to 97b1a6e Compare June 4, 2025 13:02
@adcoelho adcoelho force-pushed the cai-index-creation-and-backfill branch from 57f4511 to 81063e5 Compare June 4, 2025 13:53
(
source["cases-comments"].type == "externalReference" &&
source["cases-comments"].externalReferenceAttachmentTypeId != ".files"
) &&
Copy link
Member

Choose a reason for hiding this comment

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

This should be ||, correct?

@elasticmachine
Copy link
Contributor

elasticmachine commented Jun 10, 2025

💔 Build Failed

Failed CI Steps

History

cc @adcoelho

@adcoelho adcoelho merged commit fa8ef65 into elastic:cases-analytics-index Jun 11, 2025
9 of 10 checks passed
adcoelho added a commit that referenced this pull request Jun 24, 2025
This PR is for a feature branch that is being merged into main.

The relevant PRs are:
- #219211
- #222820
- #223241
- #224388
- #224682

## Summary

This PR adds 4 new indexes with case analytics data, which are created
when the cases plugin starts.

  - `.internal.cases`
  - `.internal.cases-comments`
  - `.internal.cases-attachments`
  - `.internal.cases-activity`

After the indexes are created, a backfill task for each of them is
scheduled to run 1 minute after creation. This task populates the
indexes with relevant data from `.kibana_alerting_cases`.

A second type of task is registered, the index synchronization task.
Four of these tasks, one for each index, are scheduled to run every 5
minutes. The synchronization tasks populated the indexes with data from
`.kibana_alerting_cases` that was created or updated in the last five
minutes.

## How to test

You might want to start Kibana with `--verbose` to see relevant index
messages in the console.

Alternatively(what I normally do), is go to `analytics_index.ts`,
`backfill_task_runner.ts`, and `synchronization_task_runner.ts`, and
change the `logDebug` function to call `this.logger.info` instead. This
way, you will have less spam in the console.

Every log message starts with the index name between square brackets, so
you can look for `[.internal.cases-` and follow what is happening.

1. You should have some existing case data, so before anything else,
please create some activity, attachments, etc.
2. Add `xpack.cases.analytics.index.enabled: true` to `kibana.dev.yml`
and restart Kibana.
3. Check out [this
branch](elastic/elasticsearch#129414) from the
ES project.
4. Start Elastic Search with `yarn es source`. This will use the above
version of Elasticsearch.
5. Wait a bit for the indexes to be created and populated(backfilled).
6. Using the dev tools:
    - Confirm the indexes exist.
- Check the index mapping. Does it match the one in the code? Is the
`_meta` field correct?
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/mappings.ts`
    - Check that the painless scripts match the code.
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/painless_scripts.ts`
- Confirm your existing case data is in the indexes. (See **Queries**
section below.)
7. Play around with cases. Some examples:
    - Create a case
    - Change status/severity
    - Attach alerts
    - Add files
    - Change category/tags
    - Add comments
    - etc
8. Go to the dev tools again and confirm all this shows up in the
relevant indexes. (See **Queries** section below.)

## Queries

In addition to the ones, below I have a few more. Things like reindexing
with specific scripts or fetching relevant data from
`.kibana_alerting_cases`. Ping me if you want those queries.

### Checking index content
```
GET /.internal.cases/_search
GET /.internal.cases-comments/_search
GET /.internal.cases-attachments/_search
GET /.internal.cases-activity/_search
```

### Checking index mappings
```
GET /.internal.cases
GET /.internal.cases-comments
GET /.internal.cases-attachments
GET /.internal.cases-activity
```

### Fetching the painless scripts
```
GET /_scripts/cai_cases_script_1
GET /_scripts/cai_attachments_script_1
GET /_scripts/cai_comments_script_1
GET /_scripts/cai_activity_script_1
```

### Emptying the indexes

It is sometimes useful for testing.
```
POST /.internal.cases/_delete_by_query
POST /.internal.cases-comments/_delete_by_query
POST /.internal.cases-attachments/_delete_by_query
POST /.internal.cases-activity/_delete_by_query
```

### Deleting the indexes

It is sometimes useful for testing.
```
DELETE /.internal.cases
DELETE /.internal.cases-comments
DELETE /.internal.cases-attachments
DELETE /.internal.cases-activity
```

## Release notes

Four dedicated case analytics indexes were created, allowing users to
build dashboards and metrics over case data. These indexes are created
on Kibana startup and updated periodically with cases, comments,
attachments, and activity data.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: Christos Nasikas <christos.nasikas@elastic.co>
adcoelho added a commit to adcoelho/kibana that referenced this pull request Jun 24, 2025
This PR is for a feature branch that is being merged into main.

The relevant PRs are:
- elastic#219211
- elastic#222820
- elastic#223241
- elastic#224388
- elastic#224682

## Summary

This PR adds 4 new indexes with case analytics data, which are created
when the cases plugin starts.

  - `.internal.cases`
  - `.internal.cases-comments`
  - `.internal.cases-attachments`
  - `.internal.cases-activity`

After the indexes are created, a backfill task for each of them is
scheduled to run 1 minute after creation. This task populates the
indexes with relevant data from `.kibana_alerting_cases`.

A second type of task is registered, the index synchronization task.
Four of these tasks, one for each index, are scheduled to run every 5
minutes. The synchronization tasks populated the indexes with data from
`.kibana_alerting_cases` that was created or updated in the last five
minutes.

## How to test

You might want to start Kibana with `--verbose` to see relevant index
messages in the console.

Alternatively(what I normally do), is go to `analytics_index.ts`,
`backfill_task_runner.ts`, and `synchronization_task_runner.ts`, and
change the `logDebug` function to call `this.logger.info` instead. This
way, you will have less spam in the console.

Every log message starts with the index name between square brackets, so
you can look for `[.internal.cases-` and follow what is happening.

1. You should have some existing case data, so before anything else,
please create some activity, attachments, etc.
2. Add `xpack.cases.analytics.index.enabled: true` to `kibana.dev.yml`
and restart Kibana.
3. Check out [this
branch](elastic/elasticsearch#129414) from the
ES project.
4. Start Elastic Search with `yarn es source`. This will use the above
version of Elasticsearch.
5. Wait a bit for the indexes to be created and populated(backfilled).
6. Using the dev tools:
    - Confirm the indexes exist.
- Check the index mapping. Does it match the one in the code? Is the
`_meta` field correct?
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/mappings.ts`
    - Check that the painless scripts match the code.
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/painless_scripts.ts`
- Confirm your existing case data is in the indexes. (See **Queries**
section below.)
7. Play around with cases. Some examples:
    - Create a case
    - Change status/severity
    - Attach alerts
    - Add files
    - Change category/tags
    - Add comments
    - etc
8. Go to the dev tools again and confirm all this shows up in the
relevant indexes. (See **Queries** section below.)

## Queries

In addition to the ones, below I have a few more. Things like reindexing
with specific scripts or fetching relevant data from
`.kibana_alerting_cases`. Ping me if you want those queries.

### Checking index content
```
GET /.internal.cases/_search
GET /.internal.cases-comments/_search
GET /.internal.cases-attachments/_search
GET /.internal.cases-activity/_search
```

### Checking index mappings
```
GET /.internal.cases
GET /.internal.cases-comments
GET /.internal.cases-attachments
GET /.internal.cases-activity
```

### Fetching the painless scripts
```
GET /_scripts/cai_cases_script_1
GET /_scripts/cai_attachments_script_1
GET /_scripts/cai_comments_script_1
GET /_scripts/cai_activity_script_1
```

### Emptying the indexes

It is sometimes useful for testing.
```
POST /.internal.cases/_delete_by_query
POST /.internal.cases-comments/_delete_by_query
POST /.internal.cases-attachments/_delete_by_query
POST /.internal.cases-activity/_delete_by_query
```

### Deleting the indexes

It is sometimes useful for testing.
```
DELETE /.internal.cases
DELETE /.internal.cases-comments
DELETE /.internal.cases-attachments
DELETE /.internal.cases-activity
```

## Release notes

Four dedicated case analytics indexes were created, allowing users to
build dashboards and metrics over case data. These indexes are created
on Kibana startup and updated periodically with cases, comments,
attachments, and activity data.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: Christos Nasikas <christos.nasikas@elastic.co>
(cherry picked from commit e566fec)

# Conflicts:
#	x-pack/platform/plugins/shared/task_manager/server/mocks.ts
akowalska622 pushed a commit to akowalska622/kibana that referenced this pull request Jun 25, 2025
This PR is for a feature branch that is being merged into main.

The relevant PRs are:
- elastic#219211
- elastic#222820
- elastic#223241
- elastic#224388
- elastic#224682

## Summary

This PR adds 4 new indexes with case analytics data, which are created
when the cases plugin starts.

  - `.internal.cases`
  - `.internal.cases-comments`
  - `.internal.cases-attachments`
  - `.internal.cases-activity`

After the indexes are created, a backfill task for each of them is
scheduled to run 1 minute after creation. This task populates the
indexes with relevant data from `.kibana_alerting_cases`.

A second type of task is registered, the index synchronization task.
Four of these tasks, one for each index, are scheduled to run every 5
minutes. The synchronization tasks populated the indexes with data from
`.kibana_alerting_cases` that was created or updated in the last five
minutes.

## How to test

You might want to start Kibana with `--verbose` to see relevant index
messages in the console.

Alternatively(what I normally do), is go to `analytics_index.ts`,
`backfill_task_runner.ts`, and `synchronization_task_runner.ts`, and
change the `logDebug` function to call `this.logger.info` instead. This
way, you will have less spam in the console.

Every log message starts with the index name between square brackets, so
you can look for `[.internal.cases-` and follow what is happening.

1. You should have some existing case data, so before anything else,
please create some activity, attachments, etc.
2. Add `xpack.cases.analytics.index.enabled: true` to `kibana.dev.yml`
and restart Kibana.
3. Check out [this
branch](elastic/elasticsearch#129414) from the
ES project.
4. Start Elastic Search with `yarn es source`. This will use the above
version of Elasticsearch.
5. Wait a bit for the indexes to be created and populated(backfilled).
6. Using the dev tools:
    - Confirm the indexes exist.
- Check the index mapping. Does it match the one in the code? Is the
`_meta` field correct?
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/mappings.ts`
    - Check that the painless scripts match the code.
-
`x-pack/platform/plugins/shared/cases/server/cases_analytics/******_index/painless_scripts.ts`
- Confirm your existing case data is in the indexes. (See **Queries**
section below.)
7. Play around with cases. Some examples:
    - Create a case
    - Change status/severity
    - Attach alerts
    - Add files
    - Change category/tags
    - Add comments
    - etc
8. Go to the dev tools again and confirm all this shows up in the
relevant indexes. (See **Queries** section below.)

## Queries

In addition to the ones, below I have a few more. Things like reindexing
with specific scripts or fetching relevant data from
`.kibana_alerting_cases`. Ping me if you want those queries.

### Checking index content
```
GET /.internal.cases/_search
GET /.internal.cases-comments/_search
GET /.internal.cases-attachments/_search
GET /.internal.cases-activity/_search
```

### Checking index mappings
```
GET /.internal.cases
GET /.internal.cases-comments
GET /.internal.cases-attachments
GET /.internal.cases-activity
```

### Fetching the painless scripts
```
GET /_scripts/cai_cases_script_1
GET /_scripts/cai_attachments_script_1
GET /_scripts/cai_comments_script_1
GET /_scripts/cai_activity_script_1
```

### Emptying the indexes

It is sometimes useful for testing.
```
POST /.internal.cases/_delete_by_query
POST /.internal.cases-comments/_delete_by_query
POST /.internal.cases-attachments/_delete_by_query
POST /.internal.cases-activity/_delete_by_query
```

### Deleting the indexes

It is sometimes useful for testing.
```
DELETE /.internal.cases
DELETE /.internal.cases-comments
DELETE /.internal.cases-attachments
DELETE /.internal.cases-activity
```

## Release notes

Four dedicated case analytics indexes were created, allowing users to
build dashboards and metrics over case data. These indexes are created
on Kibana startup and updated periodically with cases, comments,
attachments, and activity data.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: Christos Nasikas <christos.nasikas@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature:Cases Cases feature Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t//

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants