From 0be4e1046239f1ed62bb73d4df51a7b9eb1a135f Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 14 Dec 2021 18:12:37 +0900 Subject: enhance(backend): improve chart engine --- .../src/services/chart/charts/active-users.ts | 51 ++++ .../services/chart/charts/classes/active-users.ts | 47 ---- .../src/services/chart/charts/classes/drive.ts | 91 ------- .../services/chart/charts/classes/federation.ts | 62 ----- .../src/services/chart/charts/classes/hashtag.ts | 47 ---- .../src/services/chart/charts/classes/instance.ts | 217 ----------------- .../src/services/chart/charts/classes/network.ts | 45 ---- .../src/services/chart/charts/classes/notes.ts | 97 -------- .../chart/charts/classes/per-user-drive.ts | 64 ----- .../chart/charts/classes/per-user-following.ts | 121 ---------- .../chart/charts/classes/per-user-notes.ts | 72 ------ .../chart/charts/classes/per-user-reactions.ts | 44 ---- .../services/chart/charts/classes/test-grouped.ts | 58 ----- .../services/chart/charts/classes/test-unique.ts | 36 --- .../src/services/chart/charts/classes/test.ts | 69 ------ .../src/services/chart/charts/classes/users.ts | 76 ------ .../backend/src/services/chart/charts/drive.ts | 95 ++++++++ .../services/chart/charts/entities/active-users.ts | 36 +++ .../src/services/chart/charts/entities/drive.ts | 72 ++++++ .../services/chart/charts/entities/federation.ts | 30 +++ .../src/services/chart/charts/entities/hashtag.ts | 36 +++ .../src/services/chart/charts/entities/instance.ts | 158 ++++++++++++ .../src/services/chart/charts/entities/network.ts | 32 +++ .../src/services/chart/charts/entities/notes.ts | 60 +++++ .../chart/charts/entities/per-user-drive.ts | 59 +++++ .../chart/charts/entities/per-user-following.ts | 90 +++++++ .../chart/charts/entities/per-user-notes.ts | 47 ++++ .../chart/charts/entities/per-user-reactions.ts | 32 +++ .../services/chart/charts/entities/test-grouped.ts | 32 +++ .../services/chart/charts/entities/test-unique.ts | 20 ++ .../src/services/chart/charts/entities/test.ts | 32 +++ .../src/services/chart/charts/entities/users.ts | 48 ++++ .../src/services/chart/charts/federation.ts | 66 +++++ .../backend/src/services/chart/charts/hashtag.ts | 51 ++++ .../backend/src/services/chart/charts/instance.ts | 221 +++++++++++++++++ .../backend/src/services/chart/charts/network.ts | 49 ++++ .../backend/src/services/chart/charts/notes.ts | 101 ++++++++ .../src/services/chart/charts/per-user-drive.ts | 68 ++++++ .../services/chart/charts/per-user-following.ts | 125 ++++++++++ .../src/services/chart/charts/per-user-notes.ts | 76 ++++++ .../services/chart/charts/per-user-reactions.ts | 48 ++++ .../services/chart/charts/schemas/active-users.ts | 35 --- .../src/services/chart/charts/schemas/drive.ts | 68 ------ .../services/chart/charts/schemas/federation.ts | 29 --- .../src/services/chart/charts/schemas/hashtag.ts | 35 --- .../src/services/chart/charts/schemas/instance.ts | 157 ------------ .../src/services/chart/charts/schemas/network.ts | 31 --- .../src/services/chart/charts/schemas/notes.ts | 56 ----- .../chart/charts/schemas/per-user-drive.ts | 55 ----- .../chart/charts/schemas/per-user-following.ts | 86 ------- .../chart/charts/schemas/per-user-notes.ts | 43 ---- .../chart/charts/schemas/per-user-reactions.ts | 31 --- .../services/chart/charts/schemas/test-grouped.ts | 28 --- .../services/chart/charts/schemas/test-unique.ts | 16 -- .../src/services/chart/charts/schemas/test.ts | 28 --- .../src/services/chart/charts/schemas/users.ts | 44 ---- .../src/services/chart/charts/test-grouped.ts | 62 +++++ .../src/services/chart/charts/test-unique.ts | 40 ++++ packages/backend/src/services/chart/charts/test.ts | 73 ++++++ .../backend/src/services/chart/charts/users.ts | 80 +++++++ packages/backend/src/services/chart/core.ts | 265 ++++++++++++--------- packages/backend/src/services/chart/entities.ts | 40 ++-- packages/backend/src/services/chart/index.ts | 25 +- 63 files changed, 2175 insertions(+), 2033 deletions(-) create mode 100644 packages/backend/src/services/chart/charts/active-users.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/active-users.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/drive.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/federation.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/hashtag.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/instance.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/network.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/notes.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/per-user-drive.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/per-user-following.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/per-user-notes.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/per-user-reactions.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/test-grouped.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/test-unique.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/test.ts delete mode 100644 packages/backend/src/services/chart/charts/classes/users.ts create mode 100644 packages/backend/src/services/chart/charts/drive.ts create mode 100644 packages/backend/src/services/chart/charts/entities/active-users.ts create mode 100644 packages/backend/src/services/chart/charts/entities/drive.ts create mode 100644 packages/backend/src/services/chart/charts/entities/federation.ts create mode 100644 packages/backend/src/services/chart/charts/entities/hashtag.ts create mode 100644 packages/backend/src/services/chart/charts/entities/instance.ts create mode 100644 packages/backend/src/services/chart/charts/entities/network.ts create mode 100644 packages/backend/src/services/chart/charts/entities/notes.ts create mode 100644 packages/backend/src/services/chart/charts/entities/per-user-drive.ts create mode 100644 packages/backend/src/services/chart/charts/entities/per-user-following.ts create mode 100644 packages/backend/src/services/chart/charts/entities/per-user-notes.ts create mode 100644 packages/backend/src/services/chart/charts/entities/per-user-reactions.ts create mode 100644 packages/backend/src/services/chart/charts/entities/test-grouped.ts create mode 100644 packages/backend/src/services/chart/charts/entities/test-unique.ts create mode 100644 packages/backend/src/services/chart/charts/entities/test.ts create mode 100644 packages/backend/src/services/chart/charts/entities/users.ts create mode 100644 packages/backend/src/services/chart/charts/federation.ts create mode 100644 packages/backend/src/services/chart/charts/hashtag.ts create mode 100644 packages/backend/src/services/chart/charts/instance.ts create mode 100644 packages/backend/src/services/chart/charts/network.ts create mode 100644 packages/backend/src/services/chart/charts/notes.ts create mode 100644 packages/backend/src/services/chart/charts/per-user-drive.ts create mode 100644 packages/backend/src/services/chart/charts/per-user-following.ts create mode 100644 packages/backend/src/services/chart/charts/per-user-notes.ts create mode 100644 packages/backend/src/services/chart/charts/per-user-reactions.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/active-users.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/drive.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/federation.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/hashtag.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/instance.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/network.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/notes.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/per-user-drive.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/per-user-following.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/per-user-notes.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/per-user-reactions.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/test-grouped.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/test-unique.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/test.ts delete mode 100644 packages/backend/src/services/chart/charts/schemas/users.ts create mode 100644 packages/backend/src/services/chart/charts/test-grouped.ts create mode 100644 packages/backend/src/services/chart/charts/test-unique.ts create mode 100644 packages/backend/src/services/chart/charts/test.ts create mode 100644 packages/backend/src/services/chart/charts/users.ts (limited to 'packages/backend/src/services/chart') diff --git a/packages/backend/src/services/chart/charts/active-users.ts b/packages/backend/src/services/chart/charts/active-users.ts new file mode 100644 index 0000000000..9490101e36 --- /dev/null +++ b/packages/backend/src/services/chart/charts/active-users.ts @@ -0,0 +1,51 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { User } from '@/models/entities/user'; +import { SchemaType } from '@/misc/schema'; +import { Users } from '@/models/index'; +import { name, schema } from './entities/active-users'; + +type ActiveUsersLog = SchemaType; + +/** + * アクティブユーザーに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class ActiveUsersChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: ActiveUsersLog): DeepPartial { + return {}; + } + + @autobind + protected aggregate(logs: ActiveUsersLog[]): ActiveUsersLog { + return { + local: { + users: logs.reduce((a, b) => a.concat(b.local.users), [] as ActiveUsersLog['local']['users']), + }, + remote: { + users: logs.reduce((a, b) => a.concat(b.remote.users), [] as ActiveUsersLog['remote']['users']), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + return {}; + } + + @autobind + public async update(user: { id: User['id'], host: User['host'] }): Promise { + const update: Obj = { + users: [user.id], + }; + + await this.inc({ + [Users.isLocalUser(user) ? 'local' : 'remote']: update, + }); + } +} diff --git a/packages/backend/src/services/chart/charts/classes/active-users.ts b/packages/backend/src/services/chart/charts/classes/active-users.ts deleted file mode 100644 index 029e93a391..0000000000 --- a/packages/backend/src/services/chart/charts/classes/active-users.ts +++ /dev/null @@ -1,47 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { User } from '@/models/entities/user'; -import { SchemaType } from '@/misc/schema'; -import { Users } from '@/models/index'; -import { name, schema } from '../schemas/active-users'; - -type ActiveUsersLog = SchemaType; - -export default class ActiveUsersChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: ActiveUsersLog): DeepPartial { - return {}; - } - - @autobind - protected aggregate(logs: ActiveUsersLog[]): ActiveUsersLog { - return { - local: { - users: logs.reduce((a, b) => a.concat(b.local.users), [] as ActiveUsersLog['local']['users']), - }, - remote: { - users: logs.reduce((a, b) => a.concat(b.remote.users), [] as ActiveUsersLog['remote']['users']), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - return {}; - } - - @autobind - public async update(user: { id: User['id'], host: User['host'] }) { - const update: Obj = { - users: [user.id], - }; - - await this.inc({ - [Users.isLocalUser(user) ? 'local' : 'remote']: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/drive.ts b/packages/backend/src/services/chart/charts/classes/drive.ts deleted file mode 100644 index 2ed8e7fad1..0000000000 --- a/packages/backend/src/services/chart/charts/classes/drive.ts +++ /dev/null @@ -1,91 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { DriveFiles } from '@/models/index'; -import { Not, IsNull } from 'typeorm'; -import { DriveFile } from '@/models/entities/drive-file'; -import { name, schema } from '../schemas/drive'; - -type DriveLog = SchemaType; - -export default class DriveChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: DriveLog): DeepPartial { - return { - local: { - totalCount: latest.local.totalCount, - totalSize: latest.local.totalSize, - }, - remote: { - totalCount: latest.remote.totalCount, - totalSize: latest.remote.totalSize, - }, - }; - } - - @autobind - protected aggregate(logs: DriveLog[]): DriveLog { - return { - local: { - totalCount: logs[0].local.totalCount, - totalSize: logs[0].local.totalSize, - incCount: logs.reduce((a, b) => a + b.local.incCount, 0), - incSize: logs.reduce((a, b) => a + b.local.incSize, 0), - decCount: logs.reduce((a, b) => a + b.local.decCount, 0), - decSize: logs.reduce((a, b) => a + b.local.decSize, 0), - }, - remote: { - totalCount: logs[0].remote.totalCount, - totalSize: logs[0].remote.totalSize, - incCount: logs.reduce((a, b) => a + b.remote.incCount, 0), - incSize: logs.reduce((a, b) => a + b.remote.incSize, 0), - decCount: logs.reduce((a, b) => a + b.remote.decCount, 0), - decSize: logs.reduce((a, b) => a + b.remote.decSize, 0), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - const [localCount, remoteCount, localSize, remoteSize] = await Promise.all([ - DriveFiles.count({ userHost: null }), - DriveFiles.count({ userHost: Not(IsNull()) }), - DriveFiles.calcDriveUsageOfLocal(), - DriveFiles.calcDriveUsageOfRemote(), - ]); - - return { - local: { - totalCount: localCount, - totalSize: localSize, - }, - remote: { - totalCount: remoteCount, - totalSize: remoteSize, - }, - }; - } - - @autobind - public async update(file: DriveFile, isAdditional: boolean) { - const update: Obj = {}; - - update.totalCount = isAdditional ? 1 : -1; - update.totalSize = isAdditional ? file.size : -file.size; - if (isAdditional) { - update.incCount = 1; - update.incSize = file.size; - } else { - update.decCount = 1; - update.decSize = file.size; - } - - await this.inc({ - [file.userHost === null ? 'local' : 'remote']: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/federation.ts b/packages/backend/src/services/chart/charts/classes/federation.ts deleted file mode 100644 index 9f8b7ec0ae..0000000000 --- a/packages/backend/src/services/chart/charts/classes/federation.ts +++ /dev/null @@ -1,62 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { Instances } from '@/models/index'; -import { name, schema } from '../schemas/federation'; - -type FederationLog = SchemaType; - -export default class FederationChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: FederationLog): DeepPartial { - return { - instance: { - total: latest.instance.total, - }, - }; - } - - @autobind - protected aggregate(logs: FederationLog[]): FederationLog { - return { - instance: { - total: logs[0].instance.total, - inc: logs.reduce((a, b) => a + b.instance.inc, 0), - dec: logs.reduce((a, b) => a + b.instance.dec, 0), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - const [total] = await Promise.all([ - Instances.count({}), - ]); - - return { - instance: { - total: total, - }, - }; - } - - @autobind - public async update(isAdditional: boolean) { - const update: Obj = {}; - - update.total = isAdditional ? 1 : -1; - if (isAdditional) { - update.inc = 1; - } else { - update.dec = 1; - } - - await this.inc({ - instance: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/hashtag.ts b/packages/backend/src/services/chart/charts/classes/hashtag.ts deleted file mode 100644 index 53774e566d..0000000000 --- a/packages/backend/src/services/chart/charts/classes/hashtag.ts +++ /dev/null @@ -1,47 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { User } from '@/models/entities/user'; -import { SchemaType } from '@/misc/schema'; -import { Users } from '@/models/index'; -import { name, schema } from '../schemas/hashtag'; - -type HashtagLog = SchemaType; - -export default class HashtagChart extends Chart { - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: HashtagLog): DeepPartial { - return {}; - } - - @autobind - protected aggregate(logs: HashtagLog[]): HashtagLog { - return { - local: { - users: logs.reduce((a, b) => a.concat(b.local.users), [] as HashtagLog['local']['users']), - }, - remote: { - users: logs.reduce((a, b) => a.concat(b.remote.users), [] as HashtagLog['remote']['users']), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - return {}; - } - - @autobind - public async update(hashtag: string, user: { id: User['id'], host: User['host'] }) { - const update: Obj = { - users: [user.id], - }; - - await this.inc({ - [Users.isLocalUser(user) ? 'local' : 'remote']: update, - }, hashtag); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/instance.ts b/packages/backend/src/services/chart/charts/classes/instance.ts deleted file mode 100644 index 8e7902d163..0000000000 --- a/packages/backend/src/services/chart/charts/classes/instance.ts +++ /dev/null @@ -1,217 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { DriveFiles, Followings, Users, Notes } from '@/models/index'; -import { DriveFile } from '@/models/entities/drive-file'; -import { name, schema } from '../schemas/instance'; -import { Note } from '@/models/entities/note'; -import { toPuny } from '@/misc/convert-host'; - -type InstanceLog = SchemaType; - -export default class InstanceChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: InstanceLog): DeepPartial { - return { - notes: { - total: latest.notes.total, - }, - users: { - total: latest.users.total, - }, - following: { - total: latest.following.total, - }, - followers: { - total: latest.followers.total, - }, - drive: { - totalFiles: latest.drive.totalFiles, - totalUsage: latest.drive.totalUsage, - }, - }; - } - - @autobind - protected aggregate(logs: InstanceLog[]): InstanceLog { - return { - requests: { - failed: logs.reduce((a, b) => a + b.requests.failed, 0), - succeeded: logs.reduce((a, b) => a + b.requests.succeeded, 0), - received: logs.reduce((a, b) => a + b.requests.received, 0), - }, - notes: { - total: logs[0].notes.total, - inc: logs.reduce((a, b) => a + b.notes.inc, 0), - dec: logs.reduce((a, b) => a + b.notes.dec, 0), - diffs: { - reply: logs.reduce((a, b) => a + b.notes.diffs.reply, 0), - renote: logs.reduce((a, b) => a + b.notes.diffs.renote, 0), - normal: logs.reduce((a, b) => a + b.notes.diffs.normal, 0), - }, - }, - users: { - total: logs[0].users.total, - inc: logs.reduce((a, b) => a + b.users.inc, 0), - dec: logs.reduce((a, b) => a + b.users.dec, 0), - }, - following: { - total: logs[0].following.total, - inc: logs.reduce((a, b) => a + b.following.inc, 0), - dec: logs.reduce((a, b) => a + b.following.dec, 0), - }, - followers: { - total: logs[0].followers.total, - inc: logs.reduce((a, b) => a + b.followers.inc, 0), - dec: logs.reduce((a, b) => a + b.followers.dec, 0), - }, - drive: { - totalFiles: logs[0].drive.totalFiles, - totalUsage: logs[0].drive.totalUsage, - incFiles: logs.reduce((a, b) => a + b.drive.incFiles, 0), - incUsage: logs.reduce((a, b) => a + b.drive.incUsage, 0), - decFiles: logs.reduce((a, b) => a + b.drive.decFiles, 0), - decUsage: logs.reduce((a, b) => a + b.drive.decUsage, 0), - }, - }; - } - - @autobind - protected async fetchActual(group: string): Promise> { - const [ - notesCount, - usersCount, - followingCount, - followersCount, - driveFiles, - driveUsage, - ] = await Promise.all([ - Notes.count({ userHost: group }), - Users.count({ host: group }), - Followings.count({ followerHost: group }), - Followings.count({ followeeHost: group }), - DriveFiles.count({ userHost: group }), - DriveFiles.calcDriveUsageOfHost(group), - ]); - - return { - notes: { - total: notesCount, - }, - users: { - total: usersCount, - }, - following: { - total: followingCount, - }, - followers: { - total: followersCount, - }, - drive: { - totalFiles: driveFiles, - totalUsage: driveUsage, - }, - }; - } - - @autobind - public async requestReceived(host: string) { - await this.inc({ - requests: { - received: 1, - }, - }, toPuny(host)); - } - - @autobind - public async requestSent(host: string, isSucceeded: boolean) { - const update: Obj = {}; - - if (isSucceeded) { - update.succeeded = 1; - } else { - update.failed = 1; - } - - await this.inc({ - requests: update, - }, toPuny(host)); - } - - @autobind - public async newUser(host: string) { - await this.inc({ - users: { - total: 1, - inc: 1, - }, - }, toPuny(host)); - } - - @autobind - public async updateNote(host: string, note: Note, isAdditional: boolean) { - const diffs = {} as any; - - if (note.replyId != null) { - diffs.reply = isAdditional ? 1 : -1; - } else if (note.renoteId != null) { - diffs.renote = isAdditional ? 1 : -1; - } else { - diffs.normal = isAdditional ? 1 : -1; - } - - await this.inc({ - notes: { - total: isAdditional ? 1 : -1, - inc: isAdditional ? 1 : 0, - dec: isAdditional ? 0 : 1, - diffs: diffs, - }, - }, toPuny(host)); - } - - @autobind - public async updateFollowing(host: string, isAdditional: boolean) { - await this.inc({ - following: { - total: isAdditional ? 1 : -1, - inc: isAdditional ? 1 : 0, - dec: isAdditional ? 0 : 1, - }, - }, toPuny(host)); - } - - @autobind - public async updateFollowers(host: string, isAdditional: boolean) { - await this.inc({ - followers: { - total: isAdditional ? 1 : -1, - inc: isAdditional ? 1 : 0, - dec: isAdditional ? 0 : 1, - }, - }, toPuny(host)); - } - - @autobind - public async updateDrive(file: DriveFile, isAdditional: boolean) { - const update: Obj = {}; - - update.totalFiles = isAdditional ? 1 : -1; - update.totalUsage = isAdditional ? file.size : -file.size; - if (isAdditional) { - update.incFiles = 1; - update.incUsage = file.size; - } else { - update.decFiles = 1; - update.decUsage = file.size; - } - - await this.inc({ - drive: update, - }, file.userHost); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/network.ts b/packages/backend/src/services/chart/charts/classes/network.ts deleted file mode 100644 index 838eec4916..0000000000 --- a/packages/backend/src/services/chart/charts/classes/network.ts +++ /dev/null @@ -1,45 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { name, schema } from '../schemas/network'; - -type NetworkLog = SchemaType; - -export default class NetworkChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: NetworkLog): DeepPartial { - return {}; - } - - @autobind - protected aggregate(logs: NetworkLog[]): NetworkLog { - return { - incomingRequests: logs.reduce((a, b) => a + b.incomingRequests, 0), - outgoingRequests: logs.reduce((a, b) => a + b.outgoingRequests, 0), - totalTime: logs.reduce((a, b) => a + b.totalTime, 0), - incomingBytes: logs.reduce((a, b) => a + b.incomingBytes, 0), - outgoingBytes: logs.reduce((a, b) => a + b.outgoingBytes, 0), - }; - } - - @autobind - protected async fetchActual(): Promise> { - return {}; - } - - @autobind - public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) { - const inc: DeepPartial = { - incomingRequests: incomingRequests, - totalTime: time, - incomingBytes: incomingBytes, - outgoingBytes: outgoingBytes, - }; - - await this.inc(inc); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/notes.ts b/packages/backend/src/services/chart/charts/classes/notes.ts deleted file mode 100644 index f8a580b3c0..0000000000 --- a/packages/backend/src/services/chart/charts/classes/notes.ts +++ /dev/null @@ -1,97 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { Notes } from '@/models/index'; -import { Not, IsNull } from 'typeorm'; -import { Note } from '@/models/entities/note'; -import { name, schema } from '../schemas/notes'; - -type NotesLog = SchemaType; - -export default class NotesChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: NotesLog): DeepPartial { - return { - local: { - total: latest.local.total, - }, - remote: { - total: latest.remote.total, - }, - }; - } - - @autobind - protected aggregate(logs: NotesLog[]): NotesLog { - return { - local: { - total: logs[0].local.total, - inc: logs.reduce((a, b) => a + b.local.inc, 0), - dec: logs.reduce((a, b) => a + b.local.dec, 0), - diffs: { - reply: logs.reduce((a, b) => a + b.local.diffs.reply, 0), - renote: logs.reduce((a, b) => a + b.local.diffs.renote, 0), - normal: logs.reduce((a, b) => a + b.local.diffs.normal, 0), - }, - }, - remote: { - total: logs[0].remote.total, - inc: logs.reduce((a, b) => a + b.remote.inc, 0), - dec: logs.reduce((a, b) => a + b.remote.dec, 0), - diffs: { - reply: logs.reduce((a, b) => a + b.remote.diffs.reply, 0), - renote: logs.reduce((a, b) => a + b.remote.diffs.renote, 0), - normal: logs.reduce((a, b) => a + b.remote.diffs.normal, 0), - }, - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - const [localCount, remoteCount] = await Promise.all([ - Notes.count({ userHost: null }), - Notes.count({ userHost: Not(IsNull()) }), - ]); - - return { - local: { - total: localCount, - }, - remote: { - total: remoteCount, - }, - }; - } - - @autobind - public async update(note: Note, 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({ - [note.userHost === null ? 'local' : 'remote']: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/per-user-drive.ts b/packages/backend/src/services/chart/charts/classes/per-user-drive.ts deleted file mode 100644 index 33d49d9ebb..0000000000 --- a/packages/backend/src/services/chart/charts/classes/per-user-drive.ts +++ /dev/null @@ -1,64 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { DriveFiles } from '@/models/index'; -import { DriveFile } from '@/models/entities/drive-file'; -import { name, schema } from '../schemas/per-user-drive'; - -type PerUserDriveLog = SchemaType; - -export default class PerUserDriveChart extends Chart { - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: PerUserDriveLog): DeepPartial { - return { - totalCount: latest.totalCount, - totalSize: latest.totalSize, - }; - } - - @autobind - protected aggregate(logs: PerUserDriveLog[]): PerUserDriveLog { - return { - totalCount: logs[0].totalCount, - totalSize: logs[0].totalSize, - incCount: logs.reduce((a, b) => a + b.incCount, 0), - incSize: logs.reduce((a, b) => a + b.incSize, 0), - decCount: logs.reduce((a, b) => a + b.decCount, 0), - decSize: logs.reduce((a, b) => a + b.decSize, 0), - }; - } - - @autobind - protected async fetchActual(group: string): Promise> { - const [count, size] = await Promise.all([ - DriveFiles.count({ userId: group }), - DriveFiles.calcDriveUsageOf(group), - ]); - - return { - totalCount: count, - totalSize: size, - }; - } - - @autobind - public async update(file: DriveFile, isAdditional: boolean) { - const update: Obj = {}; - - update.totalCount = isAdditional ? 1 : -1; - update.totalSize = isAdditional ? file.size : -file.size; - if (isAdditional) { - update.incCount = 1; - update.incSize = file.size; - } else { - update.decCount = 1; - update.decSize = file.size; - } - - await this.inc(update, file.userId); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/per-user-following.ts b/packages/backend/src/services/chart/charts/classes/per-user-following.ts deleted file mode 100644 index 67b623057d..0000000000 --- a/packages/backend/src/services/chart/charts/classes/per-user-following.ts +++ /dev/null @@ -1,121 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { Followings, Users } from '@/models/index'; -import { Not, IsNull } from 'typeorm'; -import { User } from '@/models/entities/user'; -import { name, schema } from '../schemas/per-user-following'; - -type PerUserFollowingLog = SchemaType; - -export default class PerUserFollowingChart extends Chart { - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: PerUserFollowingLog): DeepPartial { - return { - local: { - followings: { - total: latest.local.followings.total, - }, - followers: { - total: latest.local.followers.total, - }, - }, - remote: { - followings: { - total: latest.remote.followings.total, - }, - followers: { - total: latest.remote.followers.total, - }, - }, - }; - } - - @autobind - protected aggregate(logs: PerUserFollowingLog[]): PerUserFollowingLog { - return { - local: { - followings: { - total: logs[0].local.followings.total, - inc: logs.reduce((a, b) => a + b.local.followings.inc, 0), - dec: logs.reduce((a, b) => a + b.local.followings.dec, 0), - }, - followers: { - total: logs[0].local.followers.total, - inc: logs.reduce((a, b) => a + b.local.followers.inc, 0), - dec: logs.reduce((a, b) => a + b.local.followers.dec, 0), - }, - }, - remote: { - followings: { - total: logs[0].remote.followings.total, - inc: logs.reduce((a, b) => a + b.remote.followings.inc, 0), - dec: logs.reduce((a, b) => a + b.remote.followings.dec, 0), - }, - followers: { - total: logs[0].remote.followers.total, - inc: logs.reduce((a, b) => a + b.remote.followers.inc, 0), - dec: logs.reduce((a, b) => a + b.remote.followers.dec, 0), - }, - }, - }; - } - - @autobind - protected async fetchActual(group: string): Promise> { - const [ - localFollowingsCount, - localFollowersCount, - remoteFollowingsCount, - remoteFollowersCount, - ] = await Promise.all([ - Followings.count({ followerId: group, followeeHost: null }), - Followings.count({ followeeId: group, followerHost: null }), - Followings.count({ followerId: group, followeeHost: Not(IsNull()) }), - Followings.count({ followeeId: group, followerHost: Not(IsNull()) }), - ]); - - return { - local: { - followings: { - total: localFollowingsCount, - }, - followers: { - total: localFollowersCount, - }, - }, - remote: { - followings: { - total: remoteFollowingsCount, - }, - followers: { - total: remoteFollowersCount, - }, - }, - }; - } - - @autobind - public async update(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }, isFollow: boolean) { - const update: Obj = {}; - - update.total = isFollow ? 1 : -1; - - if (isFollow) { - update.inc = 1; - } else { - update.dec = 1; - } - - this.inc({ - [Users.isLocalUser(follower) ? 'local' : 'remote']: { followings: update }, - }, follower.id); - this.inc({ - [Users.isLocalUser(followee) ? 'local' : 'remote']: { followers: update }, - }, followee.id); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/per-user-notes.ts b/packages/backend/src/services/chart/charts/classes/per-user-notes.ts deleted file mode 100644 index 94760c0492..0000000000 --- a/packages/backend/src/services/chart/charts/classes/per-user-notes.ts +++ /dev/null @@ -1,72 +0,0 @@ -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 '../schemas/per-user-notes'; - -type PerUserNotesLog = SchemaType; - -export default class PerUserNotesChart extends Chart { - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: PerUserNotesLog): DeepPartial { - 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> { - const [count] = await Promise.all([ - Notes.count({ userId: group }), - ]); - - return { - total: count, - }; - } - - @autobind - public async update(user: { id: User['id'] }, note: Note, 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); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/per-user-reactions.ts b/packages/backend/src/services/chart/charts/classes/per-user-reactions.ts deleted file mode 100644 index 62508eacdf..0000000000 --- a/packages/backend/src/services/chart/charts/classes/per-user-reactions.ts +++ /dev/null @@ -1,44 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { DeepPartial } from '../../core'; -import { User } from '@/models/entities/user'; -import { Note } from '@/models/entities/note'; -import { SchemaType } from '@/misc/schema'; -import { Users } from '@/models/index'; -import { name, schema } from '../schemas/per-user-reactions'; - -type PerUserReactionsLog = SchemaType; - -export default class PerUserReactionsChart extends Chart { - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: PerUserReactionsLog): DeepPartial { - return {}; - } - - @autobind - protected aggregate(logs: PerUserReactionsLog[]): PerUserReactionsLog { - return { - local: { - count: logs.reduce((a, b) => a + b.local.count, 0), - }, - remote: { - count: logs.reduce((a, b) => a + b.remote.count, 0), - }, - }; - } - - @autobind - protected async fetchActual(group: string): Promise> { - return {}; - } - - @autobind - public async update(user: { id: User['id'], host: User['host'] }, note: Note) { - this.inc({ - [Users.isLocalUser(user) ? 'local' : 'remote']: { count: 1 }, - }, note.userId); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/test-grouped.ts b/packages/backend/src/services/chart/charts/classes/test-grouped.ts deleted file mode 100644 index 2b11930af2..0000000000 --- a/packages/backend/src/services/chart/charts/classes/test-grouped.ts +++ /dev/null @@ -1,58 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { name, schema } from '../schemas/test-grouped'; - -type TestGroupedLog = SchemaType; - -export default class TestGroupedChart extends Chart { - private total = {} as Record; - - constructor() { - super(name, schema, true); - } - - @autobind - protected genNewLog(latest: TestGroupedLog): DeepPartial { - return { - foo: { - total: latest.foo.total, - }, - }; - } - - @autobind - protected aggregate(logs: TestGroupedLog[]): TestGroupedLog { - return { - foo: { - total: logs[0].foo.total, - inc: logs.reduce((a, b) => a + b.foo.inc, 0), - dec: logs.reduce((a, b) => a + b.foo.dec, 0), - }, - }; - } - - @autobind - protected async fetchActual(group: string): Promise> { - return { - foo: { - total: this.total[group], - }, - }; - } - - @autobind - public async increment(group: string) { - if (this.total[group] == null) this.total[group] = 0; - - const update: Obj = {}; - - update.total = 1; - update.inc = 1; - this.total[group]++; - - await this.inc({ - foo: update, - }, group); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/test-unique.ts b/packages/backend/src/services/chart/charts/classes/test-unique.ts deleted file mode 100644 index f8655804d4..0000000000 --- a/packages/backend/src/services/chart/charts/classes/test-unique.ts +++ /dev/null @@ -1,36 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { name, schema } from '../schemas/test-unique'; - -type TestUniqueLog = SchemaType; - -export default class TestUniqueChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: TestUniqueLog): DeepPartial { - return {}; - } - - @autobind - protected aggregate(logs: TestUniqueLog[]): TestUniqueLog { - return { - foo: logs.reduce((a, b) => a.concat(b.foo), [] as TestUniqueLog['foo']), - }; - } - - @autobind - protected async fetchActual(): Promise> { - return {}; - } - - @autobind - public async uniqueIncrement(key: string) { - await this.inc({ - foo: [key], - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/test.ts b/packages/backend/src/services/chart/charts/classes/test.ts deleted file mode 100644 index b3f6b76a44..0000000000 --- a/packages/backend/src/services/chart/charts/classes/test.ts +++ /dev/null @@ -1,69 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { name, schema } from '../schemas/test'; - -type TestLog = SchemaType; - -export default class TestChart extends Chart { - public total = 0; // publicにするのはテストのため - - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: TestLog): DeepPartial { - return { - foo: { - total: latest.foo.total, - }, - }; - } - - @autobind - protected aggregate(logs: TestLog[]): TestLog { - return { - foo: { - total: logs[0].foo.total, - inc: logs.reduce((a, b) => a + b.foo.inc, 0), - dec: logs.reduce((a, b) => a + b.foo.dec, 0), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - return { - foo: { - total: this.total, - }, - }; - } - - @autobind - public async increment() { - const update: Obj = {}; - - update.total = 1; - update.inc = 1; - this.total++; - - await this.inc({ - foo: update, - }); - } - - @autobind - public async decrement() { - const update: Obj = {}; - - update.total = -1; - update.dec = 1; - this.total--; - - await this.inc({ - foo: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/classes/users.ts b/packages/backend/src/services/chart/charts/classes/users.ts deleted file mode 100644 index 6762410abf..0000000000 --- a/packages/backend/src/services/chart/charts/classes/users.ts +++ /dev/null @@ -1,76 +0,0 @@ -import autobind from 'autobind-decorator'; -import Chart, { Obj, DeepPartial } from '../../core'; -import { SchemaType } from '@/misc/schema'; -import { Users } from '@/models/index'; -import { Not, IsNull } from 'typeorm'; -import { User } from '@/models/entities/user'; -import { name, schema } from '../schemas/users'; - -type UsersLog = SchemaType; - -export default class UsersChart extends Chart { - constructor() { - super(name, schema); - } - - @autobind - protected genNewLog(latest: UsersLog): DeepPartial { - return { - local: { - total: latest.local.total, - }, - remote: { - total: latest.remote.total, - }, - }; - } - - @autobind - protected aggregate(logs: UsersLog[]): UsersLog { - return { - local: { - total: logs[0].local.total, - inc: logs.reduce((a, b) => a + b.local.inc, 0), - dec: logs.reduce((a, b) => a + b.local.dec, 0), - }, - remote: { - total: logs[0].remote.total, - inc: logs.reduce((a, b) => a + b.remote.inc, 0), - dec: logs.reduce((a, b) => a + b.remote.dec, 0), - }, - }; - } - - @autobind - protected async fetchActual(): Promise> { - const [localCount, remoteCount] = await Promise.all([ - Users.count({ host: null }), - Users.count({ host: Not(IsNull()) }), - ]); - - return { - local: { - total: localCount, - }, - remote: { - total: remoteCount, - }, - }; - } - - @autobind - public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean) { - const update: Obj = {}; - - update.total = isAdditional ? 1 : -1; - if (isAdditional) { - update.inc = 1; - } else { - update.dec = 1; - } - - await this.inc({ - [Users.isLocalUser(user) ? 'local' : 'remote']: update, - }); - } -} diff --git a/packages/backend/src/services/chart/charts/drive.ts b/packages/backend/src/services/chart/charts/drive.ts new file mode 100644 index 0000000000..06cf7ebeeb --- /dev/null +++ b/packages/backend/src/services/chart/charts/drive.ts @@ -0,0 +1,95 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { DriveFiles } from '@/models/index'; +import { Not, IsNull } from 'typeorm'; +import { DriveFile } from '@/models/entities/drive-file'; +import { name, schema } from './entities/drive'; + +type DriveLog = SchemaType; + +/** + * ドライブに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class DriveChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: DriveLog): DeepPartial { + return { + local: { + totalCount: latest.local.totalCount, + totalSize: latest.local.totalSize, + }, + remote: { + totalCount: latest.remote.totalCount, + totalSize: latest.remote.totalSize, + }, + }; + } + + @autobind + protected aggregate(logs: DriveLog[]): DriveLog { + return { + local: { + totalCount: logs[0].local.totalCount, + totalSize: logs[0].local.totalSize, + incCount: logs.reduce((a, b) => a + b.local.incCount, 0), + incSize: logs.reduce((a, b) => a + b.local.incSize, 0), + decCount: logs.reduce((a, b) => a + b.local.decCount, 0), + decSize: logs.reduce((a, b) => a + b.local.decSize, 0), + }, + remote: { + totalCount: logs[0].remote.totalCount, + totalSize: logs[0].remote.totalSize, + incCount: logs.reduce((a, b) => a + b.remote.incCount, 0), + incSize: logs.reduce((a, b) => a + b.remote.incSize, 0), + decCount: logs.reduce((a, b) => a + b.remote.decCount, 0), + decSize: logs.reduce((a, b) => a + b.remote.decSize, 0), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + const [localCount, remoteCount, localSize, remoteSize] = await Promise.all([ + DriveFiles.count({ userHost: null }), + DriveFiles.count({ userHost: Not(IsNull()) }), + DriveFiles.calcDriveUsageOfLocal(), + DriveFiles.calcDriveUsageOfRemote(), + ]); + + return { + local: { + totalCount: localCount, + totalSize: localSize, + }, + remote: { + totalCount: remoteCount, + totalSize: remoteSize, + }, + }; + } + + @autobind + public async update(file: DriveFile, isAdditional: boolean): Promise { + const update: Obj = {}; + + update.totalCount = isAdditional ? 1 : -1; + update.totalSize = isAdditional ? file.size : -file.size; + if (isAdditional) { + update.incCount = 1; + update.incSize = file.size; + } else { + update.decCount = 1; + update.decSize = file.size; + } + + await this.inc({ + [file.userHost === null ? 'local' : 'remote']: update, + }); + } +} diff --git a/packages/backend/src/services/chart/charts/entities/active-users.ts b/packages/backend/src/services/chart/charts/entities/active-users.ts new file mode 100644 index 0000000000..d6b49c86c3 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/active-users.ts @@ -0,0 +1,36 @@ +import Chart from '../../core'; + +export const name = 'activeUsers'; + +const logSchema = { + /** + * アクティブユーザー + */ + users: { + type: 'array' as const, + optional: false as const, nullable: false as const, + items: { + type: 'string' as const, + optional: false as const, nullable: false as const, + }, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/drive.ts b/packages/backend/src/services/chart/charts/entities/drive.ts new file mode 100644 index 0000000000..3362cbd4cb --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/drive.ts @@ -0,0 +1,72 @@ +import Chart from '../../core'; + +export const name = 'drive'; + +const logSchema = { + /** + * 集計期間時点での、全ドライブファイル数 + */ + totalCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 集計期間時点での、全ドライブファイルの合計サイズ + */ + totalSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 増加したドライブファイル数 + */ + incCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 増加したドライブ使用量 + */ + incSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 減少したドライブファイル数 + */ + decCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 減少したドライブ使用量 + */ + decSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/federation.ts b/packages/backend/src/services/chart/charts/entities/federation.ts new file mode 100644 index 0000000000..836116bd06 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/federation.ts @@ -0,0 +1,30 @@ +import Chart from '../../core'; + +export const name = 'federation'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + instance: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/hashtag.ts b/packages/backend/src/services/chart/charts/entities/hashtag.ts new file mode 100644 index 0000000000..43e15456a5 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/hashtag.ts @@ -0,0 +1,36 @@ +import Chart from '../../core'; + +export const name = 'hashtag'; + +const logSchema = { + /** + * 投稿したユーザー + */ + users: { + type: 'array' as const, + optional: false as const, nullable: false as const, + items: { + type: 'string' as const, + optional: false as const, nullable: false as const, + }, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/instance.ts b/packages/backend/src/services/chart/charts/entities/instance.ts new file mode 100644 index 0000000000..9d1f651dbb --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/instance.ts @@ -0,0 +1,158 @@ +import Chart from '../../core'; + +export const name = 'instance'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + requests: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + failed: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + succeeded: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + received: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + + notes: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + diffs: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + normal: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + reply: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + renote: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, + }, + + users: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + + following: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + + followers: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + + drive: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + totalFiles: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + totalUsage: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + incFiles: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + incUsage: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + decFiles: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + decUsage: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/network.ts b/packages/backend/src/services/chart/charts/entities/network.ts new file mode 100644 index 0000000000..3d4fffb855 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/network.ts @@ -0,0 +1,32 @@ +import Chart from '../../core'; + +export const name = 'network'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + incomingRequests: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + outgoingRequests: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + totalTime: { // TIP: (totalTime / incomingRequests) でひとつのリクエストに平均でどれくらいの時間がかかったか知れる + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + incomingBytes: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + outgoingBytes: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/notes.ts b/packages/backend/src/services/chart/charts/entities/notes.ts new file mode 100644 index 0000000000..554d3abe12 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/notes.ts @@ -0,0 +1,60 @@ +import Chart from '../../core'; + +export const name = 'notes'; + +const logSchema = { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + diffs: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + normal: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + reply: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + renote: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/per-user-drive.ts b/packages/backend/src/services/chart/charts/entities/per-user-drive.ts new file mode 100644 index 0000000000..ebf64e733e --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/per-user-drive.ts @@ -0,0 +1,59 @@ +import Chart from '../../core'; + +export const name = 'perUserDrive'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + /** + * 集計期間時点での、全ドライブファイル数 + */ + totalCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 集計期間時点での、全ドライブファイルの合計サイズ + */ + totalSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 増加したドライブファイル数 + */ + incCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 増加したドライブ使用量 + */ + incSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 減少したドライブファイル数 + */ + decCount: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 減少したドライブ使用量 + */ + decSize: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/per-user-following.ts b/packages/backend/src/services/chart/charts/entities/per-user-following.ts new file mode 100644 index 0000000000..8016c5fe97 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/per-user-following.ts @@ -0,0 +1,90 @@ +import Chart from '../../core'; + +export const name = 'perUserFollowing'; + +const logSchema = { + /** + * フォローしている + */ + followings: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + /** + * フォローしている合計 + */ + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * フォローした数 + */ + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * フォロー解除した数 + */ + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + + /** + * フォローされている + */ + followers: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + /** + * フォローされている合計 + */ + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * フォローされた数 + */ + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * フォロー解除された数 + */ + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/per-user-notes.ts b/packages/backend/src/services/chart/charts/entities/per-user-notes.ts new file mode 100644 index 0000000000..d8f645b36e --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/per-user-notes.ts @@ -0,0 +1,47 @@ +import Chart from '../../core'; + +export const name = 'perUserNotes'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + diffs: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + normal: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + reply: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + renote: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/per-user-reactions.ts b/packages/backend/src/services/chart/charts/entities/per-user-reactions.ts new file mode 100644 index 0000000000..bcb7012661 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/per-user-reactions.ts @@ -0,0 +1,32 @@ +import Chart from '../../core'; + +export const name = 'perUserReaction'; + +const logSchema = { + /** + * 被リアクション数 + */ + count: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/test-grouped.ts b/packages/backend/src/services/chart/charts/entities/test-grouped.ts new file mode 100644 index 0000000000..ca1c8c5700 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/test-grouped.ts @@ -0,0 +1,32 @@ +import Chart from '../../core'; + +export const name = 'testGrouped'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + foo: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/services/chart/charts/entities/test-unique.ts b/packages/backend/src/services/chart/charts/entities/test-unique.ts new file mode 100644 index 0000000000..2e917ee9ed --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/test-unique.ts @@ -0,0 +1,20 @@ +import Chart from '../../core'; + +export const name = 'testUnique'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + foo: { + type: 'array' as const, + optional: false as const, nullable: false as const, + items: { + type: 'string' as const, + optional: false as const, nullable: false as const, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/test.ts b/packages/backend/src/services/chart/charts/entities/test.ts new file mode 100644 index 0000000000..fa536ff2cf --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/test.ts @@ -0,0 +1,32 @@ +import Chart from '../../core'; + +export const name = 'test'; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + foo: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + }, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/entities/users.ts b/packages/backend/src/services/chart/charts/entities/users.ts new file mode 100644 index 0000000000..08d51c9414 --- /dev/null +++ b/packages/backend/src/services/chart/charts/entities/users.ts @@ -0,0 +1,48 @@ +import Chart from '../../core'; + +export const name = 'users'; + +const logSchema = { + /** + * 集計期間時点での、全ユーザー数 + */ + total: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 増加したユーザー数 + */ + inc: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, + + /** + * 減少したユーザー数 + */ + dec: { + type: 'number' as const, + optional: false as const, nullable: false as const, + }, +}; + +export const schema = { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: { + local: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + remote: { + type: 'object' as const, + optional: false as const, nullable: false as const, + properties: logSchema, + }, + }, +}; + +export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/services/chart/charts/federation.ts b/packages/backend/src/services/chart/charts/federation.ts new file mode 100644 index 0000000000..8abb18b51f --- /dev/null +++ b/packages/backend/src/services/chart/charts/federation.ts @@ -0,0 +1,66 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { Instances } from '@/models/index'; +import { name, schema } from './entities/federation'; + +type FederationLog = SchemaType; + +/** + * フェデレーションに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class FederationChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: FederationLog): DeepPartial { + return { + instance: { + total: latest.instance.total, + }, + }; + } + + @autobind + protected aggregate(logs: FederationLog[]): FederationLog { + return { + instance: { + total: logs[0].instance.total, + inc: logs.reduce((a, b) => a + b.instance.inc, 0), + dec: logs.reduce((a, b) => a + b.instance.dec, 0), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + const [total] = await Promise.all([ + Instances.count({}), + ]); + + return { + instance: { + total: total, + }, + }; + } + + @autobind + public async update(isAdditional: boolean): Promise { + const update: Obj = {}; + + update.total = isAdditional ? 1 : -1; + if (isAdditional) { + update.inc = 1; + } else { + update.dec = 1; + } + + await this.inc({ + instance: update, + }); + } +} diff --git a/packages/backend/src/services/chart/charts/hashtag.ts b/packages/backend/src/services/chart/charts/hashtag.ts new file mode 100644 index 0000000000..34e0614643 --- /dev/null +++ b/packages/backend/src/services/chart/charts/hashtag.ts @@ -0,0 +1,51 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { User } from '@/models/entities/user'; +import { SchemaType } from '@/misc/schema'; +import { Users } from '@/models/index'; +import { name, schema } from './entities/hashtag'; + +type HashtagLog = SchemaType; + +/** + * ハッシュタグに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class HashtagChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: HashtagLog): DeepPartial { + return {}; + } + + @autobind + protected aggregate(logs: HashtagLog[]): HashtagLog { + return { + local: { + users: logs.reduce((a, b) => a.concat(b.local.users), [] as HashtagLog['local']['users']), + }, + remote: { + users: logs.reduce((a, b) => a.concat(b.remote.users), [] as HashtagLog['remote']['users']), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + return {}; + } + + @autobind + public async update(hashtag: string, user: { id: User['id'], host: User['host'] }): Promise { + const update: Obj = { + users: [user.id], + }; + + await this.inc({ + [Users.isLocalUser(user) ? 'local' : 'remote']: update, + }, hashtag); + } +} diff --git a/packages/backend/src/services/chart/charts/instance.ts b/packages/backend/src/services/chart/charts/instance.ts new file mode 100644 index 0000000000..7f3419b69c --- /dev/null +++ b/packages/backend/src/services/chart/charts/instance.ts @@ -0,0 +1,221 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { DriveFiles, Followings, Users, Notes } from '@/models/index'; +import { DriveFile } from '@/models/entities/drive-file'; +import { Note } from '@/models/entities/note'; +import { toPuny } from '@/misc/convert-host'; +import { name, schema } from './entities/instance'; + +type InstanceLog = SchemaType; + +/** + * インスタンスごとのチャート + */ +// eslint-disable-next-line import/no-default-export +export default class InstanceChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: InstanceLog): DeepPartial { + return { + notes: { + total: latest.notes.total, + }, + users: { + total: latest.users.total, + }, + following: { + total: latest.following.total, + }, + followers: { + total: latest.followers.total, + }, + drive: { + totalFiles: latest.drive.totalFiles, + totalUsage: latest.drive.totalUsage, + }, + }; + } + + @autobind + protected aggregate(logs: InstanceLog[]): InstanceLog { + return { + requests: { + failed: logs.reduce((a, b) => a + b.requests.failed, 0), + succeeded: logs.reduce((a, b) => a + b.requests.succeeded, 0), + received: logs.reduce((a, b) => a + b.requests.received, 0), + }, + notes: { + total: logs[0].notes.total, + inc: logs.reduce((a, b) => a + b.notes.inc, 0), + dec: logs.reduce((a, b) => a + b.notes.dec, 0), + diffs: { + reply: logs.reduce((a, b) => a + b.notes.diffs.reply, 0), + renote: logs.reduce((a, b) => a + b.notes.diffs.renote, 0), + normal: logs.reduce((a, b) => a + b.notes.diffs.normal, 0), + }, + }, + users: { + total: logs[0].users.total, + inc: logs.reduce((a, b) => a + b.users.inc, 0), + dec: logs.reduce((a, b) => a + b.users.dec, 0), + }, + following: { + total: logs[0].following.total, + inc: logs.reduce((a, b) => a + b.following.inc, 0), + dec: logs.reduce((a, b) => a + b.following.dec, 0), + }, + followers: { + total: logs[0].followers.total, + inc: logs.reduce((a, b) => a + b.followers.inc, 0), + dec: logs.reduce((a, b) => a + b.followers.dec, 0), + }, + drive: { + totalFiles: logs[0].drive.totalFiles, + totalUsage: logs[0].drive.totalUsage, + incFiles: logs.reduce((a, b) => a + b.drive.incFiles, 0), + incUsage: logs.reduce((a, b) => a + b.drive.incUsage, 0), + decFiles: logs.reduce((a, b) => a + b.drive.decFiles, 0), + decUsage: logs.reduce((a, b) => a + b.drive.decUsage, 0), + }, + }; + } + + @autobind + protected async fetchActual(group: string): Promise> { + const [ + notesCount, + usersCount, + followingCount, + followersCount, + driveFiles, + driveUsage, + ] = await Promise.all([ + Notes.count({ userHost: group }), + Users.count({ host: group }), + Followings.count({ followerHost: group }), + Followings.count({ followeeHost: group }), + DriveFiles.count({ userHost: group }), + DriveFiles.calcDriveUsageOfHost(group), + ]); + + return { + notes: { + total: notesCount, + }, + users: { + total: usersCount, + }, + following: { + total: followingCount, + }, + followers: { + total: followersCount, + }, + drive: { + totalFiles: driveFiles, + totalUsage: driveUsage, + }, + }; + } + + @autobind + public async requestReceived(host: string): Promise { + await this.inc({ + requests: { + received: 1, + }, + }, toPuny(host)); + } + + @autobind + public async requestSent(host: string, isSucceeded: boolean): Promise { + const update: Obj = {}; + + if (isSucceeded) { + update.succeeded = 1; + } else { + update.failed = 1; + } + + await this.inc({ + requests: update, + }, toPuny(host)); + } + + @autobind + public async newUser(host: string): Promise { + await this.inc({ + users: { + total: 1, + inc: 1, + }, + }, toPuny(host)); + } + + @autobind + public async updateNote(host: string, note: Note, isAdditional: boolean): Promise { + const diffs = {} as Record; + + if (note.replyId != null) { + diffs.reply = isAdditional ? 1 : -1; + } else if (note.renoteId != null) { + diffs.renote = isAdditional ? 1 : -1; + } else { + diffs.normal = isAdditional ? 1 : -1; + } + + await this.inc({ + notes: { + total: isAdditional ? 1 : -1, + inc: isAdditional ? 1 : 0, + dec: isAdditional ? 0 : 1, + diffs: diffs, + }, + }, toPuny(host)); + } + + @autobind + public async updateFollowing(host: string, isAdditional: boolean): Promise { + await this.inc({ + following: { + total: isAdditional ? 1 : -1, + inc: isAdditional ? 1 : 0, + dec: isAdditional ? 0 : 1, + }, + }, toPuny(host)); + } + + @autobind + public async updateFollowers(host: string, isAdditional: boolean): Promise { + await this.inc({ + followers: { + total: isAdditional ? 1 : -1, + inc: isAdditional ? 1 : 0, + dec: isAdditional ? 0 : 1, + }, + }, toPuny(host)); + } + + @autobind + public async updateDrive(file: DriveFile, isAdditional: boolean): Promise { + const update: Obj = {}; + + update.totalFiles = isAdditional ? 1 : -1; + update.totalUsage = isAdditional ? file.size : -file.size; + if (isAdditional) { + update.incFiles = 1; + update.incUsage = file.size; + } else { + update.decFiles = 1; + update.decUsage = file.size; + } + + await this.inc({ + drive: update, + }, file.userHost); + } +} diff --git a/packages/backend/src/services/chart/charts/network.ts b/packages/backend/src/services/chart/charts/network.ts new file mode 100644 index 0000000000..73ea2f7e19 --- /dev/null +++ b/packages/backend/src/services/chart/charts/network.ts @@ -0,0 +1,49 @@ +import autobind from 'autobind-decorator'; +import Chart, { DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { name, schema } from './entities/network'; + +type NetworkLog = SchemaType; + +/** + * ネットワークに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class NetworkChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: NetworkLog): DeepPartial { + return {}; + } + + @autobind + protected aggregate(logs: NetworkLog[]): NetworkLog { + return { + incomingRequests: logs.reduce((a, b) => a + b.incomingRequests, 0), + outgoingRequests: logs.reduce((a, b) => a + b.outgoingRequests, 0), + totalTime: logs.reduce((a, b) => a + b.totalTime, 0), + incomingBytes: logs.reduce((a, b) => a + b.incomingBytes, 0), + outgoingBytes: logs.reduce((a, b) => a + b.outgoingBytes, 0), + }; + } + + @autobind + protected async fetchActual(): Promise> { + return {}; + } + + @autobind + public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number): Promise { + const inc: DeepPartial = { + incomingRequests: incomingRequests, + totalTime: time, + incomingBytes: incomingBytes, + outgoingBytes: outgoingBytes, + }; + + await this.inc(inc); + } +} diff --git a/packages/backend/src/services/chart/charts/notes.ts b/packages/backend/src/services/chart/charts/notes.ts new file mode 100644 index 0000000000..86cda17225 --- /dev/null +++ b/packages/backend/src/services/chart/charts/notes.ts @@ -0,0 +1,101 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { Notes } from '@/models/index'; +import { Not, IsNull } from 'typeorm'; +import { Note } from '@/models/entities/note'; +import { name, schema } from './entities/notes'; + +type NotesLog = SchemaType; + +/** + * ノートに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class NotesChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: NotesLog): DeepPartial { + return { + local: { + total: latest.local.total, + }, + remote: { + total: latest.remote.total, + }, + }; + } + + @autobind + protected aggregate(logs: NotesLog[]): NotesLog { + return { + local: { + total: logs[0].local.total, + inc: logs.reduce((a, b) => a + b.local.inc, 0), + dec: logs.reduce((a, b) => a + b.local.dec, 0), + diffs: { + reply: logs.reduce((a, b) => a + b.local.diffs.reply, 0), + renote: logs.reduce((a, b) => a + b.local.diffs.renote, 0), + normal: logs.reduce((a, b) => a + b.local.diffs.normal, 0), + }, + }, + remote: { + total: logs[0].remote.total, + inc: logs.reduce((a, b) => a + b.remote.inc, 0), + dec: logs.reduce((a, b) => a + b.remote.dec, 0), + diffs: { + reply: logs.reduce((a, b) => a + b.remote.diffs.reply, 0), + renote: logs.reduce((a, b) => a + b.remote.diffs.renote, 0), + normal: logs.reduce((a, b) => a + b.remote.diffs.normal, 0), + }, + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + const [localCount, remoteCount] = await Promise.all([ + Notes.count({ userHost: null }), + Notes.count({ userHost: Not(IsNull()) }), + ]); + + return { + local: { + total: localCount, + }, + remote: { + total: remoteCount, + }, + }; + } + + @autobind + public async update(note: Note, isAdditional: boolean): Promise { + 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({ + [note.userHost === null ? 'local' : 'remote']: update, + }); + } +} diff --git a/packages/backend/src/services/chart/charts/per-user-drive.ts b/packages/backend/src/services/chart/charts/per-user-drive.ts new file mode 100644 index 0000000000..fff790367f --- /dev/null +++ b/packages/backend/src/services/chart/charts/per-user-drive.ts @@ -0,0 +1,68 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { DriveFiles } from '@/models/index'; +import { DriveFile } from '@/models/entities/drive-file'; +import { name, schema } from './entities/per-user-drive'; + +type PerUserDriveLog = SchemaType; + +/** + * ユーザーごとのドライブに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class PerUserDriveChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: PerUserDriveLog): DeepPartial { + return { + totalCount: latest.totalCount, + totalSize: latest.totalSize, + }; + } + + @autobind + protected aggregate(logs: PerUserDriveLog[]): PerUserDriveLog { + return { + totalCount: logs[0].totalCount, + totalSize: logs[0].totalSize, + incCount: logs.reduce((a, b) => a + b.incCount, 0), + incSize: logs.reduce((a, b) => a + b.incSize, 0), + decCount: logs.reduce((a, b) => a + b.decCount, 0), + decSize: logs.reduce((a, b) => a + b.decSize, 0), + }; + } + + @autobind + protected async fetchActual(group: string): Promise> { + const [count, size] = await Promise.all([ + DriveFiles.count({ userId: group }), + DriveFiles.calcDriveUsageOf(group), + ]); + + return { + totalCount: count, + totalSize: size, + }; + } + + @autobind + public async update(file: DriveFile, isAdditional: boolean): Promise { + const update: Obj = {}; + + update.totalCount = isAdditional ? 1 : -1; + update.totalSize = isAdditional ? file.size : -file.size; + if (isAdditional) { + update.incCount = 1; + update.incSize = file.size; + } else { + update.decCount = 1; + update.decSize = file.size; + } + + await this.inc(update, file.userId); + } +} diff --git a/packages/backend/src/services/chart/charts/per-user-following.ts b/packages/backend/src/services/chart/charts/per-user-following.ts new file mode 100644 index 0000000000..d0a80abdaf --- /dev/null +++ b/packages/backend/src/services/chart/charts/per-user-following.ts @@ -0,0 +1,125 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { Followings, Users } from '@/models/index'; +import { Not, IsNull } from 'typeorm'; +import { User } from '@/models/entities/user'; +import { name, schema } from './entities/per-user-following'; + +type PerUserFollowingLog = SchemaType; + +/** + * ユーザーごとのフォローに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class PerUserFollowingChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: PerUserFollowingLog): DeepPartial { + return { + local: { + followings: { + total: latest.local.followings.total, + }, + followers: { + total: latest.local.followers.total, + }, + }, + remote: { + followings: { + total: latest.remote.followings.total, + }, + followers: { + total: latest.remote.followers.total, + }, + }, + }; + } + + @autobind + protected aggregate(logs: PerUserFollowingLog[]): PerUserFollowingLog { + return { + local: { + followings: { + total: logs[0].local.followings.total, + inc: logs.reduce((a, b) => a + b.local.followings.inc, 0), + dec: logs.reduce((a, b) => a + b.local.followings.dec, 0), + }, + followers: { + total: logs[0].local.followers.total, + inc: logs.reduce((a, b) => a + b.local.followers.inc, 0), + dec: logs.reduce((a, b) => a + b.local.followers.dec, 0), + }, + }, + remote: { + followings: { + total: logs[0].remote.followings.total, + inc: logs.reduce((a, b) => a + b.remote.followings.inc, 0), + dec: logs.reduce((a, b) => a + b.remote.followings.dec, 0), + }, + followers: { + total: logs[0].remote.followers.total, + inc: logs.reduce((a, b) => a + b.remote.followers.inc, 0), + dec: logs.reduce((a, b) => a + b.remote.followers.dec, 0), + }, + }, + }; + } + + @autobind + protected async fetchActual(group: string): Promise> { + const [ + localFollowingsCount, + localFollowersCount, + remoteFollowingsCount, + remoteFollowersCount, + ] = await Promise.all([ + Followings.count({ followerId: group, followeeHost: null }), + Followings.count({ followeeId: group, followerHost: null }), + Followings.count({ followerId: group, followeeHost: Not(IsNull()) }), + Followings.count({ followeeId: group, followerHost: Not(IsNull()) }), + ]); + + return { + local: { + followings: { + total: localFollowingsCount, + }, + followers: { + total: localFollowersCount, + }, + }, + remote: { + followings: { + total: remoteFollowingsCount, + }, + followers: { + total: remoteFollowersCount, + }, + }, + }; + } + + @autobind + public async update(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }, isFollow: boolean): Promise { + const update: Obj = {}; + + update.total = isFollow ? 1 : -1; + + if (isFollow) { + update.inc = 1; + } else { + update.dec = 1; + } + + this.inc({ + [Users.isLocalUser(follower) ? 'local' : 'remote']: { followings: update }, + }, follower.id); + this.inc({ + [Users.isLocalUser(followee) ? 'local' : 'remote']: { followers: update }, + }, followee.id); + } +} 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; + +/** + * ユーザーごとのノートに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class PerUserNotesChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: PerUserNotesLog): DeepPartial { + 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> { + 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 { + 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); + } +} diff --git a/packages/backend/src/services/chart/charts/per-user-reactions.ts b/packages/backend/src/services/chart/charts/per-user-reactions.ts new file mode 100644 index 0000000000..2f5353340d --- /dev/null +++ b/packages/backend/src/services/chart/charts/per-user-reactions.ts @@ -0,0 +1,48 @@ +import autobind from 'autobind-decorator'; +import Chart, { DeepPartial } from '../core'; +import { User } from '@/models/entities/user'; +import { Note } from '@/models/entities/note'; +import { SchemaType } from '@/misc/schema'; +import { Users } from '@/models/index'; +import { name, schema } from './entities/per-user-reactions'; + +type PerUserReactionsLog = SchemaType; + +/** + * ユーザーごとのリアクションに関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class PerUserReactionsChart extends Chart { + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: PerUserReactionsLog): DeepPartial { + return {}; + } + + @autobind + protected aggregate(logs: PerUserReactionsLog[]): PerUserReactionsLog { + return { + local: { + count: logs.reduce((a, b) => a + b.local.count, 0), + }, + remote: { + count: logs.reduce((a, b) => a + b.remote.count, 0), + }, + }; + } + + @autobind + protected async fetchActual(group: string): Promise> { + return {}; + } + + @autobind + public async update(user: { id: User['id'], host: User['host'] }, note: Note): Promise { + this.inc({ + [Users.isLocalUser(user) ? 'local' : 'remote']: { count: 1 }, + }, note.userId); + } +} diff --git a/packages/backend/src/services/chart/charts/schemas/active-users.ts b/packages/backend/src/services/chart/charts/schemas/active-users.ts deleted file mode 100644 index 322fdb8691..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/active-users.ts +++ /dev/null @@ -1,35 +0,0 @@ -export const logSchema = { - /** - * アクティブユーザー - */ - users: { - type: 'array' as const, - optional: false as const, nullable: false as const, - items: { - type: 'string' as const, - optional: false as const, nullable: false as const, - }, - }, -}; - -/** - * アクティブユーザーに関するチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'activeUsers'; diff --git a/packages/backend/src/services/chart/charts/schemas/drive.ts b/packages/backend/src/services/chart/charts/schemas/drive.ts deleted file mode 100644 index e7d3267931..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/drive.ts +++ /dev/null @@ -1,68 +0,0 @@ -const logSchema = { - /** - * 集計期間時点での、全ドライブファイル数 - */ - totalCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 集計期間時点での、全ドライブファイルの合計サイズ - */ - totalSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 増加したドライブファイル数 - */ - incCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 増加したドライブ使用量 - */ - incSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 減少したドライブファイル数 - */ - decCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 減少したドライブ使用量 - */ - decSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, -}; - -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'drive'; diff --git a/packages/backend/src/services/chart/charts/schemas/federation.ts b/packages/backend/src/services/chart/charts/schemas/federation.ts deleted file mode 100644 index 1b8bbce0a8..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/federation.ts +++ /dev/null @@ -1,29 +0,0 @@ -/** - * フェデレーションに関するチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - instance: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, -}; - -export const name = 'federation'; diff --git a/packages/backend/src/services/chart/charts/schemas/hashtag.ts b/packages/backend/src/services/chart/charts/schemas/hashtag.ts deleted file mode 100644 index 6b4eca2978..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/hashtag.ts +++ /dev/null @@ -1,35 +0,0 @@ -export const logSchema = { - /** - * 投稿したユーザー - */ - users: { - type: 'array' as const, - optional: false as const, nullable: false as const, - items: { - type: 'string' as const, - optional: false as const, nullable: false as const, - }, - }, -}; - -/** - * ハッシュタグに関するチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'hashtag'; diff --git a/packages/backend/src/services/chart/charts/schemas/instance.ts b/packages/backend/src/services/chart/charts/schemas/instance.ts deleted file mode 100644 index 5f17ac0d11..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/instance.ts +++ /dev/null @@ -1,157 +0,0 @@ -/** - * インスタンスごとのチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - requests: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - failed: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - succeeded: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - received: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - - notes: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - diffs: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - normal: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - reply: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - renote: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, - }, - - users: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - - following: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - - followers: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - - drive: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - totalFiles: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - totalUsage: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - incFiles: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - incUsage: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - decFiles: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - decUsage: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, -}; - -export const name = 'instance'; diff --git a/packages/backend/src/services/chart/charts/schemas/network.ts b/packages/backend/src/services/chart/charts/schemas/network.ts deleted file mode 100644 index 1aeb2a8ad5..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/network.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * ネットワークに関するチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - incomingRequests: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - outgoingRequests: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - totalTime: { // TIP: (totalTime / incomingRequests) でひとつのリクエストに平均でどれくらいの時間がかかったか知れる - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - incomingBytes: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - outgoingBytes: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, -}; - -export const name = 'network'; diff --git a/packages/backend/src/services/chart/charts/schemas/notes.ts b/packages/backend/src/services/chart/charts/schemas/notes.ts deleted file mode 100644 index a33e7aa8b9..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/notes.ts +++ /dev/null @@ -1,56 +0,0 @@ -const logSchema = { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - diffs: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - normal: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - reply: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - renote: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, -}; - -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'notes'; diff --git a/packages/backend/src/services/chart/charts/schemas/per-user-drive.ts b/packages/backend/src/services/chart/charts/schemas/per-user-drive.ts deleted file mode 100644 index 1c7c57afda..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/per-user-drive.ts +++ /dev/null @@ -1,55 +0,0 @@ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - /** - * 集計期間時点での、全ドライブファイル数 - */ - totalCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 集計期間時点での、全ドライブファイルの合計サイズ - */ - totalSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 増加したドライブファイル数 - */ - incCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 増加したドライブ使用量 - */ - incSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 減少したドライブファイル数 - */ - decCount: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 減少したドライブ使用量 - */ - decSize: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, -}; - -export const name = 'perUserDrive'; diff --git a/packages/backend/src/services/chart/charts/schemas/per-user-following.ts b/packages/backend/src/services/chart/charts/schemas/per-user-following.ts deleted file mode 100644 index 9e7e71f3bb..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/per-user-following.ts +++ /dev/null @@ -1,86 +0,0 @@ -export const logSchema = { - /** - * フォローしている - */ - followings: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - /** - * フォローしている合計 - */ - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * フォローした数 - */ - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * フォロー解除した数 - */ - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - - /** - * フォローされている - */ - followers: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - /** - * フォローされている合計 - */ - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * フォローされた数 - */ - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * フォロー解除された数 - */ - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, -}; - -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'perUserFollowing'; diff --git a/packages/backend/src/services/chart/charts/schemas/per-user-notes.ts b/packages/backend/src/services/chart/charts/schemas/per-user-notes.ts deleted file mode 100644 index 55b4ff40c0..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/per-user-notes.ts +++ /dev/null @@ -1,43 +0,0 @@ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - diffs: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - normal: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - reply: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - renote: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, -}; - -export const name = 'perUserNotes'; diff --git a/packages/backend/src/services/chart/charts/schemas/per-user-reactions.ts b/packages/backend/src/services/chart/charts/schemas/per-user-reactions.ts deleted file mode 100644 index 9af6c3cd54..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/per-user-reactions.ts +++ /dev/null @@ -1,31 +0,0 @@ -export const logSchema = { - /** - * フォローしている合計 - */ - count: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, -}; - -/** - * ユーザーごとのリアクションに関するチャート - */ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'perUserReaction'; diff --git a/packages/backend/src/services/chart/charts/schemas/test-grouped.ts b/packages/backend/src/services/chart/charts/schemas/test-grouped.ts deleted file mode 100644 index c2f2e7e886..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/test-grouped.ts +++ /dev/null @@ -1,28 +0,0 @@ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - foo: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, -}; - -export const name = 'testGrouped'; diff --git a/packages/backend/src/services/chart/charts/schemas/test-unique.ts b/packages/backend/src/services/chart/charts/schemas/test-unique.ts deleted file mode 100644 index b8da60dc53..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/test-unique.ts +++ /dev/null @@ -1,16 +0,0 @@ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - foo: { - type: 'array' as const, - optional: false as const, nullable: false as const, - items: { - type: 'string' as const, - optional: false as const, nullable: false as const, - }, - }, - }, -}; - -export const name = 'testUnique'; diff --git a/packages/backend/src/services/chart/charts/schemas/test.ts b/packages/backend/src/services/chart/charts/schemas/test.ts deleted file mode 100644 index 19c732fece..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/test.ts +++ /dev/null @@ -1,28 +0,0 @@ -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - foo: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - }, - }, - }, -}; - -export const name = 'test'; diff --git a/packages/backend/src/services/chart/charts/schemas/users.ts b/packages/backend/src/services/chart/charts/schemas/users.ts deleted file mode 100644 index 47e7791974..0000000000 --- a/packages/backend/src/services/chart/charts/schemas/users.ts +++ /dev/null @@ -1,44 +0,0 @@ -const logSchema = { - /** - * 集計期間時点での、全ユーザー数 - */ - total: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 増加したユーザー数 - */ - inc: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, - - /** - * 減少したユーザー数 - */ - dec: { - type: 'number' as const, - optional: false as const, nullable: false as const, - }, -}; - -export const schema = { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: { - local: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - remote: { - type: 'object' as const, - optional: false as const, nullable: false as const, - properties: logSchema, - }, - }, -}; - -export const name = 'users'; diff --git a/packages/backend/src/services/chart/charts/test-grouped.ts b/packages/backend/src/services/chart/charts/test-grouped.ts new file mode 100644 index 0000000000..c851d2df01 --- /dev/null +++ b/packages/backend/src/services/chart/charts/test-grouped.ts @@ -0,0 +1,62 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { name, schema } from './entities/test-grouped'; + +type TestGroupedLog = SchemaType; + +/** + * For testing + */ +// eslint-disable-next-line import/no-default-export +export default class TestGroupedChart extends Chart { + private total = {} as Record; + + constructor() { + super(name, schema, true); + } + + @autobind + protected genNewLog(latest: TestGroupedLog): DeepPartial { + return { + foo: { + total: latest.foo.total, + }, + }; + } + + @autobind + protected aggregate(logs: TestGroupedLog[]): TestGroupedLog { + return { + foo: { + total: logs[0].foo.total, + inc: logs.reduce((a, b) => a + b.foo.inc, 0), + dec: logs.reduce((a, b) => a + b.foo.dec, 0), + }, + }; + } + + @autobind + protected async fetchActual(group: string): Promise> { + return { + foo: { + total: this.total[group], + }, + }; + } + + @autobind + public async increment(group: string): Promise { + if (this.total[group] == null) this.total[group] = 0; + + const update: Obj = {}; + + update.total = 1; + update.inc = 1; + this.total[group]++; + + await this.inc({ + foo: update, + }, group); + } +} diff --git a/packages/backend/src/services/chart/charts/test-unique.ts b/packages/backend/src/services/chart/charts/test-unique.ts new file mode 100644 index 0000000000..3564f675ad --- /dev/null +++ b/packages/backend/src/services/chart/charts/test-unique.ts @@ -0,0 +1,40 @@ +import autobind from 'autobind-decorator'; +import Chart, { DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { name, schema } from './entities/test-unique'; + +type TestUniqueLog = SchemaType; + +/** + * For testing + */ +// eslint-disable-next-line import/no-default-export +export default class TestUniqueChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: TestUniqueLog): DeepPartial { + return {}; + } + + @autobind + protected aggregate(logs: TestUniqueLog[]): TestUniqueLog { + return { + foo: logs.reduce((a, b) => a.concat(b.foo), [] as TestUniqueLog['foo']), + }; + } + + @autobind + protected async fetchActual(): Promise> { + return {}; + } + + @autobind + public async uniqueIncrement(key: string): Promise { + await this.inc({ + foo: [key], + }); + } +} diff --git a/packages/backend/src/services/chart/charts/test.ts b/packages/backend/src/services/chart/charts/test.ts new file mode 100644 index 0000000000..06add7ede9 --- /dev/null +++ b/packages/backend/src/services/chart/charts/test.ts @@ -0,0 +1,73 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { name, schema } from './entities/test'; + +type TestLog = SchemaType; + +/** + * For testing + */ +// eslint-disable-next-line import/no-default-export +export default class TestChart extends Chart { + public total = 0; // publicにするのはテストのため + + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: TestLog): DeepPartial { + return { + foo: { + total: latest.foo.total, + }, + }; + } + + @autobind + protected aggregate(logs: TestLog[]): TestLog { + return { + foo: { + total: logs[0].foo.total, + inc: logs.reduce((a, b) => a + b.foo.inc, 0), + dec: logs.reduce((a, b) => a + b.foo.dec, 0), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + return { + foo: { + total: this.total, + }, + }; + } + + @autobind + public async increment(): Promise { + const update: Obj = {}; + + update.total = 1; + update.inc = 1; + this.total++; + + await this.inc({ + foo: update, + }); + } + + @autobind + public async decrement(): Promise { + const update: Obj = {}; + + update.total = -1; + update.dec = 1; + this.total--; + + await this.inc({ + foo: update, + }); + } +} diff --git a/packages/backend/src/services/chart/charts/users.ts b/packages/backend/src/services/chart/charts/users.ts new file mode 100644 index 0000000000..c36c6cd979 --- /dev/null +++ b/packages/backend/src/services/chart/charts/users.ts @@ -0,0 +1,80 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj, DeepPartial } from '../core'; +import { SchemaType } from '@/misc/schema'; +import { Users } from '@/models/index'; +import { Not, IsNull } from 'typeorm'; +import { User } from '@/models/entities/user'; +import { name, schema } from './entities/users'; + +type UsersLog = SchemaType; + +/** + * ユーザー数に関するチャート + */ +// eslint-disable-next-line import/no-default-export +export default class UsersChart extends Chart { + constructor() { + super(name, schema); + } + + @autobind + protected genNewLog(latest: UsersLog): DeepPartial { + return { + local: { + total: latest.local.total, + }, + remote: { + total: latest.remote.total, + }, + }; + } + + @autobind + protected aggregate(logs: UsersLog[]): UsersLog { + return { + local: { + total: logs[0].local.total, + inc: logs.reduce((a, b) => a + b.local.inc, 0), + dec: logs.reduce((a, b) => a + b.local.dec, 0), + }, + remote: { + total: logs[0].remote.total, + inc: logs.reduce((a, b) => a + b.remote.inc, 0), + dec: logs.reduce((a, b) => a + b.remote.dec, 0), + }, + }; + } + + @autobind + protected async fetchActual(): Promise> { + const [localCount, remoteCount] = await Promise.all([ + Users.count({ host: null }), + Users.count({ host: Not(IsNull()) }), + ]); + + return { + local: { + total: localCount, + }, + remote: { + total: remoteCount, + }, + }; + } + + @autobind + public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean): Promise { + const update: Obj = {}; + + update.total = isAdditional ? 1 : -1; + if (isAdditional) { + update.inc = 1; + } else { + update.dec = 1; + } + + await this.inc({ + [Users.isLocalUser(user) ? 'local' : 'remote']: update, + }); + } +} diff --git a/packages/backend/src/services/chart/core.ts b/packages/backend/src/services/chart/core.ts index 59149dbc08..f97fa521d3 100644 --- a/packages/backend/src/services/chart/core.ts +++ b/packages/backend/src/services/chart/core.ts @@ -30,7 +30,7 @@ type Log = { /** * 集計のグループ */ - group: string | null; + group?: string | null; /** * 集計日時のUnixタイムスタンプ(秒) @@ -38,7 +38,7 @@ type Log = { date: number; }; -const camelToSnake = (str: string) => { +const camelToSnake = (str: string): string => { return str.replace(/([A-Z])/g, s => '_' + s.charAt(0).toLowerCase()); }; @@ -47,6 +47,7 @@ const removeDuplicates = (array: any[]) => Array.from(new Set(array)); /** * 様々なチャートの管理を司るクラス */ +// eslint-disable-next-line import/no-default-export export default abstract class Chart> { private static readonly columnPrefix = '___'; private static readonly columnDot = '_'; @@ -57,7 +58,8 @@ export default abstract class Chart> { group: string | null; }[] = []; public schema: SimpleSchema; - protected repository: Repository; + protected repositoryForHour: Repository; + protected repositoryForDay: Repository; protected abstract genNewLog(latest: T): DeepPartial; @@ -181,9 +183,15 @@ export default abstract class Chart> { } @autobind - public static schemaToEntity(name: string, schema: SimpleSchema): EntitySchema { - return new EntitySchema({ - name: `__chart__${camelToSnake(name)}`, + public static schemaToEntity(name: string, schema: SimpleSchema, grouped = false): { + hour: EntitySchema, + day: EntitySchema, + } { + const createEntity = (span: 'hour' | 'day'): EntitySchema => new EntitySchema({ + name: + span === 'hour' ? `__chart__${camelToSnake(name)}` : + span === 'day' ? `__chart_day__${camelToSnake(name)}` : + new Error('not happen') as never, columns: { id: { type: 'integer', @@ -193,37 +201,45 @@ export default abstract class Chart> { date: { type: 'integer', }, - group: { - type: 'varchar', - length: 128, - nullable: true, - }, + ...(grouped ? { + group: { + type: 'varchar', + length: 128, + }, + } : {}), ...Chart.convertSchemaToFlatColumnDefinitions(schema), }, indices: [{ - columns: ['date', 'group'], - unique: true, - }, { // groupにnullが含まれると↑のuniqueは機能しないので↓の部分インデックスでカバー - columns: ['date'], + columns: grouped ? ['date', 'group'] : ['date'], unique: true, - where: '"group" IS NULL', }], + uniques: [{ + columns: grouped ? ['date', 'group'] : ['date'], + }], + relations: { + /* TODO + group: { + target: () => Foo, + type: 'many-to-one', + onDelete: 'CASCADE', + }, + */ + }, }); + + return { + hour: createEntity('hour'), + day: createEntity('day'), + }; } constructor(name: string, schema: SimpleSchema, grouped = false) { this.name = name; this.schema = schema; - const entity = Chart.schemaToEntity(name, schema); - - const keys = ['date']; - if (grouped) keys.push('group'); - - entity.options.uniques = [{ - columns: keys, - }]; - this.repository = getRepository(entity); + const { hour, day } = Chart.schemaToEntity(name, schema, grouped); + this.repositoryForHour = getRepository(hour); + this.repositoryForDay = getRepository(day); } @autobind @@ -247,24 +263,40 @@ export default abstract class Chart> { } @autobind - private getLatestLog(group: string | null = null): Promise { - return this.repository.findOne({ + private getLatestLog(group: string | null, span: 'hour' | 'day'): Promise { + const repository = + span === 'hour' ? this.repositoryForHour : + span === 'day' ? this.repositoryForDay : + new Error('not happen') as never; + + return repository.findOne(group ? { group: group, - }, { + } : {}, { order: { date: -1, }, }).then(x => x || null); } + /** + * 現在(=今のHour or Day)のログをデータベースから探して、あればそれを返し、なければ作成して返します。 + */ @autobind - private async getCurrentLog(group: string | null = null): Promise { + private async claimCurrentLog(group: string | null, span: 'hour' | 'day'): Promise { const [y, m, d, h] = Chart.getCurrentDate(); - const current = dateUTC([y, m, d, h]); + const current = dateUTC( + span === 'hour' ? [y, m, d, h] : + span === 'day' ? [y, m, d] : + new Error('not happen') as never); - // 現在(=今のHour)のログ - const currentLog = await this.repository.findOne({ + const repository = + span === 'hour' ? this.repositoryForHour : + span === 'day' ? this.repositoryForDay : + new Error('not happen') as never; + + // 現在(=今のHour or Day)のログ + const currentLog = await repository.findOne({ date: Chart.dateToTimestamp(current), ...(group ? { group: group } : {}), }); @@ -283,7 +315,7 @@ export default abstract class Chart> { // * 昨日何もチャートを更新するような出来事がなかった場合は、 // * ログがそもそも作られずドキュメントが存在しないということがあり得るため、 // * 「昨日の」と決め打ちせずに「もっとも最近の」とします - const latest = await this.getLatestLog(group); + const latest = await this.getLatestLog(group, span); if (latest != null) { const obj = Chart.convertFlattenColumnsToObject(latest) as T; @@ -297,16 +329,16 @@ export default abstract class Chart> { // 初期ログデータを作成 data = this.getNewLog(null); - logger.info(`${this.name + (group ? `:${group}` : '')}: Initial commit created`); + logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): Initial commit created`); } const date = Chart.dateToTimestamp(current); - const lockKey = `${this.name}:${date}:${group}`; + const lockKey = group ? `${this.name}:${date}:${span}:${group}` : `${this.name}:${date}:${span}`; const unlock = await getChartInsertLock(lockKey); try { // ロック内でもう1回チェックする - const currentLog = await this.repository.findOne({ + const currentLog = await repository.findOne({ date: date, ...(group ? { group: group } : {}), }); @@ -315,13 +347,13 @@ export default abstract class Chart> { if (currentLog != null) return currentLog; // 新規ログ挿入 - log = await this.repository.insert({ - group: group, + log = await repository.insert({ date: date, + ...(group ? { group: group } : {}), ...Chart.convertObjectToFlattenColumns(data), - }).then(x => this.repository.findOneOrFail(x.identifiers[0])); + }).then(x => repository.findOneOrFail(x.identifiers[0])); - logger.info(`${this.name + (group ? `:${group}` : '')}: New commit created`); + logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): New commit created`); return log; } finally { @@ -349,10 +381,10 @@ export default abstract class Chart> { // そのログは本来は 01:00~ のログとしてDBに保存されて欲しいのに、02:00~ のログ扱いになってしまう。 // これを回避するための実装は複雑になりそうなため、一旦保留。 - const update = async (log: Log) => { + const update = async (logHour: Log, logDay: Log): Promise => { const finalDiffs = {} as Record; - for (const diff of this.buffer.filter(q => q.group === log.group).map(q => q.diff)) { + for (const diff of this.buffer.filter(q => q.group == null || (q.group === logHour.group)).map(q => q.diff)) { const columns = Chart.convertObjectToFlattenColumns(diff); for (const [k, v] of Object.entries(columns)) { @@ -371,36 +403,60 @@ export default abstract class Chart> { const query = Chart.convertQuery(finalDiffs); // ログ更新 - await this.repository.createQueryBuilder() - .update() - .set(query) - .where('id = :id', { id: log.id }) - .execute(); - - logger.info(`${this.name + (log.group ? `:${log.group}` : '')}: Updated`); + await Promise.all([ + this.repositoryForHour.createQueryBuilder() + .update() + .set(query) + .where('id = :id', { id: logHour.id }) + .execute(), + this.repositoryForDay.createQueryBuilder() + .update() + .set(query) + .where('id = :id', { id: logDay.id }) + .execute(), + ]); + + logger.info(`${this.name + (logHour.group ? `:${logHour.group}` : '')}: Updated`); // TODO: この一連の処理が始まった後に新たにbufferに入ったものは消さないようにする - this.buffer = this.buffer.filter(q => q.group !== log.group); + this.buffer = this.buffer.filter(q => q.group != null && (q.group !== logHour.group)); }; const groups = removeDuplicates(this.buffer.map(log => log.group)); - await Promise.all(groups.map(group => this.getCurrentLog(group).then(log => update(log)))); + await Promise.all( + groups.map(group => + Promise.all([ + this.claimCurrentLog(group, 'hour'), + this.claimCurrentLog(group, 'day'), + ]).then(([logHour, logDay]) => + update(logHour, logDay)))); } @autobind - public async resync(group: string | null = null): Promise { + public async resync(group: string | null = null): Promise { const data = await this.fetchActual(group); - const update = async (log: Log) => { - await this.repository.createQueryBuilder() - .update() - .set(Chart.convertObjectToFlattenColumns(data)) - .where('id = :id', { id: log.id }) - .execute(); + const update = async (logHour: Log, logDay: Log): Promise => { + await Promise.all([ + this.repositoryForHour.createQueryBuilder() + .update() + .set(Chart.convertObjectToFlattenColumns(data)) + .where('id = :id', { id: logHour.id }) + .execute(), + this.repositoryForDay.createQueryBuilder() + .update() + .set(Chart.convertObjectToFlattenColumns(data)) + .where('id = :id', { id: logDay.id }) + .execute(), + ]); }; - return this.getCurrentLog(group).then(log => update(log)); + return Promise.all([ + this.claimCurrentLog(group, 'hour'), + this.claimCurrentLog(group, 'day'), + ]).then(([logHour, logDay]) => + update(logHour, logDay)); } @autobind @@ -418,13 +474,18 @@ export default abstract class Chart> { const gt = span === 'day' ? subtractTime(cursor ? dateUTC([y2, m2, d2, 0]) : dateUTC([y, m, d, 0]), amount - 1, 'day') : span === 'hour' ? subtractTime(cursor ? dateUTC([y2, m2, d2, h2]) : dateUTC([y, m, d, h]), amount - 1, 'hour') : - null as never; + new Error('not happen') as never; + + const repository = + span === 'hour' ? this.repositoryForHour : + span === 'day' ? this.repositoryForDay : + new Error('not happen') as never; // ログ取得 - let logs = await this.repository.find({ + let logs = await repository.find({ where: { - group: group, date: Between(Chart.dateToTimestamp(gt), Chart.dateToTimestamp(lt)), + ...(group ? { group: group } : {}), }, order: { date: -1, @@ -435,9 +496,9 @@ export default abstract class Chart> { if (logs.length === 0) { // もっとも新しいログを持ってくる // (すくなくともひとつログが無いと隙間埋めできないため) - const recentLog = await this.repository.findOne({ + const recentLog = await repository.findOne(group ? { group: group, - }, { + } : {}, { order: { date: -1, }, @@ -451,9 +512,9 @@ export default abstract class Chart> { } else if (!isTimeSame(new Date(logs[logs.length - 1].date * 1000), gt)) { // 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する // (隙間埋めできないため) - const outdatedLog = await this.repository.findOne({ - group: group, + const outdatedLog = await repository.findOne({ date: LessThan(Chart.dateToTimestamp(gt)), + ...(group ? { group: group } : {}), }, { order: { date: -1, @@ -467,60 +528,26 @@ export default abstract class Chart> { const chart: T[] = []; - if (span === 'hour') { - for (let i = (amount - 1); i >= 0; i--) { - const current = subtractTime(dateUTC([y, m, d, h]), i, 'hour'); - - const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current)); - - if (log) { - const data = Chart.convertFlattenColumnsToObject(log); - chart.unshift(Chart.countUniqueFields(data) as T); - } else { - // 隙間埋め - const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current)); - const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null; - chart.unshift(Chart.countUniqueFields(this.getNewLog(data)) as T); - } - } - } else if (span === 'day') { - const logsForEachDays: T[][] = []; - let currentDay = -1; - let currentDayIndex = -1; - for (let i = ((amount - 1) * 24) + h; i >= 0; i--) { - const current = subtractTime(dateUTC([y, m, d, h]), i, 'hour'); - const _currentDay = Chart.parseDate(current)[2]; - if (currentDay != _currentDay) currentDayIndex++; - currentDay = _currentDay; - - const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current)); - - if (log) { - if (logsForEachDays[currentDayIndex]) { - logsForEachDays[currentDayIndex].unshift(Chart.convertFlattenColumnsToObject(log) as T); - } else { - logsForEachDays[currentDayIndex] = [Chart.convertFlattenColumnsToObject(log) as T]; - } - } else { - // 隙間埋め - const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current)); - const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null; - const newLog = this.getNewLog(data); - if (logsForEachDays[currentDayIndex]) { - logsForEachDays[currentDayIndex].unshift(newLog); - } else { - logsForEachDays[currentDayIndex] = [newLog]; - } - } - } - - for (const logs of logsForEachDays) { - const log = this.aggregate(logs); - chart.unshift(Chart.countUniqueFields(log) as T); + for (let i = (amount - 1); i >= 0; i--) { + const current = + span === 'hour' ? subtractTime(dateUTC([y, m, d, h]), i, 'hour') : + span === 'day' ? subtractTime(dateUTC([y, m, d]), i, 'day') : + new Error('not happen') as never; + + const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current)); + + if (log) { + const data = Chart.convertFlattenColumnsToObject(log); + chart.unshift(Chart.countUniqueFields(data) as T); + } else { + // 隙間埋め + const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current)); + const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null; + chart.unshift(Chart.countUniqueFields(this.getNewLog(data)) as T); } } - const res: ArrayValue = {} as any; + const res = {} as Record; /** * [{ foo: 1, bar: 5 }, { foo: 2, bar: 6 }, { foo: 3, bar: 7 }] @@ -528,7 +555,7 @@ export default abstract class Chart> { * { foo: [1, 2, 3], bar: [5, 6, 7] } * にする */ - const compact = (x: Obj, path?: string) => { + const compact = (x: Obj, path?: string): void => { for (const [k, v] of Object.entries(x)) { const p = path ? `${path}.${k}` : k; if (typeof v === 'object' && !Array.isArray(v)) { @@ -542,7 +569,7 @@ export default abstract class Chart> { compact(chart[0]); - return res; + return res as ArrayValue; } } diff --git a/packages/backend/src/services/chart/entities.ts b/packages/backend/src/services/chart/entities.ts index 9d96a8a7ee..dedbd47080 100644 --- a/packages/backend/src/services/chart/entities.ts +++ b/packages/backend/src/services/chart/entities.ts @@ -1,15 +1,27 @@ -import { fileURLToPath } from 'url'; -import { dirname } from 'path'; -import Chart from './core'; +import { entity as FederationChart } from './charts/entities/federation'; +import { entity as NotesChart } from './charts/entities/notes'; +import { entity as UsersChart } from './charts/entities/users'; +import { entity as NetworkChart } from './charts/entities/network'; +import { entity as ActiveUsersChart } from './charts/entities/active-users'; +import { entity as InstanceChart } from './charts/entities/instance'; +import { entity as PerUserNotesChart } from './charts/entities/per-user-notes'; +import { entity as DriveChart } from './charts/entities/drive'; +import { entity as PerUserReactionsChart } from './charts/entities/per-user-reactions'; +import { entity as HashtagChart } from './charts/entities/hashtag'; +import { entity as PerUserFollowingChart } from './charts/entities/per-user-following'; +import { entity as PerUserDriveChart } from './charts/entities/per-user-drive'; -//const _filename = fileURLToPath(import.meta.url); -const _filename = __filename; -const _dirname = dirname(_filename); - -export const entities = Object.values(require('require-all')({ - dirname: _dirname + '/charts/schemas', - filter: /^.+\.[jt]s$/, - resolve: (x: any) => { - return Chart.schemaToEntity(x.name, x.schema); - }, -})); +export const entities = [ + FederationChart.hour, FederationChart.day, + NotesChart.hour, NotesChart.day, + UsersChart.hour, UsersChart.day, + NetworkChart.hour, NetworkChart.day, + ActiveUsersChart.hour, ActiveUsersChart.day, + InstanceChart.hour, InstanceChart.day, + PerUserNotesChart.hour, PerUserNotesChart.day, + DriveChart.hour, DriveChart.day, + PerUserReactionsChart.hour, PerUserReactionsChart.day, + HashtagChart.hour, HashtagChart.day, + PerUserFollowingChart.hour, PerUserFollowingChart.day, + PerUserDriveChart.hour, PerUserDriveChart.day, +]; diff --git a/packages/backend/src/services/chart/index.ts b/packages/backend/src/services/chart/index.ts index 61eb431ea3..0b9887b36f 100644 --- a/packages/backend/src/services/chart/index.ts +++ b/packages/backend/src/services/chart/index.ts @@ -1,17 +1,18 @@ -import FederationChart from './charts/classes/federation'; -import NotesChart from './charts/classes/notes'; -import UsersChart from './charts/classes/users'; -import NetworkChart from './charts/classes/network'; -import ActiveUsersChart from './charts/classes/active-users'; -import InstanceChart from './charts/classes/instance'; -import PerUserNotesChart from './charts/classes/per-user-notes'; -import DriveChart from './charts/classes/drive'; -import PerUserReactionsChart from './charts/classes/per-user-reactions'; -import HashtagChart from './charts/classes/hashtag'; -import PerUserFollowingChart from './charts/classes/per-user-following'; -import PerUserDriveChart from './charts/classes/per-user-drive'; import { beforeShutdown } from '@/misc/before-shutdown'; +import FederationChart from './charts/federation'; +import NotesChart from './charts/notes'; +import UsersChart from './charts/users'; +import NetworkChart from './charts/network'; +import ActiveUsersChart from './charts/active-users'; +import InstanceChart from './charts/instance'; +import PerUserNotesChart from './charts/per-user-notes'; +import DriveChart from './charts/drive'; +import PerUserReactionsChart from './charts/per-user-reactions'; +import HashtagChart from './charts/hashtag'; +import PerUserFollowingChart from './charts/per-user-following'; +import PerUserDriveChart from './charts/per-user-drive'; + export const federationChart = new FederationChart(); export const notesChart = new NotesChart(); export const usersChart = new UsersChart(); -- cgit v1.2.3-freya