diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-08-19 00:55:07 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-19 00:55:07 +0900 |
| commit | 0e45d0d47fca2f9cf2caf87a25442d3090bea2fb (patch) | |
| tree | 4591218074ce73b4f5d3be7582d76a170dc1b672 /src/services | |
| parent | New translations ja.yml (Japanese (Kansai-ben)) (diff) | |
| parent | Merge pull request #2330 from syuilo/patch (diff) | |
| download | misskey-0e45d0d47fca2f9cf2caf87a25442d3090bea2fb.tar.gz misskey-0e45d0d47fca2f9cf2caf87a25442d3090bea2fb.tar.bz2 misskey-0e45d0d47fca2f9cf2caf87a25442d3090bea2fb.zip | |
Merge branch 'master' into l10n_master
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/drive/add-file.ts | 4 | ||||
| -rw-r--r-- | src/services/drive/delete-file.ts | 4 | ||||
| -rw-r--r-- | src/services/note/create.ts | 4 | ||||
| -rw-r--r-- | src/services/note/delete.ts | 4 | ||||
| -rw-r--r-- | src/services/update-chart.ts | 223 |
5 files changed, 238 insertions, 1 deletions
diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index da0d3fd82f..b090d56cee 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -17,6 +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 { updateDriveStats } from '../update-chart'; const log = debug('misskey:drive:add-file'); @@ -377,7 +378,8 @@ export default async function( publishDriveStream(user._id, 'file_created', packedFile); }); - // TODO: サムネイル生成 + // 統計を更新 + updateDriveStats(driveFile, true); return driveFile; } diff --git a/src/services/drive/delete-file.ts b/src/services/drive/delete-file.ts index 445d231d66..73532a2953 100644 --- a/src/services/drive/delete-file.ts +++ b/src/services/drive/delete-file.ts @@ -2,6 +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 { updateDriveStats } from '../update-chart'; export default async function(file: IDriveFile, isExpired = false) { if (file.metadata.storage == 'minio') { @@ -45,4 +46,7 @@ export default async function(file: IDriveFile, isExpired = false) { await DriveFileThumbnail.remove({ _id: thumbnail._id }); } //#endregion + + // 統計を更新 + updateDriveStats(file, false); } diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 521750dc84..d8f0f57b63 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -23,6 +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 { updateNoteStats } from '../update-chart'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; @@ -142,6 +143,9 @@ export default async (user: IUser, data: Option, silent = false) => new Promise< return; } + // 統計を更新 + updateNoteStats(note, true); + // ハッシュタグ登録 tags.map(tag => registerHashtag(user, tag)); diff --git a/src/services/note/delete.ts b/src/services/note/delete.ts index 7f245958b0..d444b13a8b 100644 --- a/src/services/note/delete.ts +++ b/src/services/note/delete.ts @@ -6,6 +6,7 @@ import pack from '../../remote/activitypub/renderer'; import { deliver } from '../../queue'; import Following from '../../models/following'; import renderNote from '../../remote/activitypub/renderer/note'; +import { updateNoteStats } from '../update-chart'; /** * 投稿を削除します。 @@ -43,4 +44,7 @@ export default async function(user: IUser, note: INote) { }); } //#endregion + + // 統計を更新 + updateNoteStats(note, false); } diff --git a/src/services/update-chart.ts b/src/services/update-chart.ts new file mode 100644 index 0000000000..7998baca9d --- /dev/null +++ b/src/services/update-chart.ts @@ -0,0 +1,223 @@ +import { INote } from '../models/note'; +import Stats, { IStats } from '../models/stats'; +import { isLocalUser, IUser } from '../models/user'; +import { IDriveFile } from '../models/drive-file'; + +type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; + +async function getTodayStats(): Promise<IStats> { + const now = new Date(); + const y = now.getFullYear(); + const m = now.getMonth(); + const d = now.getDate(); + const today = new Date(y, m, d); + + // 今日の統計 + const todayStats = await Stats.findOne({ + date: today + }); + + // 日付が変わってから、初めてのチャート更新なら + if (todayStats == null) { + // 最も最近の統計を持ってくる + // * 昨日何もチャートを更新するような出来事がなかった場合は、 + // 統計がそもそも作られずドキュメントが存在しないということがあり得るため、 + // 「昨日の」と決め打ちせずに「もっとも最近の」とします + const mostRecentStats = await Stats.findOne({}, { + sort: { + date: -1 + } + }); + + // 統計が存在しなかったら + // * Misskeyインスタンスを建てて初めてのチャート更新時など + if (mostRecentStats == null) { + // 空の統計を作成 + const chart: Omit<IStats, '_id'> = { + date: today, + users: { + local: { + total: 0, + diff: 0 + }, + remote: { + total: 0, + diff: 0 + } + }, + notes: { + local: { + total: 0, + diff: 0, + diffs: { + normal: 0, + reply: 0, + renote: 0 + } + }, + remote: { + total: 0, + diff: 0, + diffs: { + normal: 0, + reply: 0, + renote: 0 + } + } + }, + drive: { + local: { + totalCount: 0, + totalSize: 0, + diffCount: 0, + diffSize: 0 + }, + remote: { + totalCount: 0, + totalSize: 0, + diffCount: 0, + diffSize: 0 + } + } + }; + + const stats = await Stats.insert(chart); + + return stats; + } else { + // 今日の統計を初期挿入 + const chart: Omit<IStats, '_id'> = { + date: today, + users: { + local: { + total: mostRecentStats.users.local.total, + diff: 0 + }, + remote: { + total: mostRecentStats.users.remote.total, + diff: 0 + } + }, + notes: { + local: { + total: mostRecentStats.notes.local.total, + diff: 0, + diffs: { + normal: 0, + reply: 0, + renote: 0 + } + }, + remote: { + total: mostRecentStats.notes.remote.total, + diff: 0, + diffs: { + normal: 0, + reply: 0, + renote: 0 + } + } + }, + drive: { + local: { + totalCount: mostRecentStats.drive.local.totalCount, + totalSize: mostRecentStats.drive.local.totalSize, + diffCount: 0, + diffSize: 0 + }, + remote: { + totalCount: mostRecentStats.drive.remote.totalCount, + totalSize: mostRecentStats.drive.remote.totalSize, + diffCount: 0, + diffSize: 0 + } + } + }; + + const stats = await Stats.insert(chart); + + return stats; + } + } else { + return todayStats; + } +} + +async function update(inc: any) { + const stats = await getTodayStats(); + + await Stats.findOneAndUpdate({ + _id: stats._id + }, { + $inc: inc + }); +} + +export async function updateUserStats(user: IUser, isAdditional: boolean) { + const inc = {} as any; + + const amount = isAdditional ? 1 : -1; + + if (isLocalUser(user)) { + inc['users.local.total'] = amount; + inc['users.local.diff'] = amount; + } else { + inc['users.remote.total'] = amount; + inc['users.remote.diff'] = amount; + } + + await update(inc); +} + +export async function updateNoteStats(note: INote, isAdditional: boolean) { + const inc = {} as any; + + const amount = isAdditional ? 1 : -1; + + if (isLocalUser(note._user)) { + inc['notes.local.total'] = amount; + inc['notes.local.diff'] = amount; + + if (note.replyId != null) { + inc['notes.local.diffs.reply'] = amount; + } else if (note.renoteId != null) { + inc['notes.local.diffs.renote'] = amount; + } else { + inc['notes.local.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); +} + +export async function updateDriveStats(file: IDriveFile, isAdditional: boolean) { + const inc = {} as any; + + const amount = isAdditional ? 1 : -1; + const size = isAdditional ? file.length : -file.length; + + if (isLocalUser(file.metadata._user)) { + inc['drive.local.totalCount'] = amount; + inc['drive.local.diffCount'] = amount; + inc['drive.local.totalSize'] = size; + inc['drive.local.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); +} |