From b929bffea5301b98e3a6c6775cd77823d113bba0 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Tue, 8 Mar 2022 22:56:46 +0900
Subject: [PATCH] fix federation chart

---
 ...2390560-chart-federation-active-sub-pub.js | 21 +++++++++++
 .../chart/charts/entities/federation.ts       |  3 +-
 .../src/services/chart/charts/federation.ts   | 35 ++++++++++++++-----
 packages/client/src/components/chart.vue      | 13 ++++---
 4 files changed, 59 insertions(+), 13 deletions(-)
 create mode 100644 packages/backend/migration/1646732390560-chart-federation-active-sub-pub.js

diff --git a/packages/backend/migration/1646732390560-chart-federation-active-sub-pub.js b/packages/backend/migration/1646732390560-chart-federation-active-sub-pub.js
new file mode 100644
index 0000000000..c9a847cbcf
--- /dev/null
+++ b/packages/backend/migration/1646732390560-chart-federation-active-sub-pub.js
@@ -0,0 +1,21 @@
+export class chartFederationActiveSubPub1646732390560 {
+    name = 'chartFederationActiveSubPub1646732390560'
+
+    async up(queryRunner) {
+        await queryRunner.query(`ALTER TABLE "__chart__federation" DROP COLUMN "___active"`);
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" DROP COLUMN "___active"`);
+        await queryRunner.query(`ALTER TABLE "__chart__federation" ADD "___subActive" smallint NOT NULL DEFAULT '0'`);
+        await queryRunner.query(`ALTER TABLE "__chart__federation" ADD "___pubActive" smallint NOT NULL DEFAULT '0'`);
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" ADD "___subActive" smallint NOT NULL DEFAULT '0'`);
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" ADD "___pubActive" smallint NOT NULL DEFAULT '0'`);
+    }
+
+    async down(queryRunner) {
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" DROP COLUMN "___pubActive"`);
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" DROP COLUMN "___subActive"`);
+        await queryRunner.query(`ALTER TABLE "__chart__federation" DROP COLUMN "___pubActive"`);
+        await queryRunner.query(`ALTER TABLE "__chart__federation" DROP COLUMN "___subActive"`);
+        await queryRunner.query(`ALTER TABLE "__chart_day__federation" ADD "___active" smallint NOT NULL DEFAULT '0'`);
+        await queryRunner.query(`ALTER TABLE "__chart__federation" ADD "___active" smallint NOT NULL DEFAULT '0'`);
+    }
+}
diff --git a/packages/backend/src/services/chart/charts/entities/federation.ts b/packages/backend/src/services/chart/charts/entities/federation.ts
index c6d59b67f6..a8466b0b4c 100644
--- a/packages/backend/src/services/chart/charts/entities/federation.ts
+++ b/packages/backend/src/services/chart/charts/entities/federation.ts
@@ -9,7 +9,8 @@ export const schema = {
 	'sub': { accumulate: true, range: 'small' },
 	'pub': { accumulate: true, range: 'small' },
 	'pubsub': { accumulate: true, range: 'small' },
-	'active': { accumulate: true, range: 'small' },
+	'subActive': { accumulate: true, range: 'small' },
+	'pubActive': { accumulate: true, range: 'small' },
 } as const;
 
 export const entity = Chart.schemaToEntity(name, schema);
diff --git a/packages/backend/src/services/chart/charts/federation.ts b/packages/backend/src/services/chart/charts/federation.ts
index 2a28500ace..10221ee1e3 100644
--- a/packages/backend/src/services/chart/charts/federation.ts
+++ b/packages/backend/src/services/chart/charts/federation.ts
@@ -2,7 +2,6 @@ import Chart, { KVs } from '../core.js';
 import { Followings, Instances } from '@/models/index.js';
 import { name, schema } from './entities/federation.js';
 import { fetchMeta } from '@/misc/fetch-meta.js';
-import { In, MoreThan, Not } from 'typeorm';
 
 /**
  * フェデレーションに関するチャート
@@ -29,7 +28,15 @@ export default class FederationChart extends Chart<typeof schema> {
 			.select('f.followerHost')
 			.where('f.followerHost IS NOT NULL');
 
-		const [sub, pub, pubsub, active] = await Promise.all([
+		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([
 			Followings.createQueryBuilder('following')
 				.select('COUNT(DISTINCT following.followeeHost)')
 				.where('following.followeeHost IS NOT NULL')
@@ -53,18 +60,30 @@ export default class FederationChart extends Chart<typeof schema> {
 				.setParameters(pubsubSubQuery.getParameters())
 				.getRawOne()
 				.then(x => parseInt(x.count, 10)),
-			Instances.count({
-				host: Not(In(meta.blockedHosts)),
-				isSuspended: false,
-				lastCommunicatedAt: MoreThan(new Date(Date.now() - (1000 * 60 * 60 * 24 * 30))),
-			}),
+			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)),
 		]);
 
 		return {
 			'sub': sub,
 			'pub': pub,
 			'pubsub': pubsub,
-			'active': active,
+			'subActive': subActive,
+			'pubActive': pubActive,
 		};
 	}
 
diff --git a/packages/client/src/components/chart.vue b/packages/client/src/components/chart.vue
index 7a716036bb..13ca60efa8 100644
--- a/packages/client/src/components/chart.vue
+++ b/packages/client/src/components/chart.vue
@@ -70,7 +70,7 @@ const colors = {
 	red: '#FF4560',
 	purple: '#e300db',
 	orange: '#fe6919',
-	lime: '#c7f400',
+	lime: '#bde800',
 	cyan: '#00efef',
 };
 const colorSets = [colors.blue, colors.green, colors.yellow, colors.red, colors.purple];
@@ -390,10 +390,15 @@ export default defineComponent({
 					data: format(raw.stalled),
 					color: colors.red,
 				}, {
-					name: 'Active',
+					name: 'Pub Active',
 					type: 'line',
-					data: format(raw.active),
-					color: colors.lime,
+					data: format(raw.pubActive),
+					color: colors.purple,
+				}, {
+					name: 'Sub Active',
+					type: 'line',
+					data: format(raw.subActive),
+					color: colors.orange,
 				}, {
 					name: 'Pub & Sub',
 					type: 'line',