Resolve #2328
This commit is contained in:
@ -6,6 +6,15 @@ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
||||
|
||||
function migrateStats(stats: IStats[]) {
|
||||
stats.forEach(stat => {
|
||||
if (stat.network == null) {
|
||||
stat.network = {
|
||||
requests: 0,
|
||||
totalTime: 0,
|
||||
incomingBytes: 0,
|
||||
outgoingBytes: 0
|
||||
};
|
||||
}
|
||||
|
||||
const isOldData =
|
||||
stat.users.local.inc == null ||
|
||||
stat.users.local.dec == null ||
|
||||
@ -180,6 +189,12 @@ export default (params: any) => new Promise(async (res, rej) => {
|
||||
decCount: 0,
|
||||
decSize: 0
|
||||
}
|
||||
},
|
||||
network: {
|
||||
requests: 0,
|
||||
totalTime: 0,
|
||||
incomingBytes: 0,
|
||||
outgoingBytes: 0
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@ -236,6 +251,12 @@ export default (params: any) => new Promise(async (res, rej) => {
|
||||
decCount: 0,
|
||||
decSize: 0
|
||||
}
|
||||
},
|
||||
network: {
|
||||
requests: 0,
|
||||
totalTime: 0,
|
||||
incomingBytes: 0,
|
||||
outgoingBytes: 0
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -11,11 +11,13 @@ import * as Router from 'koa-router';
|
||||
import * as mount from 'koa-mount';
|
||||
import * as compress from 'koa-compress';
|
||||
import * as logger from 'koa-logger';
|
||||
const requestStats = require('request-stats');
|
||||
//const slow = require('koa-slow');
|
||||
|
||||
import activityPub from './activitypub';
|
||||
import webFinger from './webfinger';
|
||||
import config from '../config';
|
||||
import { updateNetworkStats } from '../services/update-chart';
|
||||
|
||||
// Init app
|
||||
const app = new Koa();
|
||||
@ -81,4 +83,27 @@ export default () => new Promise(resolve => {
|
||||
|
||||
// Listen
|
||||
server.listen(config.port, resolve);
|
||||
|
||||
//#region Network stats
|
||||
let queue: any[] = [];
|
||||
|
||||
requestStats(server, (stats: any) => {
|
||||
if (stats.ok) {
|
||||
queue.push(stats);
|
||||
}
|
||||
});
|
||||
|
||||
// Bulk write
|
||||
setInterval(() => {
|
||||
if (queue.length == 0) return;
|
||||
|
||||
const requests = queue.length;
|
||||
const time = queue.reduce((a, b) => a + b.time, 0);
|
||||
const incomingBytes = queue.reduce((a, b) => a + b.req.bytes, 0);
|
||||
const outgoingBytes = queue.reduce((a, b) => a + b.res.bytes, 0);
|
||||
queue = [];
|
||||
|
||||
updateNetworkStats(requests, time, incomingBytes, outgoingBytes);
|
||||
}, 5000);
|
||||
//#endregion
|
||||
});
|
||||
|
Reference in New Issue
Block a user