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, DomainEventPublisherInterface,
EmailBackupRequestedEvent, EmailBackupRequestedEvent,
} from '@standardnotes/domain-events' } from '@standardnotes/domain-events'
import { EmailLevel } from '@standardnotes/domain-core' import { EmailLevel, Uuid } from '@standardnotes/domain-core'
import { Logger } from 'winston' import { Logger } from 'winston'
import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface' import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface'
import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface' import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface'
@ -32,6 +32,17 @@ export class EmailBackupRequestedEventHandler implements DomainEventHandlerInter
event: EmailBackupRequestedEvent, event: EmailBackupRequestedEvent,
itemRepository: ItemRepositoryInterface, itemRepository: ItemRepositoryInterface,
): Promise<void> { ): 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 = { const itemQuery: ItemQuery = {
userUuid: event.payload.userUuid, userUuid: event.payload.userUuid,
sortBy: 'updated_at_timestamp', sortBy: 'updated_at_timestamp',
@ -42,6 +53,7 @@ export class EmailBackupRequestedEventHandler implements DomainEventHandlerInter
const itemUuidBundles = await this.itemTransferCalculator.computeItemUuidBundlesToFetch( const itemUuidBundles = await this.itemTransferCalculator.computeItemUuidBundlesToFetch(
itemContentSizeDescriptors, itemContentSizeDescriptors,
this.emailAttachmentMaxByteSize, this.emailAttachmentMaxByteSize,
userUuid,
) )
const backupFileNames: string[] = [] const backupFileNames: string[] = []

View file

@ -4,13 +4,18 @@ import { Logger } from 'winston'
import { ItemTransferCalculator } from './ItemTransferCalculator' import { ItemTransferCalculator } from './ItemTransferCalculator'
import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor'
import { Uuid } from '@standardnotes/domain-core'
describe('ItemTransferCalculator', () => { describe('ItemTransferCalculator', () => {
let logger: Logger let logger: Logger
const createCalculator = () => new ItemTransferCalculator(logger) const createCalculator = () => new ItemTransferCalculator(logger)
let userUuid: Uuid
beforeEach(() => { beforeEach(() => {
userUuid = Uuid.create('00000000-0000-0000-0000-000000000000').getValue()
logger = {} as jest.Mocked<Logger> logger = {} as jest.Mocked<Logger>
logger.warn = jest.fn() logger.warn = jest.fn()
}) })
@ -23,7 +28,7 @@ describe('ItemTransferCalculator', () => {
ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), 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({ expect(result).toEqual({
uuids: [ uuids: [
@ -42,7 +47,7 @@ describe('ItemTransferCalculator', () => {
ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), 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({ expect(result).toEqual({
uuids: ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], 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(), 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({ expect(result).toEqual({
uuids: [ uuids: [
@ -76,7 +81,7 @@ describe('ItemTransferCalculator', () => {
ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), 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({ expect(result).toEqual({
uuids: ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], 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(), 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([ expect(result).toEqual([
[ [
@ -111,7 +116,7 @@ describe('ItemTransferCalculator', () => {
ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), 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([ expect(result).toEqual([
['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], ['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(), 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([ expect(result).toEqual([
[ [
@ -144,7 +149,7 @@ describe('ItemTransferCalculator', () => {
ItemContentSizeDescriptor.create('00000000-0000-0000-0000-000000000002', 20).getValue(), 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([ expect(result).toEqual([
['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001'], ['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 { ItemTransferCalculatorInterface } from './ItemTransferCalculatorInterface'
import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor'
import { Uuid } from '@standardnotes/domain-core'
export class ItemTransferCalculator implements ItemTransferCalculatorInterface { export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
constructor(private logger: Logger) {} constructor(private logger: Logger) {}
@ -9,6 +10,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
async computeItemUuidsToFetch( async computeItemUuidsToFetch(
itemContentSizeDescriptors: ItemContentSizeDescriptor[], itemContentSizeDescriptors: ItemContentSizeDescriptor[],
bytesTransferLimit: number, bytesTransferLimit: number,
userUuid: Uuid,
): Promise<{ uuids: Array<string>; transferLimitBreachedBeforeEndOfItems: boolean }> { ): Promise<{ uuids: Array<string>; transferLimitBreachedBeforeEndOfItems: boolean }> {
const itemUuidsToFetch = [] const itemUuidsToFetch = []
let totalContentSizeInBytes = 0 let totalContentSizeInBytes = 0
@ -24,6 +26,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
bytesTransferLimit, bytesTransferLimit,
itemUuidsToFetch, itemUuidsToFetch,
itemContentSizeDescriptors, itemContentSizeDescriptors,
userUuid,
}) })
if (transferLimitBreached) { if (transferLimitBreached) {
@ -41,6 +44,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
async computeItemUuidBundlesToFetch( async computeItemUuidBundlesToFetch(
itemContentSizeDescriptors: ItemContentSizeDescriptor[], itemContentSizeDescriptors: ItemContentSizeDescriptor[],
bytesTransferLimit: number, bytesTransferLimit: number,
userUuid: Uuid,
): Promise<Array<Array<string>>> { ): Promise<Array<Array<string>>> {
let itemUuidsToFetch = [] let itemUuidsToFetch = []
let totalContentSizeInBytes = 0 let totalContentSizeInBytes = 0
@ -56,6 +60,7 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
bytesTransferLimit, bytesTransferLimit,
itemUuidsToFetch, itemUuidsToFetch,
itemContentSizeDescriptors, itemContentSizeDescriptors,
userUuid,
}) })
if (transferLimitBreached) { if (transferLimitBreached) {
@ -77,15 +82,20 @@ export class ItemTransferCalculator implements ItemTransferCalculatorInterface {
bytesTransferLimit: number bytesTransferLimit: number
itemUuidsToFetch: Array<string> itemUuidsToFetch: Array<string>
itemContentSizeDescriptors: ItemContentSizeDescriptor[] itemContentSizeDescriptors: ItemContentSizeDescriptor[]
userUuid: Uuid
}): boolean { }): boolean {
const transferLimitBreached = dto.totalContentSizeInBytes >= dto.bytesTransferLimit const transferLimitBreached = dto.totalContentSizeInBytes >= dto.bytesTransferLimit
const transferLimitBreachedAtFirstItem = const transferLimitBreachedAtFirstItem =
transferLimitBreached && dto.itemUuidsToFetch.length === 1 && dto.itemContentSizeDescriptors.length > 1 transferLimitBreached && dto.itemUuidsToFetch.length === 1 && dto.itemContentSizeDescriptors.length > 1
if (transferLimitBreachedAtFirstItem) { if (transferLimitBreachedAtFirstItem) {
this.logger.warn( this.logger.warn('Item is breaching the content size transfer limit at first item in the bundle to fetch.', {
`Item ${dto.itemUuidsToFetch[0]} is breaching the content size transfer limit: ${dto.bytesTransferLimit}`, codeTag: 'ItemTransferCalculator',
) itemUuid: dto.itemUuidsToFetch[0],
totalContentSizeInBytes: dto.totalContentSizeInBytes,
bytesTransferLimit: dto.bytesTransferLimit,
userId: dto.userUuid.value,
})
} }
return transferLimitBreached && !transferLimitBreachedAtFirstItem return transferLimitBreached && !transferLimitBreachedAtFirstItem

View file

@ -1,12 +1,16 @@
import { Uuid } from '@standardnotes/domain-core'
import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor' import { ItemContentSizeDescriptor } from './ItemContentSizeDescriptor'
export interface ItemTransferCalculatorInterface { export interface ItemTransferCalculatorInterface {
computeItemUuidsToFetch( computeItemUuidsToFetch(
itemContentSizeDescriptors: ItemContentSizeDescriptor[], itemContentSizeDescriptors: ItemContentSizeDescriptor[],
bytesTransferLimit: number, bytesTransferLimit: number,
userUuid: Uuid,
): Promise<{ uuids: Array<string>; transferLimitBreachedBeforeEndOfItems: boolean }> ): Promise<{ uuids: Array<string>; transferLimitBreachedBeforeEndOfItems: boolean }>
computeItemUuidBundlesToFetch( computeItemUuidBundlesToFetch(
itemContentSizeDescriptors: ItemContentSizeDescriptor[], itemContentSizeDescriptors: ItemContentSizeDescriptor[],
bytesTransferLimit: number, bytesTransferLimit: number,
userUuid: Uuid,
): Promise<Array<Array<string>>> ): Promise<Array<Array<string>>>
} }

View file

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