refactor: migrate to typeorm 3.0 (#8443)
* wip * wip * wip * Update following.ts * wip * wip * wip * Update resolve-user.ts * maxQueryExecutionTime * wip * wip
This commit is contained in:
		@@ -22,11 +22,11 @@ export default class InstanceChart extends Chart<typeof schema> {
 | 
			
		||||
			followersCount,
 | 
			
		||||
			driveFiles,
 | 
			
		||||
		] = await Promise.all([
 | 
			
		||||
			Notes.count({ userHost: group }),
 | 
			
		||||
			Users.count({ host: group }),
 | 
			
		||||
			Followings.count({ followerHost: group }),
 | 
			
		||||
			Followings.count({ followeeHost: group }),
 | 
			
		||||
			DriveFiles.count({ userHost: group }),
 | 
			
		||||
			Notes.countBy({ userHost: group }),
 | 
			
		||||
			Users.countBy({ host: group }),
 | 
			
		||||
			Followings.countBy({ followerHost: group }),
 | 
			
		||||
			Followings.countBy({ followeeHost: group }),
 | 
			
		||||
			DriveFiles.countBy({ userHost: group }),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,8 +15,8 @@ export default class NotesChart extends Chart<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()) }),
 | 
			
		||||
			Notes.countBy({ userHost: IsNull() }),
 | 
			
		||||
			Notes.countBy({ userHost: Not(IsNull()) }),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ export default class PerUserDriveChart extends Chart<typeof schema> {
 | 
			
		||||
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [count, size] = await Promise.all([
 | 
			
		||||
			DriveFiles.count({ userId: group }),
 | 
			
		||||
			DriveFiles.countBy({ userId: group }),
 | 
			
		||||
			DriveFiles.calcDriveUsageOf(group),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -20,10 +20,10 @@ export default class PerUserFollowingChart extends Chart<typeof schema> {
 | 
			
		||||
			remoteFollowingsCount,
 | 
			
		||||
			remoteFollowersCount,
 | 
			
		||||
		] = await Promise.all([
 | 
			
		||||
			Followings.count({ followerId: group, followeeHost: null }),
 | 
			
		||||
			Followings.count({ followeeId: group, followerHost: null }),
 | 
			
		||||
			Followings.count({ followerId: group, followeeHost: Not(IsNull()) }),
 | 
			
		||||
			Followings.count({ followeeId: group, followerHost: Not(IsNull()) }),
 | 
			
		||||
			Followings.countBy({ followerId: group, followeeHost: IsNull() }),
 | 
			
		||||
			Followings.countBy({ followeeId: group, followerHost: IsNull() }),
 | 
			
		||||
			Followings.countBy({ followerId: group, followeeHost: Not(IsNull()) }),
 | 
			
		||||
			Followings.countBy({ followeeId: group, followerHost: Not(IsNull()) }),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ export default class PerUserNotesChart extends Chart<typeof schema> {
 | 
			
		||||
 | 
			
		||||
	protected async tickMajor(group: string): Promise<Partial<KVs<typeof schema>>> {
 | 
			
		||||
		const [count] = await Promise.all([
 | 
			
		||||
			Notes.count({ userId: group }),
 | 
			
		||||
			Notes.countBy({ userId: group }),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,8 +15,8 @@ export default class UsersChart extends Chart<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()) }),
 | 
			
		||||
			Users.countBy({ host: IsNull() }),
 | 
			
		||||
			Users.countBy({ host: Not(IsNull()) }),
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		return {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,9 +6,10 @@
 | 
			
		||||
 | 
			
		||||
import * as nestedProperty from 'nested-property';
 | 
			
		||||
import Logger from '../logger.js';
 | 
			
		||||
import { EntitySchema, getRepository, Repository, LessThan, Between } from 'typeorm';
 | 
			
		||||
import { EntitySchema, Repository, LessThan, Between } from 'typeorm';
 | 
			
		||||
import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/prelude/time.js';
 | 
			
		||||
import { getChartInsertLock } from '@/misc/app-lock.js';
 | 
			
		||||
import { db } from '@/db/postgre.js';
 | 
			
		||||
 | 
			
		||||
const logger = new Logger('chart', 'white', process.env.NODE_ENV !== 'test');
 | 
			
		||||
 | 
			
		||||
@@ -241,8 +242,8 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
		this.schema = schema;
 | 
			
		||||
 | 
			
		||||
		const { hour, day } = Chart.schemaToEntity(name, schema, grouped);
 | 
			
		||||
		this.repositoryForHour = getRepository<{ id: number; group?: string | null; date: number; }>(hour);
 | 
			
		||||
		this.repositoryForDay = getRepository<{ id: number; group?: string | null; date: number; }>(day);
 | 
			
		||||
		this.repositoryForHour = db.getRepository<{ id: number; group?: string | null; date: number; }>(hour);
 | 
			
		||||
		this.repositoryForDay = db.getRepository<{ id: number; group?: string | null; date: number; }>(day);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private convertRawRecord(x: RawRecord<T>): KVs<T> {
 | 
			
		||||
@@ -271,9 +272,10 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
			span === 'day' ? this.repositoryForDay :
 | 
			
		||||
			new Error('not happen') as never;
 | 
			
		||||
 | 
			
		||||
		return repository.findOne(group ? {
 | 
			
		||||
			group: group,
 | 
			
		||||
		} : {}, {
 | 
			
		||||
		return repository.findOne({
 | 
			
		||||
			where: group ? {
 | 
			
		||||
				group: group,
 | 
			
		||||
			} : {},
 | 
			
		||||
			order: {
 | 
			
		||||
				date: -1,
 | 
			
		||||
			},
 | 
			
		||||
@@ -297,7 +299,7 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
			new Error('not happen') as never;
 | 
			
		||||
 | 
			
		||||
		// 現在(=今のHour or Day)のログ
 | 
			
		||||
		const currentLog = await repository.findOne({
 | 
			
		||||
		const currentLog = await repository.findOneBy({
 | 
			
		||||
			date: Chart.dateToTimestamp(current),
 | 
			
		||||
			...(group ? { group: group } : {}),
 | 
			
		||||
		}) as RawRecord<T> | undefined;
 | 
			
		||||
@@ -337,7 +339,7 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
		const unlock = await getChartInsertLock(lockKey);
 | 
			
		||||
		try {
 | 
			
		||||
			// ロック内でもう1回チェックする
 | 
			
		||||
			const currentLog = await repository.findOne({
 | 
			
		||||
			const currentLog = await repository.findOneBy({
 | 
			
		||||
				date: date,
 | 
			
		||||
				...(group ? { group: group } : {}),
 | 
			
		||||
			}) as RawRecord<T> | undefined;
 | 
			
		||||
@@ -356,7 +358,7 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
				date: date,
 | 
			
		||||
				...(group ? { group: group } : {}),
 | 
			
		||||
				...columns,
 | 
			
		||||
			}).then(x => repository.findOneOrFail(x.identifiers[0])) as RawRecord<T>;
 | 
			
		||||
			}).then(x => repository.findOneByOrFail(x.identifiers[0])) as RawRecord<T>;
 | 
			
		||||
 | 
			
		||||
			logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): New commit created`);
 | 
			
		||||
 | 
			
		||||
@@ -598,9 +600,10 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
		if (logs.length === 0) {
 | 
			
		||||
			// もっとも新しいログを持ってくる
 | 
			
		||||
			// (すくなくともひとつログが無いと隙間埋めできないため)
 | 
			
		||||
			const recentLog = await repository.findOne(group ? {
 | 
			
		||||
				group: group,
 | 
			
		||||
			} : {}, {
 | 
			
		||||
			const recentLog = await repository.findOne({
 | 
			
		||||
				where: group ? {
 | 
			
		||||
					group: group,
 | 
			
		||||
				} : {},
 | 
			
		||||
				order: {
 | 
			
		||||
					date: -1,
 | 
			
		||||
				},
 | 
			
		||||
@@ -615,9 +618,10 @@ export default abstract class Chart<T extends Schema> {
 | 
			
		||||
			// 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する
 | 
			
		||||
			// (隙間埋めできないため)
 | 
			
		||||
			const outdatedLog = await repository.findOne({
 | 
			
		||||
				date: LessThan(Chart.dateToTimestamp(gt)),
 | 
			
		||||
				...(group ? { group: group } : {}),
 | 
			
		||||
			}, {
 | 
			
		||||
				where: {
 | 
			
		||||
					date: LessThan(Chart.dateToTimestamp(gt)),
 | 
			
		||||
					...(group ? { group: group } : {}),
 | 
			
		||||
				},
 | 
			
		||||
				order: {
 | 
			
		||||
					date: -1,
 | 
			
		||||
				},
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user