From 6b0d48423d249b386d2428b46a14dea76bf231e9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 21 Oct 2018 15:08:07 +0900 Subject: [PATCH] Refactor --- src/remote/activitypub/models/person.ts | 4 +- src/server/api/endpoints/charts/drive.ts | 4 +- src/server/api/endpoints/charts/network.ts | 4 +- src/server/api/endpoints/charts/notes.ts | 4 +- src/server/api/endpoints/charts/users.ts | 4 +- src/server/api/private/signup.ts | 4 +- src/server/index.ts | 4 +- src/services/drive/add-file.ts | 4 +- src/services/drive/delete-file.ts | 4 +- src/services/note/create.ts | 4 +- src/services/note/delete.ts | 4 +- src/services/stats.ts | 114 ++++++++++----------- 12 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index 845b11861..c359693c8 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -10,7 +10,7 @@ import { isCollectionOrOrderedCollection, isCollection, IPerson } from '../type' import { IDriveFile } from '../../../models/drive-file'; import Meta from '../../../models/meta'; import htmlToMFM from '../../../mfm/html-to-mfm'; -import { usersChart } from '../../../services/stats'; +import { usersStats } from '../../../services/stats'; import { URL } from 'url'; import { resolveNote } from './note'; @@ -180,7 +180,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise new Promise(async (res, rej) => { const [ps, psErr] = getParams(meta, params); if (psErr) throw psErr; - const stats = await driveChart.getStats(ps.span as any, ps.limit); + const stats = await driveStats.getChart(ps.span as any, ps.limit); res(stats); }); diff --git a/src/server/api/endpoints/charts/network.ts b/src/server/api/endpoints/charts/network.ts index 236674de7..07ba19d5e 100644 --- a/src/server/api/endpoints/charts/network.ts +++ b/src/server/api/endpoints/charts/network.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import getParams from '../../get-params'; -import { networkChart } from '../../../../services/stats'; +import { networkStats } from '../../../../services/stats'; export const meta = { desc: { @@ -27,7 +27,7 @@ export default (params: any) => new Promise(async (res, rej) => { const [ps, psErr] = getParams(meta, params); if (psErr) throw psErr; - const stats = await networkChart.getStats(ps.span as any, ps.limit); + const stats = await networkStats.getChart(ps.span as any, ps.limit); res(stats); }); diff --git a/src/server/api/endpoints/charts/notes.ts b/src/server/api/endpoints/charts/notes.ts index 1efd58751..06ed3b7d4 100644 --- a/src/server/api/endpoints/charts/notes.ts +++ b/src/server/api/endpoints/charts/notes.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import getParams from '../../get-params'; -import { notesChart } from '../../../../services/stats'; +import { notesStats } from '../../../../services/stats'; export const meta = { desc: { @@ -27,7 +27,7 @@ export default (params: any) => new Promise(async (res, rej) => { const [ps, psErr] = getParams(meta, params); if (psErr) throw psErr; - const stats = await notesChart.getStats(ps.span as any, ps.limit); + const stats = await notesStats.getChart(ps.span as any, ps.limit); res(stats); }); diff --git a/src/server/api/endpoints/charts/users.ts b/src/server/api/endpoints/charts/users.ts index 21f5e0352..1dea4d5c4 100644 --- a/src/server/api/endpoints/charts/users.ts +++ b/src/server/api/endpoints/charts/users.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import getParams from '../../get-params'; -import { usersChart } from '../../../../services/stats'; +import { usersStats } from '../../../../services/stats'; export const meta = { desc: { @@ -27,7 +27,7 @@ export default (params: any) => new Promise(async (res, rej) => { const [ps, psErr] = getParams(meta, params); if (psErr) throw psErr; - const stats = await usersChart.getStats(ps.span as any, ps.limit); + const stats = await usersStats.getChart(ps.span as any, ps.limit); res(stats); }); diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index 99ad9cbcc..caab0267c 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -7,7 +7,7 @@ import generateUserToken from '../common/generate-native-user-token'; import config from '../../../config'; import Meta from '../../../models/meta'; import RegistrationTicket from '../../../models/registration-tickets'; -import { usersChart } from '../../../services/stats'; +import { usersStats } from '../../../services/stats'; if (config.recaptcha) { recaptcha.init({ @@ -130,7 +130,7 @@ export default async (ctx: Koa.Context) => { }, { upsert: true }); //#endregion - usersChart.update(account, true); + usersStats.update(account, true); const res = await pack(account, account, { detail: true, diff --git a/src/server/index.ts b/src/server/index.ts index ae989caff..848727fb1 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -17,7 +17,7 @@ const requestStats = require('request-stats'); import activityPub from './activitypub'; import webFinger from './webfinger'; import config from '../config'; -import { networkChart } from '../services/stats'; +import { networkStats } from '../services/stats'; import apiServer from './api'; // Init app @@ -104,7 +104,7 @@ export default () => new Promise(resolve => { const outgoingBytes = queue.reduce((a, b) => a + b.res.bytes, 0); queue = []; - networkChart.update(requests, time, incomingBytes, outgoingBytes); + networkStats.update(requests, time, incomingBytes, outgoingBytes); }, 5000); //#endregion }); diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index e2f48e0d9..fd8be6513 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -17,7 +17,7 @@ import { isLocalUser, IUser, IRemoteUser } from '../../models/user'; import delFile from './delete-file'; import config from '../../config'; import { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail'; -import { driveChart } from '../stats'; +import { driveStats } from '../stats'; const log = debug('misskey:drive:add-file'); @@ -389,7 +389,7 @@ export default async function( }); // 統計を更新 - driveChart.update(driveFile, true); + driveStats.update(driveFile, true); return driveFile; } diff --git a/src/services/drive/delete-file.ts b/src/services/drive/delete-file.ts index 96083a29e..e6aa1a064 100644 --- a/src/services/drive/delete-file.ts +++ b/src/services/drive/delete-file.ts @@ -2,7 +2,7 @@ import * as Minio from 'minio'; import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file'; import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail'; import config from '../../config'; -import { driveChart } from '../stats'; +import { driveStats } from '../stats'; export default async function(file: IDriveFile, isExpired = false) { if (file.metadata.storage == 'minio') { @@ -48,5 +48,5 @@ export default async function(file: IDriveFile, isExpired = false) { //#endregion // 統計を更新 - driveChart.update(file, false); + driveStats.update(file, false); } diff --git a/src/services/note/create.ts b/src/services/note/create.ts index a6ccc6cec..298c499d2 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -23,7 +23,7 @@ import registerHashtag from '../register-hashtag'; import isQuote from '../../misc/is-quote'; import { TextElementMention } from '../../mfm/parse/elements/mention'; import { TextElementHashtag } from '../../mfm/parse/elements/hashtag'; -import { notesChart } from '../stats'; +import { notesStats } from '../stats'; import { erase, unique } from '../../prelude/array'; import insertNoteUnread from './unread'; @@ -165,7 +165,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise< } // 統計を更新 - notesChart.update(note, true); + notesStats.update(note, true); // ハッシュタグ登録 tags.map(tag => registerHashtag(user, tag)); diff --git a/src/services/note/delete.ts b/src/services/note/delete.ts index 4041b28d5..26a1d5f6b 100644 --- a/src/services/note/delete.ts +++ b/src/services/note/delete.ts @@ -6,7 +6,7 @@ import pack from '../../remote/activitypub/renderer'; import { deliver } from '../../queue'; import Following from '../../models/following'; import renderTombstone from '../../remote/activitypub/renderer/tombstone'; -import { notesChart } from '../stats'; +import { notesStats } from '../stats'; import config from '../../config'; import NoteUnread from '../../models/note-unread'; import read from './read'; @@ -63,5 +63,5 @@ export default async function(user: IUser, note: INote) { //#endregion // 統計を更新 - notesChart.update(note, false); + notesStats.update(note, false); } diff --git a/src/services/stats.ts b/src/services/stats.ts index 419a80013..f9acc8d94 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -24,7 +24,7 @@ type ArrayValue = { type Span = 'day' | 'hour'; //#region Chart Core -type ChartDocument = { +type Log = { _id: mongo.ObjectID; /** @@ -56,12 +56,12 @@ type ChartDocument = { /** * 様々なチャートの管理を司るクラス */ -abstract class Chart { - protected collection: ICollection>; - protected abstract async generateTemplate(initial: boolean, latestStats?: T): Promise; +abstract class Stats { + protected collection: ICollection>; + protected abstract async generateTemplate(init: boolean, latestLog?: T): Promise; constructor(name: string) { - this.collection = db.get>(`stats.${name}`); + this.collection = db.get>(`stats.${name}`); this.collection.createIndex({ span: -1, date: -1 }, { unique: true }); this.collection.createIndex('group'); } @@ -87,7 +87,7 @@ abstract class Chart { } @autobind - private async getCurrentStats(span: Span, group?: Obj): Promise> { + private async getCurrentLog(span: Span, group?: Obj): Promise> { const now = new Date(); const y = now.getFullYear(); const m = now.getMonth(); @@ -100,14 +100,14 @@ abstract class Chart { null; // 現在(今日または今のHour)の統計 - const currentStats = await this.collection.findOne({ + const currentLog = await this.collection.findOne({ group: group, span: span, date: current }); - if (currentStats) { - return currentStats; + if (currentLog) { + return currentLog; } // 集計期間が変わってから、初めてのチャート更新なら @@ -116,7 +116,7 @@ abstract class Chart { // * 昨日何もチャートを更新するような出来事がなかった場合は、 // * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、 // * 「昨日の」と決め打ちせずに「もっとも最近の」とします - const latestStats = await this.collection.findOne({ + const latestLog = await this.collection.findOne({ group: group, span: span }, { @@ -125,18 +125,18 @@ abstract class Chart { } }); - if (latestStats) { + if (latestLog) { // 現在の統計を初期挿入 - const data = await this.generateTemplate(false, latestStats.data); + const data = await this.generateTemplate(false, latestLog.data); - const stats = await this.collection.insert({ + const log = await this.collection.insert({ group: group, span: span, date: current, data: data }); - return stats; + return log; } else { // 統計が存在しなかったら // * Misskeyインスタンスを建てて初めてのチャート更新時など @@ -144,26 +144,26 @@ abstract class Chart { // 空の統計を作成 const data = await this.generateTemplate(true); - const stats = await this.collection.insert({ + const log = await this.collection.insert({ group: group, span: span, date: current, data: data }); - return stats; + return log; } } @autobind protected commit(query: Obj, group?: Obj, uniqueKey?: string, uniqueValue?: string): void { - const update = (stats: ChartDocument) => { + const update = (log: Log) => { // ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く if ( uniqueKey && - stats.unique && - stats.unique[uniqueKey] && - stats.unique[uniqueKey].includes(uniqueValue) + log.unique && + log.unique[uniqueKey] && + log.unique[uniqueKey].includes(uniqueValue) ) return; // ユニークインクリメントの指定のキーに値を追加 @@ -174,12 +174,12 @@ abstract class Chart { } this.collection.update({ - _id: stats._id + _id: log._id }, query); }; - this.getCurrentStats('day', group).then(stats => update(stats)); - this.getCurrentStats('hour', group).then(stats => update(stats)); + this.getCurrentLog('day', group).then(log => update(log)); + this.getCurrentLog('hour', group).then(log => update(log)); } @autobind @@ -197,7 +197,7 @@ abstract class Chart { } @autobind - public async getStats(span: Span, range: number, group?: Obj): Promise> { + public async getChart(span: Span, range: number, group?: Obj): Promise> { const promisedChart: Promise[] = []; const now = new Date(); @@ -210,7 +210,7 @@ abstract class Chart { span == 'day' ? new Date(y, m, d - range) : span == 'hour' ? new Date(y, m, d, h - range) : null; - const stats = await this.collection.find({ + const logs = await this.collection.find({ group: group, span: span, date: { @@ -231,12 +231,12 @@ abstract class Chart { span == 'hour' ? new Date(y, m, d, h - i) : null; - const stat = stats.find(s => s.date.getTime() == current.getTime()); + const log = logs.find(l => l.date.getTime() == current.getTime()); - if (stat) { - promisedChart.unshift(Promise.resolve(stat.data)); + if (log) { + promisedChart.unshift(Promise.resolve(log.data)); } else { // 隙間埋め - const latest = stats.find(s => s.date.getTime() < current.getTime()); + const latest = logs.find(l => l.date.getTime() < current.getTime()); promisedChart.unshift(this.generateTemplate(false, latest ? latest.data : null)); } } @@ -288,7 +288,7 @@ abstract class Chart { /** * ユーザーに関する統計 */ -type UsersStats = { +type UsersLog = { local: { /** * 集計期間時点での、全ユーザー数 (ローカル) @@ -324,19 +324,19 @@ type UsersStats = { }; }; -class UsersChart extends Chart { +class UsersStats extends Stats { constructor() { super('users'); } @autobind - protected async generateTemplate(initial: boolean, latestStats?: UsersStats): Promise { - const [localCount, remoteCount] = initial ? await Promise.all([ + protected async generateTemplate(init: boolean, latestLog?: UsersLog): Promise { + const [localCount, remoteCount] = init ? await Promise.all([ User.count({ host: null }), User.count({ host: { $ne: null } }) ]) : [ - latestStats ? latestStats.local.total : 0, - latestStats ? latestStats.remote.total : 0 + latestLog ? latestLog.local.total : 0, + latestLog ? latestLog.remote.total : 0 ]; return { @@ -370,14 +370,14 @@ class UsersChart extends Chart { } } -export const usersChart = new UsersChart(); +export const usersStats = new UsersStats(); //#endregion //#region Notes stats /** * 投稿に関する統計 */ -type NotesStats = { +type NotesLog = { local: { /** * 集計期間時点での、全投稿数 (ローカル) @@ -447,19 +447,19 @@ type NotesStats = { }; }; -class NotesChart extends Chart { +class NotesStats extends Stats { constructor() { super('notes'); } @autobind - protected async generateTemplate(initial: boolean, latestStats?: NotesStats): Promise { - const [localCount, remoteCount] = initial ? await Promise.all([ + protected async generateTemplate(init: boolean, latestLog?: NotesLog): Promise { + const [localCount, remoteCount] = init ? await Promise.all([ Note.count({ '_user.host': null }), Note.count({ '_user.host': { $ne: null } }) ]) : [ - latestStats ? latestStats.local.total : 0, - latestStats ? latestStats.remote.total : 0 + latestLog ? latestLog.local.total : 0, + latestLog ? latestLog.remote.total : 0 ]; return { @@ -514,14 +514,14 @@ class NotesChart extends Chart { } } -export const notesChart = new NotesChart(); +export const notesStats = new NotesStats(); //#endregion //#region Drive stats /** * ドライブに関する統計 */ -type DriveStats = { +type DriveLog = { local: { /** * 集計期間時点での、全ドライブファイル数 (ローカル) @@ -587,13 +587,13 @@ type DriveStats = { }; }; -class DriveChart extends Chart { +class DriveStats extends Stats { constructor() { super('drive'); } @autobind - protected async generateTemplate(initial: boolean, latestStats?: DriveStats): Promise { + protected async generateTemplate(init: boolean, latestLog?: DriveLog): Promise { const calcSize = (local: boolean) => DriveFile .aggregate([{ $match: { @@ -612,16 +612,16 @@ class DriveChart extends Chart { }]) .then(res => res.length > 0 ? res[0].usage : 0); - const [localCount, remoteCount, localSize, remoteSize] = initial ? await Promise.all([ + const [localCount, remoteCount, localSize, remoteSize] = init ? await Promise.all([ DriveFile.count({ 'metadata._user.host': null }), DriveFile.count({ 'metadata._user.host': { $ne: null } }), calcSize(true), calcSize(false) ]) : [ - latestStats ? latestStats.local.totalCount : 0, - latestStats ? latestStats.remote.totalCount : 0, - latestStats ? latestStats.local.totalSize : 0, - latestStats ? latestStats.remote.totalSize : 0 + latestLog ? latestLog.local.totalCount : 0, + latestLog ? latestLog.remote.totalCount : 0, + latestLog ? latestLog.local.totalSize : 0, + latestLog ? latestLog.remote.totalSize : 0 ]; return { @@ -664,14 +664,14 @@ class DriveChart extends Chart { } } -export const driveChart = new DriveChart(); +export const driveStats = new DriveStats(); //#endregion //#region Network stats /** * ネットワークに関する統計 */ -type NetworkStats = { +type NetworkLog = { /** * 受信したリクエスト数 */ @@ -699,13 +699,13 @@ type NetworkStats = { outgoingBytes: number; }; -class NetworkChart extends Chart { +class NetworkStats extends Stats { constructor() { super('network'); } @autobind - protected async generateTemplate(initial: boolean, latestStats?: NetworkStats): Promise { + protected async generateTemplate(init: boolean, latestLog?: NetworkLog): Promise { return { incomingRequests: 0, outgoingRequests: 0, @@ -717,7 +717,7 @@ class NetworkChart extends Chart { @autobind public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) { - const inc: Partial = { + const inc: Partial = { incomingRequests: incomingRequests, totalTime: time, incomingBytes: incomingBytes, @@ -728,5 +728,5 @@ class NetworkChart extends Chart { } } -export const networkChart = new NetworkChart(); +export const networkStats = new NetworkStats(); //#endregion