From 199a0fbb02ff4f596433638317154a77bcb77e39 Mon Sep 17 00:00:00 2001 From: Jan Monschke Date: Tue, 8 Jul 2025 09:57:20 +0200 Subject: [PATCH 1/2] improve default config --- .../shared/cases/common/constants/incremental_id.ts | 9 +++++++++ x-pack/platform/plugins/shared/cases/server/config.ts | 8 ++++++-- .../tasks/incremental_id/incremental_id_task_manager.ts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 x-pack/platform/plugins/shared/cases/common/constants/incremental_id.ts diff --git a/x-pack/platform/plugins/shared/cases/common/constants/incremental_id.ts b/x-pack/platform/plugins/shared/cases/common/constants/incremental_id.ts new file mode 100644 index 0000000000000..192732451d8ab --- /dev/null +++ b/x-pack/platform/plugins/shared/cases/common/constants/incremental_id.ts @@ -0,0 +1,9 @@ +/* + * 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. + */ + +export const DEFAULT_TASK_INTERVAL_MINUTES = 10; +export const DEFAULT_TASK_START_DELAY_MINUTES = 10; diff --git a/x-pack/platform/plugins/shared/cases/server/config.ts b/x-pack/platform/plugins/shared/cases/server/config.ts index c8cedcd042d54..d375b774cf200 100644 --- a/x-pack/platform/plugins/shared/cases/server/config.ts +++ b/x-pack/platform/plugins/shared/cases/server/config.ts @@ -8,6 +8,10 @@ import type { TypeOf } from '@kbn/config-schema'; import { schema } from '@kbn/config-schema'; import { ALLOWED_MIME_TYPES } from '../common/constants/mime_types'; +import { + DEFAULT_TASK_INTERVAL_MINUTES, + DEFAULT_TASK_START_DELAY_MINUTES, +} from '../common/constants/incremental_id'; export const ConfigSchema = schema.object({ markdownPlugins: schema.object({ @@ -32,14 +36,14 @@ export const ConfigSchema = schema.object({ * The interval that the task should be scheduled at */ taskIntervalMinutes: schema.number({ - defaultValue: 10, + defaultValue: DEFAULT_TASK_INTERVAL_MINUTES, min: 5, }), /** * The initial delay the task will be started with */ taskStartDelayMinutes: schema.number({ - defaultValue: 10, + defaultValue: DEFAULT_TASK_START_DELAY_MINUTES, min: 1, }), }), diff --git a/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts b/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts index 69199a9cc8dea..271e511ed23ae 100644 --- a/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts +++ b/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts @@ -125,7 +125,7 @@ export class IncrementalIdTaskManager { id: CASES_INCREMENTAL_ID_SYNC_TASK_ID, taskType: CASES_INCREMENTAL_ID_SYNC_TASK_TYPE, // start delayed to give the system some time to start up properly - runAt: new Date(new Date().getTime() + this.config.taskIntervalMinutes * 60 * 1000), + runAt: new Date(new Date().getTime() + this.config.taskStartDelayMinutes * 60 * 1000), schedule: { interval: `${this.config.taskIntervalMinutes}m`, }, From b6b51a6c2b17844d341bc263444b0ebee249d938 Mon Sep 17 00:00:00 2001 From: Jan Monschke Date: Tue, 8 Jul 2025 09:59:58 +0200 Subject: [PATCH 2/2] add success/error usage counter --- .../plugins/shared/cases/server/plugin.ts | 17 ++++--- .../incremental_id_task_manager.ts | 49 +++++++++++++------ 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/x-pack/platform/plugins/shared/cases/server/plugin.ts b/x-pack/platform/plugins/shared/cases/server/plugin.ts index 524af9599650c..1b667340e6613 100644 --- a/x-pack/platform/plugins/shared/cases/server/plugin.ts +++ b/x-pack/platform/plugins/shared/cases/server/plugin.ts @@ -141,14 +141,6 @@ export class CasePlugin } if (plugins.taskManager) { - if (this.caseConfig.incrementalId.enabled) { - this.incrementalIdTaskManager = new IncrementalIdTaskManager( - plugins.taskManager, - this.caseConfig.incrementalId, - this.logger - ); - } - if (plugins.usageCollection) { createCasesTelemetry({ core, @@ -158,6 +150,15 @@ export class CasePlugin kibanaVersion: this.kibanaVersion, }); } + + if (this.caseConfig.incrementalId.enabled) { + this.incrementalIdTaskManager = new IncrementalIdTaskManager( + plugins.taskManager, + this.caseConfig.incrementalId, + this.logger, + plugins.usageCollection + ); + } } const router = core.http.createRouter(); diff --git a/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts b/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts index 271e511ed23ae..9f85757645499 100644 --- a/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts +++ b/x-pack/platform/plugins/shared/cases/server/tasks/incremental_id/incremental_id_task_manager.ts @@ -10,6 +10,8 @@ import { type TaskManagerSetupContract, type TaskManagerStartContract, } from '@kbn/task-manager-plugin/server'; +import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; +import type { IUsageCounter } from '@kbn/usage-collection-plugin/server/usage_counters/usage_counter'; import { CASE_SAVED_OBJECT, CASE_ID_INCREMENTER_SAVED_OBJECT } from '../../../common/constants'; import { CasesIncrementalIdService } from '../../services/incremental_id'; import type { ConfigType } from '../../config'; @@ -24,16 +26,21 @@ export class IncrementalIdTaskManager { private logger: Logger; private internalSavedObjectsClient?: SavedObjectsClient; private taskManager?: TaskManagerStartContract; - + private successErrorUsageCounter?: IUsageCounter; constructor( taskManager: TaskManagerSetupContract, config: ConfigType['incrementalId'], - logger: Logger + logger: Logger, + usageCollection?: UsageCollectionSetup ) { this.config = config; this.logger = logger.get('incremental_id_task'); this.logger.info('Registering Case Incremental ID Task Manager'); + if (usageCollection) { + this.successErrorUsageCounter = usageCollection?.createUsageCounter('CasesIncrementalId'); + } + taskManager.registerTaskDefinitions({ [CASES_INCREMENTAL_ID_SYNC_TASK_TYPE]: { title: 'Cases Numerical ID assignment', @@ -64,20 +71,32 @@ export class IncrementalIdTaskManager { this.logger.debug( `${casesWithoutIncrementalId.length} cases without incremental ids` ); - // Increment the case ids - const processedAmount = await casesIncrementService.incrementCaseIds( - casesWithoutIncrementalId - ); - this.logger.debug( - `Applied incremental ids to ${processedAmount} out of ${casesWithoutIncrementalId.length} cases` - ); - const endTime = performance.now(); - this.logger.debug( - `Task terminated ${CASES_INCREMENTAL_ID_SYNC_TASK_ID}. Task run took ${ - endTime - startTime - }ms [ started: ${initializedTime}, ended: ${new Date().toISOString()} ]` - ); + try { + // Increment the case ids + const processedAmount = await casesIncrementService.incrementCaseIds( + casesWithoutIncrementalId + ); + this.logger.debug( + `Applied incremental ids to ${processedAmount} out of ${casesWithoutIncrementalId.length} cases` + ); + + const endTime = performance.now(); + this.logger.debug( + `Task terminated ${CASES_INCREMENTAL_ID_SYNC_TASK_ID}. Task run took ${ + endTime - startTime + }ms [ started: ${initializedTime}, ended: ${new Date().toISOString()} ]` + ); + this.successErrorUsageCounter?.incrementCounter({ + counterName: 'incrementIdTaskSuccess', + incrementBy: 1, + }); + } catch (_) { + this.successErrorUsageCounter?.incrementCounter({ + counterName: 'incrementIdTaskError', + incrementBy: 1, + }); + } }, cancel: async () => { casesIncrementService.stopService();