From 29fc6de33055e3aa7c35f2354b79a3329e518524 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 22 Oct 2018 03:30:45 +0900 Subject: [PATCH] Refactor --- src/services/stats.ts | 44 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/services/stats.ts b/src/services/stats.ts index 12ebe4386..6318623d8 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -59,7 +59,7 @@ type Log = { */ abstract class Stats { protected collection: ICollection>; - protected abstract async getTemplate(init: boolean, latestLog?: T, group?: any): Promise; + protected abstract async getTemplate(init: boolean, latest?: T, group?: any): Promise; constructor(name: string) { this.collection = db.get>(`stats.${name}`); @@ -117,7 +117,7 @@ abstract class Stats { // * 昨日何もチャートを更新するような出来事がなかった場合は、 // * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、 // * 「昨日の」と決め打ちせずに「もっとも最近の」とします - const latestLog = await this.collection.findOne({ + const latest = await this.collection.findOne({ group: group, span: span }, { @@ -126,9 +126,9 @@ abstract class Stats { } }); - if (latestLog) { + if (latest) { // 現在の統計を初期挿入 - const data = await this.getTemplate(false, latestLog.data); + const data = await this.getTemplate(false, latest.data); const log = await this.collection.insert({ group: group, @@ -316,13 +316,13 @@ class UsersStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: UsersLog): Promise { + protected async getTemplate(init: boolean, latest?: UsersLog): Promise { const [localCount, remoteCount] = init ? await Promise.all([ User.count({ host: null }), User.count({ host: { $ne: null } }) ]) : [ - latestLog ? latestLog.local.total : 0, - latestLog ? latestLog.remote.total : 0 + latest ? latest.local.total : 0, + latest ? latest.remote.total : 0 ]; return { @@ -407,13 +407,13 @@ class NotesStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: NotesLog): Promise { + protected async getTemplate(init: boolean, latest?: NotesLog): Promise { const [localCount, remoteCount] = init ? await Promise.all([ Note.count({ '_user.host': null }), Note.count({ '_user.host': { $ne: null } }) ]) : [ - latestLog ? latestLog.local.total : 0, - latestLog ? latestLog.remote.total : 0 + latest ? latest.local.total : 0, + latest ? latest.remote.total : 0 ]; return { @@ -517,7 +517,7 @@ class DriveStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: DriveLog): Promise { + protected async getTemplate(init: boolean, latest?: DriveLog): Promise { const calcSize = (local: boolean) => DriveFile .aggregate([{ $match: { @@ -542,10 +542,10 @@ class DriveStats extends Stats { calcSize(true), calcSize(false) ]) : [ - latestLog ? latestLog.local.totalCount : 0, - latestLog ? latestLog.remote.totalCount : 0, - latestLog ? latestLog.local.totalSize : 0, - latestLog ? latestLog.remote.totalSize : 0 + latest ? latest.local.totalCount : 0, + latest ? latest.remote.totalCount : 0, + latest ? latest.local.totalSize : 0, + latest ? latest.remote.totalSize : 0 ]; return { @@ -629,7 +629,7 @@ class NetworkStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: NetworkLog): Promise { + protected async getTemplate(init: boolean, latest?: NetworkLog): Promise { return { incomingRequests: 0, outgoingRequests: 0, @@ -672,7 +672,7 @@ class HashtagStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: HashtagLog): Promise { + protected async getTemplate(init: boolean, latest?: HashtagLog): Promise { return { count: 0 }; @@ -747,7 +747,7 @@ class FollowingStats extends Stats { } @autobind - protected async getTemplate(init: boolean, latestLog?: FollowingLog, group?: any): Promise { + protected async getTemplate(init: boolean, latest?: FollowingLog, group?: any): Promise { const [ localFollowingsCount, localFollowersCount, @@ -759,10 +759,10 @@ class FollowingStats extends Stats { Following.count({ followerId: group, '_followee.host': { $ne: null } }), Following.count({ followeeId: group, '_follower.host': { $ne: null } }) ]) : [ - latestLog ? latestLog.local.followings.total : 0, - latestLog ? latestLog.local.followers.total : 0, - latestLog ? latestLog.remote.followings.total : 0, - latestLog ? latestLog.remote.followers.total : 0 + latest ? latest.local.followings.total : 0, + latest ? latest.local.followers.total : 0, + latest ? latest.remote.followings.total : 0, + latest ? latest.remote.followers.total : 0 ]; return {