-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1930 from RedisInsight/be/feature/RI-4290-upload_…
…data_in_bulk #RI-4380 BE Upload data in bulk base implementation
- Loading branch information
Showing
10 changed files
with
527 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
redisinsight/api/src/modules/bulk-actions/bulk-import.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
Body, | ||
ClassSerializerInterceptor, | ||
Controller, HttpCode, Post, | ||
UseInterceptors, UsePipes, ValidationPipe, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiConsumes, ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { ApiEndpoint } from 'src/decorators/api-endpoint.decorator'; | ||
import { FormDataRequest } from 'nestjs-form-data'; | ||
import { BulkImportService } from 'src/modules/bulk-actions/bulk-import.service'; | ||
import { UploadImportFileDto } from 'src/modules/bulk-actions/dto/upload-import-file.dto'; | ||
import { ClientMetadataParam } from 'src/common/decorators'; | ||
import { ClientMetadata } from 'src/common/models'; | ||
import { IBulkActionOverview } from 'src/modules/bulk-actions/interfaces/bulk-action-overview.interface'; | ||
|
||
@UsePipes(new ValidationPipe({ transform: true })) | ||
@UseInterceptors(ClassSerializerInterceptor) | ||
@ApiTags('Bulk Actions') | ||
@Controller('/bulk-actions') | ||
export class BulkImportController { | ||
constructor(private readonly service: BulkImportService) {} | ||
|
||
@Post('import') | ||
@ApiConsumes('multipart/form-data') | ||
@HttpCode(200) | ||
@FormDataRequest() | ||
@ApiEndpoint({ | ||
description: 'Import data from file', | ||
responses: [ | ||
{ | ||
type: Object, | ||
}, | ||
], | ||
}) | ||
async import( | ||
@Body() dto: UploadImportFileDto, | ||
@ClientMetadataParam() clientMetadata: ClientMetadata, | ||
): Promise<IBulkActionOverview> { | ||
return this.service.import(clientMetadata, dto); | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
redisinsight/api/src/modules/bulk-actions/bulk-import.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BulkImportService } from 'src/modules/bulk-actions/bulk-import.service'; | ||
import { DatabaseConnectionService } from 'src/modules/database/database-connection.service'; | ||
import { | ||
mockClientMetadata, | ||
mockDatabaseConnectionService, | ||
mockIORedisClient, | ||
mockIORedisCluster, MockType | ||
} from 'src/__mocks__'; | ||
import { MemoryStoredFile } from 'nestjs-form-data'; | ||
import { BulkActionSummary } from 'src/modules/bulk-actions/models/bulk-action-summary'; | ||
import { IBulkActionOverview } from 'src/modules/bulk-actions/interfaces/bulk-action-overview.interface'; | ||
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/constants'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
|
||
const generateNCommandsBuffer = (n: number) => Buffer.from( | ||
(new Array(n)).fill(1).map(() => ['set', ['foo', 'bar']]).join('\n'), | ||
); | ||
const generateNBatchCommands = (n: number) => (new Array(n)).fill(1).map(() => ['set', ['foo', 'bar']]); | ||
const generateNBatchCommandsResults = (n: number) => (new Array(n)).fill(1).map(() => [null, 'OK']); | ||
const mockBatchCommands = generateNBatchCommands(100); | ||
const mockBatchCommandsResult = generateNBatchCommandsResults(100); | ||
const mockBatchCommandsResultWithErrors = [...(new Array(99)).fill(1).map(() => [null, 'OK']), ['ReplyError']]; | ||
const mockSummary: BulkActionSummary = Object.assign(new BulkActionSummary(), { | ||
processed: 100, | ||
succeed: 100, | ||
failed: 0, | ||
errors: [], | ||
}); | ||
|
||
const mockSummaryWithErrors = Object.assign(new BulkActionSummary(), { | ||
processed: 100, | ||
succeed: 99, | ||
failed: 1, | ||
errors: [], | ||
}); | ||
|
||
const mockImportResult: IBulkActionOverview = { | ||
id: 'empty', | ||
databaseId: mockClientMetadata.databaseId, | ||
type: BulkActionType.Import, | ||
summary: mockSummary.getOverview(), | ||
progress: null, | ||
filter: null, | ||
status: BulkActionStatus.Completed, | ||
duration: 100, | ||
}; | ||
|
||
const mockUploadImportFileDto = { | ||
file: { | ||
originalname: 'filename', | ||
size: 1, | ||
buffer: Buffer.from('SET foo bar'), | ||
} as unknown as MemoryStoredFile, | ||
}; | ||
|
||
describe('BulkImportService', () => { | ||
let service: BulkImportService; | ||
let databaseConnectionService: MockType<DatabaseConnectionService>; | ||
|
||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
BulkImportService, | ||
{ | ||
provide: DatabaseConnectionService, | ||
useFactory: mockDatabaseConnectionService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get(BulkImportService); | ||
databaseConnectionService = module.get(DatabaseConnectionService); | ||
}); | ||
|
||
describe('executeBatch', () => { | ||
it('should execute batch in pipeline for standalone', async () => { | ||
mockIORedisClient.exec.mockResolvedValueOnce(mockBatchCommandsResult); | ||
expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual(mockSummary); | ||
}); | ||
it('should execute batch in pipeline for standalone with errors', async () => { | ||
mockIORedisClient.exec.mockResolvedValueOnce(mockBatchCommandsResultWithErrors); | ||
expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual(mockSummaryWithErrors); | ||
}); | ||
it('should return all failed in case of global error', async () => { | ||
mockIORedisClient.exec.mockRejectedValueOnce(new Error()); | ||
expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual({ | ||
...mockSummary.getOverview(), | ||
succeed: 0, | ||
failed: mockSummary.getOverview().processed, | ||
}); | ||
}); | ||
it('should execute batch of commands without pipeline for cluster', async () => { | ||
mockIORedisCluster.call.mockRejectedValueOnce(new Error()); | ||
mockIORedisCluster.call.mockResolvedValue('OK'); | ||
expect(await service['executeBatch'](mockIORedisCluster, mockBatchCommands)).toEqual(mockSummaryWithErrors); | ||
}); | ||
}); | ||
|
||
describe('import', () => { | ||
let spy; | ||
|
||
beforeEach(() => { | ||
spy = jest.spyOn(service as any, 'executeBatch'); | ||
}); | ||
|
||
it('should import data', async () => { | ||
spy.mockResolvedValue(mockSummary); | ||
expect(await service.import(mockClientMetadata, mockUploadImportFileDto)).toEqual({ | ||
...mockImportResult, | ||
duration: jasmine.anything(), | ||
}); | ||
}); | ||
|
||
it('should import data (100K) from file in batches 10K each', async () => { | ||
spy.mockResolvedValue(Object.assign(new BulkActionSummary(), { | ||
processed: 10_000, | ||
succeed: 10_000, | ||
failed: 0, | ||
})); | ||
expect(await service.import(mockClientMetadata, { | ||
file: { | ||
...mockUploadImportFileDto.file, | ||
buffer: generateNCommandsBuffer(100_000), | ||
} as unknown as MemoryStoredFile, | ||
})).toEqual({ | ||
...mockImportResult, | ||
summary: { | ||
processed: 100_000, | ||
succeed: 100_000, | ||
failed: 0, | ||
errors: [], | ||
}, | ||
duration: jasmine.anything(), | ||
}); | ||
}); | ||
|
||
it('should not import any data due to parse error', async () => { | ||
spy.mockResolvedValue(Object.assign(new BulkActionSummary(), { | ||
processed: 0, | ||
succeed: 0, | ||
failed: 0, | ||
})); | ||
expect(await service.import(mockClientMetadata, { | ||
file: { | ||
...mockUploadImportFileDto.file, | ||
buffer: Buffer.from('{"incorrectdata"}\n{"incorrectdata"}'), | ||
} as unknown as MemoryStoredFile, | ||
})).toEqual({ | ||
...mockImportResult, | ||
summary: { | ||
processed: 2, | ||
succeed: 0, | ||
failed: 2, | ||
errors: [], | ||
}, | ||
duration: jasmine.anything(), | ||
}); | ||
}); | ||
|
||
it('should throw an error in case of global error', async () => { | ||
try { | ||
databaseConnectionService.createClient.mockRejectedValueOnce(new NotFoundException()); | ||
|
||
await service.import(mockClientMetadata, mockUploadImportFileDto); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(NotFoundException); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.