1
0
forked from mirror/misskey

Implement per user drive stats

This commit is contained in:
syuilo 2018-10-22 04:44:10 +09:00
parent 0085e1f3ab
commit 99aa588ae7
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
3 changed files with 104 additions and 2 deletions

View File

@ -17,7 +17,7 @@ import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
import delFile from './delete-file'; import delFile from './delete-file';
import config from '../../config'; import config from '../../config';
import { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail'; import { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail';
import { driveStats } from '../stats'; import { driveStats, perUserDriveStats } from '../stats';
const log = debug('misskey:drive:add-file'); const log = debug('misskey:drive:add-file');
@ -394,6 +394,7 @@ export default async function(
// 統計を更新 // 統計を更新
driveStats.update(driveFile, true); driveStats.update(driveFile, true);
perUserDriveStats.update(driveFile, true);
return driveFile; return driveFile;
} }

View File

@ -2,7 +2,7 @@ import * as Minio from 'minio';
import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file'; import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file';
import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail'; import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail';
import config from '../../config'; import config from '../../config';
import { driveStats } from '../stats'; import { driveStats, perUserDriveStats } from '../stats';
export default async function(file: IDriveFile, isExpired = false) { export default async function(file: IDriveFile, isExpired = false) {
if (file.metadata.storage == 'minio') { if (file.metadata.storage == 'minio') {
@ -49,4 +49,5 @@ export default async function(file: IDriveFile, isExpired = false) {
// 統計を更新 // 統計を更新
driveStats.update(file, false); driveStats.update(file, false);
perUserDriveStats.update(file, false);
} }

View File

@ -911,3 +911,103 @@ class PerUserNotesStats extends Stats<PerUserNotesLog> {
export const perUserNotesStats = new PerUserNotesStats(); export const perUserNotesStats = new PerUserNotesStats();
//#endregion //#endregion
//#region Per user drive stats
/**
*
*/
type PerUserDriveLog = {
/**
*
*/
totalCount: number;
/**
*
*/
totalSize: number;
/**
*
*/
incCount: number;
/**
* 使
*/
incSize: number;
/**
*
*/
decCount: number;
/**
* 使
*/
decSize: number;
};
class PerUserDriveStats extends Stats<PerUserDriveLog> {
constructor() {
super('perUserDrive', true);
}
@autobind
protected async getTemplate(init: boolean, latest?: PerUserDriveLog, group?: any): Promise<PerUserDriveLog> {
const calcSize = () => DriveFile
.aggregate([{
$match: {
'metadata.userId': group,
'metadata.deletedAt': { $exists: false }
}
}, {
$project: {
length: true
}
}, {
$group: {
_id: null,
usage: { $sum: '$length' }
}
}])
.then(res => res.length > 0 ? res[0].usage : 0);
const [count, size] = init ? await Promise.all([
DriveFile.count({ 'metadata.userId': group }),
calcSize()
]) : [
latest ? latest.totalCount : 0,
latest ? latest.totalSize : 0
];
return {
totalCount: count,
totalSize: size,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
};
}
@autobind
public async update(file: IDriveFile, isAdditional: boolean) {
const update: Obj = {};
update.totalCount = isAdditional ? 1 : -1;
update.totalSize = isAdditional ? file.length : -file.length;
if (isAdditional) {
update.incCount = 1;
update.incSize = file.length;
} else {
update.decCount = 1;
update.decSize = file.length;
}
await this.inc(update, file.metadata.userId);
}
}
export const perUserDriveStats = new PerUserDriveStats();
//#endregion