diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2021-12-14 18:12:37 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2021-12-14 18:12:37 +0900 |
| commit | 0be4e1046239f1ed62bb73d4df51a7b9eb1a135f (patch) | |
| tree | 5e0c0d37036490e25073e14e2f9520d257eaa6d4 /packages/backend/src/services/chart/charts/per-user-notes.ts | |
| parent | New Crowdin updates (#8033) (diff) | |
| download | misskey-0be4e1046239f1ed62bb73d4df51a7b9eb1a135f.tar.gz misskey-0be4e1046239f1ed62bb73d4df51a7b9eb1a135f.tar.bz2 misskey-0be4e1046239f1ed62bb73d4df51a7b9eb1a135f.zip | |
enhance(backend): improve chart engine
Diffstat (limited to 'packages/backend/src/services/chart/charts/per-user-notes.ts')
| -rw-r--r-- | packages/backend/src/services/chart/charts/per-user-notes.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/packages/backend/src/services/chart/charts/per-user-notes.ts b/packages/backend/src/services/chart/charts/per-user-notes.ts new file mode 100644 index 0000000000..d048c88885 --- /dev/null +++ b/packages/backend/src/services/chart/charts/per-user-notes.ts @@ -0,0 +1,76 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { User } from '@/models/entities/user'; +import { SchemaType } from '@/misc/schema'; +import { Notes } from '@/models/index'; +import { Note } from '@/models/entities/note'; +import { name, schema } from './entities/per-user-notes'; + +type PerUserNotesLog = SchemaType<typeof schema>; + +/** + * ユーザーごとのノートに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class PerUserNotesChart extends Chart<PerUserNotesLog> { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: PerUserNotesLog): DeepPartial<PerUserNotesLog> { + return { + total: latest.total, + }; + } + + @autobind + protected aggregate(logs: PerUserNotesLog[]): PerUserNotesLog { + return { + total: logs[0].total, + inc: logs.reduce((a, b) => a + b.inc, 0), + dec: logs.reduce((a, b) => a + b.dec, 0), + diffs: { + reply: logs.reduce((a, b) => a + b.diffs.reply, 0), + renote: logs.reduce((a, b) => a + b.diffs.renote, 0), + normal: logs.reduce((a, b) => a + b.diffs.normal, 0), + }, + }; + } + + @autobind + protected async fetchActual(group: string): Promise<DeepPartial<PerUserNotesLog>> { + const [count] = await Promise.all([ + Notes.count({ userId: group }), + ]); + + return { + total: count, + }; + } + + @autobind + public async update(user: { id: User['id'] }, note: Note, isAdditional: boolean): Promise<void> { + 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); + } +} |