From 4c180869c68e313531664e2e0bfbfb445ec27d4d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 21 Oct 2018 16:54:07 +0900 Subject: [PATCH] Imprement hashtag stats --- src/services/register-hashtag.ts | 3 +++ src/services/stats.ts | 46 ++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/services/register-hashtag.ts b/src/services/register-hashtag.ts index ca6b74783b..58222c1f49 100644 --- a/src/services/register-hashtag.ts +++ b/src/services/register-hashtag.ts @@ -1,5 +1,6 @@ import { IUser } from '../models/user'; import Hashtag from '../models/hashtag'; +import { hashtagStats } from './stats'; export default async function(user: IUser, tag: string) { tag = tag.toLowerCase(); @@ -25,4 +26,6 @@ export default async function(user: IUser, tag: string) { mentionedUserIdsCount: 1 }); } + + hashtagStats.update(tag, user._id); } diff --git a/src/services/stats.ts b/src/services/stats.ts index f9acc8d94c..d64335e11e 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -87,7 +87,7 @@ abstract class Stats { } @autobind - private async getCurrentLog(span: Span, group?: Obj): Promise> { + private async getCurrentLog(span: Span, group?: any): Promise> { const now = new Date(); const y = now.getFullYear(); const m = now.getMonth(); @@ -156,7 +156,7 @@ abstract class Stats { } @autobind - protected commit(query: Obj, group?: Obj, uniqueKey?: string, uniqueValue?: string): void { + protected commit(query: Obj, group?: any, uniqueKey?: string, uniqueValue?: string): void { const update = (log: Log) => { // ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く if ( @@ -183,21 +183,21 @@ abstract class Stats { } @autobind - protected inc(inc: Partial, group?: Obj): void { + protected inc(inc: Partial, group?: any): void { this.commit({ $inc: this.convertQuery(inc, 'data') }, group); } @autobind - protected incIfUnique(inc: Partial, key: string, value: string, group?: Obj): void { + protected incIfUnique(inc: Partial, key: string, value: string, group?: any): void { this.commit({ $inc: this.convertQuery(inc, 'data') }, group, key, value); } @autobind - public async getChart(span: Span, range: number, group?: Obj): Promise> { + public async getChart(span: Span, range: number, group?: any): Promise> { const promisedChart: Promise[] = []; const now = new Date(); @@ -730,3 +730,39 @@ class NetworkStats extends Stats { export const networkStats = new NetworkStats(); //#endregion + +//#region Hashtag stats +/** + * ハッシュタグに関する統計 + */ +type HashtagLog = { + /** + * 投稿された数 + */ + count: number; +}; + +class HashtagStats extends Stats { + constructor() { + super('hashtag'); + } + + @autobind + protected async generateTemplate(init: boolean, latestLog?: HashtagLog): Promise { + return { + count: 0 + }; + } + + @autobind + public async update(hashtag: string, userId: mongo.ObjectId) { + const inc: Partial = { + count: 1 + }; + + await this.incIfUnique(inc, 'users', userId.toHexString(), hashtag); + } +} + +export const hashtagStats = new HashtagStats(); +//#endregion