2022-02-27 03:07:39 +01:00
|
|
|
import Chart, { KVs } from '../core.js';
|
2022-03-04 08:26:10 +01:00
|
|
|
import { Followings, Instances } from '@/models/index.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { name, schema } from './entities/federation.js';
|
2022-03-04 08:26:10 +01:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-12-14 10:12:37 +01:00
|
|
|
/**
|
|
|
|
* フェデレーションに関するチャート
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-05 16:13:52 +01:00
|
|
|
export default class FederationChart extends Chart<typeof schema> {
|
2019-04-07 14:50:36 +02:00
|
|
|
constructor() {
|
|
|
|
super(name, schema);
|
|
|
|
}
|
|
|
|
|
2022-02-10 09:45:12 +01:00
|
|
|
protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
|
2019-04-07 14:50:36 +02:00
|
|
|
return {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-10 09:45:12 +01:00
|
|
|
protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
|
2022-03-04 08:26:10 +01:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
|
|
|
const suspendedInstancesQuery = Instances.createQueryBuilder('instance')
|
|
|
|
.select('instance.host')
|
|
|
|
.where('instance.isSuspended = true');
|
|
|
|
|
2022-02-23 08:17:16 +01:00
|
|
|
const pubsubSubQuery = Followings.createQueryBuilder('f')
|
|
|
|
.select('f.followerHost')
|
|
|
|
.where('f.followerHost IS NOT NULL');
|
|
|
|
|
2022-03-08 14:56:46 +01:00
|
|
|
const subInstancesQuery = Followings.createQueryBuilder('f')
|
|
|
|
.select('f.followeeHost')
|
|
|
|
.where('f.followeeHost IS NOT NULL');
|
|
|
|
|
|
|
|
const pubInstancesQuery = Followings.createQueryBuilder('f')
|
|
|
|
.select('f.followerHost')
|
|
|
|
.where('f.followerHost IS NOT NULL');
|
|
|
|
|
|
|
|
const [sub, pub, pubsub, subActive, pubActive] = await Promise.all([
|
2022-02-10 09:45:12 +01:00
|
|
|
Followings.createQueryBuilder('following')
|
|
|
|
.select('COUNT(DISTINCT following.followeeHost)')
|
|
|
|
.where('following.followeeHost IS NOT NULL')
|
2022-03-04 10:18:37 +01:00
|
|
|
.andWhere(meta.blockedHosts.length === 0 ? '1=1' : `following.followeeHost NOT IN (:...blocked)`, { blocked: meta.blockedHosts })
|
2022-03-04 08:26:10 +01:00
|
|
|
.andWhere(`following.followeeHost NOT IN (${ suspendedInstancesQuery.getQuery() })`)
|
2022-02-10 09:45:12 +01:00
|
|
|
.getRawOne()
|
|
|
|
.then(x => parseInt(x.count, 10)),
|
|
|
|
Followings.createQueryBuilder('following')
|
|
|
|
.select('COUNT(DISTINCT following.followerHost)')
|
|
|
|
.where('following.followerHost IS NOT NULL')
|
2022-03-04 10:18:37 +01:00
|
|
|
.andWhere(meta.blockedHosts.length === 0 ? '1=1' : `following.followerHost NOT IN (:...blocked)`, { blocked: meta.blockedHosts })
|
2022-03-04 08:26:10 +01:00
|
|
|
.andWhere(`following.followerHost NOT IN (${ suspendedInstancesQuery.getQuery() })`)
|
2022-02-10 09:45:12 +01:00
|
|
|
.getRawOne()
|
|
|
|
.then(x => parseInt(x.count, 10)),
|
2022-02-23 08:17:16 +01:00
|
|
|
Followings.createQueryBuilder('following')
|
|
|
|
.select('COUNT(DISTINCT following.followeeHost)')
|
|
|
|
.where('following.followeeHost IS NOT NULL')
|
2022-03-04 10:18:37 +01:00
|
|
|
.andWhere(meta.blockedHosts.length === 0 ? '1=1' : `following.followeeHost NOT IN (:...blocked)`, { blocked: meta.blockedHosts })
|
2022-03-04 08:26:10 +01:00
|
|
|
.andWhere(`following.followeeHost NOT IN (${ suspendedInstancesQuery.getQuery() })`)
|
2022-03-04 07:32:50 +01:00
|
|
|
.andWhere(`following.followeeHost IN (${ pubsubSubQuery.getQuery() })`)
|
2022-02-23 08:17:16 +01:00
|
|
|
.setParameters(pubsubSubQuery.getParameters())
|
|
|
|
.getRawOne()
|
|
|
|
.then(x => parseInt(x.count, 10)),
|
2022-03-08 14:56:46 +01:00
|
|
|
Instances.createQueryBuilder('instance')
|
|
|
|
.select('COUNT(instance.id)')
|
|
|
|
.where(`instance.host IN (${ subInstancesQuery.getQuery() })`)
|
|
|
|
.andWhere(meta.blockedHosts.length === 0 ? '1=1' : `instance.host NOT IN (:...blocked)`, { blocked: meta.blockedHosts })
|
|
|
|
.andWhere(`instance.isSuspended = false`)
|
|
|
|
.andWhere(`instance.lastCommunicatedAt > :gt`, { gt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)) })
|
|
|
|
.getRawOne()
|
|
|
|
.then(x => parseInt(x.count, 10)),
|
|
|
|
Instances.createQueryBuilder('instance')
|
|
|
|
.select('COUNT(instance.id)')
|
|
|
|
.where(`instance.host IN (${ pubInstancesQuery.getQuery() })`)
|
|
|
|
.andWhere(meta.blockedHosts.length === 0 ? '1=1' : `instance.host NOT IN (:...blocked)`, { blocked: meta.blockedHosts })
|
|
|
|
.andWhere(`instance.isSuspended = false`)
|
|
|
|
.andWhere(`instance.lastCommunicatedAt > :gt`, { gt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)) })
|
|
|
|
.getRawOne()
|
|
|
|
.then(x => parseInt(x.count, 10)),
|
2022-02-10 09:45:12 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
'sub': sub,
|
|
|
|
'pub': pub,
|
2022-02-23 08:17:16 +01:00
|
|
|
'pubsub': pubsub,
|
2022-03-08 14:56:46 +01:00
|
|
|
'subActive': subActive,
|
|
|
|
'pubActive': pubActive,
|
2022-02-10 09:45:12 +01:00
|
|
|
};
|
2022-02-05 16:13:52 +01:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-02-08 15:43:51 +01:00
|
|
|
public async deliverd(host: string, succeeded: boolean): Promise<void> {
|
|
|
|
await this.commit(succeeded ? {
|
2022-02-05 16:13:52 +01:00
|
|
|
'deliveredInstances': [host],
|
2022-02-08 15:43:51 +01:00
|
|
|
} : {
|
|
|
|
'stalled': [host],
|
2022-02-05 16:13:52 +01:00
|
|
|
});
|
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-02-05 16:13:52 +01:00
|
|
|
public async inbox(host: string): Promise<void> {
|
|
|
|
await this.commit({
|
|
|
|
'inboxInstances': [host],
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|