enhance: improve federation chart
This commit is contained in:
		@@ -18,7 +18,12 @@ export default class ActiveUsersChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,12 @@ export default class ApRequestChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,12 @@ export default class DriveChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,12 +3,11 @@ import Chart from '../../core';
 | 
			
		||||
export const name = 'federation';
 | 
			
		||||
 | 
			
		||||
export const schema = {
 | 
			
		||||
	'instance.total': { accumulate: true },
 | 
			
		||||
	'instance.inc': { range: 'small' },
 | 
			
		||||
	'instance.dec': { range: 'small' },
 | 
			
		||||
	'deliveredInstances': { uniqueIncrement: true, range: 'small' },
 | 
			
		||||
	'inboxInstances': { uniqueIncrement: true, range: 'small' },
 | 
			
		||||
	'stalled': { uniqueIncrement: true, range: 'small' },
 | 
			
		||||
	'sub': { accumulate: true, range: 'small' },
 | 
			
		||||
	'pub': { accumulate: true, range: 'small' },
 | 
			
		||||
} as const;
 | 
			
		||||
 | 
			
		||||
export const entity = Chart.schemaToEntity(name, schema);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
import autobind from 'autobind-decorator';
 | 
			
		||||
import Chart, { KVs } from '../core';
 | 
			
		||||
import { Instances } from '@/models/index';
 | 
			
		||||
import { Followings } from '@/models/index';
 | 
			
		||||
import { name, schema } from './entities/federation';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -13,23 +13,30 @@ export default class FederationChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [total] = await Promise.all([
 | 
			
		||||
			Instances.count({}),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {
 | 
			
		||||
			'instance.total': total,
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(isAdditional: boolean): Promise<void> {
 | 
			
		||||
		await this.commit({
 | 
			
		||||
			'instance.total': isAdditional ? 1 : -1,
 | 
			
		||||
			'instance.inc': isAdditional ? 1 : 0,
 | 
			
		||||
			'instance.dec': isAdditional ? 0 : 1,
 | 
			
		||||
		});
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [sub, pub] = await Promise.all([
 | 
			
		||||
			Followings.createQueryBuilder('following')
 | 
			
		||||
				.select('COUNT(DISTINCT following.followeeHost)')
 | 
			
		||||
				.where('following.followeeHost IS NOT NULL')
 | 
			
		||||
				.getRawOne()
 | 
			
		||||
				.then(x => parseInt(x.count, 10)),
 | 
			
		||||
			Followings.createQueryBuilder('following')
 | 
			
		||||
				.select('COUNT(DISTINCT following.followerHost)')
 | 
			
		||||
				.where('following.followerHost IS NOT NULL')
 | 
			
		||||
				.getRawOne()
 | 
			
		||||
				.then(x => parseInt(x.count, 10)),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
			'sub': sub,
 | 
			
		||||
			'pub': pub,
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,12 @@ export default class HashtagChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ export default class InstanceChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [
 | 
			
		||||
			notesCount,
 | 
			
		||||
			usersCount,
 | 
			
		||||
@@ -42,6 +42,11 @@ export default class InstanceChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async requestReceived(host: string): Promise<void> {
 | 
			
		||||
		await this.commit({
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ export default class NotesChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [localCount, remoteCount] = await Promise.all([
 | 
			
		||||
			Notes.count({ userHost: null }),
 | 
			
		||||
			Notes.count({ userHost: Not(IsNull()) }),
 | 
			
		||||
@@ -27,6 +27,11 @@ export default class NotesChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(note: Note, isAdditional: boolean): Promise<void> {
 | 
			
		||||
		const prefix = note.userHost === null ? 'local' : 'remote';
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ export default class PerUserDriveChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [count, size] = await Promise.all([
 | 
			
		||||
			DriveFiles.count({ userId: group }),
 | 
			
		||||
			DriveFiles.calcDriveUsageOf(group),
 | 
			
		||||
@@ -26,6 +26,11 @@ export default class PerUserDriveChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(file: DriveFile, isAdditional: boolean): Promise<void> {
 | 
			
		||||
		const fileSizeKb = file.size / 1000;
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ export default class PerUserFollowingChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [
 | 
			
		||||
			localFollowingsCount,
 | 
			
		||||
			localFollowersCount,
 | 
			
		||||
@@ -36,6 +36,11 @@ export default class PerUserFollowingChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }, isFollow: boolean): Promise<void> {
 | 
			
		||||
		const prefixFollower = Users.isLocalUser(follower) ? 'local' : 'remote';
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ export default class PerUserNotesChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [count] = await Promise.all([
 | 
			
		||||
			Notes.count({ userId: group }),
 | 
			
		||||
		]);
 | 
			
		||||
@@ -25,6 +25,11 @@ export default class PerUserNotesChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(user: { id: User['id'] }, note: Note, isAdditional: boolean): Promise<void> {
 | 
			
		||||
		await this.commit({
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,12 @@ export default class PerUserReactionsChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,12 +14,17 @@ export default class TestGroupedChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {
 | 
			
		||||
			'foo.total': this.total[group],
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async increment(group: string): Promise<void> {
 | 
			
		||||
		if (this.total[group] == null) this.total[group] = 0;
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,12 @@ export default class TestIntersectionChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,12 @@ export default class TestUniqueChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,12 +14,17 @@ export default class TestChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {
 | 
			
		||||
			'foo.total': this.total,
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async increment(): Promise<void> {
 | 
			
		||||
		this.total++;
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ export default class UsersChart extends Chart<typeof schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async queryCurrentState(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
	protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [localCount, remoteCount] = await Promise.all([
 | 
			
		||||
			Users.count({ host: null }),
 | 
			
		||||
			Users.count({ host: Not(IsNull()) }),
 | 
			
		||||
@@ -27,6 +27,11 @@ export default class UsersChart extends Chart<typeof schema> {
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		return {};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean): Promise<void> {
 | 
			
		||||
		const prefix = Users.isLocalUser(user) ? 'local' : 'remote';
 | 
			
		||||
 
 | 
			
		||||
@@ -81,7 +81,15 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
	protected repositoryForHour: Repository<RawRecord<T>>;
 | 
			
		||||
	protected repositoryForDay: Repository<RawRecord<T>>;
 | 
			
		||||
 | 
			
		||||
	protected abstract queryCurrentState(group: string | null): Promise<Partial<KVs<T>>>;
 | 
			
		||||
	/**
 | 
			
		||||
	 * 1日に一回程度実行されれば良いような計算処理を入れる(主にCASCADE削除などアプリケーション側で感知できない変動によるズレの修正用)
 | 
			
		||||
	 */
 | 
			
		||||
	protected abstract tickMajor(group: string | null): Promise<Partial<KVs<T>>>;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 少なくとも最小スパン内に1回は実行されて欲しい計算処理を入れる
 | 
			
		||||
	 */
 | 
			
		||||
	protected abstract tickMinor(group: string | null): Promise<Partial<KVs<T>>>;
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	private static convertSchemaToColumnDefinitions(schema: Schema): Record<string, { type: string; array?: boolean; default?: any; }> {
 | 
			
		||||
@@ -445,8 +453,8 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async resync(group: string | null = null): Promise<void> {
 | 
			
		||||
		const data = await this.queryCurrentState(group);
 | 
			
		||||
	public async tick(major: boolean, group: string | null = null): Promise<void> {
 | 
			
		||||
		const data = major ? await this.tickMajor(group) : await this.tickMinor(group);
 | 
			
		||||
 | 
			
		||||
		const columns = {} as Record<string, number>;
 | 
			
		||||
		for (const [k, v] of Object.entries(data)) {
 | 
			
		||||
@@ -480,6 +488,11 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
			update(logHour, logDay));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public resync(group: string | null = null): Promise<void> {
 | 
			
		||||
		return this.tick(true, group);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@autobind
 | 
			
		||||
	public async clean(): Promise<void> {
 | 
			
		||||
		const current = dateUTC(Chart.getCurrentDate());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user