diff --git a/x-pack/plugins/reporting/server/plugin.ts b/x-pack/plugins/reporting/server/plugin.ts index 0a2318daded029c..3c7f9b521aad6fc 100644 --- a/x-pack/plugins/reporting/server/plugin.ts +++ b/x-pack/plugins/reporting/server/plugin.ts @@ -20,6 +20,7 @@ import type { ReportingStart, ReportingStartDeps, } from './types'; +import { reportingStatusSavedObjectType, updateLastReported } from './saved_objects'; import { registerReportingUsageCollector } from './usage'; export class ReportingPlugin @@ -33,6 +34,8 @@ export class ReportingPlugin } public setup(core: CoreSetup, plugins: ReportingSetupDeps) { + core.savedObjects.registerType(reportingStatusSavedObjectType); + const { http } = core; const { features, licensing, security, spaces, taskManager } = plugins; @@ -95,6 +98,7 @@ export class ReportingPlugin // async background start (async () => { await reportingCore.pluginSetsUp(); + await updateLastReported(core.savedObjects, this.initContext.env.packageInfo); const store = new ReportingStore(reportingCore, this.logger); diff --git a/x-pack/plugins/reporting/server/saved_objects/index.ts b/x-pack/plugins/reporting/server/saved_objects/index.ts new file mode 100644 index 000000000000000..21a092487b05e65 --- /dev/null +++ b/x-pack/plugins/reporting/server/saved_objects/index.ts @@ -0,0 +1,52 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { PackageInfo, SavedObjectsServiceStart, SavedObjectsType } from 'kibana/server'; +import { PLUGIN_ID } from '../../common/constants'; + +interface ReportingStatusSavedObjectAttributes { + lastReported: string; + lastVersionChecked: string; +} + +const SAVED_OBJECT_ID = 'reporting-status'; + +export const reportingStatusSavedObjectType: SavedObjectsType = + { + name: PLUGIN_ID, + namespaceType: 'agnostic', + hidden: true, + mappings: { + properties: { + lastReported: { + type: 'date', + }, + lastVersionChecked: { + ignore_above: 256, + type: 'keyword', + }, + }, + }, + }; + +export async function updateLastReported( + savedObjects: SavedObjectsServiceStart, + { version }: PackageInfo +) { + const reportingStatus: ReportingStatusSavedObjectAttributes = { + lastReported: new Date().toISOString(), + lastVersionChecked: version, + }; + + const scopedClient = savedObjects.createInternalRepository([PLUGIN_ID]); + await scopedClient.update( + PLUGIN_ID, + SAVED_OBJECT_ID, + reportingStatus, + { upsert: reportingStatus } + ); +}