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

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