rudeshark.net/src/services/stats.ts

707 lines
13 KiB
TypeScript
Raw Normal View History

2018-10-21 03:05:15 +02:00
const nestedProperty = require('nested-property');
2018-10-21 00:10:35 +02:00
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import { INote } from '../models/note';
import { isLocalUser, IUser } from '../models/user';
import { IDriveFile } from '../models/drive-file';
import { ICollection } from 'monk';
type Obj = { [key: string]: any };
type Partial<T> = {
[P in keyof T]?: Partial<T[P]>;
};
2018-10-21 03:05:15 +02:00
type ArrayValue<T> = {
[P in keyof T]: T[P] extends number ? Array<T[P]> : ArrayValue<T[P]>;
};
2018-10-21 00:10:35 +02:00
type Span = 'day' | 'hour';
2018-10-21 03:05:15 +02:00
//#region Chart Core
2018-10-21 00:10:35 +02:00
type ChartDocument<T extends Obj> = {
_id: mongo.ObjectID;
2018-10-21 02:20:11 +02:00
/**
*
*/
group?: any;
2018-10-21 00:10:35 +02:00
/**
*
*/
date: Date;
/**
*
*/
span: Span;
/**
*
*/
data: T;
};
abstract class Chart<T> {
protected collection: ICollection<ChartDocument<T>>;
protected abstract generateInitialStats(): T;
protected abstract generateEmptyStats(mostRecentStats: T): T;
constructor(dbCollectionName: string) {
this.collection = db.get<ChartDocument<T>>(dbCollectionName);
this.collection.createIndex({ span: -1, date: -1 }, { unique: true });
2018-10-21 02:20:11 +02:00
this.collection.createIndex('group');
2018-10-21 00:10:35 +02:00
}
protected async getCurrentStats(span: Span, group?: Obj): Promise<ChartDocument<T>> {
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 02:20:11 +02:00
const currentStats = await this.collection.findOne({
group: group,
2018-10-21 00:10:35 +02:00
span: span,
date: current
2018-10-21 02:20:11 +02:00
});
2018-10-21 00:10:35 +02:00
if (currentStats) {
return currentStats;
} else {
// 集計期間が変わってから、初めてのチャート更新なら
// 最も最近の統計を持ってくる
// * 例えば集計期間が「日」である場合で考えると、
// * 昨日何もチャートを更新するような出来事がなかった場合は、
// * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
2018-10-21 02:20:11 +02:00
const mostRecentStats = await this.collection.findOne({
group: group,
2018-10-21 00:10:35 +02:00
span: span
2018-10-21 02:20:11 +02:00
}, {
2018-10-21 00:10:35 +02:00
sort: {
date: -1
}
});
if (mostRecentStats) {
// 現在の統計を初期挿入
const data = this.generateEmptyStats(mostRecentStats.data);
2018-10-21 02:20:11 +02:00
const stats = await this.collection.insert({
group: group,
2018-10-21 00:10:35 +02:00
span: span,
date: current,
data: data
2018-10-21 02:20:11 +02:00
});
2018-10-21 00:10:35 +02:00
return stats;
} else {
// 統計が存在しなかったら
// * Misskeyインスタンスを建てて初めてのチャート更新時など
// 空の統計を作成
const data = this.generateInitialStats();
2018-10-21 02:20:11 +02:00
const stats = await this.collection.insert({
group: group,
2018-10-21 00:10:35 +02:00
span: span,
date: current,
data: data
2018-10-21 02:20:11 +02:00
});
2018-10-21 00:10:35 +02:00
return stats;
}
}
}
2018-10-21 02:20:11 +02:00
protected inc(inc: Partial<T>, group?: Obj): void {
2018-10-21 00:10:35 +02:00
const query: Obj = {};
2018-10-21 03:05:15 +02:00
const dive = (x: Obj, path?: string) => {
2018-10-21 00:10:35 +02:00
Object.entries(x).forEach(([k, v]) => {
2018-10-21 03:05:15 +02:00
const p = path ? `${path}.${k}` : k;
2018-10-21 00:10:35 +02:00
if (typeof v === 'number') {
2018-10-21 03:05:15 +02:00
query[`data.${p}`] = v;
2018-10-21 00:10:35 +02:00
} else {
2018-10-21 03:05:15 +02:00
dive(v, p);
2018-10-21 00:10:35 +02:00
}
});
};
2018-10-21 03:05:15 +02:00
dive(inc);
2018-10-21 00:10:35 +02:00
this.getCurrentStats('day', group).then(stats => {
this.collection.findOneAndUpdate({
_id: stats._id
}, {
$inc: query
});
});
this.getCurrentStats('hour', group).then(stats => {
this.collection.findOneAndUpdate({
_id: stats._id
}, {
$inc: query
});
});
}
2018-10-21 03:05:15 +02:00
public async getStats(span: Span, range: number, group?: Obj): Promise<ArrayValue<T>> {
2018-10-21 00:10:35 +02:00
const chart: T[] = [];
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 02:20:11 +02:00
const stats = await this.collection.find({
group: group,
2018-10-21 00:10:35 +02:00
span: span,
date: {
$gt: gt
}
2018-10-21 02:20:11 +02:00
}, {
2018-10-21 00:10:35 +02: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;
const stat = stats.find(s => s.date.getTime() == current.getTime());
if (stat) {
chart.unshift(stat.data);
} else { // 隙間埋め
const mostRecent = stats.find(s => s.date.getTime() < current.getTime());
if (mostRecent) {
chart.unshift(this.generateEmptyStats(mostRecent.data));
} else {
chart.unshift(this.generateInitialStats());
}
}
}
2018-10-21 03:05:15 +02:00
const res: ArrayValue<T> = {} as any;
/**
* [{
* x: 1,
* y: 5
* }, {
* x: 2,
* y: 6
* }, {
* x: 3,
* y: 7
* }]
*
*
*
* {
* x: [1, 2, 3],
* y: [5, 6, 7]
* }
*
*
*/
const dive = (x: Obj, path?: string) => {
Object.entries(x).forEach(([k, v]) => {
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-21 00:10:35 +02:00
}
}
2018-10-21 03:05:15 +02:00
//#endregion
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
//#region Users stats
/**
*
*/
type UsersStats = {
local: {
/**
* ()
*/
total: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* ()
*/
inc: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* ()
*/
dec: number;
};
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
remote: {
/**
* ()
*/
total: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* ()
*/
inc: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* ()
*/
dec: number;
2018-10-21 00:10:35 +02:00
};
2018-10-21 02:20:11 +02:00
};
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
class UsersChart extends Chart<UsersStats> {
constructor() {
super('usersStats');
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
protected generateInitialStats(): UsersStats {
return {
local: {
total: 0,
inc: 0,
dec: 0
},
remote: {
total: 0,
inc: 0,
dec: 0
}
2018-10-21 00:10:35 +02:00
};
2018-10-21 02:20:11 +02:00
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
protected generateEmptyStats(mostRecentStats: UsersStats): UsersStats {
return {
local: {
total: mostRecentStats.local.total,
inc: 0,
dec: 0
},
remote: {
total: mostRecentStats.remote.total,
inc: 0,
dec: 0
}
2018-10-21 00:10:35 +02:00
};
2018-10-21 02:20:11 +02:00
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
public async update(user: IUser, isAdditional: boolean) {
const update: Obj = {};
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
update.total = isAdditional ? 1 : -1;
if (isAdditional) {
update.inc = 1;
} else {
update.dec = 1;
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
await this.inc({
[isLocalUser(user) ? 'local' : 'remote']: update
});
}
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
export const usersChart = new UsersChart();
//#endregion
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
//#region Notes stats
/**
* 稿
*/
type NotesStats = {
local: {
/**
* 稿 ()
*/
total: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* 稿 ()
*/
inc: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* 稿 ()
*/
dec: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
diffs: {
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* 稿 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
normal: number;
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* 稿 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
reply: number;
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* Renoteの投稿数の差分 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
renote: number;
2018-10-21 00:10:35 +02:00
};
};
2018-10-21 02:20:11 +02:00
remote: {
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* 稿 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
total: number;
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* 稿 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
inc: number;
2018-10-21 00:10:35 +02:00
/**
2018-10-21 02:20:11 +02:00
* 稿 ()
2018-10-21 00:10:35 +02:00
*/
2018-10-21 02:20:11 +02:00
dec: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
diffs: {
/**
* 稿 ()
*/
normal: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* 稿 ()
*/
reply: number;
/**
* Renoteの投稿数の差分 ()
*/
renote: number;
};
2018-10-21 00:10:35 +02:00
};
};
2018-10-21 02:20:11 +02:00
class NotesChart extends Chart<NotesStats> {
2018-10-21 00:10:35 +02:00
constructor() {
2018-10-21 02:20:11 +02:00
super('notesStats');
2018-10-21 00:10:35 +02:00
}
2018-10-21 02:20:11 +02:00
protected generateInitialStats(): NotesStats {
2018-10-21 00:10:35 +02:00
return {
2018-10-21 02:20:11 +02:00
local: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
2018-10-21 00:10:35 +02:00
}
},
2018-10-21 02:20:11 +02:00
remote: {
total: 0,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
2018-10-21 00:10:35 +02:00
}
}
};
}
2018-10-21 02:20:11 +02:00
protected generateEmptyStats(mostRecentStats: NotesStats): NotesStats {
2018-10-21 00:10:35 +02:00
return {
2018-10-21 02:20:11 +02:00
local: {
total: mostRecentStats.local.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
2018-10-21 00:10:35 +02:00
}
},
2018-10-21 02:20:11 +02:00
remote: {
total: mostRecentStats.remote.total,
inc: 0,
dec: 0,
diffs: {
normal: 0,
reply: 0,
renote: 0
2018-10-21 00:10:35 +02:00
}
}
};
}
2018-10-21 02:20:11 +02:00
public async update(note: INote, isAdditional: boolean) {
2018-10-21 00:10:35 +02:00
const update: Obj = {};
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-21 02:20:11 +02:00
await this.inc({
[isLocalUser(note._user) ? 'local' : 'remote']: update
});
}
}
export const notesChart = new NotesChart();
//#endregion
//#region Drive stats
/**
*
*/
type DriveStats = {
local: {
/**
* ()
*/
totalCount: number;
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
remote: {
/**
* ()
*/
totalCount: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
* ()
*/
totalSize: number;
/**
* ()
*/
incCount: number;
/**
* 使 ()
*/
incSize: number;
/**
* ()
*/
decCount: number;
/**
* 使 ()
*/
decSize: number;
};
};
class DriveChart extends Chart<DriveStats> {
constructor() {
super('driveStats');
2018-10-21 00:10:35 +02:00
}
2018-10-21 02:20:11 +02:00
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
}
};
}
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
public async update(file: IDriveFile, isAdditional: boolean) {
2018-10-21 00:10:35 +02: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-21 02:20:11 +02:00
await this.inc({
[isLocalUser(file.metadata._user) ? 'local' : 'remote']: update
});
}
}
export const driveChart = new DriveChart();
//#endregion
//#region Network stats
/**
*
*/
type NetworkStats = {
/**
*
*/
incomingRequests: number;
/**
*
*/
outgoingRequests: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
*
* TIP: (totalTime / incomingRequests)
*/
totalTime: number;
2018-10-21 00:10:35 +02:00
2018-10-21 02:20:11 +02:00
/**
*
*/
incomingBytes: number;
/**
*
*/
outgoingBytes: number;
};
class NetworkChart extends Chart<NetworkStats> {
constructor() {
super('networkStats');
2018-10-21 00:10:35 +02:00
}
2018-10-21 02:20:11 +02:00
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
2018-10-21 00:10:35 +02:00
};
2018-10-21 02:20:11 +02:00
await this.inc(inc);
2018-10-21 00:10:35 +02:00
}
}
2018-10-21 02:20:11 +02:00
export const networkChart = new NetworkChart();
//#endregion