diff --git a/src/plugins/telemetry_collection_manager/server/types.ts b/src/plugins/telemetry_collection_manager/server/types.ts index 2cc272ca55fe0..0e48efaec98f4 100644 --- a/src/plugins/telemetry_collection_manager/server/types.ts +++ b/src/plugins/telemetry_collection_manager/server/types.ts @@ -82,7 +82,9 @@ export interface BasicStatsPayload { version: string; cluster_stats: object; collection?: string; - stack_stats: object; + // Needed for testing to make fields easier to access + // eslint-disable-next-line @typescript-eslint/no-explicit-any + stack_stats: Record; } export interface UsageStatsPayload extends BasicStatsPayload { diff --git a/x-pack/test/api_integration/services/usage_api.ts b/x-pack/test/api_integration/services/usage_api.ts index 64089d0a9b8ef..c9fcb9bdfc08c 100644 --- a/x-pack/test/api_integration/services/usage_api.ts +++ b/x-pack/test/api_integration/services/usage_api.ts @@ -5,49 +5,39 @@ * 2.0. */ -import { TelemetryCollectionManagerPlugin } from '@kbn/telemetry-collection-manager-plugin/server/plugin'; +import { UsageStatsPayload } from '@kbn/telemetry-collection-manager-plugin/server'; import { FtrProviderContext } from '../ftr_provider_context'; export function UsageAPIProvider({ getService }: FtrProviderContext) { const supertest = getService('supertest'); - const supertestNoAuth = getService('supertestWithoutAuth'); - return { - async getUsageStatsNoAuth(): Promise { - const { body } = await supertestNoAuth - .get('/api/stats?extended=true') - .set('kbn-xsrf', 'xxx') - .expect(401); - return body.usage; - }, - - /** - * Public stats API: it returns the usage in camelCase format - */ - async getUsageStats() { - const { body } = await supertest - .get('/api/stats?extended=true') - .set('kbn-xsrf', 'xxx') - .expect(200); - return body.usage; - }, + async function getTelemetryStats(payload: { + unencrypted: true; + refreshCache?: boolean; + }): Promise>; + async function getTelemetryStats(payload: { + unencrypted: false; + refreshCache?: boolean; + }): Promise>; + async function getTelemetryStats(payload: { + unencrypted?: boolean; + refreshCache?: boolean; + }): Promise> { + const { body } = await supertest + .post('/api/telemetry/v2/clusters/_stats') + .set('kbn-xsrf', 'xxx') + .send({ refreshCache: true, ...payload }) + .expect(200); + return body; + } + return { /** * Retrieve the stats via the private telemetry API: * It returns the usage in as a string encrypted blob or the plain payload if `unencrypted: false` * * @param payload Request parameters to retrieve the telemetry stats */ - async getTelemetryStats(payload: { - unencrypted?: boolean; - refreshCache?: boolean; - }): Promise> { - const { body } = await supertest - .post('/api/telemetry/v2/clusters/_stats') - .set('kbn-xsrf', 'xxx') - .send({ refreshCache: true, ...payload }) - .expect(200); - return body; - }, + getTelemetryStats, }; } diff --git a/x-pack/test/reporting_api_integration/reporting_and_security.config.ts b/x-pack/test/reporting_api_integration/reporting_and_security.config.ts index 3574ee5b8b2b1..237bb94ed1dc6 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security.config.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security.config.ts @@ -37,6 +37,8 @@ export default async function ({ readConfigFile }: FtrConfigProviderContext) { `--xpack.reporting.capture.maxAttempts=1`, `--xpack.reporting.csv.maxSizeBytes=6000`, '--xpack.reporting.roles.enabled=false', // Reporting access control is implemented by sub-feature application privileges + // for testing set buffer duration to 0 to immediately flush counters into saved objects. + '--usageCollection.usageCounters.bufferDuration=0', ], }, }; diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/api_counters.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/api_counters.ts index e490d88820756..880a77f1468c1 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/api_counters.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/api_counters.ts @@ -6,6 +6,7 @@ */ import expect from '@kbn/expect'; +import { BasicStatsPayload } from '@kbn/telemetry-collection-manager-plugin/server/types'; import { createPdfV2Params, createPngV2Params } from '..'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -35,10 +36,11 @@ export default function ({ getService }: FtrProviderContext) { describe('server', function () { this.tags('skipCloud'); it('configuration settings of the tests_server', async () => { - const usage = await usageAPI.getUsageStats(); - expect(usage.kibana_config_usage.xpack_reporting_capture_max_attempts).to.be(1); - expect(usage.kibana_config_usage.xpack_reporting_csv_max_size_bytes).to.be(6000); - expect(usage.kibana_config_usage.xpack_reporting_roles_enabled).to.be(false); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins; + expect(usage.kibana_config_usage['xpack.reporting.capture.maxAttempts']).to.be(1); + expect(usage.kibana_config_usage['xpack.reporting.csv.maxSizeBytes']).to.be(6000); + expect(usage.kibana_config_usage['xpack.reporting.roles.enabled']).to.be(false); }); }); @@ -52,12 +54,12 @@ export default function ({ getService }: FtrProviderContext) { DIAG_SCREENSHOT = '/api/reporting/diagnose/screenshot', } - let initialStats: any; - let stats: any; + let initialStats: BasicStatsPayload; + let stats: BasicStatsPayload; const CALL_COUNT = 3; before('call APIs', async () => { - initialStats = await usageAPI.getUsageStats(); + [{ stats: initialStats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); await Promise.all( Object.keys(paths).map(async (key) => { @@ -73,30 +75,36 @@ export default function ({ getService }: FtrProviderContext) { await waitOnAggregation(); // determine the result usage count - stats = await usageAPI.getUsageStats(); + [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); }); it('job listing', async () => { - expect(getUsageCount(initialStats, `get ${paths.LIST}`)).to.be(0); - expect(getUsageCount(stats, `get ${paths.LIST}`)).to.be(CALL_COUNT); + const initialCount = getUsageCount(initialStats, `get ${paths.LIST}`); + expect(getUsageCount(stats, `get ${paths.LIST}`)).to.be(CALL_COUNT + initialCount); }); it('job count', async () => { - expect(getUsageCount(initialStats, `get ${paths.COUNT}`)).to.be(0); - expect(getUsageCount(stats, `get ${paths.COUNT}`)).to.be(CALL_COUNT); + const initialCount = getUsageCount(initialStats, `get ${paths.COUNT}`); + expect(getUsageCount(stats, `get ${paths.COUNT}`)).to.be(CALL_COUNT + initialCount); }); it('job info', async () => { - expect( - getUsageCount(initialStats, `get /api/reporting/jobs/info/{docId}:printable_pdf`) - ).to.be(0); + const initialCount = getUsageCount( + initialStats, + `get /api/reporting/jobs/info/{docId}:printable_pdf` + ); expect(getUsageCount(stats, `get /api/reporting/jobs/info/{docId}:printable_pdf`)).to.be( - CALL_COUNT + CALL_COUNT + initialCount ); }); }); describe('downloading and deleting', () => { + let initialStats: BasicStatsPayload; + before('gather initial stats', async () => { + [{ stats: initialStats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + }); + it('downloading', async () => { try { await Promise.all([ @@ -119,12 +127,14 @@ export default function ({ getService }: FtrProviderContext) { log.info(`waiting on aggregation completed.`); log.info(`calling getUsageStats...`); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const initialCount = getUsageCount( + initialStats, + `get /api/reporting/jobs/download/{docId}:printable_pdf` + ); expect( - getUsageCount( - await usageAPI.getUsageStats(), - `get /api/reporting/jobs/download/{docId}:printable_pdf` - ) - ).to.be(3); + getUsageCount(stats, `get /api/reporting/jobs/download/{docId}:printable_pdf`) + ).to.be(3 + initialCount); }); it('deleting', async () => { @@ -145,17 +155,19 @@ export default function ({ getService }: FtrProviderContext) { log.info(`waiting on aggregation completed.`); log.info(`calling getUsageStats...`); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const initialCount = getUsageCount( + initialStats, + `delete /api/reporting/jobs/delete/{docId}:csv_searchsource` + ); expect( - getUsageCount( - await usageAPI.getUsageStats(), - `delete /api/reporting/jobs/delete/{docId}:csv_searchsource` - ) - ).to.be(1); + getUsageCount(stats, `delete /api/reporting/jobs/delete/{docId}:csv_searchsource`) + ).to.be(1 + initialCount); }); }); describe('API counters: job generation', () => { - let stats: any; + let stats: BasicStatsPayload; before(async () => { // call generation APIs @@ -172,7 +184,7 @@ export default function ({ getService }: FtrProviderContext) { await waitOnAggregation(); - stats = await usageAPI.getUsageStats(); + [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); }); it('PNG', async () => { @@ -201,10 +213,10 @@ export default function ({ getService }: FtrProviderContext) { }); }; - const getUsageCount = (checkUsage: any, counterName: string): number => { + const getUsageCount = (checkUsage: BasicStatsPayload, counterName: string): number => { return ( - checkUsage.usage_counters.daily_events.find( - (item: any) => item.counter_name === counterName + checkUsage.stack_stats.kibana.plugins.usage_counters.dailyEvents.find( + (item: any) => item.counterName === counterName )?.total || 0 ); }; diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/archived_data.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/archived_data.ts index f81b29cb65572..610d301963a7d 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/archived_data.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/archived_data.ts @@ -16,7 +16,8 @@ export default function ({ getService }: FtrProviderContext) { describe('from archive data', () => { it('generated from 6.2', async () => { await esArchiver.load('x-pack/test/functional/es_archives/reporting/bwc/6_2'); - const usage = await usageAPI.getUsageStats(); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectRecentJobTypeTotalStats(usage, 'printable_pdf', 0); reportingAPI.expectAllTimeJobTypeTotalStats(usage, 'printable_pdf', 7); @@ -36,7 +37,8 @@ export default function ({ getService }: FtrProviderContext) { it('generated from 6.3', async () => { await esArchiver.load('x-pack/test/functional/es_archives/reporting/bwc/6_3'); - const usage = await usageAPI.getUsageStats(); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectRecentJobTypeTotalStats(usage, 'printable_pdf', 0); reportingAPI.expectRecentPdfAppStats(usage, 'visualization', 0); diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/error_codes.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/error_codes.ts index 73cabb26d9aa9..11bf6e5fb9450 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/error_codes.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/error_codes.ts @@ -6,16 +6,8 @@ */ import expect from '@kbn/expect'; +import { ReportingUsageType } from '@kbn/reporting-plugin/server/usage/types'; import { FtrProviderContext } from '../../ftr_provider_context'; -import { UsageStats } from '../../services/usage'; - -type ReportingUsage = UsageStats['reporting']; -interface ReportingUsageApiResponse { - all: ReportingUsage['_all']; - csv_searchsource: ReportingUsage['csv_searchsource']; - pngv_2: ReportingUsage['PNGV2']; - printable_pdf_v_2: ReportingUsage['printable_pdf_v2']; -} const DATA_ARCHIVE_PATH = 'x-pack/test/functional/es_archives/reporting/errors'; @@ -26,12 +18,13 @@ export default function ({ getService }: FtrProviderContext) { const usageAPI = getService('usageAPI'); describe(`error codes`, () => { - let reporting: ReportingUsageApiResponse; + let reporting: ReportingUsageType; before(async () => { await reportingAPI.deleteAllReports(); await esArchiver.load(DATA_ARCHIVE_PATH); - ({ reporting } = await usageAPI.getUsageStats()); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + reporting = stats.stack_stats.kibana.plugins.reporting; }); after(async () => { @@ -40,10 +33,10 @@ export default function ({ getService }: FtrProviderContext) { }); it('includes error code statistics', async () => { - expect(reporting.all).equal(3); + expect(reporting._all).equal(3); expectSnapshot( - ['csv_searchsource', 'pngv_2', 'printable_pdf_v_2'].map((k) => { - const field = reporting[k as keyof Omit]; + (['csv_searchsource', 'PNGV2', 'printable_pdf_v2'] as const).map((k) => { + const field = reporting[k]; return { key: k, error_codes: field.error_codes, total: field.total }; }) ).toMatchInline(` @@ -55,7 +48,7 @@ export default function ({ getService }: FtrProviderContext) { }, Object { "error_codes": undefined, - "key": "pngv_2", + "key": "PNGV2", "total": 0, }, Object { @@ -63,7 +56,7 @@ export default function ({ getService }: FtrProviderContext) { "queue_timeout_error": 1, "unknown_error": 1, }, - "key": "printable_pdf_v_2", + "key": "printable_pdf_v2", "total": 3, }, ] diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/initial.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/initial.ts index 3104de755da88..546fef7e1d164 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/initial.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/initial.ts @@ -6,8 +6,8 @@ */ import expect from '@kbn/expect'; +import { ReportingUsageType } from '@kbn/reporting-plugin/server/usage/types'; import { FtrProviderContext } from '../../ftr_provider_context'; -import { UsageStats } from '../../services/usage'; // eslint-disable-next-line import/no-default-export export default function ({ getService }: FtrProviderContext) { @@ -16,18 +16,19 @@ export default function ({ getService }: FtrProviderContext) { const usageAPI = getService('usageAPI'); describe('initial state', () => { - let usage: UsageStats; + let usage: ReportingUsageType; before(async () => { await retry.try(async () => { // use retry for stability - usage API could return 503 - usage = (await usageAPI.getUsageStats()) as UsageStats; + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + usage = stats.stack_stats.kibana.plugins.reporting; }); }); it('shows reporting as available and enabled', async () => { - expect(usage.reporting.available).to.be(true); - expect(usage.reporting.enabled).to.be(true); + expect(usage.available).to.be(true); + expect(usage.enabled).to.be(true); }); it('all counts are 0', async () => { diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/metrics.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/metrics.ts index d8645025ef6b8..219514040d665 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/metrics.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/metrics.ts @@ -6,8 +6,8 @@ */ import expect from '@kbn/expect'; +import { ReportingUsageType } from '@kbn/reporting-plugin/server/usage/types'; import { FtrProviderContext } from '../../ftr_provider_context'; -import { UsageStats } from '../../services/usage'; import * as urls from './_post_urls'; const OSS_KIBANA_ARCHIVE_PATH = 'test/functional/fixtures/kbn_archiver/dashboard/current/kibana'; @@ -21,8 +21,8 @@ export default function ({ getService }: FtrProviderContext) { const usageAPI = getService('usageAPI'); describe(`metrics and stats`, () => { - let reporting: UsageStats['reporting']; - let last7Days: UsageStats['reporting']['last_7_days']; + let reporting: ReportingUsageType; + let last7Days: ReportingUsageType['last7Days']; before(async () => { await kibanaServer.savedObjects.cleanStandardList(); @@ -38,8 +38,9 @@ export default function ({ getService }: FtrProviderContext) { ]) ); - ({ reporting } = await usageAPI.getUsageStats()); - ({ last_7_days: last7Days } = reporting); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + reporting = stats.stack_stats.kibana.plugins.reporting; + ({ last7Days } = reporting); }); after(async () => { @@ -50,8 +51,8 @@ export default function ({ getService }: FtrProviderContext) { it('includes report stats', async () => { // over all time - expectSnapshot(reporting._all).toMatchInline(`undefined`); - expect(reporting.output_size).keys(['1_0', '25_0', '50_0', '5_0', '75_0', '95_0', '99_0']); + expect(reporting._all).to.eql(3); + expect(reporting.output_size).keys(['1.0', '25.0', '50.0', '5.0', '75.0', '95.0', '99.0']); expectSnapshot(reporting.status).toMatchInline(` Object { "completed": 3, @@ -60,8 +61,8 @@ export default function ({ getService }: FtrProviderContext) { `); // over last 7 days - expectSnapshot(last7Days._all).toMatchInline(`undefined`); - expect(last7Days.output_size).keys(['1_0', '25_0', '50_0', '5_0', '75_0', '95_0', '99_0']); + expect(last7Days._all).to.eql(3); + expect(last7Days.output_size).keys(['1.0', '25.0', '50.0', '5.0', '75.0', '95.0', '99.0']); expectSnapshot(last7Days.status).toMatchInline(` Object { "completed": 3, @@ -78,31 +79,31 @@ export default function ({ getService }: FtrProviderContext) { it('includes report metrics (not for job types under last_7_days)', async () => { expect(reporting.printable_pdf.output_size).keys([ - '1_0', - '25_0', - '50_0', - '5_0', - '75_0', - '95_0', - '99_0', + '1.0', + '25.0', + '50.0', + '5.0', + '75.0', + '95.0', + '99.0', ]); expectSnapshot(reporting.printable_pdf.metrics?.pdf_pages).toMatchInline(` Object { "values": Object { - "50_0": 1, - "75_0": 1, - "95_0": 1, - "99_0": 1, + "50.0": 1, + "75.0": 1, + "95.0": 1, + "99.0": 1, }, } `); expectSnapshot(reporting.csv_searchsource.metrics?.csv_rows).toMatchInline(` Object { "values": Object { - "50_0": 71, - "75_0": 71, - "95_0": 71, - "99_0": 71, + "50.0": 71, + "75.0": 71, + "95.0": 71, + "99.0": 71, }, } `); diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/usage/new_jobs.ts b/x-pack/test/reporting_api_integration/reporting_and_security/usage/new_jobs.ts index 0377b277defbb..fc4f93c6e3e7c 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/usage/new_jobs.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/usage/new_jobs.ts @@ -37,7 +37,8 @@ export default function ({ getService }: FtrProviderContext) { await Promise.all([reportingAPI.postJob(urls.JOB_PARAMS_CSV_DEFAULT_SPACE)]) ); - const usage = await usageAPI.getUsageStats(); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectRecentJobTypeTotalStats(usage, 'csv_searchsource', 1); }); @@ -49,7 +50,8 @@ export default function ({ getService }: FtrProviderContext) { ]) ); - const usage = await usageAPI.getUsageStats(); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectRecentPdfLayoutStats(usage, 'preserve_layout', 2); reportingAPI.expectAllTimePdfLayoutStats(usage, 'preserve_layout', 2); }); @@ -62,7 +64,8 @@ export default function ({ getService }: FtrProviderContext) { ]) ); - const usage = await usageAPI.getUsageStats(); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + const usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectRecentPdfLayoutStats(usage, 'print', 1); reportingAPI.expectAllTimePdfLayoutStats(usage, 'print', 1); }); diff --git a/x-pack/test/reporting_api_integration/services/usage.ts b/x-pack/test/reporting_api_integration/services/usage.ts index 07c97475421f9..dd65ef5e38727 100644 --- a/x-pack/test/reporting_api_integration/services/usage.ts +++ b/x-pack/test/reporting_api_integration/services/usage.ts @@ -16,13 +16,6 @@ import { } from '@kbn/reporting-plugin/server/usage/types'; import { FtrProviderContext } from '../ftr_provider_context'; -// NOTE: the usage stats come from an HTTP API, which converts key names to snake_case -export interface UsageStats { - reporting: ReportingUsageType & { - last_7_days: ReportingUsageType['last7Days']; - }; -} - export function createUsageServices({ getService }: FtrProviderContext) { const log = getService('log'); const esSupertest = getService('esSupertest'); @@ -102,55 +95,53 @@ export function createUsageServices({ getService }: FtrProviderContext) { ); }, - expectRecentPdfAppStats(stats: UsageStats, app: string, count: number) { - const actual = - stats.reporting.last_7_days.printable_pdf.app![app as keyof AvailableTotal['app']]; + expectRecentPdfAppStats(stats: ReportingUsageType, app: string, count: number) { + const actual = stats.last7Days.printable_pdf.app![app as keyof AvailableTotal['app']]; log.info(`expecting recent ${app} stats to have ${count} printable pdfs (actual: ${actual})`); expect(actual).to.be(count); }, - expectAllTimePdfAppStats(stats: UsageStats, app: string, count: number) { - const actual = stats.reporting.printable_pdf.app![app as keyof AvailableTotal['app']]; + expectAllTimePdfAppStats(stats: ReportingUsageType, app: string, count: number) { + const actual = stats.printable_pdf.app![app as keyof AvailableTotal['app']]; log.info( `expecting all time pdf ${app} stats to have ${count} printable pdfs (actual: ${actual})` ); expect(actual).to.be(count); }, - expectRecentPdfLayoutStats(stats: UsageStats, layout: string, count: number) { - const actual = - stats.reporting.last_7_days.printable_pdf.layout![layout as keyof LayoutCounts]; + expectRecentPdfLayoutStats(stats: ReportingUsageType, layout: string, count: number) { + const actual = stats.last7Days.printable_pdf.layout![layout as keyof LayoutCounts]; log.info(`expecting recent stats to report ${count} ${layout} layouts (actual: ${actual})`); expect(actual).to.be(count); }, - expectAllTimePdfLayoutStats(stats: UsageStats, layout: string, count: number) { - const actual = stats.reporting.printable_pdf.layout![layout as keyof LayoutCounts]; + expectAllTimePdfLayoutStats(stats: ReportingUsageType, layout: string, count: number) { + const actual = stats.printable_pdf.layout![layout as keyof LayoutCounts]; log.info(`expecting all time stats to report ${count} ${layout} layouts (actual: ${actual})`); expect(actual).to.be(count); }, - expectRecentJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) { - const actual = stats.reporting.last_7_days[jobType as keyof JobTypes].total; + expectRecentJobTypeTotalStats(stats: ReportingUsageType, jobType: string, count: number) { + const actual = stats.last7Days[jobType as keyof JobTypes].total; log.info( `expecting recent stats to report ${count} ${jobType} job types (actual: ${actual})` ); expect(actual).to.be(count); }, - expectAllTimeJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) { - const actual = stats.reporting[jobType as keyof JobTypes].total; + expectAllTimeJobTypeTotalStats(stats: ReportingUsageType, jobType: string, count: number) { + const actual = stats[jobType as keyof JobTypes].total; log.info( `expecting all time stats to report ${count} ${jobType} job types (actual: ${actual})` ); expect(actual).to.be(count); }, - getCompletedReportCount(stats: UsageStats) { - return stats.reporting.status.completed; + getCompletedReportCount(stats: ReportingUsageType) { + return stats.status.completed; }, - expectCompletedReportCount(stats: UsageStats, count: number) { + expectCompletedReportCount(stats: ReportingUsageType, count: number) { expect(this.getCompletedReportCount(stats)).to.be(count); }, }; diff --git a/x-pack/test/upgrade/apps/reporting/reporting_smoke_tests.ts b/x-pack/test/upgrade/apps/reporting/reporting_smoke_tests.ts index 2aa1cf113b35b..1395ac9556d85 100644 --- a/x-pack/test/upgrade/apps/reporting/reporting_smoke_tests.ts +++ b/x-pack/test/upgrade/apps/reporting/reporting_smoke_tests.ts @@ -7,12 +7,8 @@ import expect from '@kbn/expect'; import { parse } from 'url'; +import { ReportingUsageType } from '@kbn/reporting-plugin/server/usage/types'; import { FtrProviderContext } from '../../ftr_provider_context'; -import { ReportingUsageStats } from '../../services/reporting_upgrade_services'; - -interface UsageStats { - reporting: ReportingUsageStats; -} export default function ({ getService, getPageObjects }: FtrProviderContext) { const reportingAPI = getService('reportingAPI'); @@ -43,15 +39,16 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { describe('reporting ', () => { let completedReportCount: number; - let usage: UsageStats; + let usage: ReportingUsageType; describe('initial state', () => { before(async () => { - usage = (await usageAPI.getUsageStats()) as UsageStats; + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + usage = stats.stack_stats.kibana.plugins.reporting; }); it('shows reporting as available and enabled', async () => { - expect(usage.reporting.available).to.be(true); - expect(usage.reporting.enabled).to.be(true); + expect(usage.available).to.be(true); + expect(usage.enabled).to.be(true); }); }); spaces.forEach(({ space, basePath }) => { @@ -61,8 +58,9 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { basePath, }); await PageObjects.header.waitUntilLoadingHasFinished(); - usage = (await usageAPI.getUsageStats()) as UsageStats; - completedReportCount = reportingAPI.getCompletedReportCount(usage); + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + usage = stats.stack_stats.kibana.plugins.reporting; + completedReportCount = reportingAPI.getCompletedReportCount(usage)!; }); reportingTests.forEach(({ name, type, link }) => { it('name: ' + name + ' type: ' + type, async () => { @@ -118,7 +116,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { } await retry.tryForTime(50000, async () => { - usage = (await usageAPI.getUsageStats()) as UsageStats; + const [{ stats }] = await usageAPI.getTelemetryStats({ unencrypted: true }); + usage = stats.stack_stats.kibana.plugins.reporting; reportingAPI.expectCompletedReportCount(usage, completedReportCount + 1); }); log.debug(`Elapsed Time: ${new Date().getTime() - startTime.getTime()}`); diff --git a/x-pack/test/upgrade/services/reporting_upgrade_services.ts b/x-pack/test/upgrade/services/reporting_upgrade_services.ts index 1a1e6113c3735..b583cce66d76f 100644 --- a/x-pack/test/upgrade/services/reporting_upgrade_services.ts +++ b/x-pack/test/upgrade/services/reporting_upgrade_services.ts @@ -7,37 +7,16 @@ import expect from '@kbn/expect'; import { indexTimestamp } from '@kbn/reporting-plugin/server/lib/store/index_timestamp'; +import { + AppCounts, + JobTypes, + LayoutCounts, + ReportingUsageType, +} from '@kbn/reporting-plugin/server/usage/types'; import { services as xpackServices } from '../../functional/services'; import { services as apiIntegrationServices } from '../../api_integration/services'; import { FtrProviderContext } from '../ftr_provider_context'; -interface PDFAppCounts { - app: { - [appName: string]: number; - }; - layout: { - [layoutType: string]: number; - }; -} - -export interface ReportingUsageStats { - available: boolean; - enabled: boolean; - total: number; - last_7_days: { - total: number; - printable_pdf: PDFAppCounts; - [jobType: string]: any; - }; - printable_pdf: PDFAppCounts; - status: any; - [jobType: string]: any; -} - -interface UsageStats { - reporting: ReportingUsageStats; -} - function removeWhitespace(str: string) { return str.replace(/\s/g, ''); } @@ -148,60 +127,76 @@ export function ReportingAPIProvider({ getService }: FtrProviderContext) { }); }, - expectRecentPdfAppStats(stats: UsageStats, app: string, count: number) { - expect(stats.reporting.last_7_days.printable_pdf.app[app]).to.be(count); + expectRecentPdfAppStats(stats: ReportingUsageType, app: keyof AppCounts, count: number) { + expect(stats.last7Days.printable_pdf.app[app]).to.be(count); }, - expectAllTimePdfAppStats(stats: UsageStats, app: string, count: number) { - expect(stats.reporting.printable_pdf.app[app]).to.be(count); + expectAllTimePdfAppStats(stats: ReportingUsageType, app: keyof AppCounts, count: number) { + expect(stats.printable_pdf.app[app]).to.be(count); }, - expectRecentPdfLayoutStats(stats: UsageStats, layout: string, count: number) { - expect(stats.reporting.last_7_days.printable_pdf.layout[layout]).to.be(count); + expectRecentPdfLayoutStats( + stats: ReportingUsageType, + layout: keyof LayoutCounts, + count: number + ) { + expect(stats.last7Days.printable_pdf.layout[layout]).to.be(count); }, - expectAllTimePdfLayoutStats(stats: UsageStats, layout: string, count: number) { - expect(stats.reporting.printable_pdf.layout[layout]).to.be(count); + expectAllTimePdfLayoutStats( + stats: ReportingUsageType, + layout: keyof LayoutCounts, + count: number + ) { + expect(stats.printable_pdf.layout[layout]).to.be(count); }, - expectRecentJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) { - expect(stats.reporting.last_7_days[jobType].total).to.be(count); + expectRecentJobTypeTotalStats( + stats: ReportingUsageType, + jobType: keyof JobTypes, + count: number + ) { + expect(stats.last7Days[jobType].total).to.be(count); }, - expectAllTimeJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) { - expect(stats.reporting[jobType].total).to.be(count); + expectAllTimeJobTypeTotalStats( + stats: ReportingUsageType, + jobType: keyof JobTypes, + count: number + ) { + expect(stats[jobType].total).to.be(count); }, - getCompletedReportCount(stats: UsageStats) { - return stats.reporting.status.completed; + getCompletedReportCount(stats: ReportingUsageType) { + return stats.status.completed; }, - expectCompletedReportCount(stats: UsageStats, count: number) { + expectCompletedReportCount(stats: ReportingUsageType, count: number) { expect(this.getCompletedReportCount(stats)).to.be(count); }, - getRecentPdfAppStats(stats: UsageStats, app: string) { - return stats.reporting.last_7_days.printable_pdf.app[app]; + getRecentPdfAppStats(stats: ReportingUsageType, app: keyof AppCounts) { + return stats.last7Days.printable_pdf.app[app]; }, - getAllTimePdfAppStats(stats: UsageStats, app: string) { - return stats.reporting.printable_pdf.app[app]; + getAllTimePdfAppStats(stats: ReportingUsageType, app: keyof AppCounts) { + return stats.printable_pdf.app[app]; }, - getRecentPdfLayoutStats(stats: UsageStats, layout: string) { - return stats.reporting.last_7_days.printable_pdf.layout[layout]; + getRecentPdfLayoutStats(stats: ReportingUsageType, layout: keyof LayoutCounts) { + return stats.last7Days.printable_pdf.layout[layout]; }, - getAllTimePdfLayoutStats(stats: UsageStats, layout: string) { - return stats.reporting.printable_pdf.layout[layout]; + getAllTimePdfLayoutStats(stats: ReportingUsageType, layout: keyof LayoutCounts) { + return stats.printable_pdf.layout[layout]; }, - getRecentJobTypeTotalStats(stats: UsageStats, jobType: string) { - return stats.reporting.last_7_days[jobType].total; + getRecentJobTypeTotalStats(stats: ReportingUsageType, jobType: keyof JobTypes) { + return stats.last7Days[jobType].total; }, - getAllTimeJobTypeTotalStats(stats: UsageStats, jobType: string) { - return stats.reporting[jobType].total; + getAllTimeJobTypeTotalStats(stats: ReportingUsageType, jobType: keyof JobTypes) { + return stats[jobType].total; }, }; }