From 969e9df889367159e64fcabadfd2150b1dfd685d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 1 Jan 2023 17:45:49 +0900 Subject: feat: add per user pv chart --- .../src/core/chart/ChartManagementService.ts | 5 ++- .../src/core/chart/charts/entities/per-user-pv.ts | 12 +++++ .../backend/src/core/chart/charts/per-user-pv.ts | 51 ++++++++++++++++++++++ packages/backend/src/core/chart/entities.ts | 2 + 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/core/chart/charts/entities/per-user-pv.ts create mode 100644 packages/backend/src/core/chart/charts/per-user-pv.ts (limited to 'packages/backend/src/core/chart') diff --git a/packages/backend/src/core/chart/ChartManagementService.ts b/packages/backend/src/core/chart/ChartManagementService.ts index 13ee06c6c5..37de30b71c 100644 --- a/packages/backend/src/core/chart/ChartManagementService.ts +++ b/packages/backend/src/core/chart/ChartManagementService.ts @@ -1,11 +1,13 @@ import { Injectable, Inject } from '@nestjs/common'; +import { bindThis } from '@/decorators.js'; import FederationChart from './charts/federation.js'; import NotesChart from './charts/notes.js'; import UsersChart from './charts/users.js'; import ActiveUsersChart from './charts/active-users.js'; import InstanceChart from './charts/instance.js'; import PerUserNotesChart from './charts/per-user-notes.js'; +import PerUserPvChart from './charts/per-user-pv.js'; import DriveChart from './charts/drive.js'; import PerUserReactionsChart from './charts/per-user-reactions.js'; import HashtagChart from './charts/hashtag.js'; @@ -13,7 +15,6 @@ import PerUserFollowingChart from './charts/per-user-following.js'; import PerUserDriveChart from './charts/per-user-drive.js'; import ApRequestChart from './charts/ap-request.js'; import type { OnApplicationShutdown } from '@nestjs/common'; -import { bindThis } from '@/decorators.js'; @Injectable() export class ChartManagementService implements OnApplicationShutdown { @@ -27,6 +28,7 @@ export class ChartManagementService implements OnApplicationShutdown { private activeUsersChart: ActiveUsersChart, private instanceChart: InstanceChart, private perUserNotesChart: PerUserNotesChart, + private perUserPvChart: PerUserPvChart, private driveChart: DriveChart, private perUserReactionsChart: PerUserReactionsChart, private hashtagChart: HashtagChart, @@ -41,6 +43,7 @@ export class ChartManagementService implements OnApplicationShutdown { this.activeUsersChart, this.instanceChart, this.perUserNotesChart, + this.perUserPvChart, this.driveChart, this.perUserReactionsChart, this.hashtagChart, diff --git a/packages/backend/src/core/chart/charts/entities/per-user-pv.ts b/packages/backend/src/core/chart/charts/entities/per-user-pv.ts new file mode 100644 index 0000000000..64c8ed1fb1 --- /dev/null +++ b/packages/backend/src/core/chart/charts/entities/per-user-pv.ts @@ -0,0 +1,12 @@ +import Chart from '../../core.js'; + +export const name = 'perUserPv'; + +export const schema = { + 'upv.user': { uniqueIncrement: true, range: 'small' }, + 'pv.user': { range: 'small' }, + 'upv.visitor': { uniqueIncrement: true, range: 'small' }, + 'pv.visitor': { range: 'small' }, +} as const; + +export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/per-user-pv.ts b/packages/backend/src/core/chart/charts/per-user-pv.ts new file mode 100644 index 0000000000..53c89d8a9a --- /dev/null +++ b/packages/backend/src/core/chart/charts/per-user-pv.ts @@ -0,0 +1,51 @@ +import { Injectable, Inject } from '@nestjs/common'; +import { DataSource } from 'typeorm'; +import type { User } from '@/models/entities/User.js'; +import { AppLockService } from '@/core/AppLockService.js'; +import { DI } from '@/di-symbols.js'; +import { bindThis } from '@/decorators.js'; +import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; +import { name, schema } from './entities/per-user-pv.js'; +import type { KVs } from '../core.js'; + +/** + * ユーザーごとのプロフィール被閲覧数に関するチャート + */ +// eslint-disable-next-line import/no-default-export +@Injectable() +export default class PerUserPvChart extends Chart { + constructor( + @Inject(DI.db) + private db: DataSource, + + private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, + ) { + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); + } + + protected async tickMajor(): Promise>> { + return {}; + } + + protected async tickMinor(): Promise>> { + return {}; + } + + @bindThis + public async commitByUser(user: { id: User['id'] }, key: string): Promise { + await this.commit({ + 'upv.user': [key], + 'pv.user': 1, + }, user.id); + } + + @bindThis + public async commitByVisitor(user: { id: User['id'] }, key: string): Promise { + await this.commit({ + 'upv.visitor': [key], + 'pv.visitor': 1, + }, user.id); + } +} diff --git a/packages/backend/src/core/chart/entities.ts b/packages/backend/src/core/chart/entities.ts index a9eeabd639..c2759e8b3c 100644 --- a/packages/backend/src/core/chart/entities.ts +++ b/packages/backend/src/core/chart/entities.ts @@ -4,6 +4,7 @@ import { entity as UsersChart } from './charts/entities/users.js'; import { entity as ActiveUsersChart } from './charts/entities/active-users.js'; import { entity as InstanceChart } from './charts/entities/instance.js'; import { entity as PerUserNotesChart } from './charts/entities/per-user-notes.js'; +import { entity as PerUserPvChart } from './charts/entities/per-user-pv.js'; import { entity as DriveChart } from './charts/entities/drive.js'; import { entity as PerUserReactionsChart } from './charts/entities/per-user-reactions.js'; import { entity as HashtagChart } from './charts/entities/hashtag.js'; @@ -23,6 +24,7 @@ export const entities = [ ActiveUsersChart.hour, ActiveUsersChart.day, InstanceChart.hour, InstanceChart.day, PerUserNotesChart.hour, PerUserNotesChart.day, + PerUserPvChart.hour, PerUserPvChart.day, DriveChart.hour, DriveChart.day, PerUserReactionsChart.hour, PerUserReactionsChart.day, HashtagChart.hour, HashtagChart.day, -- cgit v1.2.3-freya