fix(syncing-server): add metadata to transfer breach logs

This commit is contained in:
Karol Sójko 2024-01-05 12:35:38 +01:00
parent 9380900aaf
commit 73c2cc1222
No known key found for this signature in database
GPG key ID: C2F813669419D05F
5 changed files with 44 additions and 12 deletions

View file

@ -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<void> {
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[] = []

View file

@ -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>
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'],

View file

@ -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<string>; 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<Array<Array<string>>> {
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<string>
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

View file

@ -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<string>; transferLimitBreachedBeforeEndOfItems: boolean }>
computeItemUuidBundlesToFetch(
itemContentSizeDescriptors: ItemContentSizeDescriptor[],
bytesTransferLimit: number,
userUuid: Uuid,
): Promise<Array<Array<string>>>
}

View file

@ -63,6 +63,7 @@ export class GetItems implements UseCaseInterface<GetItemsResult> {
const { uuids, transferLimitBreachedBeforeEndOfItems } = await this.itemTransferCalculator.computeItemUuidsToFetch(
itemContentSizeDescriptors,
this.contentSizeTransferLimit,
userUuid,
)
let items: Array<Item> = []
if (uuids.length > 0) {