-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
337 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MapListVersion } from '@momentum/constants'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsInt } from 'class-validator'; | ||
|
||
export class MapListVersionDto implements MapListVersion { | ||
@ApiProperty({ description: 'Latest version of the main map list' }) | ||
@IsInt() | ||
approved: number; | ||
|
||
@ApiProperty({ description: 'Latest version of the submission map list' }) | ||
@IsInt() | ||
submissions: number; | ||
} |
81 changes: 81 additions & 0 deletions
81
apps/backend/src/app/modules/maps/map-list.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,81 @@ | ||
import { FlatMapList } from '@momentum/constants'; | ||
import { MapListService } from './map-list.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PRISMA_MOCK_PROVIDER } from '../../../../test/prisma-mock.const'; | ||
import { mockDeep } from 'jest-mock-extended'; | ||
import { FileStoreService } from '../filestore/file-store.service'; | ||
|
||
describe('MapListService', () => { | ||
describe('onModuleInit', () => { | ||
let service: MapListService; | ||
const fileStoreMock = { | ||
listFileKeys: jest.fn(() => Promise.resolve([])), | ||
deleteFiles: jest.fn() | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
MapListService, | ||
PRISMA_MOCK_PROVIDER, | ||
{ provide: FileStoreService, useValue: fileStoreMock } | ||
] | ||
}) | ||
.useMocker(mockDeep) | ||
.compile(); | ||
|
||
service = module.get(MapListService); | ||
}); | ||
|
||
it('should set version values based on files in storage', async () => { | ||
fileStoreMock.listFileKeys.mockResolvedValueOnce([ | ||
'maplist/approved/1.json.deflate' | ||
]); | ||
fileStoreMock.listFileKeys.mockResolvedValueOnce([ | ||
'maplist/submissions/15012024.json.deflate' | ||
]); | ||
|
||
await service.onModuleInit(); | ||
|
||
expect(service['version']).toMatchObject({ | ||
[FlatMapList.APPROVED]: 1, | ||
[FlatMapList.SUBMISSION]: 15012024 | ||
}); | ||
|
||
expect(fileStoreMock.deleteFiles).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should set version to 0 when no versions exist in storage', async () => { | ||
await service.onModuleInit(); | ||
|
||
expect(service['version']).toMatchObject({ | ||
[FlatMapList.APPROVED]: 0, | ||
[FlatMapList.SUBMISSION]: 0 | ||
}); | ||
|
||
expect(fileStoreMock.deleteFiles).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should pick most recent when multiple versions exist in storage, and wipe old versions', async () => { | ||
fileStoreMock.listFileKeys.mockResolvedValueOnce([ | ||
'maplist/approved/4.json.deflate', | ||
'maplist/approved/5.json.deflate', | ||
'maplist/approved/3.json.deflate', | ||
'maplist/approved/1.json.deflate' | ||
]); | ||
|
||
await service.onModuleInit(); | ||
|
||
expect(service['version']).toMatchObject({ | ||
[FlatMapList.APPROVED]: 5, | ||
[FlatMapList.SUBMISSION]: 0 | ||
}); | ||
|
||
expect(fileStoreMock.deleteFiles).toHaveBeenCalledWith([ | ||
'maplist/approved/4.json.deflate', | ||
'maplist/approved/3.json.deflate', | ||
'maplist/approved/1.json.deflate' | ||
]); | ||
}); | ||
}); | ||
}); |
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,100 @@ | ||
import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { FileStoreService } from '../filestore/file-store.service'; | ||
import { EXTENDED_PRISMA_SERVICE } from '../database/db.constants'; | ||
import { ExtendedPrismaService } from '../database/prisma.extension'; | ||
import { DtoFactory } from '../../dto'; | ||
import { MapListVersionDto } from '../../dto/map/map-list-version.dto'; | ||
import { | ||
CombinedMapStatuses, | ||
FlatMapList, | ||
mapListDir, | ||
mapListPath, | ||
MapStatusNew | ||
} from '@momentum/constants'; | ||
import * as zlib from 'node:zlib'; | ||
import { promisify } from 'node:util'; | ||
|
||
@Injectable() | ||
export class MapListService implements OnModuleInit { | ||
constructor( | ||
@Inject(EXTENDED_PRISMA_SERVICE) private readonly db: ExtendedPrismaService, | ||
private readonly fileStoreService: FileStoreService | ||
) {} | ||
|
||
private version: Record<FlatMapList, number> = { | ||
[FlatMapList.APPROVED]: 0, | ||
[FlatMapList.SUBMISSION]: 0 | ||
}; | ||
|
||
async onModuleInit(): Promise<void> { | ||
for (const type of [FlatMapList.APPROVED, FlatMapList.SUBMISSION]) { | ||
const keys = await this.fileStoreService.listFileKeys(mapListDir(type)); | ||
|
||
if (keys.length === 0) { | ||
this.version[type] = 0; | ||
} else if (keys.length === 1) { | ||
this.version[type] = this.extractVersionFromFileKey(keys[0]); | ||
} else { | ||
// If > 1 we have some old versions sitting around for some reason, | ||
// just delete. | ||
const sortedKeys = keys | ||
.map((k) => this.extractVersionFromFileKey(k)) | ||
.sort((a, b) => b - a); // Largest to smallest | ||
this.version[type] = sortedKeys[0]; | ||
await this.fileStoreService.deleteFiles( | ||
sortedKeys.slice(1).map((k) => mapListPath(type, k)) | ||
); | ||
} | ||
} | ||
} | ||
|
||
getMapList(): MapListVersionDto { | ||
return DtoFactory(MapListVersionDto, { | ||
approved: this.version[FlatMapList.APPROVED], | ||
submissions: this.version[FlatMapList.SUBMISSION] | ||
}); | ||
} | ||
|
||
async updateMapList(type: FlatMapList): Promise<void> { | ||
// Important: Seed script (seed.ts) copies this logic, if changing here, | ||
// change there as well. | ||
const maps = await this.db.mMap.findMany({ | ||
where: { | ||
status: | ||
type === FlatMapList.APPROVED | ||
? MapStatusNew.APPROVED | ||
: { in: CombinedMapStatuses.IN_SUBMISSION } | ||
}, | ||
select: { | ||
id: true, | ||
name: true, | ||
fileName: true, | ||
hash: true, | ||
status: true, | ||
createdAt: true, | ||
thumbnail: true, | ||
leaderboards: true, | ||
info: true | ||
} | ||
}); | ||
|
||
const mapListJson = JSON.stringify(maps); | ||
const compressed = await promisify(zlib.deflate)(mapListJson); | ||
|
||
const oldVersion = this.version[type]; | ||
const newVersion = this.updateMapListVersion(type); | ||
const oldKey = mapListPath(type, oldVersion); | ||
const newKey = mapListPath(type, newVersion); | ||
|
||
await this.fileStoreService.deleteFile(oldKey); | ||
await this.fileStoreService.storeFile(compressed, newKey); | ||
} | ||
|
||
private updateMapListVersion(type: FlatMapList): number { | ||
return ++this.version[type]; | ||
} | ||
|
||
private extractVersionFromFileKey(key: string): number { | ||
return Number(/(?<=\/)\d+(?=.json)/.exec(key)[0]); | ||
} | ||
} |
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
Oops, something went wrong.