#!/usr/bin/php
<?php
# @package      hubzero-metrics
# @file         gen_tool_stats
# @author       Swaroop Shivarajapura <swaroop@purdue.edu>
# @copyright    Copyright (c) 2011-2013 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2011-2013 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_stats [<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];
}

$sql = 'SELECT id, alias FROM '.$hub_db.'.'.$db_prefix.'resources WHERE type="7" AND alias <> "" ORDER by title';
$result = mysql_query($sql, $db_hub);
if($result) {
	if(mysql_num_rows($result) > 0) {
    	while($row = mysql_fetch_row($result)) {
			$resid = $row[0];
			$filter = '';
			$tool = '"'.$row[1].'"';
			$filter = get_tool_versions_aliases($db_hub, $tool);

			$periods  = array(0, 1, 3, 12, 13, 14);
			foreach ($periods as $period) {
				$id = '0';
		    	$dates = get_dates($dthis_, $period);
			   	$period = dbquote($period);
			   	$dstart = dbquote($dates['start']);
			   	$dstop = dbquote($dates['stop']);
				$dthis = dbquote($dates['dthis']);

				$sql_ = 'SELECT id FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE restype = "7" AND datetime = '.$dthis.' AND resid = '.dbquote($resid).' AND period = '.$period;
				$id = mysql_fetch($db_hub, $sql_);
				compute_data($db_hub, $dthis, $dstart, $dstop, $period, $resid, $filter, $id);
				update_stats($db_hub, $dthis, $period, $resid);
			}
		}
	}
} else {
	$msg = mysql_error($db_hub).' while executing '.$sql.n;
	clean_exit($msg);
}

db_close($db_hub);

function compute_data($db_hub, $dthis, $dstart, $dstop, $period, $resid, $filter, $id) {

	global $debug, $db_prefix, $hub_db;

	$users = 0;
	$sessions = 0;
	$simulations = 0;
	$jobs = 0;
	$avg_wall = 0;
	$tot_wall = 0;
	$avg_cpu = 0;
	$tot_cpu = 0;
	$avg_view = 0;
	$tot_view = 0;
	$avg_wait = 0;
	$tot_wait = 0;
	$tot_cpus = 0;
	$avg_cpus = 0;

	$sql = 'SELECT COUNT(DISTINCT username) FROM '.$hub_db.'.sessionlog WHERE appname IN ('.$filter.') AND start > '.$dstart.' AND start < '.$dstop;
	$users = mysql_fetch($db_hub, $sql);

	if ($users) {

		//  Narwhal Jobs - Counting everything including job 0. Use job > 0 otherwise
		$sql = 'SELECT COUNT(*) FROM '.$hub_db.'.joblog AS j, '.$hub_db.'.sessionlog AS s WHERE s.sessnum = j.sessnum AND s.appname IN ('.$filter.') AND s.start > '.$dstart.' AND s.start < '.$dstop.' AND j.event <> "[waiting]" AND j.job > 0';
		$jobs = mysql_fetch($db_hub, $sql);

		$sql = 'SELECT COUNT(*) FROM '.$hub_db.'.sessionlog WHERE appname in ('.$filter.') AND start > '.$dstart.' AND start < '.$dstop;
		$sessions = mysql_fetch($db_hub, $sql);

		$sql = 'SELECT COUNT(*) FROM '.$hub_db.'.sessionlog AS s, '.$hub_db.'.joblog AS j WHERE s.sessnum = j.sessnum AND s.appname IN ('.$filter.') AND s.start > '.$dstart.' AND s.start < '.$dstop.' AND j.event <> "application" AND j.superjob = 0';
		$simulations = mysql_fetch($db_hub, $sql);
	
		$sql = 'SELECT COALESCE(SUM(walltime),0) FROM '.$hub_db.'.sessionlog WHERE appname IN ('.$filter.') AND start > '.$dstart.' AND start < '.$dstop;
		$tot_wall = mysql_fetch($db_hub, $sql);
		if ($tot_wall && $simulations) {
			$avg_wall = $tot_wall/$simulations;
		}

		// Job > 0 as job 0 contains cputime of all childprocesses
		$sql = 'SELECT COALESCE(SUM(j.cputime),0) FROM '.$hub_db.'.joblog AS j, '.$hub_db.'.sessionlog AS s WHERE s.sessnum = j.sessnum AND s.appname IN ('.$filter.') AND s.start > '.$dstart.' AND s.start < '.$dstop.' AND j.event <> "[waiting]" AND j.job > 0';
		$tot_cpu = mysql_fetch($db_hub, $sql);
		if ($tot_cpu && $simulations) {
			$avg_cpu = $tot_cpu/$simulations;
		}
		
		$sql = 'SELECT COALESCE(SUM(viewtime),0) FROM '.$hub_db.'.sessionlog WHERE appname IN ('.$filter.') AND start > '.$dstart.' AND start < '.$dstop;
		$tot_view = mysql_fetch($db_hub, $sql);
		if ($tot_view && $simulations) {
			$avg_view = $tot_view/$simulations;
		}

		$sql = 'SELECT COALESCE(SUM(j.walltime),0) FROM '.$hub_db.'.joblog AS j, '.$hub_db.'.sessionlog AS s WHERE s.sessnum = j.sessnum AND s.appname IN ('.$filter.') AND s.start > '.$dstart.' AND s.start < '.$dstop.' AND j.event = "[waiting]" AND j.job > 0';
		$tot_wait = mysql_fetch($db_hub, $sql);
		if ($tot_wait && $simulations) {
			$avg_wait = $tot_wait/$simulations;
		}

		$sql = 'SELECT SUM(j.ncpus) FROM '.$hub_db.'.sessionlog AS s, '.$hub_db.'.joblog AS j WHERE s.sessnum = j.sessnum AND s.appname IN ('.$filter.') AND s.start > '.$dstart.' AND s.start < '.$dstop;
		$tot_cpus = mysql_fetch($db_hub, $sql);
		if ($tot_cpus && $simulations) {
		 	$avg_cpus = round($tot_cpus/$simulations);
		}
	}

	$sql_ins='';

	if (!$id) {
		$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'resource_stats_tools (resid, restype, users, sessions, simulations, jobs, avg_wall, tot_wall, avg_cpu, tot_cpu, avg_view, tot_view, avg_wait, tot_wait, avg_cpus, tot_cpus, datetime, period) VALUES ('.dbquote($resid).',"7",'.dbquote($users).','.dbquote($sessions).','.dbquote($simulations).','.dbquote($jobs).','.dbquote($avg_wall).','.dbquote($tot_wall).','.dbquote($avg_cpu).','.dbquote($tot_cpu).','.dbquote($avg_view).','.dbquote($tot_view).','.dbquote($avg_wait).','.dbquote($tot_wait).','.dbquote($avg_cpus).','.dbquote($tot_cpus).','.$dthis.', '.$period.')';
	} else {
		$sql_ins = 'UPDATE '.$hub_db.'.'.$db_prefix.'resource_stats_tools SET users = '.dbquote($users).', sessions = '.dbquote($sessions).', simulations = '.dbquote($simulations).', jobs = '.dbquote($jobs).' , avg_wall = '.dbquote($avg_wall).', tot_wall = '.dbquote($tot_wall).', avg_cpu = '.dbquote($avg_cpu).', tot_cpu = '.dbquote($tot_cpu).', avg_view = '.dbquote($avg_view).', tot_view = '.dbquote($tot_view).', avg_wait = '.dbquote($avg_wait).', tot_wait = '.dbquote($tot_wait).', avg_cpus = '.dbquote($avg_cpus).', tot_cpus = '.dbquote($tot_cpus).' WHERE id = '.dbquote($id);
	}
	mysql_exec($db_hub, $sql_ins);
}

function update_stats($db_hub, $dthis, $period, $resid) {

	global $db_prefix, $hub_db;

	$sql = 'SELECT id FROM '.$hub_db.'.'.$db_prefix.'resource_stats WHERE restype = "7" AND DATETIME = '.$dthis.' AND period = '.$period.' AND resid = '.dbquote($resid);
	$id = mysql_fetch($db_hub, $sql);

	$sql = 'SELECT resid, restype, users, jobs, avg_wall, tot_wall, avg_cpu, tot_cpu FROM '.$hub_db.'.'.$db_prefix.'resource_stats_tools WHERE datetime = '.$dthis.' AND period = '.$period.' AND resid = '.dbquote($resid);
	$result = mysql_query($sql, $db_hub);
	if($result) {
		if(mysql_num_rows($result) > 0) {
    		while($row = mysql_fetch_row($result)) {
				$resid = dbquote($row[0]);
				$restype = dbquote($row[1]);
				$users = dbquote($row[2]);
				$jobs = dbquote($row[3]);
				$avg_wall = dbquote($row[4]);
				$tot_wall = dbquote($row[5]);
				$avg_cpu = dbquote($row[6]);
				$tot_cpu = dbquote($row[7]);
				if (!$id) {
				$sql_ = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'resource_stats (resid, restype, users, jobs, avg_wall, tot_wall, avg_cpu, tot_cpu, datetime, period) VALUES ('.$resid.','.$restype.','.$users.','.$jobs.','.$avg_wall.','.$tot_wall.','.$avg_cpu.','.$tot_cpu.','.$dthis.','.$period.')';
				} else {
				$sql_ = 'UPDATE '.$hub_db.'.'.$db_prefix.'resource_stats SET resid = '.$resid.', restype = '.$restype.', users = '.$users.', jobs = '.$jobs.', avg_wall = '.$avg_wall.', tot_wall = '.$tot_wall.', avg_cpu = '.$avg_cpu.', tot_cpu = '.$tot_cpu.' WHERE id = '.dbquote($id);
				}
				mysql_exec($db_hub, $sql_);
			}
		}
	}
}

?>
