server: rewrite skipped instances query in raw SQL #230

Manually merged
Johann150 merged 2 commits from sql/skipped-instances into main 2022-11-18 21:04:25 +00:00

View file

@ -1,4 +1,5 @@
import { Brackets } from 'typeorm'; import { Brackets } from 'typeorm';
import { db } from '@/db/postgre.js';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import { Instances } from '@/models/index.js'; import { Instances } from '@/models/index.js';
import { Instance } from '@/models/entities/instance.js'; import { Instance } from '@/models/entities/instance.js';
@ -25,19 +26,16 @@ export async function skippedInstances(hosts: Array<Instace['host']>): Array<Ins
const deadTime = new Date(Date.now() - deadThreshold); const deadTime = new Date(Date.now() - deadThreshold);
return skipped.concat( return skipped.concat(
await Instances.createQueryBuilder('instance') await db.query(
.where('instance.host in (:...hosts)', { `SELECT host FROM instance WHERE ("isSuspended" OR "latestStatus" = 410 OR "lastCommunicatedAt" < $1::date) AND host = ANY(string_to_array($2, ','))`,
Johann150 marked this conversation as resolved Outdated
Outdated
Review

This could use template literals instead to avoid having to escape single quotes.

This could use template literals instead to avoid having to escape single quotes.
[
deadTime.toISOString(),
// don't check hosts again that we already know are suspended // don't check hosts again that we already know are suspended
// also avoids adding duplicates to the list // also avoids adding duplicates to the list
hosts: hosts.filter(host => !skipped.includes(host)), hosts.filter(host => !skipped.includes(host) && !host.includes(',')).join(','),
}) ],
.andWhere(new Brackets(qb => { qb )
.where('instance.isSuspended') .then(res => res.map(row => row.host))
.orWhere('instance.lastCommunicatedAt < :deadTime', { deadTime })
.orWhere('instance.latestStatus = 410');
}))
.select('host')
.getRawMany()
); );
} }