Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.16] Add filters option to ftr_helper api (#196886) #197508

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions test/analytics/plugins/analytics_ftr_helpers/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand All @@ -31,7 +31,7 @@ export class AnalyticsFTRHelpers implements Plugin {
}),
},
},
(context, req, res) => {
(_context, req, res) => {
const { consent } = req.query;

optIn({ global: { enabled: consent } });
Expand Down Expand Up @@ -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 });
Expand All @@ -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 } });
}
Expand Down
12 changes: 9 additions & 3 deletions test/analytics/services/kibana_ebt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,18 +38,24 @@ 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')
.expect(200);

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);
Expand Down
24 changes: 24 additions & 0 deletions test/analytics/tests/analytics_from_the_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Expand Down