2018-10-20 20:35:37 -05:00
|
|
|
/**
|
|
|
|
* このファイルでは、チャートに関する処理を行います。
|
|
|
|
*/
|
|
|
|
|
2018-10-20 20:05:15 -05:00
|
|
|
const nestedProperty = require('nested-property');
|
2018-10-20 22:37:00 -05:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-10-20 17:10:35 -05:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import db from '../db/mongodb';
|
2018-10-21 00:08:05 -05:00
|
|
|
import Note, { INote } from '../models/note';
|
|
|
|
import User, { isLocalUser, IUser } from '../models/user';
|
|
|
|
import DriveFile, { IDriveFile } from '../models/drive-file';
|
2018-10-20 17:10:35 -05:00
|
|
|
import { ICollection } from 'monk';
|
2018-10-21 03:51:35 -05:00
|
|
|
import Following from '../models/following';
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
type Obj = { [key: string]: any };
|
|
|
|
|
|
|
|
type Partial<T> = {
|
|
|
|
[P in keyof T]?: Partial<T[P]>;
|
|
|
|
};
|
|
|
|
|
2018-10-20 20:05:15 -05:00
|
|
|
type ArrayValue<T> = {
|
|
|
|
[P in keyof T]: T[P] extends number ? Array<T[P]> : ArrayValue<T[P]>;
|
|
|
|
};
|
|
|
|
|
2018-10-20 17:10:35 -05:00
|
|
|
type Span = 'day' | 'hour';
|
|
|
|
|
2018-10-20 20:05:15 -05:00
|
|
|
//#region Chart Core
|
2018-10-21 01:08:07 -05:00
|
|
|
type Log<T extends Obj> = {
|
2018-10-20 17:10:35 -05:00
|
|
|
_id: mongo.ObjectID;
|
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
|
|
|
* 集計のグループ
|
|
|
|
*/
|
|
|
|
group?: any;
|
|
|
|
|
2018-10-20 17:10:35 -05:00
|
|
|
/**
|
|
|
|
* 集計日時
|
|
|
|
*/
|
|
|
|
date: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計期間
|
|
|
|
*/
|
|
|
|
span: Span;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* データ
|
|
|
|
*/
|
|
|
|
data: T;
|
2018-10-20 22:37:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ユニークインクリメント用
|
|
|
|
*/
|
|
|
|
unique?: Obj;
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
|
|
|
|
2018-10-20 20:35:37 -05:00
|
|
|
/**
|
|
|
|
* 様々なチャートの管理を司るクラス
|
|
|
|
*/
|
2018-10-21 01:08:07 -05:00
|
|
|
abstract class Stats<T> {
|
|
|
|
protected collection: ICollection<Log<T>>;
|
2018-10-21 03:51:35 -05:00
|
|
|
protected abstract async getTemplate(init: boolean, latestLog?: T, group?: any): Promise<T>;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 00:08:05 -05:00
|
|
|
constructor(name: string) {
|
2018-10-21 01:08:07 -05:00
|
|
|
this.collection = db.get<Log<T>>(`stats.${name}`);
|
2018-10-20 17:10:35 -05:00
|
|
|
this.collection.createIndex({ span: -1, date: -1 }, { unique: true });
|
2018-10-20 19:20:11 -05:00
|
|
|
this.collection.createIndex('group');
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
|
|
|
private convertQuery(x: Obj, path: string): Obj {
|
|
|
|
const query: Obj = {};
|
|
|
|
|
|
|
|
const dive = (x: Obj, path: string) => {
|
|
|
|
Object.entries(x).forEach(([k, v]) => {
|
|
|
|
const p = path ? `${path}.${k}` : k;
|
|
|
|
if (typeof v === 'number') {
|
|
|
|
query[p] = v;
|
|
|
|
} else {
|
|
|
|
dive(v, p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
dive(x, path);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2018-10-21 02:54:07 -05:00
|
|
|
private async getCurrentLog(span: Span, group?: any): Promise<Log<T>> {
|
2018-10-20 17:10:35 -05:00
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth();
|
|
|
|
const d = now.getDate();
|
|
|
|
const h = now.getHours();
|
|
|
|
|
|
|
|
const current =
|
|
|
|
span == 'day' ? new Date(y, m, d) :
|
|
|
|
span == 'hour' ? new Date(y, m, d, h) :
|
|
|
|
null;
|
|
|
|
|
|
|
|
// 現在(今日または今のHour)の統計
|
2018-10-21 01:08:07 -05:00
|
|
|
const currentLog = await this.collection.findOne({
|
2018-10-20 19:20:11 -05:00
|
|
|
group: group,
|
2018-10-20 17:10:35 -05:00
|
|
|
span: span,
|
|
|
|
date: current
|
2018-10-20 19:20:11 -05:00
|
|
|
});
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
if (currentLog) {
|
|
|
|
return currentLog;
|
2018-10-21 00:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 集計期間が変わってから、初めてのチャート更新なら
|
|
|
|
// 最も最近の統計を持ってくる
|
|
|
|
// * 例えば集計期間が「日」である場合で考えると、
|
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
|
|
|
// * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
2018-10-21 01:08:07 -05:00
|
|
|
const latestLog = await this.collection.findOne({
|
2018-10-21 00:44:37 -05:00
|
|
|
group: group,
|
|
|
|
span: span
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
if (latestLog) {
|
2018-10-21 00:44:37 -05:00
|
|
|
// 現在の統計を初期挿入
|
2018-10-21 03:51:35 -05:00
|
|
|
const data = await this.getTemplate(false, latestLog.data);
|
2018-10-21 00:44:37 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
const log = await this.collection.insert({
|
2018-10-21 00:44:37 -05:00
|
|
|
group: group,
|
|
|
|
span: span,
|
|
|
|
date: current,
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
return log;
|
2018-10-20 17:10:35 -05:00
|
|
|
} else {
|
2018-10-21 00:44:37 -05:00
|
|
|
// 統計が存在しなかったら
|
|
|
|
// * Misskeyインスタンスを建てて初めてのチャート更新時など
|
|
|
|
|
|
|
|
// 空の統計を作成
|
2018-10-21 03:51:35 -05:00
|
|
|
const data = await this.getTemplate(true, null, group);
|
2018-10-21 00:44:37 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
const log = await this.collection.insert({
|
2018-10-20 19:20:11 -05:00
|
|
|
group: group,
|
2018-10-21 00:44:37 -05:00
|
|
|
span: span,
|
|
|
|
date: current,
|
|
|
|
data: data
|
2018-10-20 17:10:35 -05:00
|
|
|
});
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
return log;
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 02:54:07 -05:00
|
|
|
protected commit(query: Obj, group?: any, uniqueKey?: string, uniqueValue?: string): void {
|
2018-10-21 01:08:07 -05:00
|
|
|
const update = (log: Log<T>) => {
|
2018-10-20 22:37:00 -05:00
|
|
|
// ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く
|
2018-10-21 00:47:44 -05:00
|
|
|
if (
|
|
|
|
uniqueKey &&
|
2018-10-21 01:08:07 -05:00
|
|
|
log.unique &&
|
|
|
|
log.unique[uniqueKey] &&
|
|
|
|
log.unique[uniqueKey].includes(uniqueValue)
|
2018-10-21 00:47:44 -05:00
|
|
|
) return;
|
|
|
|
|
|
|
|
// ユニークインクリメントの指定のキーに値を追加
|
2018-10-20 22:37:00 -05:00
|
|
|
if (uniqueKey) {
|
|
|
|
query['$push'] = {
|
|
|
|
[`unique.${uniqueKey}`]: uniqueValue
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collection.update({
|
2018-10-21 01:08:07 -05:00
|
|
|
_id: log._id
|
2018-10-20 22:37:00 -05:00
|
|
|
}, query);
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
this.getCurrentLog('day', group).then(log => update(log));
|
|
|
|
this.getCurrentLog('hour', group).then(log => update(log));
|
2018-10-20 22:37:00 -05:00
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 02:54:07 -05:00
|
|
|
protected inc(inc: Partial<T>, group?: any): void {
|
2018-10-20 22:37:00 -05:00
|
|
|
this.commit({
|
|
|
|
$inc: this.convertQuery(inc, 'data')
|
|
|
|
}, group);
|
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 02:54:07 -05:00
|
|
|
protected incIfUnique(inc: Partial<T>, key: string, value: string, group?: any): void {
|
2018-10-20 22:37:00 -05:00
|
|
|
this.commit({
|
|
|
|
$inc: this.convertQuery(inc, 'data')
|
|
|
|
}, group, key, value);
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 02:54:07 -05:00
|
|
|
public async getChart(span: Span, range: number, group?: any): Promise<ArrayValue<T>> {
|
2018-10-21 00:08:05 -05:00
|
|
|
const promisedChart: Promise<T>[] = [];
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth();
|
|
|
|
const d = now.getDate();
|
|
|
|
const h = now.getHours();
|
|
|
|
|
|
|
|
const gt =
|
|
|
|
span == 'day' ? new Date(y, m, d - range) :
|
|
|
|
span == 'hour' ? new Date(y, m, d, h - range) : null;
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
const logs = await this.collection.find({
|
2018-10-20 19:20:11 -05:00
|
|
|
group: group,
|
2018-10-20 17:10:35 -05:00
|
|
|
span: span,
|
|
|
|
date: {
|
|
|
|
$gt: gt
|
|
|
|
}
|
2018-10-20 19:20:11 -05:00
|
|
|
}, {
|
2018-10-20 17:10:35 -05:00
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let i = (range - 1); i >= 0; i--) {
|
|
|
|
const current =
|
|
|
|
span == 'day' ? new Date(y, m, d - i) :
|
|
|
|
span == 'hour' ? new Date(y, m, d, h - i) :
|
|
|
|
null;
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
const log = logs.find(l => l.date.getTime() == current.getTime());
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
if (log) {
|
|
|
|
promisedChart.unshift(Promise.resolve(log.data));
|
2018-10-20 17:10:35 -05:00
|
|
|
} else { // 隙間埋め
|
2018-10-21 01:08:07 -05:00
|
|
|
const latest = logs.find(l => l.date.getTime() < current.getTime());
|
2018-10-21 03:51:35 -05:00
|
|
|
promisedChart.unshift(this.getTemplate(false, latest ? latest.data : null));
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 00:08:05 -05:00
|
|
|
const chart = await Promise.all(promisedChart);
|
|
|
|
|
2018-10-20 20:05:15 -05:00
|
|
|
const res: ArrayValue<T> = {} as any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [{
|
2018-10-21 00:47:44 -05:00
|
|
|
* xxxxx: 1,
|
|
|
|
* yyyyy: 5
|
2018-10-20 20:05:15 -05:00
|
|
|
* }, {
|
2018-10-21 00:47:44 -05:00
|
|
|
* xxxxx: 2,
|
|
|
|
* yyyyy: 6
|
2018-10-20 20:05:15 -05:00
|
|
|
* }, {
|
2018-10-21 00:47:44 -05:00
|
|
|
* xxxxx: 3,
|
|
|
|
* yyyyy: 7
|
2018-10-20 20:05:15 -05:00
|
|
|
* }]
|
|
|
|
*
|
|
|
|
* を
|
|
|
|
*
|
|
|
|
* {
|
2018-10-21 00:47:44 -05:00
|
|
|
* xxxxx: [1, 2, 3],
|
|
|
|
* yyyyy: [5, 6, 7]
|
2018-10-20 20:05:15 -05:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* にする
|
|
|
|
*/
|
|
|
|
const dive = (x: Obj, path?: string) => {
|
|
|
|
Object.entries(x).forEach(([k, v]) => {
|
2018-10-20 20:24:56 -05:00
|
|
|
const p = path ? `${path}.${k}` : k;
|
2018-10-20 20:05:15 -05:00
|
|
|
if (typeof v == 'object') {
|
|
|
|
dive(v, p);
|
|
|
|
} else {
|
|
|
|
nestedProperty.set(res, p, chart.map(s => nestedProperty.get(s, p)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
dive(chart[0]);
|
|
|
|
|
|
|
|
return res;
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-20 20:05:15 -05:00
|
|
|
//#endregion
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
//#region Users stats
|
|
|
|
/**
|
|
|
|
* ユーザーに関する統計
|
|
|
|
*/
|
2018-10-21 01:08:07 -05:00
|
|
|
type UsersLog = {
|
2018-10-20 19:20:11 -05:00
|
|
|
local: {
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 集計期間時点での、全ユーザー数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
total: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 増加したユーザー数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
inc: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 減少したユーザー数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
dec: number;
|
|
|
|
};
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 03:28:27 -05:00
|
|
|
remote: UsersLog['local'];
|
2018-10-20 19:20:11 -05:00
|
|
|
};
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
class UsersStats extends Stats<UsersLog> {
|
2018-10-20 19:20:11 -05:00
|
|
|
constructor() {
|
2018-10-21 00:08:05 -05:00
|
|
|
super('users');
|
2018-10-20 19:20:11 -05:00
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 03:51:35 -05:00
|
|
|
protected async getTemplate(init: boolean, latestLog?: UsersLog): Promise<UsersLog> {
|
2018-10-21 01:08:07 -05:00
|
|
|
const [localCount, remoteCount] = init ? await Promise.all([
|
2018-10-21 00:08:05 -05:00
|
|
|
User.count({ host: null }),
|
|
|
|
User.count({ host: { $ne: null } })
|
|
|
|
]) : [
|
2018-10-21 01:08:07 -05:00
|
|
|
latestLog ? latestLog.local.total : 0,
|
|
|
|
latestLog ? latestLog.remote.total : 0
|
2018-10-21 00:08:05 -05:00
|
|
|
];
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
return {
|
|
|
|
local: {
|
2018-10-21 00:08:05 -05:00
|
|
|
total: localCount,
|
2018-10-20 19:20:11 -05:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-21 00:08:05 -05:00
|
|
|
total: remoteCount,
|
2018-10-20 19:20:11 -05:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
2018-10-20 19:20:11 -05:00
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-20 19:20:11 -05:00
|
|
|
public async update(user: IUser, isAdditional: boolean) {
|
|
|
|
const update: Obj = {};
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
update.total = isAdditional ? 1 : -1;
|
|
|
|
if (isAdditional) {
|
|
|
|
update.inc = 1;
|
|
|
|
} else {
|
|
|
|
update.dec = 1;
|
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
await this.inc({
|
|
|
|
[isLocalUser(user) ? 'local' : 'remote']: update
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
export const usersStats = new UsersStats();
|
2018-10-20 19:20:11 -05:00
|
|
|
//#endregion
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
//#region Notes stats
|
|
|
|
/**
|
|
|
|
* 投稿に関する統計
|
|
|
|
*/
|
2018-10-21 01:08:07 -05:00
|
|
|
type NotesLog = {
|
2018-10-20 19:20:11 -05:00
|
|
|
local: {
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 集計期間時点での、全投稿数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
total: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 増加した投稿数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
inc: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 減少した投稿数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
dec: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
diffs: {
|
2018-10-20 17:10:35 -05:00
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 通常の投稿数の差分
|
2018-10-20 17:10:35 -05:00
|
|
|
*/
|
2018-10-20 19:20:11 -05:00
|
|
|
normal: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* リプライの投稿数の差分
|
2018-10-20 17:10:35 -05:00
|
|
|
*/
|
2018-10-20 19:20:11 -05:00
|
|
|
reply: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* Renoteの投稿数の差分
|
2018-10-20 17:10:35 -05:00
|
|
|
*/
|
2018-10-20 19:20:11 -05:00
|
|
|
renote: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-10-21 03:28:27 -05:00
|
|
|
remote: NotesLog['local'];
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
class NotesStats extends Stats<NotesLog> {
|
2018-10-20 17:10:35 -05:00
|
|
|
constructor() {
|
2018-10-21 00:08:05 -05:00
|
|
|
super('notes');
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 03:51:35 -05:00
|
|
|
protected async getTemplate(init: boolean, latestLog?: NotesLog): Promise<NotesLog> {
|
2018-10-21 01:08:07 -05:00
|
|
|
const [localCount, remoteCount] = init ? await Promise.all([
|
2018-10-21 00:08:05 -05:00
|
|
|
Note.count({ '_user.host': null }),
|
|
|
|
Note.count({ '_user.host': { $ne: null } })
|
|
|
|
]) : [
|
2018-10-21 01:08:07 -05:00
|
|
|
latestLog ? latestLog.local.total : 0,
|
|
|
|
latestLog ? latestLog.remote.total : 0
|
2018-10-21 00:08:05 -05:00
|
|
|
];
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
return {
|
2018-10-20 19:20:11 -05:00
|
|
|
local: {
|
2018-10-21 00:08:05 -05:00
|
|
|
total: localCount,
|
2018-10-20 19:20:11 -05:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
},
|
2018-10-20 19:20:11 -05:00
|
|
|
remote: {
|
2018-10-21 00:08:05 -05:00
|
|
|
total: remoteCount,
|
2018-10-20 19:20:11 -05:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-20 19:20:11 -05:00
|
|
|
public async update(note: INote, isAdditional: boolean) {
|
2018-10-20 22:37:00 -05:00
|
|
|
const update: Obj = {
|
|
|
|
diffs: {}
|
|
|
|
};
|
2018-10-20 17:10:35 -05:00
|
|
|
|
|
|
|
update.total = isAdditional ? 1 : -1;
|
|
|
|
|
|
|
|
if (isAdditional) {
|
|
|
|
update.inc = 1;
|
|
|
|
} else {
|
|
|
|
update.dec = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.replyId != null) {
|
|
|
|
update.diffs.reply = isAdditional ? 1 : -1;
|
|
|
|
} else if (note.renoteId != null) {
|
|
|
|
update.diffs.renote = isAdditional ? 1 : -1;
|
|
|
|
} else {
|
|
|
|
update.diffs.normal = isAdditional ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
await this.inc({
|
|
|
|
[isLocalUser(note._user) ? 'local' : 'remote']: update
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
export const notesStats = new NotesStats();
|
2018-10-20 19:20:11 -05:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Drive stats
|
|
|
|
/**
|
|
|
|
* ドライブに関する統計
|
|
|
|
*/
|
2018-10-21 01:08:07 -05:00
|
|
|
type DriveLog = {
|
2018-10-20 19:20:11 -05:00
|
|
|
local: {
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 集計期間時点での、全ドライブファイル数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
totalCount: number;
|
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 集計期間時点での、全ドライブファイルの合計サイズ
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
totalSize: number;
|
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 増加したドライブファイル数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
incCount: number;
|
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 増加したドライブ使用量
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
incSize: number;
|
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 減少したドライブファイル数
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
decCount: number;
|
|
|
|
|
|
|
|
/**
|
2018-10-21 03:28:27 -05:00
|
|
|
* 減少したドライブ使用量
|
2018-10-20 19:20:11 -05:00
|
|
|
*/
|
|
|
|
decSize: number;
|
|
|
|
};
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-21 03:28:27 -05:00
|
|
|
remote: DriveLog['local'];
|
2018-10-20 19:20:11 -05:00
|
|
|
};
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
class DriveStats extends Stats<DriveLog> {
|
2018-10-20 19:20:11 -05:00
|
|
|
constructor() {
|
2018-10-21 00:08:05 -05:00
|
|
|
super('drive');
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 03:51:35 -05:00
|
|
|
protected async getTemplate(init: boolean, latestLog?: DriveLog): Promise<DriveLog> {
|
2018-10-21 00:08:05 -05:00
|
|
|
const calcSize = (local: boolean) => DriveFile
|
|
|
|
.aggregate([{
|
|
|
|
$match: {
|
|
|
|
'metadata._user.host': local ? null : { $ne: null },
|
|
|
|
'metadata.deletedAt': { $exists: false }
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
$project: {
|
|
|
|
length: true
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
$group: {
|
|
|
|
_id: null,
|
|
|
|
usage: { $sum: '$length' }
|
|
|
|
}
|
|
|
|
}])
|
|
|
|
.then(res => res.length > 0 ? res[0].usage : 0);
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
const [localCount, remoteCount, localSize, remoteSize] = init ? await Promise.all([
|
2018-10-21 00:08:05 -05:00
|
|
|
DriveFile.count({ 'metadata._user.host': null }),
|
|
|
|
DriveFile.count({ 'metadata._user.host': { $ne: null } }),
|
|
|
|
calcSize(true),
|
|
|
|
calcSize(false)
|
|
|
|
]) : [
|
2018-10-21 01:08:07 -05:00
|
|
|
latestLog ? latestLog.local.totalCount : 0,
|
|
|
|
latestLog ? latestLog.remote.totalCount : 0,
|
|
|
|
latestLog ? latestLog.local.totalSize : 0,
|
|
|
|
latestLog ? latestLog.remote.totalSize : 0
|
2018-10-21 00:08:05 -05:00
|
|
|
];
|
2018-10-20 19:20:11 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
local: {
|
2018-10-21 00:08:05 -05:00
|
|
|
totalCount: localCount,
|
|
|
|
totalSize: localSize,
|
2018-10-20 19:20:11 -05:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-21 00:08:05 -05:00
|
|
|
totalCount: remoteCount,
|
|
|
|
totalSize: remoteSize,
|
2018-10-20 19:20:11 -05:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-20 19:20:11 -05:00
|
|
|
public async update(file: IDriveFile, isAdditional: boolean) {
|
2018-10-20 17:10:35 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
await this.inc({
|
|
|
|
[isLocalUser(file.metadata._user) ? 'local' : 'remote']: update
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
export const driveStats = new DriveStats();
|
2018-10-20 19:20:11 -05:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Network stats
|
|
|
|
/**
|
|
|
|
* ネットワークに関する統計
|
|
|
|
*/
|
2018-10-21 01:08:07 -05:00
|
|
|
type NetworkLog = {
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
|
|
|
* 受信したリクエスト数
|
|
|
|
*/
|
|
|
|
incomingRequests: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 送信したリクエスト数
|
|
|
|
*/
|
|
|
|
outgoingRequests: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
|
|
|
* 応答時間の合計
|
|
|
|
* TIP: (totalTime / incomingRequests) でひとつのリクエストに平均でどれくらいの時間がかかったか知れる
|
|
|
|
*/
|
|
|
|
totalTime: number;
|
2018-10-20 17:10:35 -05:00
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
/**
|
|
|
|
* 合計受信データ量
|
|
|
|
*/
|
|
|
|
incomingBytes: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 合計送信データ量
|
|
|
|
*/
|
|
|
|
outgoingBytes: number;
|
|
|
|
};
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
class NetworkStats extends Stats<NetworkLog> {
|
2018-10-20 19:20:11 -05:00
|
|
|
constructor() {
|
2018-10-21 00:08:05 -05:00
|
|
|
super('network');
|
2018-10-20 19:20:11 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-21 03:51:35 -05:00
|
|
|
protected async getTemplate(init: boolean, latestLog?: NetworkLog): Promise<NetworkLog> {
|
2018-10-20 19:20:11 -05:00
|
|
|
return {
|
|
|
|
incomingRequests: 0,
|
|
|
|
outgoingRequests: 0,
|
|
|
|
totalTime: 0,
|
|
|
|
incomingBytes: 0,
|
|
|
|
outgoingBytes: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:37:00 -05:00
|
|
|
@autobind
|
2018-10-20 19:20:11 -05:00
|
|
|
public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) {
|
2018-10-21 01:08:07 -05:00
|
|
|
const inc: Partial<NetworkLog> = {
|
2018-10-20 19:20:11 -05:00
|
|
|
incomingRequests: incomingRequests,
|
|
|
|
totalTime: time,
|
|
|
|
incomingBytes: incomingBytes,
|
|
|
|
outgoingBytes: outgoingBytes
|
2018-10-20 17:10:35 -05:00
|
|
|
};
|
|
|
|
|
2018-10-20 19:20:11 -05:00
|
|
|
await this.inc(inc);
|
2018-10-20 17:10:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 01:08:07 -05:00
|
|
|
export const networkStats = new NetworkStats();
|
2018-10-20 19:20:11 -05:00
|
|
|
//#endregion
|
2018-10-21 02:54:07 -05:00
|
|
|
|
|
|
|
//#region Hashtag stats
|
|
|
|
/**
|
|
|
|
* ハッシュタグに関する統計
|
|
|
|
*/
|
|
|
|
type HashtagLog = {
|
|
|
|
/**
|
|
|
|
* 投稿された数
|
|
|
|
*/
|
|
|
|
count: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashtagStats extends Stats<HashtagLog> {
|
|
|
|
constructor() {
|
|
|
|
super('hashtag');
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2018-10-21 03:51:35 -05:00
|
|
|
protected async getTemplate(init: boolean, latestLog?: HashtagLog): Promise<HashtagLog> {
|
2018-10-21 02:54:07 -05:00
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async update(hashtag: string, userId: mongo.ObjectId) {
|
|
|
|
const inc: Partial<HashtagLog> = {
|
|
|
|
count: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
await this.incIfUnique(inc, 'users', userId.toHexString(), hashtag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const hashtagStats = new HashtagStats();
|
|
|
|
//#endregion
|
2018-10-21 03:51:35 -05:00
|
|
|
|
|
|
|
//#region Following stats
|
|
|
|
/**
|
|
|
|
* ユーザーごとのフォローに関する統計
|
|
|
|
*/
|
|
|
|
type FollowingLog = {
|
|
|
|
local: {
|
|
|
|
/**
|
|
|
|
* フォローしている
|
|
|
|
*/
|
|
|
|
followings: {
|
|
|
|
/**
|
|
|
|
* 合計
|
|
|
|
*/
|
|
|
|
total: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* フォローした数
|
|
|
|
*/
|
|
|
|
inc: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* フォロー解除した数
|
|
|
|
*/
|
|
|
|
dec: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* フォローされている
|
|
|
|
*/
|
|
|
|
followers: {
|
|
|
|
/**
|
|
|
|
* 合計
|
|
|
|
*/
|
|
|
|
total: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* フォローされた数
|
|
|
|
*/
|
|
|
|
inc: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* フォロー解除された数
|
|
|
|
*/
|
|
|
|
dec: number;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
remote: FollowingLog['local'];
|
|
|
|
};
|
|
|
|
|
|
|
|
class FollowingStats extends Stats<FollowingLog> {
|
|
|
|
constructor() {
|
|
|
|
super('following');
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
protected async getTemplate(init: boolean, latestLog?: FollowingLog, group?: any): Promise<FollowingLog> {
|
|
|
|
const [localFollowings, localFollowers, remoteFollowings, remoteFollowers] = init ? await Promise.all([
|
|
|
|
Following.count({ followerId: group, '_followee.host': null }),
|
|
|
|
Following.count({ followeeId: group, '_user.host': null }),
|
|
|
|
Following.count({ followerId: group, '_followee.host': { $ne: null } }),
|
|
|
|
Following.count({ followeeId: group, '_user.host': { $ne: null } })
|
|
|
|
]) : [
|
|
|
|
latestLog ? latestLog.local.followings.total : 0,
|
|
|
|
latestLog ? latestLog.local.followers.total : 0,
|
|
|
|
latestLog ? latestLog.remote.followings.total : 0,
|
|
|
|
latestLog ? latestLog.remote.followers.total : 0
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
local: {
|
|
|
|
followings: {
|
|
|
|
total: localFollowings,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
},
|
|
|
|
followers: {
|
|
|
|
total: localFollowers,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
followings: {
|
|
|
|
total: remoteFollowings,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
},
|
|
|
|
followers: {
|
|
|
|
total: remoteFollowers,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async update(follower: IUser, followee: IUser, isFollow: boolean) {
|
|
|
|
const update: Obj = {};
|
|
|
|
|
|
|
|
update.total = isFollow ? 1 : -1;
|
|
|
|
|
|
|
|
if (isFollow) {
|
|
|
|
update.inc = 1;
|
|
|
|
} else {
|
|
|
|
update.dec = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.inc({
|
|
|
|
[isLocalUser(follower) ? 'local' : 'remote']: { followings: update }
|
|
|
|
});
|
|
|
|
this.inc({
|
|
|
|
[isLocalUser(followee) ? 'local' : 'remote']: { followers: update }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const followingStats = new FollowingStats();
|
2018-10-21 03:28:27 -05:00
|
|
|
//#endregion
|