2018-08-18 11:22:56 +00:00
|
|
|
import { INote } from '../models/note';
|
2018-08-18 15:27:23 +00:00
|
|
|
import Stats, { IStats } from '../models/stats';
|
2018-08-18 14:15:16 +00:00
|
|
|
import { isLocalUser, IUser } from '../models/user';
|
2018-08-18 14:48:54 +00:00
|
|
|
import { IDriveFile } from '../models/drive-file';
|
2018-08-18 11:22:56 +00:00
|
|
|
|
2018-08-18 14:23:55 +00:00
|
|
|
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
async function getCurrentStats(span: 'day' | 'hour'): Promise<IStats> {
|
2018-08-18 11:22:56 +00:00
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth();
|
|
|
|
const d = now.getDate();
|
2018-08-23 06:40:24 +00:00
|
|
|
const h = now.getHours();
|
2018-08-18 11:22:56 +00:00
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
const current =
|
|
|
|
span == 'day' ? new Date(y, m, d) :
|
|
|
|
span == 'hour' ? new Date(y, m, d, h) :
|
|
|
|
null;
|
|
|
|
|
|
|
|
// 現在(今日または今のHour)の統計
|
|
|
|
const currentStats = await Stats.findOne({
|
|
|
|
span: span,
|
|
|
|
date: current
|
2018-08-18 11:22:56 +00:00
|
|
|
});
|
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
if (currentStats) {
|
|
|
|
return currentStats;
|
|
|
|
} else {
|
|
|
|
// 集計期間が変わってから、初めてのチャート更新なら
|
2018-08-18 11:22:56 +00:00
|
|
|
// 最も最近の統計を持ってくる
|
2018-08-23 06:40:24 +00:00
|
|
|
// * 例えば集計期間が「日」である場合で考えると、
|
2018-08-18 11:22:56 +00:00
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
2018-08-23 06:40:24 +00:00
|
|
|
// * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
|
|
|
const mostRecentStats = await Stats.findOne({
|
|
|
|
span: span
|
|
|
|
}, {
|
2018-08-18 11:22:56 +00:00
|
|
|
sort: {
|
2018-08-18 14:15:16 +00:00
|
|
|
date: -1
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
if (mostRecentStats) {
|
|
|
|
// 現在の統計を初期挿入
|
2018-08-18 18:06:23 +00:00
|
|
|
const data: Omit<IStats, '_id'> = {
|
2018-08-23 06:40:24 +00:00
|
|
|
span: span,
|
|
|
|
date: current,
|
2018-08-18 11:22:56 +00:00
|
|
|
users: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: mostRecentStats.users.local.total,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-18 11:22:56 +00:00
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: mostRecentStats.users.remote.total,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: mostRecentStats.notes.local.total,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: mostRecentStats.notes.remote.total,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 14:48:54 +00:00
|
|
|
},
|
|
|
|
drive: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
totalCount: mostRecentStats.drive.local.totalCount,
|
|
|
|
totalSize: mostRecentStats.drive.local.totalSize,
|
2018-08-24 23:35:41 +00:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-18 14:48:54 +00:00
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
totalCount: mostRecentStats.drive.remote.totalCount,
|
|
|
|
totalSize: mostRecentStats.drive.remote.totalSize,
|
2018-08-24 23:35:41 +00:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-18 14:48:54 +00:00
|
|
|
}
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
2018-08-18 14:23:55 +00:00
|
|
|
};
|
|
|
|
|
2018-08-18 18:06:23 +00:00
|
|
|
const stats = await Stats.insert(data);
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
return stats;
|
|
|
|
} else {
|
2018-08-23 06:40:24 +00:00
|
|
|
// 統計が存在しなかったら
|
|
|
|
// * Misskeyインスタンスを建てて初めてのチャート更新時など
|
|
|
|
|
|
|
|
// 空の統計を作成
|
|
|
|
const emptyStat: Omit<IStats, '_id'> = {
|
|
|
|
span: span,
|
|
|
|
date: current,
|
2018-08-18 11:22:56 +00:00
|
|
|
users: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-18 11:22:56 +00:00
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
total: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 14:48:54 +00:00
|
|
|
},
|
|
|
|
drive: {
|
|
|
|
local: {
|
2018-08-23 06:40:24 +00:00
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-18 14:48:54 +00:00
|
|
|
},
|
|
|
|
remote: {
|
2018-08-23 06:40:24 +00:00
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
2018-08-24 23:35:41 +00:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-18 14:48:54 +00:00
|
|
|
}
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
2018-08-18 14:23:55 +00:00
|
|
|
};
|
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
const stats = await Stats.insert(emptyStat);
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
function update(inc: any) {
|
|
|
|
getCurrentStats('day').then(stats => {
|
|
|
|
Stats.findOneAndUpdate({
|
|
|
|
_id: stats._id
|
|
|
|
}, {
|
|
|
|
$inc: inc
|
|
|
|
});
|
|
|
|
});
|
2018-08-18 11:22:56 +00:00
|
|
|
|
2018-08-23 06:40:24 +00:00
|
|
|
getCurrentStats('hour').then(stats => {
|
|
|
|
Stats.findOneAndUpdate({
|
|
|
|
_id: stats._id
|
|
|
|
}, {
|
|
|
|
$inc: inc
|
|
|
|
});
|
2018-08-18 11:22:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-18 14:15:16 +00:00
|
|
|
export async function updateUserStats(user: IUser, isAdditional: boolean) {
|
2018-08-18 18:06:23 +00:00
|
|
|
const origin = isLocalUser(user) ? 'local' : 'remote';
|
2018-08-18 14:15:16 +00:00
|
|
|
|
2018-08-18 18:06:23 +00:00
|
|
|
const inc = {} as any;
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`users.${origin}.total`] = isAdditional ? 1 : -1;
|
|
|
|
if (isAdditional) {
|
|
|
|
inc[`users.${origin}.inc`] = 1;
|
|
|
|
} else {
|
|
|
|
inc[`users.${origin}.dec`] = 1;
|
|
|
|
}
|
2018-08-18 14:15:16 +00:00
|
|
|
|
|
|
|
await update(inc);
|
|
|
|
}
|
|
|
|
|
2018-08-18 11:26:01 +00:00
|
|
|
export async function updateNoteStats(note: INote, isAdditional: boolean) {
|
2018-08-18 18:06:23 +00:00
|
|
|
const origin = isLocalUser(note._user) ? 'local' : 'remote';
|
2018-08-18 11:26:01 +00:00
|
|
|
|
2018-08-18 18:06:23 +00:00
|
|
|
const inc = {} as any;
|
2018-08-18 11:22:56 +00:00
|
|
|
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`notes.${origin}.total`] = isAdditional ? 1 : -1;
|
|
|
|
|
|
|
|
if (isAdditional) {
|
|
|
|
inc[`notes.${origin}.inc`] = 1;
|
|
|
|
} else {
|
|
|
|
inc[`notes.${origin}.dec`] = 1;
|
|
|
|
}
|
2018-08-18 11:22:56 +00:00
|
|
|
|
2018-08-18 18:06:23 +00:00
|
|
|
if (note.replyId != null) {
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`notes.${origin}.diffs.reply`] = isAdditional ? 1 : -1;
|
2018-08-18 18:06:23 +00:00
|
|
|
} else if (note.renoteId != null) {
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`notes.${origin}.diffs.renote`] = isAdditional ? 1 : -1;
|
2018-08-18 18:06:23 +00:00
|
|
|
} else {
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`notes.${origin}.diffs.normal`] = isAdditional ? 1 : -1;
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await update(inc);
|
|
|
|
}
|
2018-08-18 14:48:54 +00:00
|
|
|
|
2018-08-18 14:56:44 +00:00
|
|
|
export async function updateDriveStats(file: IDriveFile, isAdditional: boolean) {
|
2018-08-18 18:06:23 +00:00
|
|
|
const origin = isLocalUser(file.metadata._user) ? 'local' : 'remote';
|
2018-08-18 14:48:54 +00:00
|
|
|
|
2018-08-18 18:06:23 +00:00
|
|
|
const inc = {} as any;
|
2018-08-24 23:35:41 +00:00
|
|
|
inc[`drive.${origin}.totalCount`] = isAdditional ? 1 : -1;
|
|
|
|
inc[`drive.${origin}.totalSize`] = isAdditional ? file.length : -file.length;
|
|
|
|
if (isAdditional) {
|
|
|
|
inc[`drive.${origin}.incCount`] = 1;
|
|
|
|
inc[`drive.${origin}.incSize`] = file.length;
|
|
|
|
} else {
|
|
|
|
inc[`drive.${origin}.decCount`] = 1;
|
|
|
|
inc[`drive.${origin}.decSize`] = file.length;
|
|
|
|
}
|
2018-08-18 14:48:54 +00:00
|
|
|
|
|
|
|
await update(inc);
|
|
|
|
}
|