2018-08-18 11:22:56 +00:00
|
|
|
import { INote } from '../models/note';
|
|
|
|
import Chart, { IChart } from '../models/chart';
|
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-18 11:22:56 +00:00
|
|
|
async function getTodayStats(): Promise<IChart> {
|
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth();
|
|
|
|
const d = now.getDate();
|
|
|
|
const today = new Date(y, m, d);
|
|
|
|
|
|
|
|
// 今日の統計
|
|
|
|
const todayStats = await Chart.findOne({
|
|
|
|
date: today
|
|
|
|
});
|
|
|
|
|
|
|
|
// 日付が変わってから、初めてのチャート更新なら
|
|
|
|
if (todayStats == null) {
|
|
|
|
// 最も最近の統計を持ってくる
|
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
|
|
|
// 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
// 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
|
|
|
const mostRecentStats = await Chart.findOne({}, {
|
|
|
|
sort: {
|
2018-08-18 14:15:16 +00:00
|
|
|
date: -1
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 統計が存在しなかったら
|
|
|
|
// * Misskeyインスタンスを建てて初めてのチャート更新時など
|
|
|
|
if (mostRecentStats == null) {
|
|
|
|
// 空の統計を作成
|
2018-08-18 14:23:55 +00:00
|
|
|
const chart: Omit<IChart, '_id'> = {
|
2018-08-18 11:22:56 +00:00
|
|
|
date: today,
|
|
|
|
users: {
|
|
|
|
local: {
|
|
|
|
total: 0,
|
|
|
|
diff: 0
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: 0,
|
|
|
|
diff: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
local: {
|
|
|
|
total: 0,
|
2018-08-18 14:23:55 +00:00
|
|
|
diff: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: 0,
|
2018-08-18 14:23:55 +00:00
|
|
|
diff: 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: {
|
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
|
|
|
diffCount: 0,
|
|
|
|
diffSize: 0
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
|
|
|
diffCount: 0,
|
|
|
|
diffSize: 0
|
|
|
|
}
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
2018-08-18 14:23:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const stats = await Chart.insert(chart);
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
return stats;
|
|
|
|
} else {
|
|
|
|
// 今日の統計を初期挿入
|
2018-08-18 14:23:55 +00:00
|
|
|
const chart: Omit<IChart, '_id'> = {
|
2018-08-18 11:22:56 +00:00
|
|
|
date: today,
|
|
|
|
users: {
|
|
|
|
local: {
|
|
|
|
total: mostRecentStats.users.local.total,
|
|
|
|
diff: 0
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: mostRecentStats.users.remote.total,
|
|
|
|
diff: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
local: {
|
|
|
|
total: mostRecentStats.notes.local.total,
|
2018-08-18 14:23:55 +00:00
|
|
|
diff: 0,
|
2018-08-18 11:22:56 +00:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: mostRecentStats.notes.remote.total,
|
2018-08-18 14:23:55 +00:00
|
|
|
diff: 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: {
|
|
|
|
totalCount: mostRecentStats.drive.local.totalCount,
|
|
|
|
totalSize: mostRecentStats.drive.local.totalSize,
|
|
|
|
diffCount: 0,
|
|
|
|
diffSize: 0
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
totalCount: mostRecentStats.drive.remote.totalCount,
|
|
|
|
totalSize: mostRecentStats.drive.remote.totalSize,
|
|
|
|
diffCount: 0,
|
|
|
|
diffSize: 0
|
|
|
|
}
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
2018-08-18 14:23:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const stats = await Chart.insert(chart);
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return todayStats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function update(inc: any) {
|
|
|
|
const stats = await getTodayStats();
|
|
|
|
|
|
|
|
await Chart.findOneAndUpdate({
|
|
|
|
_id: stats._id
|
|
|
|
}, {
|
|
|
|
$inc: inc
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-18 14:15:16 +00:00
|
|
|
export async function updateUserStats(user: IUser, isAdditional: boolean) {
|
|
|
|
const inc = {} as any;
|
|
|
|
|
2018-08-18 14:48:54 +00:00
|
|
|
const amount = isAdditional ? 1 : -1;
|
2018-08-18 14:15:16 +00:00
|
|
|
|
|
|
|
if (isLocalUser(user)) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['users.local.total'] = amount;
|
|
|
|
inc['users.local.diff'] = amount;
|
2018-08-18 14:15:16 +00:00
|
|
|
} else {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['users.remote.total'] = amount;
|
|
|
|
inc['users.remote.diff'] = amount;
|
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 11:22:56 +00:00
|
|
|
const inc = {} as any;
|
|
|
|
|
2018-08-18 14:48:54 +00:00
|
|
|
const amount = isAdditional ? 1 : -1;
|
2018-08-18 11:26:01 +00:00
|
|
|
|
2018-08-18 11:22:56 +00:00
|
|
|
if (isLocalUser(note._user)) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.local.total'] = amount;
|
|
|
|
inc['notes.local.diff'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
if (note.replyId != null) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.local.diffs.reply'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
} else if (note.renoteId != null) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.local.diffs.renote'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
} else {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.local.diffs.normal'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.remote.total'] = amount;
|
|
|
|
inc['notes.remote.diff'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
|
|
|
|
if (note.replyId != null) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.remote.diffs.reply'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
} else if (note.renoteId != null) {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.remote.diffs.renote'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
} else {
|
2018-08-18 14:48:54 +00:00
|
|
|
inc['notes.remote.diffs.normal'] = amount;
|
2018-08-18 11:22:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await update(inc);
|
|
|
|
}
|
2018-08-18 14:48:54 +00:00
|
|
|
|
|
|
|
export async function updateDriveStats(user: IUser, file: IDriveFile, isAdditional: boolean) {
|
|
|
|
const inc = {} as any;
|
|
|
|
|
|
|
|
const amount = isAdditional ? 1 : -1;
|
|
|
|
const size = isAdditional ? file.length : -file.length;
|
|
|
|
|
|
|
|
if (isLocalUser(user)) {
|
|
|
|
inc['drive.local.totalCount'] = amount;
|
|
|
|
inc['drive.local.diffCount'] = amount;
|
|
|
|
inc['drive.local.totalSize'] = size;
|
|
|
|
inc['drive.local.diffSize'] = size;
|
|
|
|
} else {
|
|
|
|
inc['drive.remote.total'] = amount;
|
|
|
|
inc['drive.remote.diff'] = amount;
|
|
|
|
inc['drive.remote.totalSize'] = size;
|
|
|
|
inc['drive.remote.diffSize'] = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
await update(inc);
|
|
|
|
}
|