-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: transcode in stats #14418
base: main
Are you sure you want to change the base?
feat: transcode in stats #14418
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as fs from 'node:fs'; | ||
import { SystemMetadataKey } from 'src/enum'; | ||
import { IStorageRepository } from 'src/interfaces/storage.interface'; | ||
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; | ||
|
@@ -177,8 +178,16 @@ describe(ServerService.name, () => { | |
}); | ||
}); | ||
|
||
vi.mock('fs', async () => { | ||
const actual = (await vi.importActual<typeof fs>('fs'))!; | ||
return { | ||
...actual, | ||
statSync: vi.fn().mockReturnValue({ size: 500 }), | ||
}; | ||
}); | ||
Comment on lines
+181
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now how you should be doing that. You should be mocking repo methods if you have to |
||
|
||
describe('getStats', () => { | ||
it('should total up usage by user', async () => { | ||
it('should total up usage by user and server', async () => { | ||
userMock.getUserStats.mockResolvedValue([ | ||
{ | ||
userId: 'user1', | ||
|
@@ -189,6 +198,7 @@ describe(ServerService.name, () => { | |
usagePhotos: 1, | ||
usageVideos: 11_345, | ||
quotaSizeInBytes: 0, | ||
encodedVideoPaths: ['upload/encoded-video/video1.mp4'], | ||
}, | ||
{ | ||
userId: 'user2', | ||
|
@@ -199,6 +209,7 @@ describe(ServerService.name, () => { | |
usagePhotos: 100, | ||
usageVideos: 23_456, | ||
quotaSizeInBytes: 0, | ||
encodedVideoPaths: ['upload/encoded-video/video2.mp4', 'upload/encoded-video/video3.mp4'], | ||
}, | ||
{ | ||
userId: 'user3', | ||
|
@@ -209,15 +220,17 @@ describe(ServerService.name, () => { | |
usagePhotos: 900, | ||
usageVideos: 87_654, | ||
quotaSizeInBytes: 0, | ||
encodedVideoPaths: [''], | ||
}, | ||
]); | ||
|
||
await expect(sut.getStatistics()).resolves.toEqual({ | ||
photos: 120, | ||
videos: 31, | ||
usage: 1_123_455, | ||
usage: 1_124_955, | ||
usagePhotos: 1001, | ||
usageVideos: 122_455, | ||
usageTranscode: 1500, | ||
usageByUser: [ | ||
{ | ||
photos: 10, | ||
|
@@ -228,6 +241,7 @@ describe(ServerService.name, () => { | |
userName: '1 User', | ||
userId: 'user1', | ||
videos: 11, | ||
usageTranscode: 500, | ||
}, | ||
{ | ||
photos: 10, | ||
|
@@ -238,6 +252,7 @@ describe(ServerService.name, () => { | |
userName: '2 User', | ||
userId: 'user2', | ||
videos: 20, | ||
usageTranscode: 1000, | ||
}, | ||
{ | ||
photos: 100, | ||
|
@@ -248,6 +263,7 @@ describe(ServerService.name, () => { | |
userName: '3 User', | ||
userId: 'user3', | ||
videos: 0, | ||
usageTranscode: 0, | ||
}, | ||
], | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; | ||
import * as fs from 'node:fs'; | ||
import { serverVersion } from 'src/constants'; | ||
import { StorageCore } from 'src/cores/storage.core'; | ||
import { OnEvent } from 'src/decorators'; | ||
|
@@ -131,11 +132,26 @@ export class ServerService extends BaseService { | |
usage.usageVideos = user.usageVideos; | ||
usage.quotaSizeInBytes = user.quotaSizeInBytes; | ||
|
||
for (const path of user.encodedVideoPaths) { | ||
if (path.trim() === '') { | ||
continue; | ||
} | ||
|
||
try { | ||
const stats = fs.statSync(path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure we already have a repo method for that. If we don't, we should add one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is terribly slow and will scale poorly as the number of videos increases, both in fetching every single path from the DB at once and in |
||
usage.usageTranscode += stats.size; | ||
} catch (error) { | ||
console.error(`Error accessing file at path "${path}":`, (error as Error).message); | ||
} | ||
} | ||
|
||
serverStats.photos += usage.photos; | ||
serverStats.videos += usage.videos; | ||
serverStats.usage += usage.usage; | ||
serverStats.usage += usage.usageTranscode; // Necessary because transcoded videos don't count toward quota | ||
serverStats.usagePhotos += usage.usagePhotos; | ||
serverStats.usageVideos += usage.usageVideos; | ||
serverStats.usageTranscode += usage.usageTranscode; | ||
|
||
serverStats.usageByUser.push(usage); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is gonna be veeeery slow