From 969e9df889367159e64fcabadfd2150b1dfd685d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 1 Jan 2023 17:45:49 +0900 Subject: [PATCH] feat: add per user pv chart --- CHANGELOG.md | 1 + .../migration/1672562400597-PerUserPvChart.js | 17 +++++++ packages/backend/src/core/CoreModule.ts | 6 +++ .../src/core/chart/ChartManagementService.ts | 5 +- .../core/chart/charts/entities/per-user-pv.ts | 12 +++++ .../src/core/chart/charts/per-user-pv.ts | 51 +++++++++++++++++++ packages/backend/src/core/chart/entities.ts | 2 + .../processors/CleanChartsProcessorService.ts | 5 +- .../processors/TickChartsProcessorService.ts | 5 +- .../backend/src/server/api/EndpointsModule.ts | 4 ++ packages/backend/src/server/api/endpoints.ts | 2 + .../server/api/endpoints/charts/user/pv.ts | 37 ++++++++++++++ .../src/server/api/endpoints/users/show.ts | 10 +++- 13 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 packages/backend/migration/1672562400597-PerUserPvChart.js 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 create mode 100644 packages/backend/src/server/api/endpoints/charts/user/pv.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 25e175fa85..918bb225a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ You should also include the user name that made the change. - Add Cloudflare Turnstile CAPTCHA support @CyberRex0 - Introduce retention-rate aggregation @syuilo - Make possible to export favorited notes @syuilo +- Add per user pv chart @syuilo - Server: signToActivityPubGet is set to true by default @syuilo - Server: improve syslog performance @syuilo - Server: improve note scoring for featured notes @CyberRex0 diff --git a/packages/backend/migration/1672562400597-PerUserPvChart.js b/packages/backend/migration/1672562400597-PerUserPvChart.js new file mode 100644 index 0000000000..4da6b9a8b3 --- /dev/null +++ b/packages/backend/migration/1672562400597-PerUserPvChart.js @@ -0,0 +1,17 @@ +export class PerUserPvChart1672562400597 { + name = 'PerUserPvChart1672562400597' + + async up(queryRunner) { + await queryRunner.query(`CREATE TABLE "__chart__per_user_pv" ("id" SERIAL NOT NULL, "date" integer NOT NULL, "group" character varying(128) NOT NULL, "unique_temp___upv_user" character varying array NOT NULL DEFAULT '{}', "___upv_user" smallint NOT NULL DEFAULT '0', "___pv_user" smallint NOT NULL DEFAULT '0', "unique_temp___upv_visitor" character varying array NOT NULL DEFAULT '{}', "___upv_visitor" smallint NOT NULL DEFAULT '0', "___pv_visitor" smallint NOT NULL DEFAULT '0', CONSTRAINT "UQ_f2a56da57921ca8439f45c1d95f" UNIQUE ("date", "group"), CONSTRAINT "PK_3c938a24f0203b5bd13fab51059" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE UNIQUE INDEX "IDX_f2a56da57921ca8439f45c1d95" ON "__chart__per_user_pv" ("date", "group") `); + await queryRunner.query(`CREATE TABLE "__chart_day__per_user_pv" ("id" SERIAL NOT NULL, "date" integer NOT NULL, "group" character varying(128) NOT NULL, "unique_temp___upv_user" character varying array NOT NULL DEFAULT '{}', "___upv_user" smallint NOT NULL DEFAULT '0', "___pv_user" smallint NOT NULL DEFAULT '0', "unique_temp___upv_visitor" character varying array NOT NULL DEFAULT '{}', "___upv_visitor" smallint NOT NULL DEFAULT '0', "___pv_visitor" smallint NOT NULL DEFAULT '0', CONSTRAINT "UQ_f221e45cfac5bea0ce0f3149fbb" UNIQUE ("date", "group"), CONSTRAINT "PK_0085d7542f6772e99b9dcfb0a9c" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE UNIQUE INDEX "IDX_f221e45cfac5bea0ce0f3149fb" ON "__chart_day__per_user_pv" ("date", "group") `); + } + + async down(queryRunner) { + await queryRunner.query(`DROP INDEX "public"."IDX_f221e45cfac5bea0ce0f3149fb"`); + await queryRunner.query(`DROP TABLE "__chart_day__per_user_pv"`); + await queryRunner.query(`DROP INDEX "public"."IDX_f2a56da57921ca8439f45c1d95"`); + await queryRunner.query(`DROP TABLE "__chart__per_user_pv"`); + } +} diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index 085addaa05..7c6d12abf8 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -57,6 +57,7 @@ import UsersChart from './chart/charts/users.js'; import ActiveUsersChart from './chart/charts/active-users.js'; import InstanceChart from './chart/charts/instance.js'; import PerUserNotesChart from './chart/charts/per-user-notes.js'; +import PerUserPvChart from './chart/charts/per-user-pv.js'; import DriveChart from './chart/charts/drive.js'; import PerUserReactionsChart from './chart/charts/per-user-reactions.js'; import HashtagChart from './chart/charts/hashtag.js'; @@ -176,6 +177,7 @@ const $UsersChart: Provider = { provide: 'UsersChart', useExisting: UsersChart } const $ActiveUsersChart: Provider = { provide: 'ActiveUsersChart', useExisting: ActiveUsersChart }; const $InstanceChart: Provider = { provide: 'InstanceChart', useExisting: InstanceChart }; const $PerUserNotesChart: Provider = { provide: 'PerUserNotesChart', useExisting: PerUserNotesChart }; +const $PerUserPvChart: Provider = { provide: 'PerUserPvChart', useExisting: PerUserPvChart }; const $DriveChart: Provider = { provide: 'DriveChart', useExisting: DriveChart }; const $PerUserReactionsChart: Provider = { provide: 'PerUserReactionsChart', useExisting: PerUserReactionsChart }; const $HashtagChart: Provider = { provide: 'HashtagChart', useExisting: HashtagChart }; @@ -298,6 +300,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting ActiveUsersChart, InstanceChart, PerUserNotesChart, + PerUserPvChart, DriveChart, PerUserReactionsChart, HashtagChart, @@ -414,6 +417,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $ActiveUsersChart, $InstanceChart, $PerUserNotesChart, + $PerUserPvChart, $DriveChart, $PerUserReactionsChart, $HashtagChart, @@ -530,6 +534,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting ActiveUsersChart, InstanceChart, PerUserNotesChart, + PerUserPvChart, DriveChart, PerUserReactionsChart, HashtagChart, @@ -645,6 +650,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $ActiveUsersChart, $InstanceChart, $PerUserNotesChart, + $PerUserPvChart, $DriveChart, $PerUserReactionsChart, $HashtagChart, 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, diff --git a/packages/backend/src/queue/processors/CleanChartsProcessorService.ts b/packages/backend/src/queue/processors/CleanChartsProcessorService.ts index c57086240a..2adf7cbe6d 100644 --- a/packages/backend/src/queue/processors/CleanChartsProcessorService.ts +++ b/packages/backend/src/queue/processors/CleanChartsProcessorService.ts @@ -9,15 +9,16 @@ import UsersChart from '@/core/chart/charts/users.js'; import ActiveUsersChart from '@/core/chart/charts/active-users.js'; import InstanceChart from '@/core/chart/charts/instance.js'; import PerUserNotesChart from '@/core/chart/charts/per-user-notes.js'; +import PerUserPvChart from '@/core/chart/charts/per-user-pv.js'; import DriveChart from '@/core/chart/charts/drive.js'; import PerUserReactionsChart from '@/core/chart/charts/per-user-reactions.js'; import HashtagChart from '@/core/chart/charts/hashtag.js'; import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js'; import PerUserDriveChart from '@/core/chart/charts/per-user-drive.js'; import ApRequestChart from '@/core/chart/charts/ap-request.js'; +import { bindThis } from '@/decorators.js'; import { QueueLoggerService } from '../QueueLoggerService.js'; import type Bull from 'bull'; -import { bindThis } from '@/decorators.js'; @Injectable() export class CleanChartsProcessorService { @@ -33,6 +34,7 @@ export class CleanChartsProcessorService { private activeUsersChart: ActiveUsersChart, private instanceChart: InstanceChart, private perUserNotesChart: PerUserNotesChart, + private perUserPvChart: PerUserPvChart, private driveChart: DriveChart, private perUserReactionsChart: PerUserReactionsChart, private hashtagChart: HashtagChart, @@ -56,6 +58,7 @@ export class CleanChartsProcessorService { this.activeUsersChart.clean(), this.instanceChart.clean(), this.perUserNotesChart.clean(), + this.perUserPvChart.clean(), this.driveChart.clean(), this.perUserReactionsChart.clean(), this.hashtagChart.clean(), diff --git a/packages/backend/src/queue/processors/TickChartsProcessorService.ts b/packages/backend/src/queue/processors/TickChartsProcessorService.ts index 323e5227cd..51eff2a155 100644 --- a/packages/backend/src/queue/processors/TickChartsProcessorService.ts +++ b/packages/backend/src/queue/processors/TickChartsProcessorService.ts @@ -9,15 +9,16 @@ import UsersChart from '@/core/chart/charts/users.js'; import ActiveUsersChart from '@/core/chart/charts/active-users.js'; import InstanceChart from '@/core/chart/charts/instance.js'; import PerUserNotesChart from '@/core/chart/charts/per-user-notes.js'; +import PerUserPvChart from '@/core/chart/charts/per-user-pv.js'; import DriveChart from '@/core/chart/charts/drive.js'; import PerUserReactionsChart from '@/core/chart/charts/per-user-reactions.js'; import HashtagChart from '@/core/chart/charts/hashtag.js'; import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js'; import PerUserDriveChart from '@/core/chart/charts/per-user-drive.js'; import ApRequestChart from '@/core/chart/charts/ap-request.js'; +import { bindThis } from '@/decorators.js'; import { QueueLoggerService } from '../QueueLoggerService.js'; import type Bull from 'bull'; -import { bindThis } from '@/decorators.js'; @Injectable() export class TickChartsProcessorService { @@ -33,6 +34,7 @@ export class TickChartsProcessorService { private activeUsersChart: ActiveUsersChart, private instanceChart: InstanceChart, private perUserNotesChart: PerUserNotesChart, + private perUserPvChart: PerUserPvChart, private driveChart: DriveChart, private perUserReactionsChart: PerUserReactionsChart, private hashtagChart: HashtagChart, @@ -56,6 +58,7 @@ export class TickChartsProcessorService { this.activeUsersChart.tick(false), this.instanceChart.tick(false), this.perUserNotesChart.tick(false), + this.perUserPvChart.tick(false), this.driveChart.tick(false), this.perUserReactionsChart.tick(false), this.hashtagChart.tick(false), diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index 18ba16ac79..32eff7f312 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -99,6 +99,7 @@ import * as ep___charts_notes from './endpoints/charts/notes.js'; import * as ep___charts_user_drive from './endpoints/charts/user/drive.js'; import * as ep___charts_user_following from './endpoints/charts/user/following.js'; import * as ep___charts_user_notes from './endpoints/charts/user/notes.js'; +import * as ep___charts_user_pv from './endpoints/charts/user/pv.js'; import * as ep___charts_user_reactions from './endpoints/charts/user/reactions.js'; import * as ep___charts_users from './endpoints/charts/users.js'; import * as ep___clips_addNote from './endpoints/clips/add-note.js'; @@ -419,6 +420,7 @@ const $charts_notes: Provider = { provide: 'ep:charts/notes', useClass: ep___cha const $charts_user_drive: Provider = { provide: 'ep:charts/user/drive', useClass: ep___charts_user_drive.default }; const $charts_user_following: Provider = { provide: 'ep:charts/user/following', useClass: ep___charts_user_following.default }; const $charts_user_notes: Provider = { provide: 'ep:charts/user/notes', useClass: ep___charts_user_notes.default }; +const $charts_user_pv: Provider = { provide: 'ep:charts/user/pv', useClass: ep___charts_user_pv.default }; const $charts_user_reactions: Provider = { provide: 'ep:charts/user/reactions', useClass: ep___charts_user_reactions.default }; const $charts_users: Provider = { provide: 'ep:charts/users', useClass: ep___charts_users.default }; const $clips_addNote: Provider = { provide: 'ep:clips/add-note', useClass: ep___clips_addNote.default }; @@ -743,6 +745,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $charts_user_drive, $charts_user_following, $charts_user_notes, + $charts_user_pv, $charts_user_reactions, $charts_users, $clips_addNote, @@ -1061,6 +1064,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $charts_user_drive, $charts_user_following, $charts_user_notes, + $charts_user_pv, $charts_user_reactions, $charts_users, $clips_addNote, diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index a09ffa832c..49dc3b224f 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -98,6 +98,7 @@ import * as ep___charts_notes from './endpoints/charts/notes.js'; import * as ep___charts_user_drive from './endpoints/charts/user/drive.js'; import * as ep___charts_user_following from './endpoints/charts/user/following.js'; import * as ep___charts_user_notes from './endpoints/charts/user/notes.js'; +import * as ep___charts_user_pv from './endpoints/charts/user/pv.js'; import * as ep___charts_user_reactions from './endpoints/charts/user/reactions.js'; import * as ep___charts_users from './endpoints/charts/users.js'; import * as ep___clips_addNote from './endpoints/clips/add-note.js'; @@ -416,6 +417,7 @@ const eps = [ ['charts/user/drive', ep___charts_user_drive], ['charts/user/following', ep___charts_user_following], ['charts/user/notes', ep___charts_user_notes], + ['charts/user/pv', ep___charts_user_pv], ['charts/user/reactions', ep___charts_user_reactions], ['charts/users', ep___charts_users], ['clips/add-note', ep___clips_addNote], diff --git a/packages/backend/src/server/api/endpoints/charts/user/pv.ts b/packages/backend/src/server/api/endpoints/charts/user/pv.ts new file mode 100644 index 0000000000..c920e0f57d --- /dev/null +++ b/packages/backend/src/server/api/endpoints/charts/user/pv.ts @@ -0,0 +1,37 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { getJsonSchema } from '@/core/chart/core.js'; +import { Endpoint } from '@/server/api/endpoint-base.js'; +import PerUserPvChart from '@/core/chart/charts/per-user-pv.js'; +import { schema } from '@/core/chart/charts/entities/per-user-notes.js'; + +export const meta = { + tags: ['charts', 'users'], + + res: getJsonSchema(schema), + + allowGet: true, + cacheSec: 60 * 60, +} as const; + +export const paramDef = { + type: 'object', + properties: { + span: { type: 'string', enum: ['day', 'hour'] }, + limit: { type: 'integer', minimum: 1, maximum: 500, default: 30 }, + offset: { type: 'integer', nullable: true, default: null }, + userId: { type: 'string', format: 'misskey:id' }, + }, + required: ['span', 'userId'], +} as const; + +// eslint-disable-next-line import/no-default-export +@Injectable() +export default class extends Endpoint { + constructor( + private perUserPvChart: PerUserPvChart, + ) { + super(meta, paramDef, async (ps, me) => { + return await this.perUserPvChart.getChart(ps.span, ps.limit, ps.offset ? new Date(ps.offset) : null, ps.userId); + }); + } +} diff --git a/packages/backend/src/server/api/endpoints/users/show.ts b/packages/backend/src/server/api/endpoints/users/show.ts index 7932d5cd12..48a6bbf9bc 100644 --- a/packages/backend/src/server/api/endpoints/users/show.ts +++ b/packages/backend/src/server/api/endpoints/users/show.ts @@ -6,6 +6,7 @@ import { Endpoint } from '@/server/api/endpoint-base.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js'; import { DI } from '@/di-symbols.js'; +import PerUserPvChart from '@/core/chart/charts/per-user-pv.js'; import { ApiError } from '../../error.js'; import { ApiLoggerService } from '../../ApiLoggerService.js'; import type { FindOptionsWhere } from 'typeorm'; @@ -90,9 +91,10 @@ export default class extends Endpoint { private userEntityService: UserEntityService, private remoteUserResolveService: RemoteUserResolveService, + private perUserPvChart: PerUserPvChart, private apiLoggerService: ApiLoggerService, ) { - super(meta, paramDef, async (ps, me) => { + super(meta, paramDef, async (ps, me, _1, _2, _3, ip) => { let user; const isAdminOrModerator = me && (me.isAdmin || me.isModerator); @@ -137,6 +139,12 @@ export default class extends Endpoint { throw new ApiError(meta.errors.noSuchUser); } + if (me == null && ip != null) { + this.perUserPvChart.commitByVisitor(user, ip); + } else if (me && me.id !== user.id) { + this.perUserPvChart.commitByUser(user, me.id); + } + return await this.userEntityService.pack(user, me, { detail: true, });