Jump to content

User:Technical 13/Scripts/Jobs.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var oldUsers = -1, oldPages = -1, oldJobs = -1, oldMms = -1; 

function stopRefreshing() {
	clearInterval( refreshStats );
	$( '#stopButton' ).replaceWith( '<b>Last updated</b>' );
}

function updateResults() {
	new mw.Api().get( {
		action: 'query',
		meta: 'siteinfo',
		siprop: 'statistics'
	} ).done( function( siteStats ) {
		var statPath = siteStats.query.statistics;
		if ( oldUsers === -1 ) { oldUsers = statPath.users; }
		if ( oldPages === -1 ) { oldPages = statPath.pages; }
		if ( oldJobs === -1 ) { oldJobs = statPath.jobs; }
		if ( oldMms === -1 ) { oldMms = statPath[ 'queued-massmessages' ]; }
		var userInfo = '<tr><td>Total users</td><td style="text-align: right;">' +
			statPath.users.toLocaleString() +
 			'</td><td style="text-align: right;">' + ( statPath.users -oldUsers ).toLocaleString() + '</td></tr>';
		userInfo += '<tr><td>Active users</td><td colspan="2" style="text-align: center;">' +
			( Math.round( statPath.activeusers / statPath.users * 1000000 ) / 10000 ) + '%</td></tr>';
		userInfo += '<tr><td>Admins</td><td colspan="2" style="text-align: center;">' +
			( Math.round( statPath.admins / statPath.users * 1000000 ) / 10000 ) + '%</td></tr>';
		var pageInfo = '<tr><td>Total pages</td><td style="text-align: right;">' +
			statPath.pages.toLocaleString() +
 			'</td><td style="text-align: right;">' + ( statPath.pages - oldPages ).toLocaleString() + '</td></tr>';
		pageInfo += '<tr><td>Articles</td><td colspan="2" style="text-align: center;">' +
			( Math.round( statPath.articles / statPath.pages * 1000000 ) / 10000 ) + '%</td></tr>';
		pageInfo += '<tr><td>Images</td><td colspan="2" style="text-align: center;">' +
			( Math.round( statPath.images / statPath.pages * 1000000 ) / 10000 ) + '%</td></tr>';
		var jobInfo = '<tr><td>Queued jobs</td><td style="text-align: right;">' +
			statPath.jobs.toLocaleString() +
 			'</td><td style="text-align: right;">' + ( statPath.jobs - oldJobs ).toLocaleString() + '</td></tr>';
		var mmsInfo = '<tr><td>MMS Queue</td><td style="text-align: right;">' +
			statPath[ 'queued-massmessages' ] +
 			'</td><td style="text-align: right;">' + ( statPath[ 'queued-massmessages' ] - oldMms ).toLocaleString() + '</td></tr>';
		var displayInfo = '<table style="width: 100%;" border="1">';
		displayInfo += '<tr><th style="font-variant: small-caps; font-size: 14px;">stat</th>' +
			'<th style="font-variant: small-caps; font-size: 14px;">value</th>' +
			'<th style="font-variant: small-caps; font-size: 14px;">change</th></tr>';
		displayInfo += userInfo;
		displayInfo += pageInfo;
		displayInfo += jobInfo;
		displayInfo += mmsInfo;
		var nowTimeDate = new Date( Date.now() );
		displayInfo += '<tr><td colspan="3" style="font-size: larger; text-align: center;"><a id="stopButton" href="#stopUpdating" onClick="stopRefreshing();">Stop updating</a>: ' + nowTimeDate.toLocaleTimeString() + '</td></tr>';
		displayInfo += '<tr><td colspan="3" style="font-size: larger; text-align: center;">' +
			'<a href="/wiki/User:Technical_13/Scripts/Jobs" title="User:Technical 13/Scripts/Jobs">Documentation</a>' +
			'</td></tr></table>';
		mw.notify( $( displayInfo ), { title: 'Current enwp statistics:', tag: 'siteInfo', autoHide: false } );
		oldUsers = statPath.users;
		oldJobs = statPath.jobs;
		oldMms = statPath[ 'queued-massmessages' ];
	} );
};

updateResults();
var refreshStats = window.setInterval( updateResults, 15000 );