Refactoring

This commit is contained in:
syuilo 2018-08-19 03:06:23 +09:00
parent 60c0dac5f2
commit 667b18fe24

View File

@ -33,7 +33,7 @@ async function getTodayStats(): Promise<IStats> {
// * Misskeyインスタンスを建てて初めてのチャート更新時など // * Misskeyインスタンスを建てて初めてのチャート更新時など
if (mostRecentStats == null) { if (mostRecentStats == null) {
// 空の統計を作成 // 空の統計を作成
const chart: Omit<IStats, '_id'> = { const data: Omit<IStats, '_id'> = {
date: today, date: today,
users: { users: {
local: { local: {
@ -81,12 +81,12 @@ async function getTodayStats(): Promise<IStats> {
} }
}; };
const stats = await Stats.insert(chart); const stats = await Stats.insert(data);
return stats; return stats;
} else { } else {
// 今日の統計を初期挿入 // 今日の統計を初期挿入
const chart: Omit<IStats, '_id'> = { const data: Omit<IStats, '_id'> = {
date: today, date: today,
users: { users: {
local: { local: {
@ -134,7 +134,7 @@ async function getTodayStats(): Promise<IStats> {
} }
}; };
const stats = await Stats.insert(chart); const stats = await Stats.insert(data);
return stats; return stats;
} }
@ -154,70 +154,46 @@ async function update(inc: any) {
} }
export async function updateUserStats(user: IUser, isAdditional: boolean) { export async function updateUserStats(user: IUser, isAdditional: boolean) {
const inc = {} as any;
const amount = isAdditional ? 1 : -1; const amount = isAdditional ? 1 : -1;
const origin = isLocalUser(user) ? 'local' : 'remote';
if (isLocalUser(user)) { const inc = {} as any;
inc['users.local.total'] = amount; inc[`users.${origin}.total`] = amount;
inc['users.local.diff'] = amount; inc[`users.${origin}.diff`] = amount;
} else {
inc['users.remote.total'] = amount;
inc['users.remote.diff'] = amount;
}
await update(inc); await update(inc);
} }
export async function updateNoteStats(note: INote, isAdditional: boolean) { export async function updateNoteStats(note: INote, isAdditional: boolean) {
const amount = isAdditional ? 1 : -1;
const origin = isLocalUser(note._user) ? 'local' : 'remote';
const inc = {} as any; const inc = {} as any;
const amount = isAdditional ? 1 : -1; inc[`notes.${origin}.total`] = amount;
inc[`notes.${origin}.diff`] = amount;
if (isLocalUser(note._user)) {
inc['notes.local.total'] = amount;
inc['notes.local.diff'] = amount;
if (note.replyId != null) { if (note.replyId != null) {
inc['notes.local.diffs.reply'] = amount; inc[`notes.${origin}.diffs.reply`] = amount;
} else if (note.renoteId != null) { } else if (note.renoteId != null) {
inc['notes.local.diffs.renote'] = amount; inc[`notes.${origin}.diffs.renote`] = amount;
} else { } else {
inc['notes.local.diffs.normal'] = amount; inc[`notes.${origin}.diffs.normal`] = amount;
}
} else {
inc['notes.remote.total'] = amount;
inc['notes.remote.diff'] = amount;
if (note.replyId != null) {
inc['notes.remote.diffs.reply'] = amount;
} else if (note.renoteId != null) {
inc['notes.remote.diffs.renote'] = amount;
} else {
inc['notes.remote.diffs.normal'] = amount;
}
} }
await update(inc); await update(inc);
} }
export async function updateDriveStats(file: IDriveFile, isAdditional: boolean) { export async function updateDriveStats(file: IDriveFile, isAdditional: boolean) {
const inc = {} as any;
const amount = isAdditional ? 1 : -1; const amount = isAdditional ? 1 : -1;
const size = isAdditional ? file.length : -file.length; const size = isAdditional ? file.length : -file.length;
const origin = isLocalUser(file.metadata._user) ? 'local' : 'remote';
if (isLocalUser(file.metadata._user)) { const inc = {} as any;
inc['drive.local.totalCount'] = amount; inc[`drive.${origin}.totalCount`] = amount;
inc['drive.local.diffCount'] = amount; inc[`drive.${origin}.diffCount`] = amount;
inc['drive.local.totalSize'] = size; inc[`drive.${origin}.totalSize`] = size;
inc['drive.local.diffSize'] = size; inc[`drive.${origin}.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); await update(inc);
} }