feat: add per user pv chart

This commit is contained in:
syuilo 2023-01-01 17:45:49 +09:00
parent 4c4af2ae84
commit 969e9df889
13 changed files with 153 additions and 4 deletions

View file

@ -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

View file

@ -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"`);
}
}

View file

@ -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,

View file

@ -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,

View file

@ -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);

View file

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {
return {};
}
protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
return {};
}
@bindThis
public async commitByUser(user: { id: User['id'] }, key: string): Promise<void> {
await this.commit({
'upv.user': [key],
'pv.user': 1,
}, user.id);
}
@bindThis
public async commitByVisitor(user: { id: User['id'] }, key: string): Promise<void> {
await this.commit({
'upv.visitor': [key],
'pv.visitor': 1,
}, user.id);
}
}

View file

@ -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,

View file

@ -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(),

View file

@ -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),

View file

@ -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,

View file

@ -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],

View file

@ -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<typeof meta, typeof paramDef> {
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);
});
}
}

View file

@ -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<typeof meta, typeof paramDef> {
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<typeof meta, typeof paramDef> {
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,
});