Skip to content

Commit

Permalink
report schema in a separate dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Dec 13, 2019
1 parent 4fa94f3 commit 8b1a07c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 40 deletions.
21 changes: 21 additions & 0 deletions src/plugins/usage_collection/server/report/index.ts
Original file line number Diff line number Diff line change
@@ -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';
59 changes: 59 additions & 0 deletions src/plugins/usage_collection/server/report/schema.ts
Original file line number Diff line number Diff line change
@@ -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>(METRIC_TYPE.CLICK),
schema.literal<METRIC_TYPE>(METRIC_TYPE.LOADED),
schema.literal<METRIC_TYPE>(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<typeof reportSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* under the License.
*/

import { Report } from '@kbn/analytics';
import { ReportSchemaType } from './schema';

export async function storeReport(internalRepository: any, report: Partial<Report>) {
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([
Expand Down
41 changes: 3 additions & 38 deletions src/plugins/usage_collection/server/routes/report_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,24 @@
*/

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(
{
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>(METRIC_TYPE.CLICK),
schema.literal<METRIC_TYPE>(METRIC_TYPE.LOADED),
schema.literal<METRIC_TYPE>(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,
}),
},
},
async (context, req, res) => {
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' } });
Expand Down

0 comments on commit 8b1a07c

Please sign in to comment.