refactor: introduce bindThis decorator to bind this automaticaly

This commit is contained in:
syuilo
2022-12-04 15:03:09 +09:00
parent e73581f715
commit bbb49457f9
199 changed files with 969 additions and 96 deletions

View File

@ -1,6 +1,7 @@
import { Inject, Injectable } from '@nestjs/common';
import type Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
@Injectable()
export class ChartLoggerService {

View File

@ -13,6 +13,7 @@ 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 {
@ -49,6 +50,7 @@ export class ChartManagementService implements OnApplicationShutdown {
];
}
@bindThis
public async run() {
// 20分おきにメモリ情報をDBに書き込み
this.saveIntervalId = setInterval(() => {

View File

@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { AppLockService } from '@/core/AppLockService.js';
import type { User } from '@/models/entities/User.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/active-users.js';
@ -36,6 +37,7 @@ export default class ActiveUsersChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async read(user: { id: User['id'], host: null, createdAt: User['createdAt'] }): Promise<void> {
await this.commit({
'read': [user.id],
@ -48,6 +50,7 @@ export default class ActiveUsersChart extends Chart<typeof schema> {
});
}
@bindThis
public async write(user: { id: User['id'], host: null, createdAt: User['createdAt'] }): Promise<void> {
await this.commit({
'write': [user.id],

View File

@ -2,6 +2,7 @@ import { Injectable, Inject } from '@nestjs/common';
import { DataSource } from 'typeorm';
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/ap-request.js';
@ -31,18 +32,21 @@ export default class ApRequestChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async deliverSucc(): Promise<void> {
await this.commit({
'deliverSucceeded': 1,
});
}
@bindThis
public async deliverFail(): Promise<void> {
await this.commit({
'deliverFailed': 1,
});
}
@bindThis
public async inbox(): Promise<void> {
await this.commit({
'inboxReceived': 1,

View File

@ -3,6 +3,7 @@ import { Not, IsNull, DataSource } from 'typeorm';
import type { DriveFile } from '@/models/entities/DriveFile.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/drive.js';
@ -32,6 +33,7 @@ export default class DriveChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(file: DriveFile, isAdditional: boolean): Promise<void> {
const fileSizeKb = file.size / 1000;
await this.commit(file.userHost === null ? {

View File

@ -4,6 +4,7 @@ import type { FollowingsRepository, InstancesRepository } from '@/models/index.j
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { MetaService } from '@/core/MetaService.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/federation.js';
@ -107,6 +108,7 @@ export default class FederationChart extends Chart<typeof schema> {
};
}
@bindThis
public async deliverd(host: string, succeeded: boolean): Promise<void> {
await this.commit(succeeded ? {
'deliveredInstances': [host],
@ -115,6 +117,7 @@ export default class FederationChart extends Chart<typeof schema> {
});
}
@bindThis
public async inbox(host: string): Promise<void> {
await this.commit({
'inboxInstances': [host],

View File

@ -4,6 +4,7 @@ import type { User } from '@/models/entities/User.js';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/hashtag.js';
@ -34,6 +35,7 @@ export default class HashtagChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(hashtag: string, user: { id: User['id'], host: User['host'] }): Promise<void> {
await this.commit({
'local.users': this.userEntityService.isLocalUser(user) ? [user.id] : [],

View File

@ -6,6 +6,7 @@ import type { Note } from '@/models/entities/Note.js';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/instance.js';
@ -68,12 +69,14 @@ export default class InstanceChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async requestReceived(host: string): Promise<void> {
await this.commit({
'requests.received': 1,
}, this.utilityService.toPuny(host));
}
@bindThis
public async requestSent(host: string, isSucceeded: boolean): Promise<void> {
await this.commit({
'requests.succeeded': isSucceeded ? 1 : 0,
@ -81,6 +84,7 @@ export default class InstanceChart extends Chart<typeof schema> {
}, this.utilityService.toPuny(host));
}
@bindThis
public async newUser(host: string): Promise<void> {
await this.commit({
'users.total': 1,
@ -88,6 +92,7 @@ export default class InstanceChart extends Chart<typeof schema> {
}, this.utilityService.toPuny(host));
}
@bindThis
public async updateNote(host: string, note: Note, isAdditional: boolean): Promise<void> {
await this.commit({
'notes.total': isAdditional ? 1 : -1,
@ -100,6 +105,7 @@ export default class InstanceChart extends Chart<typeof schema> {
}, this.utilityService.toPuny(host));
}
@bindThis
public async updateFollowing(host: string, isAdditional: boolean): Promise<void> {
await this.commit({
'following.total': isAdditional ? 1 : -1,
@ -108,6 +114,7 @@ export default class InstanceChart extends Chart<typeof schema> {
}, this.utilityService.toPuny(host));
}
@bindThis
public async updateFollowers(host: string, isAdditional: boolean): Promise<void> {
await this.commit({
'followers.total': isAdditional ? 1 : -1,
@ -116,6 +123,7 @@ export default class InstanceChart extends Chart<typeof schema> {
}, this.utilityService.toPuny(host));
}
@bindThis
public async updateDrive(file: DriveFile, isAdditional: boolean): Promise<void> {
const fileSizeKb = file.size / 1000;
await this.commit({

View File

@ -4,6 +4,7 @@ import type { NotesRepository } from '@/models/index.js';
import type { Note } from '@/models/entities/Note.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/notes.js';
@ -44,6 +45,7 @@ export default class NotesChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(note: Note, isAdditional: boolean): Promise<void> {
const prefix = note.userHost === null ? 'local' : 'remote';

View File

@ -5,6 +5,7 @@ import type { DriveFile } from '@/models/entities/DriveFile.js';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/per-user-drive.js';
@ -46,6 +47,7 @@ export default class PerUserDriveChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(file: DriveFile, isAdditional: boolean): Promise<void> {
const fileSizeKb = file.size / 1000;
await this.commit({

View File

@ -5,6 +5,7 @@ import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { FollowingsRepository } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/per-user-following.js';
@ -55,6 +56,7 @@ export default class PerUserFollowingChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }, isFollow: boolean): Promise<void> {
const prefixFollower = this.userEntityService.isLocalUser(follower) ? 'local' : 'remote';
const prefixFollowee = this.userEntityService.isLocalUser(followee) ? 'local' : 'remote';

View File

@ -5,6 +5,7 @@ import type { Note } from '@/models/entities/Note.js';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import type { NotesRepository } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/per-user-notes.js';
@ -43,6 +44,7 @@ export default class PerUserNotesChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(user: { id: User['id'] }, note: Note, isAdditional: boolean): Promise<void> {
await this.commit({
'total': isAdditional ? 1 : -1,

View File

@ -5,6 +5,7 @@ import type { Note } from '@/models/entities/Note.js';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/per-user-reactions.js';
@ -35,6 +36,7 @@ export default class PerUserReactionsChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(user: { id: User['id'], host: User['host'] }, note: Note): Promise<void> {
const prefix = this.userEntityService.isLocalUser(user) ? 'local' : 'remote';
this.commit({

View File

@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { name, schema } from './entities/test-grouped.js';
import type { KVs } from '../core.js';
@ -35,6 +36,7 @@ export default class TestGroupedChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async increment(group: string): Promise<void> {
if (this.total[group] == null) this.total[group] = 0;

View File

@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { name, schema } from './entities/test-intersection.js';
import type { KVs } from '../core.js';
@ -31,12 +32,14 @@ export default class TestIntersectionChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async addA(key: string): Promise<void> {
await this.commit({
a: [key],
});
}
@bindThis
public async addB(key: string): Promise<void> {
await this.commit({
b: [key],

View File

@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { name, schema } from './entities/test-unique.js';
import type { KVs } from '../core.js';
@ -31,6 +32,7 @@ export default class TestUniqueChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async uniqueIncrement(key: string): Promise<void> {
await this.commit({
foo: [key],

View File

@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { name, schema } from './entities/test.js';
import type { KVs } from '../core.js';
@ -35,6 +36,7 @@ export default class TestChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async increment(): Promise<void> {
this.total++;
@ -44,6 +46,7 @@ export default class TestChart extends Chart<typeof schema> {
});
}
@bindThis
public async decrement(): Promise<void> {
this.total--;

View File

@ -5,6 +5,7 @@ import { AppLockService } from '@/core/AppLockService.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { UsersRepository } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
import Chart from '../core.js';
import { ChartLoggerService } from '../ChartLoggerService.js';
import { name, schema } from './entities/users.js';
@ -46,6 +47,7 @@ export default class UsersChart extends Chart<typeof schema> {
return {};
}
@bindThis
public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean): Promise<void> {
const prefix = this.userEntityService.isLocalUser(user) ? 'local' : 'remote';

View File

@ -8,6 +8,7 @@ import * as nestedProperty from 'nested-property';
import { EntitySchema, LessThan, Between } from 'typeorm';
import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/misc/prelude/time.js';
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import type { Repository, DataSource } from 'typeorm';
const columnPrefix = '___' as const;
@ -249,6 +250,7 @@ export default abstract class Chart<T extends Schema> {
this.repositoryForDay = db.getRepository<{ id: number; group?: string | null; date: number; }>(day);
}
@bindThis
private convertRawRecord(x: RawRecord<T>): KVs<T> {
const kvs = {} as Record<string, number>;
for (const k of Object.keys(x).filter((k) => k.startsWith(columnPrefix)) as (keyof Columns<T>)[]) {
@ -257,6 +259,7 @@ export default abstract class Chart<T extends Schema> {
return kvs as KVs<T>;
}
@bindThis
private getNewLog(latest: KVs<T> | null): KVs<T> {
const log = {} as Record<keyof T, number>;
for (const [k, v] of Object.entries(this.schema) as ([keyof typeof this['schema'], this['schema'][string]])[]) {
@ -269,6 +272,7 @@ export default abstract class Chart<T extends Schema> {
return log as KVs<T>;
}
@bindThis
private getLatestLog(group: string | null, span: 'hour' | 'day'): Promise<RawRecord<T> | null> {
const repository =
span === 'hour' ? this.repositoryForHour :
@ -288,6 +292,7 @@ export default abstract class Chart<T extends Schema> {
/**
* 現在(=今のHour or Day)のログをデータベースから探して、あればそれを返し、なければ作成して返します。
*/
@bindThis
private async claimCurrentLog(group: string | null, span: 'hour' | 'day'): Promise<RawRecord<T>> {
const [y, m, d, h] = Chart.getCurrentDate();
@ -380,6 +385,7 @@ export default abstract class Chart<T extends Schema> {
});
}
@bindThis
public async save(): Promise<void> {
if (this.buffer.length === 0) {
this.logger.info(`${this.name}: Write skipped`);
@ -498,6 +504,7 @@ export default abstract class Chart<T extends Schema> {
update(logHour, logDay))));
}
@bindThis
public async tick(major: boolean, group: string | null = null): Promise<void> {
const data = major ? await this.tickMajor(group) : await this.tickMinor(group);
@ -533,10 +540,12 @@ export default abstract class Chart<T extends Schema> {
update(logHour, logDay));
}
@bindThis
public resync(group: string | null = null): Promise<void> {
return this.tick(true, group);
}
@bindThis
public async clean(): Promise<void> {
const current = dateUTC(Chart.getCurrentDate());
@ -572,6 +581,7 @@ export default abstract class Chart<T extends Schema> {
]);
}
@bindThis
public async getChartRaw(span: 'hour' | 'day', amount: number, cursor: Date | null, group: string | null = null): Promise<ChartResult<T>> {
const [y, m, d, h, _m, _s, _ms] = cursor ? Chart.parseDate(subtractTime(addTime(cursor, 1, span), 1)) : Chart.getCurrentDate();
const [y2, m2, d2, h2] = cursor ? Chart.parseDate(addTime(cursor, 1, span)) : [] as never;
@ -676,6 +686,7 @@ export default abstract class Chart<T extends Schema> {
return res;
}
@bindThis
public async getChart(span: 'hour' | 'day', amount: number, cursor: Date | null, group: string | null = null): Promise<Unflatten<ChartResult<T>>> {
const result = await this.getChartRaw(span, amount, cursor, group);
const object = {};