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;
/**
*
*/
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 { 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> => {
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 {

View file

@ -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<IU
}
// Register host
registerInstance(host).then(i => {
registerOrFetchInstanceDoc(host).then(i => {
Instance.update({ _id: i._id }, {
$inc: {
usersCount: 1

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
import Instance, { IInstance } from '../models/instance';
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;
const index = await Instance.findOne({ host });