diff --git a/src/plugins/usage_collection/server/report/index.ts b/src/plugins/usage_collection/server/report/index.ts new file mode 100644 index 0000000000000..bb8c1d149fdd2 --- /dev/null +++ b/src/plugins/usage_collection/server/report/index.ts @@ -0,0 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export { storeReport } from './store_report'; +export { reportSchema } from './schema'; diff --git a/src/plugins/usage_collection/server/report/schema.ts b/src/plugins/usage_collection/server/report/schema.ts new file mode 100644 index 0000000000000..5adf7d6575a70 --- /dev/null +++ b/src/plugins/usage_collection/server/report/schema.ts @@ -0,0 +1,59 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { schema, TypeOf } from '@kbn/config-schema'; +import { METRIC_TYPE } from '@kbn/analytics'; + +export const reportSchema = schema.object({ + reportVersion: schema.maybe(schema.literal(1)), + userAgent: schema.maybe( + schema.recordOf( + schema.string(), + schema.object({ + key: schema.string(), + type: schema.string(), + appName: schema.string(), + userAgent: schema.string(), + }) + ) + ), + uiStatsMetrics: schema.maybe( + schema.recordOf( + schema.string(), + schema.object({ + key: schema.string(), + type: schema.oneOf([ + schema.literal(METRIC_TYPE.CLICK), + schema.literal(METRIC_TYPE.LOADED), + schema.literal(METRIC_TYPE.COUNT), + ]), + appName: schema.string(), + eventName: schema.string(), + stats: schema.object({ + min: schema.number(), + sum: schema.number(), + max: schema.number(), + avg: schema.number(), + }), + }) + ) + ), +}); + +export type ReportSchemaType = TypeOf; diff --git a/src/plugins/usage_collection/server/store_report.ts b/src/plugins/usage_collection/server/report/store_report.ts similarity index 96% rename from src/plugins/usage_collection/server/store_report.ts rename to src/plugins/usage_collection/server/report/store_report.ts index 0bd087e4122e2..9232a23d6151b 100644 --- a/src/plugins/usage_collection/server/store_report.ts +++ b/src/plugins/usage_collection/server/report/store_report.ts @@ -17,9 +17,9 @@ * under the License. */ -import { Report } from '@kbn/analytics'; +import { ReportSchemaType } from './schema'; -export async function storeReport(internalRepository: any, report: Partial) { +export async function storeReport(internalRepository: any, report: ReportSchemaType) { const uiStatsMetrics = report.uiStatsMetrics ? Object.entries(report.uiStatsMetrics) : []; const userAgents = report.userAgent ? Object.entries(report.userAgent) : []; return Promise.all([ diff --git a/src/plugins/usage_collection/server/routes/report_metrics.ts b/src/plugins/usage_collection/server/routes/report_metrics.ts index 3f12848f901c5..93f03ea8067d2 100644 --- a/src/plugins/usage_collection/server/routes/report_metrics.ts +++ b/src/plugins/usage_collection/server/routes/report_metrics.ts @@ -18,9 +18,8 @@ */ import { schema } from '@kbn/config-schema'; -import { METRIC_TYPE, Report } from '@kbn/analytics'; import { IRouter } from '../../../../../src/core/server'; -import { storeReport } from '../store_report'; +import { storeReport, reportSchema } from '../report'; export function registerUiMetricRoute(router: IRouter, getLegacySavedObjects: () => any) { router.post( @@ -28,41 +27,7 @@ export function registerUiMetricRoute(router: IRouter, getLegacySavedObjects: () path: '/api/ui_metric/report', validate: { body: schema.object({ - report: schema.object({ - reportVersion: schema.maybe(schema.literal(1)), - userAgent: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - key: schema.string(), - type: schema.string(), - appName: schema.string(), - userAgent: schema.string(), - }) - ) - ), - uiStatsMetrics: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - key: schema.string(), - type: schema.oneOf([ - schema.literal(METRIC_TYPE.CLICK), - schema.literal(METRIC_TYPE.LOADED), - schema.literal(METRIC_TYPE.COUNT), - ]), - appName: schema.string(), - eventName: schema.string(), - stats: schema.object({ - min: schema.number(), - sum: schema.number(), - max: schema.number(), - avg: schema.number(), - }), - }) - ) - ), - }), + report: reportSchema, }), }, }, @@ -70,7 +35,7 @@ export function registerUiMetricRoute(router: IRouter, getLegacySavedObjects: () const { report } = req.body; try { const internalRepository = getLegacySavedObjects(); - await storeReport(internalRepository, report as Report); + await storeReport(internalRepository, report); return res.ok({ body: { status: 'ok' } }); } catch (error) { return res.ok({ body: { status: 'fail' } });