[8.x] Add filters option to ftr_helper api (#196886) (#197509)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Add filters option to ftr_helper api
(#196886)](https://github.com/elastic/kibana/pull/196886)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Sebastián
Zaffarano","email":"sebastian.zaffarano@elastic.co"},"sourceCommit":{"committedDate":"2024-10-23T17:18:05Z","message":"Add
filters option to ftr_helper api
(#196886)","sha":"48fe299d5bcd230d63ce2dd8e89c62307dd0ee82","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:all-open"],"title":"Add
filters option to ftr_helper
api","number":196886,"url":"https://github.com/elastic/kibana/pull/196886","mergeCommit":{"message":"Add
filters option to ftr_helper api
(#196886)","sha":"48fe299d5bcd230d63ce2dd8e89c62307dd0ee82"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/196886","number":196886,"mergeCommit":{"message":"Add
filters option to ftr_helper api
(#196886)","sha":"48fe299d5bcd230d63ce2dd8e89c62307dd0ee82"}}]}]
BACKPORT-->

Co-authored-by: Sebastián Zaffarano <sebastian.zaffarano@elastic.co>
This commit is contained in:
Kibana Machine 2024-10-24 06:24:10 +11:00 committed by GitHub
parent 942827df09
commit 9b606f7f72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 52 additions and 7 deletions

View file

@ -14,7 +14,7 @@ import { fetchEvents } from '../common/fetch_events';
import { CustomShipper } from './custom_shipper';
export class AnalyticsFTRHelpers implements Plugin {
public setup({ analytics, http }: CoreSetup, deps: {}) {
public setup({ analytics, http }: CoreSetup, _deps: {}) {
const { optIn, registerShipper } = analytics;
const events$ = new ReplaySubject<Event>();
@ -31,7 +31,7 @@ export class AnalyticsFTRHelpers implements Plugin {
}),
},
},
(context, req, res) => {
(_context, req, res) => {
const { consent } = req.query;
optIn({ global: { enabled: consent } });
@ -67,7 +67,7 @@ export class AnalyticsFTRHelpers implements Plugin {
}),
},
},
async (context, req, res) => {
async (_context, req, res) => {
const { takeNumberOfEvents, ...options } = req.query;
const events = await fetchEvents(events$, takeNumberOfEvents, options);
return res.ok({ body: events });
@ -82,10 +82,25 @@ export class AnalyticsFTRHelpers implements Plugin {
eventTypes: schema.arrayOf(schema.string()),
withTimeoutMs: schema.number(),
fromTimestamp: schema.maybe(schema.string()),
filters: schema.maybe(
schema.recordOf(
schema.string(),
schema.recordOf(
schema.oneOf([
schema.literal('eq'),
schema.literal('gte'),
schema.literal('gt'),
schema.literal('lte'),
schema.literal('lt'),
]),
schema.any()
)
)
),
}),
},
},
async (context, req, res) => {
async (_context, req, res) => {
const events = await fetchEvents(events$, Number.MAX_SAFE_INTEGER, req.query);
return res.ok({ body: { count: events.length } });
}

View file

@ -28,7 +28,7 @@ export function KibanaEBTServerProvider({ getService }: FtrProviderContext): EBT
setOptIn,
getEvents: async (
takeNumberOfEvents,
{ eventTypes = [], withTimeoutMs, fromTimestamp } = {}
{ eventTypes = [], withTimeoutMs, fromTimestamp, filters } = {}
) => {
await setOptIn(true);
const resp = await supertest
@ -38,6 +38,7 @@ export function KibanaEBTServerProvider({ getService }: FtrProviderContext): EBT
eventTypes: JSON.stringify(eventTypes),
withTimeoutMs,
fromTimestamp,
filters: JSON.stringify(filters),
})
.set('kbn-xsrf', 'xxx')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
@ -45,11 +46,16 @@ export function KibanaEBTServerProvider({ getService }: FtrProviderContext): EBT
return resp.body;
},
getEventCount: async ({ eventTypes = [], withTimeoutMs, fromTimestamp }) => {
getEventCount: async ({ eventTypes = [], withTimeoutMs, fromTimestamp, filters }) => {
await setOptIn(true);
const resp = await supertest
.get(`/internal/analytics_ftr_helpers/count_events`)
.query({ eventTypes: JSON.stringify(eventTypes), withTimeoutMs, fromTimestamp })
.query({
eventTypes: JSON.stringify(eventTypes),
withTimeoutMs,
fromTimestamp,
filters: JSON.stringify(filters),
})
.set('kbn-xsrf', 'xxx')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.expect(200);

View file

@ -203,6 +203,30 @@ export default function ({ getService }: FtrProviderContext) {
});
expect(eventCount).to.be(1);
});
it('should return 0 events when filtering by invalid properties', async () => {
const eventCount = await ebtServerHelper.getEventCount({
withTimeoutMs: 500,
eventTypes: ['test-plugin-lifecycle'],
filters: {
'properties.plugin': {
eq: 'analyticsPluginB',
},
},
});
expect(eventCount).to.be(0);
});
it('should return 1 event when filtering by valid properties', async () => {
const eventCount = await ebtServerHelper.getEventCount({
withTimeoutMs: 500,
eventTypes: ['test-plugin-lifecycle'],
filters: {
'properties.step': {
eq: 'start',
},
},
});
expect(eventCount).to.be(1);
});
});
});
});