From 2bf45dacd01f689c911b728575cfc1d8100a817d Mon Sep 17 00:00:00 2001 From: gonzalojaubert Date: Wed, 27 Sep 2023 09:17:33 +0100 Subject: [PATCH 1/3] Add elapsed time to data migration entity --- .../entities/booster-data-migration-entity.ts | 32 +++++++++++++++++-- .../read-models/data-migrations-read-model.ts | 16 ++++++++-- .../src/concepts/data-migration.ts | 6 ++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts b/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts index 1935f4d5f..10fe97f28 100644 --- a/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts +++ b/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts @@ -1,21 +1,47 @@ -import { DataMigrationStatus } from '@boostercloud/framework-types' +import { BoosterDataMigrationEntityDuration, DataMigrationStatus } from '@boostercloud/framework-types' import { BoosterDataMigrationStarted } from '../events/booster-data-migration-started' import { BoosterDataMigrationFinished } from '../events/booster-data-migration-finished' export class BoosterDataMigrationEntity { - public constructor(public id: string, public status: DataMigrationStatus, public lastUpdated: string) {} + public constructor( + public id: string, + public status: DataMigrationStatus, + public lastUpdated: string, + public duration?: BoosterDataMigrationEntityDuration + ) {} public static started( event: BoosterDataMigrationStarted, currentDataMigration: BoosterDataMigrationEntity ): BoosterDataMigrationEntity { - return new BoosterDataMigrationEntity(event.name, DataMigrationStatus.RUNNING, event.lastUpdated) + const duration = { + start: new Date().toISOString(), + } + return new BoosterDataMigrationEntity(event.name, DataMigrationStatus.RUNNING, event.lastUpdated, duration) } public static finished( event: BoosterDataMigrationFinished, currentDataMigration: BoosterDataMigrationEntity ): BoosterDataMigrationEntity { + const current = new Date() + if (currentDataMigration.duration?.start) { + const start = currentDataMigration.duration.start + const end = current.toISOString() + const elapsedTime = this.getElapsedTime(start, current) + const duration: BoosterDataMigrationEntityDuration = { + start: start, + end: end, + elapsedMilliseconds: elapsedTime, + } + return new BoosterDataMigrationEntity(event.name, DataMigrationStatus.FINISHED, event.lastUpdated, duration) + } return new BoosterDataMigrationEntity(event.name, DataMigrationStatus.FINISHED, event.lastUpdated) } + + private static getElapsedTime(start: string, current: Date): number { + const startTime = Date.parse(start) + const endTime = current.getTime() + return endTime - startTime + } } diff --git a/packages/framework-integration-tests/src/read-models/data-migrations-read-model.ts b/packages/framework-integration-tests/src/read-models/data-migrations-read-model.ts index 23b2bc206..bf077cf1d 100644 --- a/packages/framework-integration-tests/src/read-models/data-migrations-read-model.ts +++ b/packages/framework-integration-tests/src/read-models/data-migrations-read-model.ts @@ -1,17 +1,27 @@ import { BoosterDataMigrationEntity, Projects, ReadModel } from '@boostercloud/framework-core' -import { ProjectionResult } from '@boostercloud/framework-types' +import { BoosterDataMigrationEntityDuration, ProjectionResult } from '@boostercloud/framework-types' @ReadModel({ authorize: 'all', }) export class DataMigrationsReadModel { - public constructor(readonly id: string, readonly status: string, readonly lastUpdated: string) {} + public constructor( + readonly id: string, + readonly status: string, + readonly lastUpdated: string, + readonly duration?: BoosterDataMigrationEntityDuration + ) {} @Projects(BoosterDataMigrationEntity, 'id') public static updated( migration: BoosterDataMigrationEntity, _oldMigration?: DataMigrationsReadModel ): ProjectionResult { - return new DataMigrationsReadModel(migration.id, migration.status.toString(), migration.lastUpdated) + return new DataMigrationsReadModel( + migration.id, + migration.status.toString(), + migration.lastUpdated, + migration.duration + ) } } diff --git a/packages/framework-types/src/concepts/data-migration.ts b/packages/framework-types/src/concepts/data-migration.ts index 20dab7510..a247e3248 100644 --- a/packages/framework-types/src/concepts/data-migration.ts +++ b/packages/framework-types/src/concepts/data-migration.ts @@ -15,3 +15,9 @@ export interface DataMigrationMetadata { readonly class: DataMigrationInterface migrationOptions: DataMigrationParameters } + +export interface BoosterDataMigrationEntityDuration { + start: string + end?: string + elapsedMilliseconds?: number +} From d17d2c0eab32c181a98a801948b511dcba279b58 Mon Sep 17 00:00:00 2001 From: gonzalojaubert Date: Wed, 4 Oct 2023 09:36:18 +0100 Subject: [PATCH 2/3] rush change --- ..._elapsed_time_data_migrations_2023-10-04-08-36.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@boostercloud/framework-core/add_elapsed_time_data_migrations_2023-10-04-08-36.json diff --git a/common/changes/@boostercloud/framework-core/add_elapsed_time_data_migrations_2023-10-04-08-36.json b/common/changes/@boostercloud/framework-core/add_elapsed_time_data_migrations_2023-10-04-08-36.json new file mode 100644 index 000000000..6f5ce10b1 --- /dev/null +++ b/common/changes/@boostercloud/framework-core/add_elapsed_time_data_migrations_2023-10-04-08-36.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@boostercloud/framework-core", + "comment": "Add elapsed time to data migration entities", + "type": "minor" + } + ], + "packageName": "@boostercloud/framework-core" +} \ No newline at end of file From 149f258637009b835eb0acfb1db7e541272364e9 Mon Sep 17 00:00:00 2001 From: gonzalojaubert Date: Wed, 18 Oct 2023 16:02:55 +0100 Subject: [PATCH 3/3] fix elapsed time calculation --- .../entities/booster-data-migration-entity.ts | 10 +++------- .../end-to-end/entities.integration.ts | 5 +++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts b/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts index 10fe97f28..04a02becb 100644 --- a/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts +++ b/packages/framework-core/src/core-concepts/data-migration/entities/booster-data-migration-entity.ts @@ -28,7 +28,9 @@ export class BoosterDataMigrationEntity { if (currentDataMigration.duration?.start) { const start = currentDataMigration.duration.start const end = current.toISOString() - const elapsedTime = this.getElapsedTime(start, current) + const startTime = Date.parse(start) + const endTime = current.getTime() + const elapsedTime = endTime - startTime const duration: BoosterDataMigrationEntityDuration = { start: start, end: end, @@ -38,10 +40,4 @@ export class BoosterDataMigrationEntity { } return new BoosterDataMigrationEntity(event.name, DataMigrationStatus.FINISHED, event.lastUpdated) } - - private static getElapsedTime(start: string, current: Date): number { - const startTime = Date.parse(start) - const endTime = current.getTime() - return endTime - startTime - } } diff --git a/packages/framework-integration-tests/integration/provider-unaware/end-to-end/entities.integration.ts b/packages/framework-integration-tests/integration/provider-unaware/end-to-end/entities.integration.ts index aad2ebbc2..f9c76b6bc 100644 --- a/packages/framework-integration-tests/integration/provider-unaware/end-to-end/entities.integration.ts +++ b/packages/framework-integration-tests/integration/provider-unaware/end-to-end/entities.integration.ts @@ -331,9 +331,10 @@ describe('Entities end-to-end tests', () => { }) }, (result) => { - const count = result?.data?.ListDataMigrationsReadModels?.count + const resultReadModels = result?.data?.ListDataMigrationsReadModels + const count = resultReadModels?.count if (count < 2) { - return `Waiting for ${count} migrations. Done ${count} migrations` + return `Waiting for at least 2 migrations. Done ${count} migrations. ${JSON.stringify(resultReadModels)}` } return true }