| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('../../database'); | 
					
						
							|  |  |  | var meta = require('../../meta'); | 
					
						
							|  |  |  | var plugins = require('../../plugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var dashboardController = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | dashboardController.get = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 	async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		stats: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getStats(next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		notices: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			var notices = [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					done: !meta.reloadRequired, | 
					
						
							| 
									
										
										
										
											2017-01-23 02:38:33 +01:00
										 |  |  | 					doneText: '[[admin/general/dashboard:restart-not-required]]', | 
					
						
							|  |  |  | 					notDoneText:'[[admin/general/dashboard:restart-required]]' | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					done: plugins.hasListeners('filter:search.query'), | 
					
						
							| 
									
										
										
										
											2017-01-23 02:38:33 +01:00
										 |  |  | 					doneText: '[[admin/general/dashboard:search-plugin-installed]]', | 
					
						
							|  |  |  | 					notDoneText:'[[admin/general/dashboard:search-plugin-not-installed]]', | 
					
						
							|  |  |  | 					tooltip: '[[admin/general/dashboard:search-plugin-tooltip]]', | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 					link:'/admin/extend/plugins' | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			]; | 
					
						
							|  |  |  | 			plugins.fireHook('filter:admin.notices', notices, next); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res.render('admin/general/dashboard', { | 
					
						
							|  |  |  | 			version: nconf.get('version'), | 
					
						
							|  |  |  | 			notices: results.notices, | 
					
						
							|  |  |  | 			stats: results.stats | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getStats(callback) { | 
					
						
							|  |  |  | 	async.parallel([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getStatsForSet('ip:recent', 'uniqueIPCount', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getStatsForSet('users:joindate', 'userCount', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getStatsForSet('posts:pid', 'postCount', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getStatsForSet('topics:tid', 'topicCount', next); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	], function (err, results) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-23 02:38:33 +01:00
										 |  |  | 		results[0].name = '[[admin/general/dashboard:unique-visitors]]'; | 
					
						
							|  |  |  | 		results[1].name = '[[admin/general/dashboard:users]]'; | 
					
						
							|  |  |  | 		results[2].name = '[[admin/general/dashboard:posts]'; | 
					
						
							|  |  |  | 		results[3].name = '[[admin/general/dashboard:topics]]'; | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, results); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getStatsForSet(set, field, callback) { | 
					
						
							|  |  |  | 	var terms = { | 
					
						
							|  |  |  | 		day: 86400000, | 
					
						
							|  |  |  | 		week: 604800000, | 
					
						
							|  |  |  | 		month: 2592000000 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var now = Date.now(); | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		day: function (next) { | 
					
						
							| 
									
										
										
										
											2016-03-09 22:15:03 +02:00
										 |  |  | 			db.sortedSetCount(set, now - terms.day, '+inf', next); | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		week: function (next) { | 
					
						
							| 
									
										
										
										
											2016-03-09 22:15:03 +02:00
										 |  |  | 			db.sortedSetCount(set, now - terms.week, '+inf', next); | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		month: function (next) { | 
					
						
							| 
									
										
										
										
											2016-03-09 22:15:03 +02:00
										 |  |  | 			db.sortedSetCount(set, now - terms.month, '+inf', next); | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		alltime: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 			getGlobalField(field, next); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getGlobalField(field, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	db.getObjectField('global', field, function (err, count) { | 
					
						
							| 
									
										
										
										
											2015-09-17 16:25:15 -04:00
										 |  |  | 		callback(err, parseInt(count, 10) || 0); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 02:38:33 +01:00
										 |  |  | module.exports = dashboardController; |