From 73c2cc1222b647e82de3755c7e28d283cd9f872f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Fri, 5 Jan 2024 12:35:38 +0100 Subject: [PATCH] fix(syncing-server): add metadata to transfer breach logs --- .../EmailBackupRequestedEventHandler.ts | 14 ++++++++++++- .../Item/ItemTransferCalculator.spec.ts | 21 ++++++++++++------- .../src/Domain/Item/ItemTransferCalculator.ts | 16 +++++++++++--- .../Item/ItemTransferCalculatorInterface.ts | 4 ++++ .../UseCase/Syncing/GetItems/GetItems.ts | 1 + 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/packages/syncing-server/src/Domain/Handler/EmailBackupRequestedEventHandler.ts b/packages/syncing-server/src/Domain/Handler/EmailBackupRequestedEventHandler.ts index d2cc1d7b3..3e7cff112 100644 --- a/packages/syncing-server/src/Domain/Handler/EmailBackupRequestedEventHandler.ts +++ b/packages/syncing-server/src/Domain/Handler/EmailBackupRequestedEventHandler.ts @@ -3,7 +3,7 @@ import { DomainEventPublisherInterface, EmailBackupRequestedEvent, } from '@standardnotes/domain-events' -import { EmailLevel } from '@standardnotes/domain-core' +import { EmailLevel, Uuid } from '@standardnotes/domain-core' import { Logger } from 'winston' import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface' import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface' @@ -32,6 +32,17 @@ export class EmailBackupRequestedEventHandler implements DomainEventHandlerInter event: EmailBackupRequestedEvent, itemRepository: ItemRepositoryInterface, ): Promise { + const userUuidOrError = Uuid.create(event.payload.userUuid) + if (userUuidOrError.isFailed()) { + this.logger.error('User uuid is invalid', { + userId: event.payload.userUuid, + codeTag: 'EmailBackupRequestedEventHandler', + }) + + return + } + const userUuid = userUuidOrError.getValue() + const itemQuery: ItemQuery = { userUuid: event.payload.userUuid, sortBy: 'updated_at_timestamp', @@ -42,6 +53,7 @@ export class EmailBackupRequestedEventHandler implements DomainEventHandlerInter const itemUuidBundles = await this.itemTransferCalculator.computeItemUuidBundlesToFetch( itemContentSizeDescriptors, this.emailAttachmentMaxByteSize, + userUuid, ) const backupFileNames: string[] = [] diff --git a/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.spec.ts b/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.spec.ts index 44926e6a0..7e27c1872 100644 --- a/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.spec.ts +++ b/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.spec.ts @@ -4,13 +4,18 @@ import { Logger } from 'winston' import { ItemTransferCalculator } from './ItemTransferCalculator' import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' +import { Uuid } from '@standardnotes/domain-core' describe('ItemTransferCalculator', () => { let logger: Logger const createCalculator = () => new ItemTransferCalculator(logger) + let userUuid: Uuid + beforeEach(() => { + userUuid = Uuid.create('00000000-0000-0000-0000-000000000000').getValue() + logger = {} as jest.Mocked logger.warn = jest.fn() }) @@ -23,7 +28,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 50) + const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 50, userUuid) expect(result).toEqual({ uuids: [ @@ -42,7 +47,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 40) + const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 40, userUuid) expect(result).toEqual({ uuids: ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], @@ -57,7 +62,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', null).getValue(), ] - const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 50) + const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 50, userUuid) expect(result).toEqual({ uuids: [ @@ -76,7 +81,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 40) + const result = await createCalculator().computeItemUuidsToFetch(itemContentSizeDescriptors, 40, userUuid) expect(result).toEqual({ uuids: ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], @@ -93,7 +98,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 50) + const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 50, userUuid) expect(result).toEqual([ [ @@ -111,7 +116,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 40) + const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 40, userUuid) expect(result).toEqual([ ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], @@ -126,7 +131,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', null).getValue(), ] - const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 50) + const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 50, userUuid) expect(result).toEqual([ [ @@ -144,7 +149,7 @@ describe('ItemTransferCalculator', () => { ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), ] - const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 40) + const result = await createCalculator().computeItemUuidBundlesToFetch(itemContentSizeDescriptors, 40, userUuid) expect(result).toEqual([ ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], diff --git a/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.ts b/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.ts index a32e82cea..27a96b775 100644 --- a/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.ts +++ b/packages/syncing-server/src/Domain/Item/ItemTransferCalculator.ts @@ -2,6 +2,7 @@ import { Logger } from 'winston' import { ItemTransferCalculatorInterface } from './ItemTransferCalculatorInterface' import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' +import { Uuid } from '@standardnotes/domain-core' export class ItemTransferCalculator implements ItemTransferCalculatorInterface { constructor(private logger: Logger) {} @@ -9,6 +10,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface { async computeItemUuidsToFetch( itemContentSizeDescriptors: ItemContentSizeDescriptor[], bytesTransferLimit: number, + userUuid: Uuid, ): Promise<{ uuids: Array; transferLimitBreachedBeforeEndOfItems: boolean }> { const itemUuidsToFetch = [] let totalContentSizeInBytes = 0 @@ -24,6 +26,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface { bytesTransferLimit, itemUuidsToFetch, itemContentSizeDescriptors, + userUuid, }) if (transferLimitBreached) { @@ -41,6 +44,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface { async computeItemUuidBundlesToFetch( itemContentSizeDescriptors: ItemContentSizeDescriptor[], bytesTransferLimit: number, + userUuid: Uuid, ): Promise>> { let itemUuidsToFetch = [] let totalContentSizeInBytes = 0 @@ -56,6 +60,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface { bytesTransferLimit, itemUuidsToFetch, itemContentSizeDescriptors, + userUuid, }) if (transferLimitBreached) { @@ -77,15 +82,20 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface { bytesTransferLimit: number itemUuidsToFetch: Array itemContentSizeDescriptors: ItemContentSizeDescriptor[] + userUuid: Uuid }): boolean { const transferLimitBreached = dto.totalContentSizeInBytes >= dto.bytesTransferLimit const transferLimitBreachedAtFirstItem = transferLimitBreached && dto.itemUuidsToFetch.length === 1 && dto.itemContentSizeDescriptors.length > 1 if (transferLimitBreachedAtFirstItem) { - this.logger.warn( - `Item ${dto.itemUuidsToFetch[0]} is breaching the content size transfer limit: ${dto.bytesTransferLimit}`, - ) + this.logger.warn('Item is breaching the content size transfer limit at first item in the bundle to fetch.', { + codeTag: 'ItemTransferCalculator', + itemUuid: dto.itemUuidsToFetch[0], + totalContentSizeInBytes: dto.totalContentSizeInBytes, + bytesTransferLimit: dto.bytesTransferLimit, + userId: dto.userUuid.value, + }) } return transferLimitBreached && !transferLimitBreachedAtFirstItem diff --git a/packages/syncing-server/src/Domain/Item/ItemTransferCalculatorInterface.ts b/packages/syncing-server/src/Domain/Item/ItemTransferCalculatorInterface.ts index 195e166b3..3970b9cb2 100644 --- a/packages/syncing-server/src/Domain/Item/ItemTransferCalculatorInterface.ts +++ b/packages/syncing-server/src/Domain/Item/ItemTransferCalculatorInterface.ts @@ -1,12 +1,16 @@ +import { Uuid } from '@standardnotes/domain-core' + import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' export interface ItemTransferCalculatorInterface { computeItemUuidsToFetch( itemContentSizeDescriptors: ItemContentSizeDescriptor[], bytesTransferLimit: number, + userUuid: Uuid, ): Promise<{ uuids: Array; transferLimitBreachedBeforeEndOfItems: boolean }> computeItemUuidBundlesToFetch( itemContentSizeDescriptors: ItemContentSizeDescriptor[], bytesTransferLimit: number, + userUuid: Uuid, ): Promise>> } diff --git a/packages/syncing-server/src/Domain/UseCase/Syncing/GetItems/GetItems.ts b/packages/syncing-server/src/Domain/UseCase/Syncing/GetItems/GetItems.ts index f64593776..daff57b54 100644 --- a/packages/syncing-server/src/Domain/UseCase/Syncing/GetItems/GetItems.ts +++ b/packages/syncing-server/src/Domain/UseCase/Syncing/GetItems/GetItems.ts @@ -63,6 +63,7 @@ export class GetItems implements UseCaseInterface { const { uuids, transferLimitBreachedBeforeEndOfItems } = await this.itemTransferCalculator.computeItemUuidsToFetch( itemContentSizeDescriptors, this.contentSizeTransferLimit, + userUuid, ) let items: Array = [] if (uuids.length > 0) {