This commit is contained in:
syuilo 2018-10-21 09:20:11 +09:00
parent aa50d0ee11
commit 969b6dbcad
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
14 changed files with 584 additions and 420 deletions

View file

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

View file

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

View file

@ -23,7 +23,7 @@ import registerHashtag from '../register-hashtag';
import isQuote from '../../misc/is-quote';
import { TextElementMention } from '../../mfm/parse/elements/mention';
import { TextElementHashtag } from '../../mfm/parse/elements/hashtag';
import { coreChart } from '../stats';
import { notesChart } from '../stats';
import { erase, unique } from '../../prelude/array';
import insertNoteUnread from './unread';
@ -165,7 +165,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise<
}
// 統計を更新
coreChart.updateNoteStats(note, true);
notesChart.update(note, true);
// ハッシュタグ登録
tags.map(tag => registerHashtag(user, tag));

View file

@ -6,7 +6,7 @@ import pack from '../../remote/activitypub/renderer';
import { deliver } from '../../queue';
import Following from '../../models/following';
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
import { coreChart } from '../stats';
import { notesChart } from '../stats';
import config from '../../config';
import NoteUnread from '../../models/note-unread';
import read from './read';
@ -63,5 +63,5 @@ export default async function(user: IUser, note: INote) {
//#endregion
// 統計を更新
coreChart.updateNoteStats(note, false);
notesChart.update(note, false);
}

View file

@ -16,6 +16,11 @@ type Span = 'day' | 'hour';
type ChartDocument<T extends Obj> = {
_id: mongo.ObjectID;
/**
*
*/
group?: any;
/**
*
*/
@ -40,6 +45,7 @@ abstract class Chart<T> {
constructor(dbCollectionName: string) {
this.collection = db.get<ChartDocument<T>>(dbCollectionName);
this.collection.createIndex({ span: -1, date: -1 }, { unique: true });
this.collection.createIndex('group');
}
protected async getCurrentStats(span: Span, group?: Obj): Promise<ChartDocument<T>> {
@ -55,10 +61,11 @@ abstract class Chart<T> {
null;
// 現在(今日または今のHour)の統計
const currentStats = await this.collection.findOne(Object.assign({}, {
const currentStats = await this.collection.findOne({
group: group,
span: span,
date: current
}, group));
});
if (currentStats) {
return currentStats;
@ -69,9 +76,10 @@ abstract class Chart<T> {
// * 昨日何もチャートを更新するような出来事がなかった場合は、
// * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
const mostRecentStats = await this.collection.findOne(Object.assign({}, {
const mostRecentStats = await this.collection.findOne({
group: group,
span: span
}, group), {
}, {
sort: {
date: -1
}
@ -81,11 +89,12 @@ abstract class Chart<T> {
// 現在の統計を初期挿入
const data = this.generateEmptyStats(mostRecentStats.data);
const stats = await this.collection.insert(Object.assign({}, {
const stats = await this.collection.insert({
group: group,
span: span,
date: current,
data: data
}, group));
});
return stats;
} else {
@ -95,18 +104,19 @@ abstract class Chart<T> {
// 空の統計を作成
const data = this.generateInitialStats();
const stats = await this.collection.insert(Object.assign({}, {
const stats = await this.collection.insert({
group: group,
span: span,
date: current,
data: data
}, group));
});
return stats;
}
}
}
protected update(inc: Partial<T>, group?: Obj): void {
protected inc(inc: Partial<T>, group?: Obj): void {
const query: Obj = {};
const dive = (path: string, x: Obj) => {
@ -151,12 +161,13 @@ abstract class Chart<T> {
span == 'day' ? new Date(y, m, d - range) :
span == 'hour' ? new Date(y, m, d, h - range) : null;
const stats = await this.collection.find(Object.assign({
const stats = await this.collection.find({
group: group,
span: span,
date: {
$gt: gt
}
}, group), {
}, {
sort: {
date: -1
},
@ -189,356 +200,82 @@ abstract class Chart<T> {
}
}
type CoreStats = {
/**
*
*/
users: {
local: {
/**
* ()
*/
total: number;
//#region Users stats
/**
*
*/
type UsersStats = {
local: {
/**
* ()
*/
total: number;
/**
* ()
*/
inc: number;
/**
* ()
*/
inc: number;
/**
* ()
*/
dec: number;
};
remote: {
/**
* ()
*/
total: number;
/**
* ()
*/
inc: number;
/**
* ()
*/
dec: number;
};
/**
* ()
*/
dec: number;
};
/**
* 稿
*/
notes: {
local: {
/**
* 稿 ()
*/
total: number;
/**
* 稿 ()
*/
inc: number;
/**
* 稿 ()
*/
dec: number;
diffs: {
/**
* 稿 ()
*/
normal: number;
/**
* 稿 ()
*/
reply: number;
/**
* Renoteの投稿数の差分 ()
*/
renote: number;
};
};
remote: {
/**
* 稿 ()
*/
total: number;
/**
* 稿 ()
*/
inc: number;
/**
* 稿 ()
*/
dec: number;
diffs: {
/**
* 稿 ()
*/
normal: number;
/**
* 稿 ()
*/
reply: number;
/**
* Renoteの投稿数の差分 ()
*/
renote: number;
};
};
};
/**
* ()
*/
drive: {
local: {
/**
* ()
*/
totalCount: number;
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
remote: {
/**
* ()
*/
totalCount: number;
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
};
/**
*
*/
network: {
remote: {
/**
*
* ()
*/
incomingRequests: number;
total: number;
/**
*
* ()
*/
outgoingRequests: number;
inc: number;
/**
*
* TIP: (totalTime / incomingRequests)
* ()
*/
totalTime: number;
/**
*
*/
incomingBytes: number;
/**
*
*/
outgoingBytes: number;
dec: number;
};
};
class CoreChart extends Chart<CoreStats> {
class UsersChart extends Chart<UsersStats> {
constructor() {
super('coreStats');
super('usersStats');
}
protected generateInitialStats(): CoreStats {
protected generateInitialStats(): UsersStats {
return {
users: {
local: {
total: 0,
inc: 0,
dec: 0
},
remote: {
total: 0,
inc: 0,
dec: 0
}
local: {
total: 0,
inc: 0,
dec: 0
},
notes: {
local: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
},
remote: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
}
},
drive: {
local: {
totalCount: 0,
totalSize: 0,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
},
remote: {
totalCount: 0,
totalSize: 0,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
}
},
network: {
incomingRequests: 0,
outgoingRequests: 0,
totalTime: 0,
incomingBytes: 0,
outgoingBytes: 0
remote: {
total: 0,
inc: 0,
dec: 0
}
};
}
protected generateEmptyStats(mostRecentStats: CoreStats): CoreStats {
protected generateEmptyStats(mostRecentStats: UsersStats): UsersStats {
return {
users: {
local: {
total: mostRecentStats.users.local.total,
inc: 0,
dec: 0
},
remote: {
total: mostRecentStats.users.remote.total,
inc: 0,
dec: 0
}
local: {
total: mostRecentStats.local.total,
inc: 0,
dec: 0
},
notes: {
local: {
total: mostRecentStats.notes.local.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
},
remote: {
total: mostRecentStats.notes.remote.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
}
},
drive: {
local: {
totalCount: mostRecentStats.drive.local.totalCount,
totalSize: mostRecentStats.drive.local.totalSize,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
},
remote: {
totalCount: mostRecentStats.drive.remote.totalCount,
totalSize: mostRecentStats.drive.remote.totalSize,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
}
},
network: {
incomingRequests: 0,
outgoingRequests: 0,
totalTime: 0,
incomingBytes: 0,
outgoingBytes: 0
remote: {
total: mostRecentStats.remote.total,
inc: 0,
dec: 0
}
};
}
public async updateUserStats(user: IUser, isAdditional: boolean) {
const origin = isLocalUser(user) ? 'local' : 'remote';
public async update(user: IUser, isAdditional: boolean) {
const update: Obj = {};
update.total = isAdditional ? 1 : -1;
@ -548,18 +285,145 @@ class CoreChart extends Chart<CoreStats> {
update.dec = 1;
}
const inc: Obj = {
users: {}
await this.inc({
[isLocalUser(user) ? 'local' : 'remote']: update
});
}
}
export const usersChart = new UsersChart();
//#endregion
//#region Notes stats
/**
* 稿
*/
type NotesStats = {
local: {
/**
* 稿 ()
*/
total: number;
/**
* 稿 ()
*/
inc: number;
/**
* 稿 ()
*/
dec: number;
diffs: {
/**
* 稿 ()
*/
normal: number;
/**
* 稿 ()
*/
reply: number;
/**
* Renoteの投稿数の差分 ()
*/
renote: number;
};
};
inc.users[origin] = update;
remote: {
/**
* 稿 ()
*/
total: number;
await this.update(inc);
/**
* 稿 ()
*/
inc: number;
/**
* 稿 ()
*/
dec: number;
diffs: {
/**
* 稿 ()
*/
normal: number;
/**
* 稿 ()
*/
reply: number;
/**
* Renoteの投稿数の差分 ()
*/
renote: number;
};
};
};
class NotesChart extends Chart<NotesStats> {
constructor() {
super('notesStats');
}
public async updateNoteStats(note: INote, isAdditional: boolean) {
const origin = isLocalUser(note._user) ? 'local' : 'remote';
protected generateInitialStats(): NotesStats {
return {
local: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
},
remote: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
}
};
}
protected generateEmptyStats(mostRecentStats: NotesStats): NotesStats {
return {
local: {
total: mostRecentStats.local.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
},
remote: {
total: mostRecentStats.remote.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
}
}
};
}
public async update(note: INote, isAdditional: boolean) {
const update: Obj = {};
update.total = isAdditional ? 1 : -1;
@ -578,18 +442,133 @@ class CoreChart extends Chart<CoreStats> {
update.diffs.normal = isAdditional ? 1 : -1;
}
const inc: Obj = {
notes: {}
};
await this.inc({
[isLocalUser(note._user) ? 'local' : 'remote']: update
});
}
}
inc.notes[origin] = update;
export const notesChart = new NotesChart();
//#endregion
await this.update(inc);
//#region Drive stats
/**
*
*/
type DriveStats = {
local: {
/**
* ()
*/
totalCount: number;
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
remote: {
/**
* ()
*/
totalCount: number;
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
};
class DriveChart extends Chart<DriveStats> {
constructor() {
super('driveStats');
}
public async updateDriveStats(file: IDriveFile, isAdditional: boolean) {
const origin = isLocalUser(file.metadata._user) ? 'local' : 'remote';
protected generateInitialStats(): DriveStats {
return {
local: {
totalCount: 0,
totalSize: 0,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
},
remote: {
totalCount: 0,
totalSize: 0,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
}
};
}
protected generateEmptyStats(mostRecentStats: DriveStats): DriveStats {
return {
local: {
totalCount: mostRecentStats.local.totalCount,
totalSize: mostRecentStats.local.totalSize,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
},
remote: {
totalCount: mostRecentStats.remote.totalCount,
totalSize: mostRecentStats.remote.totalSize,
incCount: 0,
incSize: 0,
decCount: 0,
decSize: 0
}
};
}
public async update(file: IDriveFile, isAdditional: boolean) {
const update: Obj = {};
update.totalCount = isAdditional ? 1 : -1;
@ -602,27 +581,83 @@ class CoreChart extends Chart<CoreStats> {
update.decSize = file.length;
}
const inc: Obj = {
drive: {}
};
inc.drive[origin] = update;
await this.update(inc);
}
public async updateNetworkStats(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) {
const inc: Partial<CoreStats> = {
network: {
incomingRequests: incomingRequests,
totalTime: time,
incomingBytes: incomingBytes,
outgoingBytes: outgoingBytes
}
};
await this.update(inc);
await this.inc({
[isLocalUser(file.metadata._user) ? 'local' : 'remote']: update
});
}
}
export const coreChart = new CoreChart();
export const driveChart = new DriveChart();
//#endregion
//#region Network stats
/**
*
*/
type NetworkStats = {
/**
*
*/
incomingRequests: number;
/**
*
*/
outgoingRequests: number;
/**
*
* TIP: (totalTime / incomingRequests)
*/
totalTime: number;
/**
*
*/
incomingBytes: number;
/**
*
*/
outgoingBytes: number;
};
class NetworkChart extends Chart<NetworkStats> {
constructor() {
super('networkStats');
}
protected generateInitialStats(): NetworkStats {
return {
incomingRequests: 0,
outgoingRequests: 0,
totalTime: 0,
incomingBytes: 0,
outgoingBytes: 0
};
}
protected generateEmptyStats(mostRecentStats: NetworkStats): NetworkStats {
return {
incomingRequests: 0,
outgoingRequests: 0,
totalTime: 0,
incomingBytes: 0,
outgoingBytes: 0
};
}
public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) {
const inc: Partial<NetworkStats> = {
incomingRequests: incomingRequests,
totalTime: time,
incomingBytes: incomingBytes,
outgoingBytes: outgoingBytes
};
await this.inc(inc);
}
}
export const networkChart = new NetworkChart();
//#endregion