#!/usr/bin/php
<?php
# @package      hubzero-metrics
# @file         gen_tool_toplists
# @author       Swaroop Samek <swaroop@purdue.edu>
# @copyright    Copyright (c) 2011-2015 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2011-2015 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#
# =========================================================================
# This script computes the simulation tool stats for the current month
#
# USAGE: ./gen_tool_toplists [<YYYY-MM>]
#

error_reporting(E_ALL & ~E_NOTICE);
@ini_set('display_errors','1');

if(!defined('__DIR__')) {
    $fPos = strrpos(__FILE__, "/");
    define("__DIR__", substr(__FILE__, 0, $fPos) . "/");
}

require_once(__DIR__."/includes/hub_parameters.php");
require_once(__DIR__."/includes/db_connect.php");
require_once(__DIR__."/includes/func_misc.php");

$db_hub = db_connect('db_hub');

if ($_SERVER['argc'] < 2) {
    $dthis_ = date('Y').'-'.date('m').'-'.date('d');
} else {
    $dthis_ = $_SERVER['argv'][1];
}

$tops  = array(2, 5, 6, 7, 8);
foreach ($tops as $top) {
	compute_data($db_hub, $dthis_, $top);
}

db_close($db_hub);

function compute_data(&$db_hub, $dthis_, $top) {

	global $hub_db, $metrics_db, $db_prefix;

	$periods  = array(0, 1, 3, 12, 13, 14);

	foreach ($periods as $period) {

		$dates = get_dates($dthis_, $period);
		$period = dbquote($period);
		$dstart = dbquote($dates['start']);
		$dstop = dbquote($dates['stop']);
		$dthis = dbquote($dates['dthis']);

		$sql = 'DELETE FROM '.$hub_db.'.'.$db_prefix.'stats_topvals WHERE top = '.dbquote($top).' AND datetime = '.$dthis.' AND period = '.$period;
		mysql_exec($db_hub, $sql);

		if($top == '2') { // Top Tools by Simulation Users

			$top_type = 'Total Simulation Users';
			$sql = 'SELECT COUNT(DISTINCT user) FROM '.$metrics_db.'.sessionlog_metrics WHERE start > '.$dstart.' AND start < '.$dstop;
			$total = mysql_fetch($db_hub, $sql);
			$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.',"0",'.dbquote($top_type).','.dbquote($total).')';
			mysql_exec($db_hub, $sql_ins);

			$sql = 'SELECT res.title, rt.resid, rt.users AS cnt FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools AS rt, '.$hub_db.'.'.$db_prefix.'resources AS res WHERE res.id = rt.resid AND res.published = 1 AND period = '.$period.' and datetime = '.$dthis.' ORDER BY cnt DESC';
			gen_top_tools($db_hub, $sql, $top, $dthis, $period);

		} else if ($top == '5') { // Top Tools by Sim Runs
	
			$top_type = "Total Simulation Runs";
			$sql = 'SELECT SUM(jobs) FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE period = '.$period.' AND datetime = '.$dthis;
			$total = mysql_fetch($db_hub, $sql);
			$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.',"0",'.dbquote($top_type).','.dbquote($total).')';
			mysql_exec($db_hub, $sql_ins);

			$sql = 'SELECT res.title, rt.resid, rt.jobs AS cnt FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools AS rt, '.$hub_db.'.'.$db_prefix.'resources AS res WHERE res.id = rt.resid AND res.published = 1 AND period = '.$period.' and datetime = '.$dthis.' ORDER BY cnt DESC';
			gen_top_tools($db_hub, $sql, $top, $dthis, $period);

		} else if ($top == '6') { // Top Tools by Sim Wall Time
	
			$top_type = "Total Simulation Wall Time";
			$sql = 'SELECT SUM(tot_wall) FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE period = '.$period.' AND datetime = '.$dthis;
			$total = mysql_fetch($db_hub, $sql);
			$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.',"0",'.dbquote($top_type).','.dbquote($total).')';
			mysql_exec($db_hub, $sql_ins);

			$sql = 'SELECT res.title, rt.resid, rt.tot_wall AS cnt FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools AS rt, '.$hub_db.'.'.$db_prefix.'resources AS res WHERE res.id = rt.resid AND res.published = 1 AND period = '.$period.' and datetime = '.$dthis.' ORDER BY cnt DESC';
			gen_top_tools($db_hub, $sql, $top, $dthis, $period);

		} else if ($top == '7') { // Top Tools by Sim CPU Time
	
			$top_type = "Total Simulation CPU Time";
			$sql = 'SELECT SUM(tot_cpu) FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE period = '.$period.' AND datetime = '.$dthis;
			$total = mysql_fetch($db_hub, $sql);
			$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.',"0",'.dbquote($top_type).','.dbquote($total).')';
			mysql_exec($db_hub, $sql_ins);

			$sql = 'SELECT res.title, rt.resid, rt.tot_cpu AS cnt FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools AS rt, '.$hub_db.'.'.$db_prefix.'resources AS res WHERE res.id = rt.resid AND res.published = 1 AND period = '.$period.' and datetime = '.$dthis.' ORDER BY cnt DESC';
			gen_top_tools($db_hub, $sql, $top, $dthis, $period);

		} else if ($top == '8') { // Top Tools by Sim Interactive Time
	
			$top_type = "Total Simulation Interaction Time";
			$sql = 'SELECT SUM(tot_view) FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE period = '.$period.' AND datetime = '.$dthis;
			$total = mysql_fetch($db_hub, $sql);
			$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.',"0",'.dbquote($top_type).','.dbquote($total).')';
			mysql_exec($db_hub, $sql_ins);

			$sql = 'SELECT res.title, rt.resid, rt.tot_view AS cnt FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools AS rt, '.$hub_db.'.'.$db_prefix.'resources AS res WHERE res.id = rt.resid AND res.published = 1 AND period = '.$period.' and datetime = '.$dthis.' ORDER BY cnt DESC';
			gen_top_tools($db_hub, $sql, $top, $dthis, $period);

		} else {
			$msg = 'No Tops to compute'.n;
			clean_exit($msg);
		}
	}
}

function gen_top_tools($db_hub, $sql, $top, $dthis, $period) {

	global $hub_db, $db_prefix;

	$rank = 1;
   	$result = mysql_query($sql, $db_hub);
	if($result) {
   		if(mysql_num_rows($result) > 0) {
       		while($row = mysql_fetch_assoc($result)) {
				$name = $row['resid']." ~ ".$row['title'];
				$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'stats_topvals VALUES ('.dbquote($top).','.$dthis.','.$period.','.dbquote($rank).','.dbquote($name).','.dbquote($row['cnt']).')';
				mysql_exec($db_hub, $sql_ins);
				$rank = $rank + 1;
       		}
   		}
	} else {
		$msg = mysql_error($db_hub).' while executing '.$sql.n;
		clean_exit($msg);
	}
}

?>
