From 336912e4422394499cb47ea1b8d42cf2020ec93f Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 7 Feb 2019 15:00:44 +0900 Subject: [PATCH] Improve instance stats --- src/models/instance.ts | 20 +++++++++++++ src/queue/processors/http/deliver.ts | 26 ++++++++++++++++- src/remote/activitypub/models/person.ts | 4 +-- src/services/following/create.ts | 28 +++++++++++++++++++ src/services/note/create.ts | 4 +-- ...e.ts => register-or-fetch-instance-doc.ts} | 2 +- 6 files changed, 78 insertions(+), 6 deletions(-) rename src/services/{register-instance.ts => register-or-fetch-instance-doc.ts} (82%) diff --git a/src/models/instance.ts b/src/models/instance.ts index d3906df42..7a94a0a04 100644 --- a/src/models/instance.ts +++ b/src/models/instance.ts @@ -32,4 +32,24 @@ export interface IInstance { * このインスタンスから受け取った投稿数 */ notesCount: number; + + /** + * このインスタンスのユーザーからフォローされている、自インスタンスのユーザーの数 + */ + followingCount: number; + + /** + * このインスタンスのユーザーをフォローしている、自インスタンスのユーザーの数 + */ + followersCount: number; + + /** + * 最近の通信日時 + */ + latestRequestSentAt?: Date; + + /** + * 最近の通信のHTTPステータス + */ + latestStatus?: number; } diff --git a/src/queue/processors/http/deliver.ts b/src/queue/processors/http/deliver.ts index d1dad55cd..cb92a4d76 100644 --- a/src/queue/processors/http/deliver.ts +++ b/src/queue/processors/http/deliver.ts @@ -2,19 +2,43 @@ import * as bq from 'bee-queue'; import request from '../../../remote/activitypub/request'; import { queueLogger } from '../../logger'; +import { registerOrFetchInstanceDoc } from '../../../services/register-or-fetch-instance-doc'; +import Instance from '../../../models/instance'; export default async (job: bq.Job, done: any): Promise => { try { await request(job.data.user, job.data.to, job.data.content); + + // Update stats + registerOrFetchInstanceDoc(job.data.user.host).then(i => { + Instance.update({ _id: i._id }, { + $set: { + latestRequestSentAt: new Date(), + latestStatus: 200 + } + }); + }); + done(); } catch (res) { + // Update stats + registerOrFetchInstanceDoc(job.data.user.host).then(i => { + Instance.update({ _id: i._id }, { + $set: { + latestRequestSentAt: new Date(), + latestStatus: res != null && res.hasOwnProperty('statusCode') ? res.statusCode : null + } + }); + }); + if (res != null && res.hasOwnProperty('statusCode')) { + queueLogger.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`); + if (res.statusCode >= 400 && res.statusCode < 500) { // HTTPステータスコード4xxはクライアントエラーであり、それはつまり // 何回再送しても成功することはないということなのでエラーにはしないでおく done(); } else { - queueLogger.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`); done(res.statusMessage); } } else { diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index a4173c5e8..2902b4b7f 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -12,7 +12,7 @@ import { fromHtml } from '../../../mfm/fromHtml'; import usersChart from '../../../chart/users'; import { URL } from 'url'; import { resolveNote, extractEmojis } from './note'; -import registerInstance from '../../../services/register-instance'; +import { registerOrFetchInstanceDoc } from '../../../services/register-or-fetch-instance-doc'; import Instance from '../../../models/instance'; import getDriveFileUrl from '../../../misc/get-drive-file-url'; import { IEmoji } from '../../../models/emoji'; @@ -188,7 +188,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise { + registerOrFetchInstanceDoc(host).then(i => { Instance.update({ _id: i._id }, { $inc: { usersCount: 1 diff --git a/src/services/following/create.ts b/src/services/following/create.ts index 936655d7d..4df271a97 100644 --- a/src/services/following/create.ts +++ b/src/services/following/create.ts @@ -10,6 +10,8 @@ import renderReject from '../../remote/activitypub/renderer/reject'; import { deliver } from '../../queue'; import createFollowRequest from './requests/create'; import perUserFollowingChart from '../../chart/per-user-following'; +import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc'; +import Instance from '../../models/instance'; export default async function(follower: IUser, followee: IUser, requestId?: string) { // check blocking @@ -97,6 +99,32 @@ export default async function(follower: IUser, followee: IUser, requestId?: stri }); //#endregion + //#region Update instance stats + if (isRemoteUser(follower) && isLocalUser(followee)) { + registerOrFetchInstanceDoc(follower.host).then(i => { + Instance.update({ _id: i._id }, { + $inc: { + followingCount: 1 + } + }); + + // TODO + //perInstanceChart.newFollowing(); + }); + } else if (isLocalUser(follower) && isRemoteUser(followee)) { + registerOrFetchInstanceDoc(followee.host).then(i => { + Instance.update({ _id: i._id }, { + $inc: { + followersCount: 1 + } + }); + + // TODO + //perInstanceChart.newFollower(); + }); + } + //#endregion + perUserFollowingChart.update(follower, followee, true); // Publish follow event diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 6afdcbbfb..7b5975bac 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -27,7 +27,7 @@ import activeUsersChart from '../../chart/active-users'; import { erase, concat } from '../../prelude/array'; import insertNoteUnread from './unread'; -import registerInstance from '../register-instance'; +import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc'; import Instance from '../../models/instance'; import extractMentions from '../../misc/extract-mentions'; import extractEmojis from '../../misc/extract-emojis'; @@ -222,7 +222,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise< // Register host if (isRemoteUser(user)) { - registerInstance(user.host).then(i => { + registerOrFetchInstanceDoc(user.host).then(i => { Instance.update({ _id: i._id }, { $inc: { notesCount: 1 diff --git a/src/services/register-instance.ts b/src/services/register-or-fetch-instance-doc.ts similarity index 82% rename from src/services/register-instance.ts rename to src/services/register-or-fetch-instance-doc.ts index 6576a9ed0..3b338b48a 100644 --- a/src/services/register-instance.ts +++ b/src/services/register-or-fetch-instance-doc.ts @@ -1,7 +1,7 @@ import Instance, { IInstance } from '../models/instance'; import federationChart from '../chart/federation'; -export default async function(host: string): Promise { +export async function registerOrFetchInstanceDoc(host: string): Promise { if (host == null) return null; const index = await Instance.findOne({ host });