2018-10-23 05:36:35 +09:00
|
|
|
/**
|
|
|
|
* チャートエンジン
|
|
|
|
*/
|
|
|
|
|
2018-11-10 23:25:09 +09:00
|
|
|
import * as moment from 'moment';
|
2019-02-01 21:08:49 +09:00
|
|
|
import * as nestedProperty from 'nested-property';
|
2018-10-23 05:36:35 +09:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import * as mongo from 'mongodb';
|
2019-02-08 04:31:33 +09:00
|
|
|
import db from '../../db/mongodb';
|
2018-10-23 05:36:35 +09:00
|
|
|
import { ICollection } from 'monk';
|
2019-02-08 04:31:33 +09:00
|
|
|
import Logger from '../../misc/logger';
|
2019-02-24 17:28:51 +09:00
|
|
|
import { Schema } from '../../misc/schema';
|
2019-02-03 13:52:21 +09:00
|
|
|
|
|
|
|
const logger = new Logger('chart');
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-11-10 23:25:09 +09:00
|
|
|
const utc = moment.utc;
|
|
|
|
|
2018-10-23 05:36:35 +09:00
|
|
|
export type Obj = { [key: string]: any };
|
|
|
|
|
|
|
|
export type Partial<T> = {
|
|
|
|
[P in keyof T]?: Partial<T[P]>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ArrayValue<T> = {
|
2019-02-04 18:27:45 +09:00
|
|
|
[P in keyof T]: T[P] extends number ? T[P][] : ArrayValue<T[P]>;
|
2018-10-23 05:36:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
type Span = 'day' | 'hour';
|
|
|
|
|
|
|
|
type Log<T extends Obj> = {
|
|
|
|
_id: mongo.ObjectID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計のグループ
|
|
|
|
*/
|
|
|
|
group?: any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計日時
|
|
|
|
*/
|
|
|
|
date: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計期間
|
|
|
|
*/
|
|
|
|
span: Span;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* データ
|
|
|
|
*/
|
|
|
|
data: T;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ユニークインクリメント用
|
|
|
|
*/
|
|
|
|
unique?: Obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 様々なチャートの管理を司るクラス
|
|
|
|
*/
|
2019-02-05 01:52:09 +09:00
|
|
|
export default abstract class Chart<T extends Obj> {
|
2018-10-23 05:36:35 +09:00
|
|
|
protected collection: ICollection<Log<T>>;
|
|
|
|
protected abstract async getTemplate(init: boolean, latest?: T, group?: any): Promise<T>;
|
2019-02-03 13:52:21 +09:00
|
|
|
private name: string;
|
2018-10-23 05:36:35 +09:00
|
|
|
|
|
|
|
constructor(name: string, grouped = false) {
|
2019-02-03 13:52:21 +09:00
|
|
|
this.name = name;
|
2018-10-23 05:36:35 +09:00
|
|
|
this.collection = db.get<Log<T>>(`chart.${name}`);
|
2019-02-03 13:52:21 +09:00
|
|
|
|
2018-12-09 18:39:13 +09:00
|
|
|
const keys = {
|
|
|
|
span: -1,
|
|
|
|
date: -1
|
2018-12-15 20:54:34 +09:00
|
|
|
} as { [key: string]: 1 | -1; };
|
2018-12-11 20:33:52 +09:00
|
|
|
if (grouped) keys.group = -1;
|
2019-02-03 13:52:21 +09:00
|
|
|
|
2018-12-09 18:39:13 +09:00
|
|
|
this.collection.createIndex(keys, { unique: true });
|
2018-10-23 05:36:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private convertQuery(x: Obj, path: string): Obj {
|
|
|
|
const query: Obj = {};
|
|
|
|
|
|
|
|
const dive = (x: Obj, path: string) => {
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const [k, v] of Object.entries(x)) {
|
2018-10-23 05:36:35 +09:00
|
|
|
const p = path ? `${path}.${k}` : k;
|
|
|
|
if (typeof v === 'number') {
|
|
|
|
query[p] = v;
|
|
|
|
} else {
|
|
|
|
dive(v, p);
|
|
|
|
}
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-10-23 05:36:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
dive(x, path);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2018-10-25 16:10:48 +09:00
|
|
|
private getCurrentDate(): [number, number, number, number] {
|
2018-11-10 23:25:09 +09:00
|
|
|
const now = moment().utc();
|
2018-10-25 16:10:48 +09:00
|
|
|
|
2018-11-10 23:25:09 +09:00
|
|
|
const y = now.year();
|
|
|
|
const m = now.month();
|
|
|
|
const d = now.date();
|
|
|
|
const h = now.hour();
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
return [y, m, d, h];
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private getLatestLog(span: Span, group?: any): Promise<Log<T>> {
|
|
|
|
return this.collection.findOne({
|
|
|
|
group: group,
|
|
|
|
span: span
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private async getCurrentLog(span: Span, group?: any): Promise<Log<T>> {
|
|
|
|
const [y, m, d, h] = this.getCurrentDate();
|
|
|
|
|
2018-10-23 05:36:35 +09:00
|
|
|
const current =
|
2018-11-10 23:25:09 +09:00
|
|
|
span == 'day' ? utc([y, m, d]) :
|
|
|
|
span == 'hour' ? utc([y, m, d, h]) :
|
2018-10-23 05:36:35 +09:00
|
|
|
null;
|
|
|
|
|
|
|
|
// 現在(今日または今のHour)のログ
|
|
|
|
const currentLog = await this.collection.findOne({
|
|
|
|
group: group,
|
|
|
|
span: span,
|
2018-11-10 23:25:09 +09:00
|
|
|
date: current.toDate()
|
2018-10-23 05:36:35 +09:00
|
|
|
});
|
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
// ログがあればそれを返して終了
|
|
|
|
if (currentLog != null) {
|
2018-10-23 05:36:35 +09:00
|
|
|
return currentLog;
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
let log: Log<T>;
|
|
|
|
let data: T;
|
|
|
|
|
2018-10-23 05:36:35 +09:00
|
|
|
// 集計期間が変わってから、初めてのチャート更新なら
|
|
|
|
// 最も最近のログを持ってくる
|
|
|
|
// * 例えば集計期間が「日」である場合で考えると、
|
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
|
|
|
// * ログがそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
2018-10-25 16:10:48 +09:00
|
|
|
const latest = await this.getLatestLog(span, group);
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
if (latest != null) {
|
|
|
|
// 空ログデータを作成
|
|
|
|
data = await this.getTemplate(false, latest.data);
|
2018-10-23 05:36:35 +09:00
|
|
|
} else {
|
|
|
|
// ログが存在しなかったら
|
2018-10-25 16:10:48 +09:00
|
|
|
// (Misskeyインスタンスを建てて初めてのチャート更新時など
|
|
|
|
// または何らかの理由でチャートコレクションを抹消した場合)
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
// 初期ログデータを作成
|
|
|
|
data = await this.getTemplate(true, null, group);
|
2019-02-03 13:52:21 +09:00
|
|
|
|
|
|
|
logger.info(`${this.name}: Initial commit created`);
|
2018-10-25 16:10:48 +09:00
|
|
|
}
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
try {
|
|
|
|
// 新規ログ挿入
|
|
|
|
log = await this.collection.insert({
|
2018-10-23 05:36:35 +09:00
|
|
|
group: group,
|
|
|
|
span: span,
|
2018-11-10 23:25:09 +09:00
|
|
|
date: current.toDate(),
|
2018-10-23 05:36:35 +09:00
|
|
|
data: data
|
|
|
|
});
|
2018-10-25 16:10:48 +09:00
|
|
|
} catch (e) {
|
|
|
|
// 11000 is duplicate key error
|
|
|
|
// 並列動作している他のチャートエンジンプロセスと処理が重なる場合がある
|
|
|
|
// その場合は再度最も新しいログを持ってくる
|
|
|
|
if (e.code === 11000) {
|
|
|
|
log = await this.getLatestLog(span, group);
|
|
|
|
} else {
|
2019-02-03 18:16:57 +09:00
|
|
|
logger.error(e);
|
2018-10-25 16:10:48 +09:00
|
|
|
throw e;
|
|
|
|
}
|
2018-10-23 05:36:35 +09:00
|
|
|
}
|
2018-10-25 16:10:48 +09:00
|
|
|
|
|
|
|
return log;
|
2018-10-23 05:36:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
protected commit(query: Obj, group?: any, uniqueKey?: string, uniqueValue?: string): void {
|
|
|
|
const update = (log: Log<T>) => {
|
|
|
|
// ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く
|
|
|
|
if (
|
|
|
|
uniqueKey &&
|
|
|
|
log.unique &&
|
|
|
|
log.unique[uniqueKey] &&
|
|
|
|
log.unique[uniqueKey].includes(uniqueValue)
|
|
|
|
) return;
|
|
|
|
|
|
|
|
// ユニークインクリメントの指定のキーに値を追加
|
|
|
|
if (uniqueKey) {
|
|
|
|
query['$push'] = {
|
|
|
|
[`unique.${uniqueKey}`]: uniqueValue
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
// ログ更新
|
2018-10-23 05:36:35 +09:00
|
|
|
this.collection.update({
|
|
|
|
_id: log._id
|
|
|
|
}, query);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getCurrentLog('day', group).then(log => update(log));
|
|
|
|
this.getCurrentLog('hour', group).then(log => update(log));
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
protected inc(inc: Partial<T>, group?: any): void {
|
|
|
|
this.commit({
|
|
|
|
$inc: this.convertQuery(inc, 'data')
|
|
|
|
}, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
protected incIfUnique(inc: Partial<T>, key: string, value: string, group?: any): void {
|
|
|
|
this.commit({
|
|
|
|
$inc: this.convertQuery(inc, 'data')
|
|
|
|
}, group, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async getChart(span: Span, range: number, group?: any): Promise<ArrayValue<T>> {
|
|
|
|
const promisedChart: Promise<T>[] = [];
|
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
const [y, m, d, h] = this.getCurrentDate();
|
2018-10-23 05:36:35 +09:00
|
|
|
|
|
|
|
const gt =
|
2018-11-11 12:43:35 +09:00
|
|
|
span == 'day' ? utc([y, m, d]).subtract(range, 'days') :
|
|
|
|
span == 'hour' ? utc([y, m, d, h]).subtract(range, 'hours') :
|
2018-10-25 16:10:48 +09:00
|
|
|
null;
|
2018-10-23 05:36:35 +09:00
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
// ログ取得
|
2018-11-03 11:36:11 +09:00
|
|
|
let logs = await this.collection.find({
|
2018-10-23 05:36:35 +09:00
|
|
|
group: group,
|
|
|
|
span: span,
|
|
|
|
date: {
|
2018-11-11 13:08:48 +09:00
|
|
|
$gte: gt.toDate()
|
2018-10-23 05:36:35 +09:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-03 11:36:11 +09:00
|
|
|
// 要求された範囲にログがひとつもなかったら
|
|
|
|
if (logs.length == 0) {
|
|
|
|
// もっとも新しいログを持ってくる
|
|
|
|
// (すくなくともひとつログが無いと隙間埋めできないため)
|
|
|
|
const recentLog = await this.collection.findOne({
|
|
|
|
group: group,
|
|
|
|
span: span
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (recentLog) {
|
|
|
|
logs = [recentLog];
|
|
|
|
}
|
2018-11-11 13:08:48 +09:00
|
|
|
|
|
|
|
// 要求された範囲の最も古い箇所に位置するログが存在しなかったら
|
|
|
|
} else if (!utc(logs[logs.length - 1].date).isSame(gt)) {
|
|
|
|
// 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する
|
|
|
|
// (隙間埋めできないため)
|
|
|
|
const outdatedLog = await this.collection.findOne({
|
|
|
|
group: group,
|
|
|
|
span: span,
|
|
|
|
date: {
|
|
|
|
$lt: gt.toDate()
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (outdatedLog) {
|
|
|
|
logs.push(outdatedLog);
|
|
|
|
}
|
2018-11-03 11:36:11 +09:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:10:48 +09:00
|
|
|
// 整形
|
2018-10-23 05:36:35 +09:00
|
|
|
for (let i = (range - 1); i >= 0; i--) {
|
|
|
|
const current =
|
2018-11-11 12:43:35 +09:00
|
|
|
span == 'day' ? utc([y, m, d]).subtract(i, 'days') :
|
|
|
|
span == 'hour' ? utc([y, m, d, h]).subtract(i, 'hours') :
|
2018-10-23 05:36:35 +09:00
|
|
|
null;
|
|
|
|
|
2018-11-10 23:25:09 +09:00
|
|
|
const log = logs.find(l => utc(l.date).isSame(current));
|
2018-10-23 05:36:35 +09:00
|
|
|
|
|
|
|
if (log) {
|
|
|
|
promisedChart.unshift(Promise.resolve(log.data));
|
2018-10-25 16:10:48 +09:00
|
|
|
} else {
|
|
|
|
// 隙間埋め
|
2018-11-10 23:25:09 +09:00
|
|
|
const latest = logs.find(l => utc(l.date).isBefore(current));
|
2018-10-23 05:36:35 +09:00
|
|
|
promisedChart.unshift(this.getTemplate(false, latest ? latest.data : null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const chart = await Promise.all(promisedChart);
|
|
|
|
|
|
|
|
const res: ArrayValue<T> = {} as any;
|
|
|
|
|
|
|
|
/**
|
2019-01-18 19:48:16 +09:00
|
|
|
* [{ foo: 1, bar: 5 }, { foo: 2, bar: 6 }, { foo: 3, bar: 7 }]
|
2018-10-23 05:36:35 +09:00
|
|
|
* を
|
2019-01-18 19:48:16 +09:00
|
|
|
* { foo: [1, 2, 3], bar: [5, 6, 7] }
|
2018-10-23 05:36:35 +09:00
|
|
|
* にする
|
|
|
|
*/
|
|
|
|
const dive = (x: Obj, path?: string) => {
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const [k, v] of Object.entries(x)) {
|
2018-10-23 05:36:35 +09:00
|
|
|
const p = path ? `${path}.${k}` : k;
|
|
|
|
if (typeof v == 'object') {
|
|
|
|
dive(v, p);
|
|
|
|
} else {
|
|
|
|
nestedProperty.set(res, p, chart.map(s => nestedProperty.get(s, p)));
|
|
|
|
}
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-10-23 05:36:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
dive(chart[0]);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2019-02-24 09:45:27 +09:00
|
|
|
|
|
|
|
export function convertLog(logSchema: Schema): Schema {
|
|
|
|
const v: Schema = JSON.parse(JSON.stringify(logSchema)); // copy
|
|
|
|
if (v.type === 'number') {
|
|
|
|
v.type = 'array';
|
|
|
|
v.items = {
|
|
|
|
type: 'number'
|
|
|
|
};
|
|
|
|
} else if (v.type === 'object') {
|
|
|
|
for (const k of Object.keys(v.properties)) {
|
|
|
|
v.properties[k] = convertLog(v.properties[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|