<commit_before>var util = require('util'),
    User = require('../models/user')

function AppStatePreparer(app){
	
	this.prepare = function(done){
		var out = {}, left = monitor_names.length;
		
		monitor_names.forEach(function(key){
			monitors[key](function(result){
				out[key] = result;
				if(--left <= 0) done(out);
			});
		});
	}
	
	var monitors = {
		clients: clientCounts
		,users: userCounts
		,channels: channelCounts
		,t: timeStamp
		,node: nodeInfo
	}, monitor_names = Object.keys(monitors);


	function clientCounts(done){
		done({
			count: app.clients.count
		});
	}
	
	function userCounts(done){
		var result = {
			connected: app.clients.userCount
		};
		User.count({},function(err, value){
			result.db = value;
			done(result);
		});
	}
	
	function channelCounts(done){
		var inuse=0;
		
		app.channels.each(function(chan){
			if(chan.clients.count > 0) inuse++;
		});

		done({
			loaded: app.channels.count
			,inuse: inuse
		});
	}
	
	function nodeInfo(done){
		done({
			mem:process.memoryUsage()
		});
	}
	
	function timeStamp(done){ done(new Date().getTime()); }
}


function AppStateMonitor(app, options){
	var appState = new AppStatePreparer(app);

	var me = this;
	
	if(!options.disabled)
		this.timer = setInterval(tick, options.interval);
	
	function tick(){
		appState.prepare(function(result){
			me.emit('new_state', result);
			me.previousState = result;
		})
	}
}

util.inherits(AppStateMonitor, require('events').EventEmitter);<commit_msg>Save DB count of rooms to app state log<commit_after>var util = require('util'),
    User = require('../models/user'),
    Room = require('../models/room');

function AppStatePreparer(app){
	
	this.prepare = function(done){
		var out = {}, left = monitor_names.length;
		
		monitor_names.forEach(function(key){
			monitors[key](function(result){
				out[key] = result;
				if(--left <= 0) done(out);
			});
		});
	}
	
	var monitors = {
		clients: clientCounts
		,users: userCounts
		,channels: channelCounts
		,t: timeStamp
		,node: nodeInfo
	}, monitor_names = Object.keys(monitors);


	function clientCounts(done){
		done({
			count: app.clients.count
		});
	}
	
	function userCounts(done){
		var result = {
			connected: app.clients.userCount
		};
		User.count({},function(err, value){
			result.db = value;
			done(result);
		});
	}
	
	function channelCounts(done){
		var inuse=0;
		
		app.channels.each(function(chan){
			if(chan.clients.count > 0) inuse++;
		});

		var result = {
			loaded: app.channels.count
			,inuse: inuse
		};

		Room.count({},function(err, value){
			result.db = value;
			done(result);
		});
	}
	
	function nodeInfo(done){
		done({
			mem:process.memoryUsage()
		});
	}
	
	function timeStamp(done){ done(new Date().getTime()); }
}


function AppStateMonitor(app, options){
	var appState = new AppStatePreparer(app);

	var me = this;
	
	if(!options.disabled)
		this.timer = setInterval(tick, options.interval);
	
	function tick(){
		appState.prepare(function(result){
			me.emit('new_state', result);
			me.previousState = result;
		})
	}
}

util.inherits(AppStateMonitor, require('events').EventEmitter);
{'fraction_non_alphanumeric': 0.1599229287090559, 'fraction_numerical': 0.0019267822736030828, 'mean_word_length': 3.741248097412481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544550', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1061, 'n_words': 203}