Improve instance stats

This commit is contained in:
syuilo 2019-02-07 15:00:44 +09:00
parent 0bf602bae6
commit 336912e442
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
6 changed files with 78 additions and 6 deletions

View file

@ -32,4 +32,24 @@ export interface IInstance {
* 稿 * 稿
*/ */
notesCount: number; notesCount: number;
/**
*
*/
followingCount: number;
/**
*
*/
followersCount: number;
/**
*
*/
latestRequestSentAt?: Date;
/**
* HTTPステータス
*/
latestStatus?: number;
} }

View file

@ -2,19 +2,43 @@ import * as bq from 'bee-queue';
import request from '../../../remote/activitypub/request'; import request from '../../../remote/activitypub/request';
import { queueLogger } from '../../logger'; 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<void> => { export default async (job: bq.Job, done: any): Promise<void> => {
try { try {
await request(job.data.user, job.data.to, job.data.content); 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(); done();
} catch (res) { } 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')) { 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) { if (res.statusCode >= 400 && res.statusCode < 500) {
// HTTPステータスコード4xxはクライアントエラーであり、それはつまり // HTTPステータスコード4xxはクライアントエラーであり、それはつまり
// 何回再送しても成功することはないということなのでエラーにはしないでおく // 何回再送しても成功することはないということなのでエラーにはしないでおく
done(); done();
} else { } else {
queueLogger.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`);
done(res.statusMessage); done(res.statusMessage);
} }
} else { } else {

View file

@ -12,7 +12,7 @@ import { fromHtml } from '../../../mfm/fromHtml';
import usersChart from '../../../chart/users'; import usersChart from '../../../chart/users';
import { URL } from 'url'; import { URL } from 'url';
import { resolveNote, extractEmojis } from './note'; 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 Instance from '../../../models/instance';
import getDriveFileUrl from '../../../misc/get-drive-file-url'; import getDriveFileUrl from '../../../misc/get-drive-file-url';
import { IEmoji } from '../../../models/emoji'; import { IEmoji } from '../../../models/emoji';
@ -188,7 +188,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<IU
} }
// Register host // Register host
registerInstance(host).then(i => { registerOrFetchInstanceDoc(host).then(i => {
Instance.update({ _id: i._id }, { Instance.update({ _id: i._id }, {
$inc: { $inc: {
usersCount: 1 usersCount: 1

View file

@ -10,6 +10,8 @@ import renderReject from '../../remote/activitypub/renderer/reject';
import { deliver } from '../../queue'; import { deliver } from '../../queue';
import createFollowRequest from './requests/create'; import createFollowRequest from './requests/create';
import perUserFollowingChart from '../../chart/per-user-following'; 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) { export default async function(follower: IUser, followee: IUser, requestId?: string) {
// check blocking // check blocking
@ -97,6 +99,32 @@ export default async function(follower: IUser, followee: IUser, requestId?: stri
}); });
//#endregion //#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); perUserFollowingChart.update(follower, followee, true);
// Publish follow event // Publish follow event

View file

@ -27,7 +27,7 @@ import activeUsersChart from '../../chart/active-users';
import { erase, concat } from '../../prelude/array'; import { erase, concat } from '../../prelude/array';
import insertNoteUnread from './unread'; import insertNoteUnread from './unread';
import registerInstance from '../register-instance'; import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
import Instance from '../../models/instance'; import Instance from '../../models/instance';
import extractMentions from '../../misc/extract-mentions'; import extractMentions from '../../misc/extract-mentions';
import extractEmojis from '../../misc/extract-emojis'; import extractEmojis from '../../misc/extract-emojis';
@ -222,7 +222,7 @@ export default async (user: IUser, data: Option, silent = false) => new Promise<
// Register host // Register host
if (isRemoteUser(user)) { if (isRemoteUser(user)) {
registerInstance(user.host).then(i => { registerOrFetchInstanceDoc(user.host).then(i => {
Instance.update({ _id: i._id }, { Instance.update({ _id: i._id }, {
$inc: { $inc: {
notesCount: 1 notesCount: 1

View file

@ -1,7 +1,7 @@
import Instance, { IInstance } from '../models/instance'; import Instance, { IInstance } from '../models/instance';
import federationChart from '../chart/federation'; import federationChart from '../chart/federation';
export default async function(host: string): Promise<IInstance> { export async function registerOrFetchInstanceDoc(host: string): Promise<IInstance> {
if (host == null) return null; if (host == null) return null;
const index = await Instance.findOne({ host }); const index = await Instance.findOne({ host });