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

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