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

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