kibana/x-pack/test/api_integration_basic/apis/aiops/permissions.ts
Walter Rafelsberger f391ed6bcf
[ML] AIOps: Update fields filter popover to be able to filter fields from analysis (not just grouping) (#188913)
## Summary

Part of #187684.

So far the popover to filter fields was only available when grouping was
enabled. This PR updates the behavior so it's available all the time and
can be used to exclude field candidates from the analysis. If we detect
the index to be based on an ECS schema, we auto-select a set of
predefined fields.

Changes in this PR:

- Creates a new route
`/internal/aiops/log_rate_analysis/field_candidates` to be able to fetch
field candidates independent of the main streaming API call.
- Fixes the code to consider "remaining" field candidates to also
consider text field candidates. This was originally developed to allow
to continue an analysis that errored for some reason. We use that option
to also pass on the custom field list from the field selection popover.
- Fetching the field candidates is done in a new redux slice
`logRateAnalysisFieldCandidatesSlice` using an async thunk.
- Filters the list of field candidates by a predefined field of allowed
fields when an ECS schema gets detected.
- Renames `fieldCandidates` to `keywordFieldCandidates` for clearer
distinction against `textFieldCandidates`.
- Refactors `getLogRateAnalysisTypeForCounts` args to a config object.
- Bump the API version for the full log rate analysis to version 3. We
missed bumping the version in
https://github.com/elastic/kibana/pull/188648. This update manages
proper versioning between v2 and v3, also the API integration tests
cover both versions.


[aiops-log-rate-analysis-fields-filter-0001.webm](https://github.com/user-attachments/assets/e3ed8d5b-f01c-42ef-8033-caa7135b8cc0)

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
2024-07-30 10:12:53 +02:00

68 lines
2.3 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import fetch from 'node-fetch';
import { format as formatUrl } from 'url';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import expect from '@kbn/expect';
import type {
AiopsLogRateAnalysisSchema,
AiopsLogRateAnalysisApiVersion as ApiVersion,
} from '@kbn/aiops-log-rate-analysis/api/schema';
import type { FtrProviderContext } from '../../ftr_provider_context';
const API_VERSIONS: ApiVersion[] = ['3'];
export default ({ getService }: FtrProviderContext) => {
const supertest = getService('supertest');
const config = getService('config');
const kibanaServerUrl = formatUrl(config.get('servers.kibana'));
describe('POST /internal/aiops/log_rate_analysis', () => {
API_VERSIONS.forEach((apiVersion) => {
describe(`with API v${apiVersion}`, () => {
const requestBody: AiopsLogRateAnalysisSchema<typeof apiVersion> = {
baselineMax: 1561719083292,
baselineMin: 1560954147006,
deviationMax: 1562254538692,
deviationMin: 1561986810992,
end: 2147483647000,
index: 'ft_ecommerce',
searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}',
start: 0,
timeFieldName: 'order_date',
};
it('should return permission denied without streaming', async () => {
await supertest
.post(`/internal/aiops/log_rate_analysis`)
.set('kbn-xsrf', 'kibana')
.set(ELASTIC_HTTP_VERSION_HEADER, apiVersion)
.send(requestBody)
.expect(403);
});
it('should return permission denied with streaming', async () => {
const response = await fetch(`${kibanaServerUrl}/internal/aiops/log_rate_analysis`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'kbn-xsrf': 'stream',
[ELASTIC_HTTP_VERSION_HEADER]: apiVersion,
},
body: JSON.stringify(requestBody),
});
expect(response.ok).to.be(false);
expect(response.status).to.be(403);
});
});
});
});
};