wip
This commit is contained in:
		
							parent
							
								
									e70fb71a04
								
							
						
					
					
						commit
						dc3c80e3ce
					
				
					 8 changed files with 140 additions and 7 deletions
				
			
		
							
								
								
									
										60
									
								
								src/daemons/hashtags-stats-child.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/daemons/hashtags-stats-child.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,60 @@
 | 
			
		|||
import Note from '../models/note';
 | 
			
		||||
 | 
			
		||||
// 10分
 | 
			
		||||
const interval = 1000 * 60 * 10;
 | 
			
		||||
 | 
			
		||||
async function tick() {
 | 
			
		||||
	const res = await Note.aggregate([{
 | 
			
		||||
		$match: {
 | 
			
		||||
			createdAt: {
 | 
			
		||||
				$gt: new Date(Date.now() - interval)
 | 
			
		||||
			},
 | 
			
		||||
			tags: {
 | 
			
		||||
				$exists: true,
 | 
			
		||||
				$ne: []
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}, {
 | 
			
		||||
		$unwind: '$tags'
 | 
			
		||||
	}, {
 | 
			
		||||
		$group: {
 | 
			
		||||
			_id: '$tags',
 | 
			
		||||
			count: {
 | 
			
		||||
				$sum: 1
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}, {
 | 
			
		||||
		$group: {
 | 
			
		||||
			_id: null,
 | 
			
		||||
			tags: {
 | 
			
		||||
				$push: {
 | 
			
		||||
					tag: '$_id',
 | 
			
		||||
					count: '$count'
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}, {
 | 
			
		||||
		$project: {
 | 
			
		||||
			_id: false,
 | 
			
		||||
			tags: true
 | 
			
		||||
		}
 | 
			
		||||
	}]) as {
 | 
			
		||||
		tags: Array<{
 | 
			
		||||
			tag: string;
 | 
			
		||||
			count: number;
 | 
			
		||||
		}>
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const stats = res.tags
 | 
			
		||||
		.sort((a, b) => a.count - b.count)
 | 
			
		||||
		.map(tag => [tag.tag, tag.count])
 | 
			
		||||
		.slice(0, 10);
 | 
			
		||||
 | 
			
		||||
	console.log(stats);
 | 
			
		||||
 | 
			
		||||
	process.send(stats);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
tick();
 | 
			
		||||
 | 
			
		||||
setInterval(tick, interval);
 | 
			
		||||
							
								
								
									
										20
									
								
								src/daemons/hashtags-stats.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/daemons/hashtags-stats.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
import * as childProcess from 'child_process';
 | 
			
		||||
import Xev from 'xev';
 | 
			
		||||
 | 
			
		||||
const ev = new Xev();
 | 
			
		||||
 | 
			
		||||
export default function() {
 | 
			
		||||
	const log = [];
 | 
			
		||||
 | 
			
		||||
	const p = childProcess.fork(__dirname + '/hashtags-stats-child.js');
 | 
			
		||||
 | 
			
		||||
	p.on('message', stats => {
 | 
			
		||||
		ev.emit('hashtagsStats', stats);
 | 
			
		||||
		log.push(stats);
 | 
			
		||||
		if (log.length > 30) log.shift();
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	ev.on('requestHashTagsStatsLog', id => {
 | 
			
		||||
		ev.emit('hashtagsStatsLog:' + id, log);
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								src/daemons/notes-stats-child.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/daemons/notes-stats-child.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
import Note from '../models/note';
 | 
			
		||||
 | 
			
		||||
const interval = 5000;
 | 
			
		||||
 | 
			
		||||
async function tick() {
 | 
			
		||||
	const [all, local] = await Promise.all([Note.count({
 | 
			
		||||
		createdAt: {
 | 
			
		||||
			$gte: new Date(Date.now() - interval)
 | 
			
		||||
		}
 | 
			
		||||
	}), Note.count({
 | 
			
		||||
		createdAt: {
 | 
			
		||||
			$gte: new Date(Date.now() - interval)
 | 
			
		||||
		},
 | 
			
		||||
		'_user.host': null
 | 
			
		||||
	})]);
 | 
			
		||||
 | 
			
		||||
	const stats = {
 | 
			
		||||
		all, local
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	process.send(stats);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
tick();
 | 
			
		||||
 | 
			
		||||
setInterval(tick, interval);
 | 
			
		||||
							
								
								
									
										20
									
								
								src/daemons/notes-stats.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/daemons/notes-stats.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
import * as childProcess from 'child_process';
 | 
			
		||||
import Xev from 'xev';
 | 
			
		||||
 | 
			
		||||
const ev = new Xev();
 | 
			
		||||
 | 
			
		||||
export default function() {
 | 
			
		||||
	const log = [];
 | 
			
		||||
 | 
			
		||||
	const p = childProcess.fork(__dirname + '/notes-stats-child.js');
 | 
			
		||||
 | 
			
		||||
	p.on('message', stats => {
 | 
			
		||||
		ev.emit('notesStats', stats);
 | 
			
		||||
		log.push(stats);
 | 
			
		||||
		if (log.length > 100) log.shift();
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	ev.on('requestNotesStatsLog', id => {
 | 
			
		||||
		ev.emit('notesStatsLog:' + id, log);
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										42
									
								
								src/daemons/server-stats.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/daemons/server-stats.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,42 @@
 | 
			
		|||
import * as os from 'os';
 | 
			
		||||
const osUtils = require('os-utils');
 | 
			
		||||
import * as diskusage from 'diskusage';
 | 
			
		||||
import Xev from 'xev';
 | 
			
		||||
 | 
			
		||||
const ev = new Xev();
 | 
			
		||||
 | 
			
		||||
const interval = 1000;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Report server stats regularly
 | 
			
		||||
 */
 | 
			
		||||
export default function() {
 | 
			
		||||
	const log = [];
 | 
			
		||||
 | 
			
		||||
	ev.on('requestServerStatsLog', id => {
 | 
			
		||||
		ev.emit('serverStatsLog:' + id, log);
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	async function tick() {
 | 
			
		||||
		osUtils.cpuUsage(cpuUsage => {
 | 
			
		||||
			const disk = diskusage.checkSync(os.platform() == 'win32' ? 'c:' : '/');
 | 
			
		||||
			const stats = {
 | 
			
		||||
				cpu_usage: cpuUsage,
 | 
			
		||||
				mem: {
 | 
			
		||||
					total: os.totalmem(),
 | 
			
		||||
					free: os.freemem()
 | 
			
		||||
				},
 | 
			
		||||
				disk,
 | 
			
		||||
				os_uptime: os.uptime(),
 | 
			
		||||
				process_uptime: process.uptime()
 | 
			
		||||
			};
 | 
			
		||||
			ev.emit('serverStats', stats);
 | 
			
		||||
			log.push(stats);
 | 
			
		||||
			if (log.length > 50) log.shift();
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tick();
 | 
			
		||||
 | 
			
		||||
	setInterval(tick, interval);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue