From b8c56c4ddab9d536b4f3c668ee0591815273982c Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 22 Oct 2018 04:30:27 +0900 Subject: [PATCH] Implemet per user notes stats --- src/services/note/create.ts | 3 +- src/services/note/delete.ts | 3 +- src/services/stats.ts | 92 +++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 298c499d22..ef0c783d16 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 { notesStats } from '../stats'; +import { notesStats, perUserNotesStats } from '../stats'; import { erase, unique } from '../../prelude/array'; import insertNoteUnread from './unread'; @@ -166,6 +166,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise< // 統計を更新 notesStats.update(note, true); + perUserNotesStats.update(user, note, true); // ハッシュタグ登録 tags.map(tag => registerHashtag(user, tag)); diff --git a/src/services/note/delete.ts b/src/services/note/delete.ts index 26a1d5f6b0..fd23c10b8e 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 { notesStats } from '../stats'; +import { notesStats, perUserNotesStats } from '../stats'; import config from '../../config'; import NoteUnread from '../../models/note-unread'; import read from './read'; @@ -64,4 +64,5 @@ export default async function(user: IUser, note: INote) { // 統計を更新 notesStats.update(note, false); + perUserNotesStats.update(user, note, false); } diff --git a/src/services/stats.ts b/src/services/stats.ts index 935b16d6f5..121a331209 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -819,3 +819,95 @@ class FollowingStats extends Stats { export const followingStats = new FollowingStats(); //#endregion + +//#region Per user notes stats +/** + * ユーザーごとの投稿に関する統計 + */ +type PerUserNotesLog = { + /** + * 集計期間時点での、全投稿数 + */ + total: number; + + /** + * 増加した投稿数 + */ + inc: number; + + /** + * 減少した投稿数 + */ + dec: number; + + diffs: { + /** + * 通常の投稿数の差分 + */ + normal: number; + + /** + * リプライの投稿数の差分 + */ + reply: number; + + /** + * Renoteの投稿数の差分 + */ + renote: number; + }; +}; + +class PerUserNotesStats extends Stats { + constructor() { + super('notes'); + } + + @autobind + protected async getTemplate(init: boolean, latest?: PerUserNotesLog, group?: any): Promise { + const [count] = init ? await Promise.all([ + Note.count({ userId: group, deletedAt: null }), + ]) : [ + latest ? latest.total : 0 + ]; + + return { + total: count, + inc: 0, + dec: 0, + diffs: { + normal: 0, + reply: 0, + renote: 0 + } + }; + } + + @autobind + public async update(user: IUser, note: INote, isAdditional: boolean) { + const update: Obj = { + diffs: {} + }; + + update.total = isAdditional ? 1 : -1; + + if (isAdditional) { + update.inc = 1; + } else { + update.dec = 1; + } + + if (note.replyId != null) { + update.diffs.reply = isAdditional ? 1 : -1; + } else if (note.renoteId != null) { + update.diffs.renote = isAdditional ? 1 : -1; + } else { + update.diffs.normal = isAdditional ? 1 : -1; + } + + await this.inc(update, user._id); + } +} + +export const perUserNotesStats = new PerUserNotesStats(); +//#endregion