showsumstats.php3 22.7 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
<?php
#
3
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23 24 25 26 27 28
#
include("defs.php3");

#
# Only known and logged in users can end experiments.
#
29 30 31
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
Leigh Stoller's avatar
Leigh Stoller committed
32

33 34 35 36
$debug  = 0;
$debug2 = 0;
$debug3 = 0;

Leigh Stoller's avatar
Leigh Stoller committed
37
# Summary data for admins only.
38
if (!$isadmin && !STUDLY()) {
Leigh Stoller's avatar
Leigh Stoller committed
39 40 41
    USERERROR("You are not allowed to view this page!", 1);
}

Leigh Stoller's avatar
Leigh Stoller committed
42 43 44
#
# Verify page arguments.
#
45 46 47
$optargs = OptionalPageArguments("showby",     PAGEARG_STRING,
				 "range",      PAGEARG_STRING,
				 "experiment", PAGEARG_EXPERIMENT);
Leigh Stoller's avatar
Leigh Stoller committed
48

49 50 51 52 53
#
# Standard Testbed Header
#
PAGEHEADER("Testbed Summary Stats");

Leigh Stoller's avatar
Leigh Stoller committed
54 55 56 57
# Page args,
if (! isset($showby)) {
    $showby = "users";
}
58 59 60
elseif (! ($showby == "users" || $showby == "projects")) {
    PAGEARGERROR("Invalid showby argument");
}
Leigh Stoller's avatar
Leigh Stoller committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
if (! isset($range)) {
    $range = "epoch";
}

echo "<b>Show: <a class='static' href='showexpstats.php3'>
                  Experiment Stats</a>,
               <a class='static' href='showstats.php3'>
                  Testbed Stats</a>.
      </b><br>\n";

echo "<b>Show by: ";
# By Users.
if ($showby != "users") {
    echo "<a class='static' href='showsumstats.php3?showby=users'>
             Users</a>, ";
}
else {
    echo "Users, ";
}
# By Projects
if ($showby != "projects") {
    echo "<a class='static' href='showsumstats.php3?showby=projects'>
             Projects</a>, ";
}
else {
    echo "Projects. ";
}
echo "</b><br>\n";

echo "<b>Range: ";
if ($range != "epoch") {
    echo "<a class='static'
93
            href='showsumstats.php3?showby=$showby&range=epoch'>
Leigh Stoller's avatar
Leigh Stoller committed
94 95 96 97 98 99 100
            Epoch</a>, ";
}
else {
    echo "Epoch, ";
}
if ($range != "day") {
    echo "<a class='static'
101
            href='showsumstats.php3?showby=$showby&range=day'>
Leigh Stoller's avatar
Leigh Stoller committed
102 103 104 105 106 107 108
            Day</a>, ";
}
else {
    echo "Day, ";
}
if ($range != "week") {
    echo "<a class='static'
109
            href='showsumstats.php3?showby=$showby&range=week'>
Leigh Stoller's avatar
Leigh Stoller committed
110 111 112 113 114 115 116
            Week</a>, ";
}
else {
    echo "Week, ";
}
if ($range != "month") {
    echo "<a class='static'
117
            href='showsumstats.php3?showby=$showby&range=month'>
Leigh Stoller's avatar
Leigh Stoller committed
118 119 120
            Month</a>, ";
}
else {
121
    echo "Month, ";
Leigh Stoller's avatar
Leigh Stoller committed
122
}
123 124 125 126 127 128 129 130
if ($range != "year") {
    echo "<a class='static'
            href='showsumstats.php3?showby=$showby&range=year'>
            Year</a> ";
}
else {
    echo "Year ";
}
131
echo "</b>";
Leigh Stoller's avatar
Leigh Stoller committed
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146
#
# Spit out a range form!
#
$formrange = "mm/dd/yy-mm/dd/yy";
if (preg_match("/^(\d*)\/(\d*)\/(\d*)-(\d*)\/(\d*)\/(\d*)$/", $range))
    $formrange = $range;

echo "<form action=showsumstats.php3 method=get>
      <input type=text
             name=range
             value=\"$formrange\">
      <input type=hidden name=showby value=\"$showby\">
      <b><input type=submit name=Get value=Get></b>\n";
echo "<br><br>\n";
Leigh Stoller's avatar
Leigh Stoller committed
147 148 149 150 151

#
# This version prints out the simple summary info for the entire table.
# No ranges, just ordered. 
#
152
function showsummary ($showby) {
Russ Fish's avatar
Russ Fish committed
153
    global $TBOPSPID;
Leigh Stoller's avatar
Leigh Stoller committed
154 155 156
    switch ($showby) {
        case "projects":
	    $which = "pid";
157
	    $table = "project_stats";
Leigh Stoller's avatar
Leigh Stoller committed
158 159
	    $title = "Project Summary Stats (Epoch)";
	    $link  = "showproject.php3?pid=";
160 161
	    $wclause = "where pid!='$TBOPSPID'";
	    $order   = "pid";
Leigh Stoller's avatar
Leigh Stoller committed
162 163 164
	    break;
        case "users":
	    $which = "uid";
165
	    $table = "user_stats";
Leigh Stoller's avatar
Leigh Stoller committed
166
	    $title = "User Summary Stats (Epoch)";
167
	    $link  = "showuser.php3?user=";
168 169
	    $wclause = "";
	    $order   = "uid";
Leigh Stoller's avatar
Leigh Stoller committed
170 171 172 173 174
	    break;
        default:
	    USERERROR("Invalid showby argument: $showby!", 1);
    }

175 176 177 178 179 180 181
    if ($showby == "users") {
	$query_result =
	    DBQueryFatal("select s.uid, allexpt_pnodes, ".
			 "allexpt_pnode_duration / (24 * 3600) as pnode_days,".
			 "allexpt_duration / (24 * 3600) as expt_days, ".
			 "exptswapin_count+exptstart_count as expt_swapins, ".
			 "exptpreload_count+exptstart_count as expt_new, ".
182
			 "exptswapmod_count as expt_swapmods, ".
183
			 "u.usr_name, s.uid_idx, u.status ".
184
			 "from $table as s ".
185
			 "left join users as u on u.uid_idx=s.uid_idx ".
186 187 188 189 190 191 192 193 194
			 "$wclause ".
			 "order by $order");
    }
    else {
	$query_result =
	    DBQueryFatal("select $which, allexpt_pnodes, ".
			 "allexpt_pnode_duration / (24 * 3600) as pnode_days,".
			 "allexpt_duration / (24 * 3600) as expt_days, ".
			 "exptswapin_count+exptstart_count as expt_swapins, ".
195
			 "exptpreload_count+exptstart_count as expt_new, ".
196
			 "exptswapmod_count as expt_swapmods, ".
197
			 "${which}_idx ".
198 199 200 201
			 "from $table  ".
			 "$wclause ".
			 "order by $order");
    }
Leigh Stoller's avatar
Leigh Stoller committed
202

203 204 205 206
    if (mysql_num_rows($query_result) == 0) {
	USERERROR("No summary stats of interest!", 1);
    }

207 208 209
    #
    # Gather some totals first.
    #
210 211 212 213 214 215
    $pnode_total   = 0;
    $pdays_total   = 0;
    $edays_total   = 0;
    $swapin_total  = 0;
    $swapmod_total = 0;
    $new_total     = 0;
216 217
    while ($row = mysql_fetch_assoc($query_result)) {
	$pnodes  = $row["allexpt_pnodes"];
218 219
	$pdays   = $row["pnode_days"];
	$edays   = $row["expt_days"];
220
	$swapins = $row["expt_swapins"];
221
	$new     = $row["expt_new"];
222
	$swapmods= $row["expt_swapmods"];
223
	
224 225 226 227 228 229
	$pnode_total   += $pnodes;
	$pdays_total   += $pdays;
	$edays_total   += $edays;
	$swapin_total  += $swapins;
	$swapmod_total += $swapmods;
	$new_total     += $new;
230 231 232 233 234 235 236 237 238 239
    }

    SUBPAGESTART();
    echo "<table>
           <tr><td colspan=2 nowrap align=center>
               <b>Totals</b></td>
           </tr>
           <tr><td nowrap align=right><b>Pnodes</b></td>
               <td align=left>$pnode_total</td>
           </tr>
240 241
           <tr><td nowrap align=right><b>Pnode Days</b></td>
               <td align=left>$pdays_total</td>
242
           </tr>
243 244
           <tr><td nowrap align=right><b>Expt Days</b></td>
               <td align=left>$edays_total</td>
245
           </tr>
246 247 248
           <tr><td nowrap align=right><b>Swapins</b></td>
               <td align=left>$swapin_total</td>
           </tr>
249 250 251
           <tr><td nowrap align=right><b>Swapmods</b></td>
               <td align=left>$swapmod_total</td>
           </tr>
252 253 254 255
           <tr><td nowrap align=right><b>New</b></td>
               <td align=left>$new_total</td>
           </tr>

256 257 258
          </table>\n";
    SUBMENUEND_2B();
    
Leigh Stoller's avatar
Leigh Stoller committed
259
    echo "<center><b>$title</b></center><br>\n";
260 261
    echo "<table align=center border=1 id=sumstats>
	  <thead class='sort'>
Leigh Stoller's avatar
Leigh Stoller committed
262
          <tr>
263 264 265 266 267 268 269 270
             <th>$which</th>
             <th>Pnodes</th>
             <th>Pnode Days</th>
             <th>Expt Days</th>
             <th>Swapins</th>
             <th>Swapmods</th>
             <th>New</th>
          </tr></thead>\n";
Leigh Stoller's avatar
Leigh Stoller committed
271

272
    mysql_data_seek($query_result, 0);    
Leigh Stoller's avatar
Leigh Stoller committed
273 274
    while ($row = mysql_fetch_assoc($query_result)) {
	$heading = $row[$which];
275
	$idx     = $row["${which}_idx"];
Leigh Stoller's avatar
Leigh Stoller committed
276 277 278
	$pnodes  = $row["allexpt_pnodes"];
	$phours  = $row["pnode_days"];
	$ehours  = $row["expt_days"];
279
	$swapins = $row["expt_swapins"];
280
	$swapmods= $row["expt_swapmods"];
281
	$new     = $row["expt_new"];
Leigh Stoller's avatar
Leigh Stoller committed
282

283 284
	echo "<tr>";
	if ($showby == "users") {
285
	    # A current, archived, or a deleted user?
286 287 288
	    $usr_name = $row["usr_name"];
	    
	    if (isset($usr_name)) {
289 290 291 292 293 294 295
		$status   = $row["status"];
		$extra    = "";

		if ($status == TBDB_USERSTATUS_ARCHIVED) {
		    $extra = "(a)";
		}
		echo "<td><A href='$link${idx}'>$heading $extra</A></td>";
296 297 298 299 300 301 302 303 304
	    }
	    else {
		echo "<td>$heading</td>";
	    }
	}
	else {
	    echo "<td><A href='$link${heading}'>$heading</A></td>";
	}
	echo "  <td>$pnodes</td>
Leigh Stoller's avatar
Leigh Stoller committed
305 306
                <td>$phours</td>
                <td>$ehours</td>
307
                <td>$swapins</td>
308
                <td>$swapmods</td>
309
                <td>$new</td>
Leigh Stoller's avatar
Leigh Stoller committed
310 311 312
              </tr>\n";
    }
    echo "</table>\n";
313
    SUBPAGEEND();
Leigh Stoller's avatar
Leigh Stoller committed
314 315
}

316
function showrange ($showby, $range) {
317
    global $TBOPSPID, $TB_EXPTSTATE_ACTIVE, $debug, $debug2, $debug3;
318
    global $experiment;
319 320 321

    $ACTIVE   = TBDB_USERSTATUS_ACTIVE;
    $ARCHIVED = TBDB_USERSTATUS_ARCHIVED;
322
    
323
    $now   = time();
324
    $inactive_swapmods = 0;
325
    unset($rangematches);
326 327
    $users_created    = 0;
    $projects_created = 0;
328
    
Leigh Stoller's avatar
Leigh Stoller committed
329 330
    switch ($range) {
        case "day":
331
	    $span = 3600 * 24 * 1;
Leigh Stoller's avatar
Leigh Stoller committed
332 333
	    break;
        case "week":
334
	    $span = 3600 * 24 * 7;
Leigh Stoller's avatar
Leigh Stoller committed
335 336
	    break;
        case "month":
337
	    $span = 3600 * 24 * 31;
Leigh Stoller's avatar
Leigh Stoller committed
338
	    break;
339 340 341
        case "year":
	    $span = 3600 * 24 * 365;
	    break;
Leigh Stoller's avatar
Leigh Stoller committed
342
        default:
343 344 345 346 347 348 349 350 351 352 353 354 355 356
	    if (!preg_match("/^(\d*)\/(\d*)\/(\d*)-(\d*)\/(\d*)\/(\d*)$/",
			    $range, $rangematches)) 
		USERERROR("Invalid range argument: $range!", 1);
    }
    if (isset($rangematches)) {
	$spanstart = mktime(1,1,1,
			    $rangematches[1],$rangematches[2],
			    $rangematches[3]);
	$spanend = mktime(23,59,59,
			    $rangematches[4],$rangematches[5],
			    $rangematches[6]);

	if ($spanend <= $spanstart)
	    USERERROR("Invalid range: $spanstart to $spanend", 1);
Leigh Stoller's avatar
Leigh Stoller committed
357
    }
358 359 360 361 362 363
    else {
	$spanend   = $now;
	$spanstart = $now - $span;
    }
    if ($debug)
	echo "start $spanstart end $spanend<br>\n";
364

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    #
    # Get users/projects created during that time.
    #
    $query_result =
	DBQueryFatal("select count(uid_idx) from users ".
		     "where UNIX_TIMESTAMP(usr_created) >= $spanstart and ".
		     "      UNIX_TIMESTAMP(usr_created) <  $spanend and ".
		     "      (status='$ACTIVE' or status='$ARCHIVED') and ".
		     "      usr_email not like '%flux.utah.edu'");
    if ($query_result && mysql_num_rows($query_result)) {
	$row = mysql_fetch_row($query_result);
	$users_created = $row[0];
    }
    $query_result =
	DBQueryFatal("select count(pid_idx) from projects ".
		     "where UNIX_TIMESTAMP(created) >= $spanstart and ".
		     "      UNIX_TIMESTAMP(created) <  $spanend and ".
		     "      approved=1");
    if ($query_result && mysql_num_rows($query_result)) {
	$row = mysql_fetch_row($query_result);
	$projects_created = $row[0];
    }

388 389
    # Summary info, indexed by pid and uid. Each entry is an array of the
    # summary info.
390 391
    $pid_summary  = array();
    $uid_summary  = array();
392
    $experiments  = array();
393

394 395 396 397
    #
    # Get current set of experiments so we can mark them as current.
    #
    $query_result =
398 399
	DBQueryFatal("select e.pid,e.eid,u.uid,u.uid_idx,r.pnodes,r.vnodes, ".
		     "    r.swapin_time,r.idx,u.status ".
400 401 402 403 404 405 406 407
		     "  from experiments as e ".
		     "left join experiment_stats as s on s.exptidx=e.idx ".
		     "left join experiment_resources as r on ".
		     "     s.rsrcidx=r.idx ".
		     "left join users as u on u.uid_idx=r.uid_idx ".
		     "where e.state='" . $TB_EXPTSTATE_ACTIVE . "'" .
		     "  and e.pid!='$TBOPSPID' and ".
		     "      not (e.pid='ron' and e.eid='all') ");
408

409 410 411 412
    while ($row = mysql_fetch_assoc($query_result)) {
	$pid         = $row["pid"];
	$eid         = $row["eid"];
	$uid         = $row["uid"];
413
	$uid_idx     = $row["uid_idx"];
414 415 416 417 418 419
	$swapin_time = $row["swapin_time"];
	$swapseconds = 0;
	$pnodes      = $row["pnodes"];
	$vnodes      = $row["vnodes"];
	$rsrcidx     = $row["idx"];

420 421 422
	if ($swapin_time > $spanend) {
	    continue;
	}
423 424 425 426
	if ($swapin_time < $spanstart) 
	    $swapseconds = $spanend - $spanstart;
	else
	    $swapseconds = $spanend - $swapin_time;
427

428 429
	if ($debug2)
	    echo "$pid $eid $uid $swapin_time $swapseconds $pnodes<br>\n";
430

431 432 433 434 435 436 437 438 439 440 441
	# uid may not be set cause of a problem with how resource records
	# converted over. Not worth worrying about.
	if (!isset($uid) || !$uid)
	    continue;

	if (!isset($pid_summary[$pid])) {
	    $pid_summary[$pid] = array('pnodes'   => 0,
				       'vnodes'   => 0,
				       'pseconds' => 0,
				       'eseconds' => 0,
				       'current'  => 1,
442 443
				       'preloaded'=> 0,
				       'started'  => 0,
444 445
				       'swapmods' => 0,
				       'swapins'  => 0);
446
	}
447 448 449 450 451 452 453 454 455 456 457 458 459
	if (!isset($uid_summary[$uid_idx])) {
	    $status = $row["status"];
	    $uid_summary[$uid_idx] = array('pnodes'   => 0,
					   'vnodes'   => 0,
					   'pseconds' => 0,
					   'eseconds' => 0,
					   'current'  => 1,
					   'preloaded'=> 0,
					   'started'  => 0,
					   'swapmods' => 0,
					   'swapins'  => 0,
					   'status'   => $status,
					   'uid'      => $uid);
460 461 462 463 464 465
	}
	$experiments[$rsrcidx]          = $rsrcidx;
	$pid_summary[$pid]["vnodes"]   += $vnodes;
	$pid_summary[$pid]["pnodes"]   += $pnodes;
	$pid_summary[$pid]["pseconds"] += $pnodes * $swapseconds;
	$pid_summary[$pid]["eseconds"] += $swapseconds;
466 467 468 469
	$uid_summary[$uid_idx]["vnodes"]   += $vnodes;
	$uid_summary[$uid_idx]["pnodes"]   += $pnodes;
	$uid_summary[$uid_idx]["pseconds"] += $pnodes * $swapseconds;
	$uid_summary[$uid_idx]["eseconds"] += $swapseconds;
470
    }
Leigh Stoller's avatar
Leigh Stoller committed
471 472

    $query_result =
473
	DBQueryFatal("select s.exptidx,s.pid,r.pnodes,r.vnodes, ".
474
		     "   swapin_time,swapout_time,swapmod_time,byswapmod, ".
475 476 477
		     "   e.state,r.idx,r.lastidx,byswapin,u.status, ".
		     "   s.creator as uid,s.creator_idx as uid_idx, ".
		     "   UNIX_TIMESTAMP(s.swapout_last) as swapout_last ".
478 479
		     " from experiment_resources as r ".
		     "left join experiment_stats as s on ".
480 481 482
		     "     r.exptidx=s.exptidx ".
		     "left join experiments as e on e.idx=s.exptidx ".
		     "left join users as u on u.uid_idx=r.uid_idx ".
483 484 485 486 487 488
		     "where (UNIX_TIMESTAMP(r.tstamp) >= $spanstart) and ".
		     "      (UNIX_TIMESTAMP(r.tstamp) <= $spanend) and ".
		     "      s.pid!='$TBOPSPID' and ".
		     "        not (s.pid='ron' and s.eid='all') ".
		     (isset($experiment) ?
		      "and r.exptidx=" . $experiment->idx() . " " : " ") .
489
		     "order by s.exptidx,UNIX_TIMESTAMP(r.tstamp)");
Leigh Stoller's avatar
Leigh Stoller committed
490 491

    while ($row = mysql_fetch_assoc($query_result)) {
492 493 494
	$exptidx      = $row["exptidx"];
	$pid          = $row["pid"];
	$uid          = $row["uid"];
495
	$uid_idx      = $row["uid_idx"];
496 497 498 499 500 501 502
	$pnodes       = $row["pnodes"];
	$vnodes       = $row["vnodes"];
	$swapin_time  = $row["swapin_time"];
	$swapout_time = $row["swapout_time"];
	$swapmod_time = $row["swapmod_time"];
	$byswapmod    = $row["byswapmod"];
	$byswapin     = $row["byswapin"];
503
	$estate	      = $row["state"];
504 505
	$rsrcidx      = $row["idx"];
	$lastidx      = $row["lastidx"];
506
	$swapout_last = $row["swapout_last"];
507 508 509 510 511 512 513 514 515
	$swapseconds  = 0;

	if ($debug2) 
	    echo "$exptidx $rsrcidx $lastidx $pid $uid $pnodes $vnodes ".
		"$swapin_time $swapout_time $swapmod_time $byswapmod<br>\n";

	# uid may not be set cause of a problem with how resource records
	# converted over. Not worth worrying about.
	if (!isset($uid) || !$uid)
Leigh Stoller's avatar
Leigh Stoller committed
516 517 518 519
	    continue;

	if (!isset($pid_summary[$pid])) {
	    $pid_summary[$pid] = array('pnodes'   => 0,
520
				       'vnodes'   => 0,
Leigh Stoller's avatar
Leigh Stoller committed
521
				       'pseconds' => 0,
522
				       'eseconds' => 0,
523
				       'current'  => 0,
524 525
				       'preloaded'=> 0,
				       'started'  => 0,
526
				       'swapmods' => 0,
527
				       'swapins'  => 0);
528
	}
529 530 531 532 533 534 535 536 537 538 539 540 541 542
	if (!isset($uid_summary[$uid_idx])) {
	    $status = $row["status"];

	    $uid_summary[$uid_idx] = array('pnodes'   => 0,
					   'vnodes'   => 0,
					   'pseconds' => 0,
					   'eseconds' => 0,
					   'current'  => 0,
					   'preloaded'=> 0,
					   'started'  => 0,
					   'swapmods' => 0,
					   'swapins'  => 0,
					   'status'   => $status,
					   'uid'      => $uid);
Leigh Stoller's avatar
Leigh Stoller committed
543 544
	}

545
	if (!$lastidx) {
546 547
	    if ($swapin_time) {
		$pid_summary[$pid]["started"]++;
548
		$uid_summary[$uid_idx]["started"]++;
549 550 551
	    }
	    else {
		$pid_summary[$pid]["preloaded"]++;
552
		$uid_summary[$uid_idx]["preloaded"]++;
553
	    }
554 555
	}
	if ($byswapin) {
556
	    $pid_summary[$pid]["swapins"]++;
557
	    $uid_summary[$uid_idx]["swapins"]++;
Leigh Stoller's avatar
Leigh Stoller committed
558
	}
559 560
	if ($byswapmod) {
	    $pid_summary[$pid]["swapmods"]++;
561
	    $uid_summary[$uid_idx]["swapmods"]++;
562 563
	    if (!($pnodes || $vnodes))
		$inactive_swapmods++;
564
	}
565

566 567 568 569 570 571 572
	# Current experiment and this resource record is the one we
	# saw in the fist query above?, then skip it.
	if (isset($experiments[$rsrcidx]))
	    continue;

	if (!$pnodes)
	    continue;
573

574 575 576 577 578 579 580 581
	$begin = $end = 0;

	#
	# If no swapin for the record skip it. Not supposed to happen!
	# If there is no swapout or swapmod, it is supposed to be a
	# current experiment.
	#
	if ($swapin_time == 0) {
582
	    if (1)
583 584
		echo "No swapin time: $exptidx, $rsrcidx<br>\n";
	    continue;
585
	}
586 587 588 589 590 591 592 593 594 595 596 597 598
	if ($swapout_time == 0 && $swapmod_time == 0 &&
	    (!isset($estate) || $estate != $TB_EXPTSTATE_ACTIVE)) {
	    #
	    # This is a fixup for missing swapout times.
	    #
	    echo "No swapout/swapmod time: $exptidx, $rsrcidx";
	    if (isset($swapout_last) && $swapout_last > $swapin_time) {
		echo " ... ";
		echo "Using swapout_last instead: $swapout_last<br>\n";
		$swapout_time = $swapout_last;
	    }
	    else {
		echo "<br>\n";
599 600
		continue;
	    }
601 602
	}
	if ($swapout_time == 0 && $swapmod_time == 0) {
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	    if ($spanend < $swapin_time)
		continue;

	    $begin = ($swapin_time < $spanstart ? $spanstart : $swapin_time);
	    $end   = $spanend;
	}
	elseif ($swapout_time) {
	    if ($spanend < $swapin_time)
		continue;

	    $begin = ($swapin_time  < $spanstart ? $spanstart : $swapin_time);
	    $end   = ($swapout_time > $spanend   ? $spanend   : $swapout_time);
	}
	else {
	    if (! $swapin_time || $spanend < $swapin_time)
		continue;

	    $begin = ($swapin_time  < $spanstart ? $spanstart : $swapin_time);
	    $end   = ($swapmod_time > $spanend   ? $spanend   : $swapmod_time);
	}
	$swapseconds = $end - $begin;

	if ($debug2) 
	    echo " $swapseconds<br>\n";
	
	if ($swapseconds < 0)
	    continue;

	$pid_summary[$pid]["vnodes"]   += $vnodes;
	$pid_summary[$pid]["pnodes"]   += $pnodes;
	$pid_summary[$pid]["pseconds"] += $pnodes * $swapseconds;
	$pid_summary[$pid]["eseconds"] += $swapseconds;
635 636 637 638
	$uid_summary[$uid_idx]["vnodes"]   += $vnodes;
	$uid_summary[$uid_idx]["pnodes"]   += $pnodes;
	$uid_summary[$uid_idx]["pseconds"] += $pnodes * $swapseconds;
	$uid_summary[$uid_idx]["eseconds"] += $swapseconds;
639
    }
640

Leigh Stoller's avatar
Leigh Stoller committed
641 642 643 644 645 646 647 648 649 650 651
    switch ($showby) {
        case "projects":
	    $which = "pid";
	    $table = $pid_summary;
	    $title = "Project Summary Stats ($range)";
	    $link  = "showproject.php3?pid=";
	    break;
        case "users":
	    $which = "uid";
	    $table = $uid_summary;
	    $title = "User Summary Stats ($range)";
652
	    $link  = "showuser.php3?user=";
Leigh Stoller's avatar
Leigh Stoller committed
653 654 655 656
	    break;
        default:
	    USERERROR("Invalid showby argument: $showby!", 1);
    }
657 658
    ksort($table);
    
659 660 661 662
    #
    # Gather some totals first.
    #
    $pnode_total  = 0;
663
    $vnode_total  = 0;
664 665
    $pdays_total  = 0;
    $edays_total  = 0;
666
    $swapin_total = 0;
667
    $swapmod_total= 0;
668 669
    $preload_total= 0;
    $started_total= 0;
670 671 672

    foreach ($table as $key => $value) {
	$pnodes  = $value["pnodes"];
673
	$vnodes  = $value["vnodes"];
674
	$swapins = $value["swapins"];
675
	$swapmods= $value["swapmods"];
676 677
	$preload = $value["preloaded"];
	$starts  = $value["started"];
678 679
	$pdays   = sprintf("%.2f", $value["pseconds"] / (3600 * 24));
	$edays   = sprintf("%.2f", $value["eseconds"] / (3600 * 24));
680

681
	if ($debug2)
682 683
	    echo "$key $value[pseconds] $value[eseconds]<br>\n";
	
684 685 686 687 688 689
	$pnode_total   += $pnodes;
	$vnode_total   += $vnodes;
	$pdays_total   += $pdays;
	$edays_total   += $edays;
	$swapin_total  += $swapins;
	$swapmod_total += $swapmods;
690 691
	$preload_total += $preload;
	$started_total += $starts;
692 693 694 695 696 697 698
    }

    SUBPAGESTART();
    echo "<table>
           <tr><td colspan=2 nowrap align=center>
               <b>Totals</b></td>
           </tr>
699 700 701 702 703 704
           <tr><td nowrap align=right><b>New Projects</b></td>
               <td align=left>$projects_created</td>
           </tr>
           <tr><td nowrap align=right><b>New Users</b></td>
               <td align=left>$users_created</td>
           </tr>
705 706 707
           <tr><td nowrap align=right><b>Pnodes</b></td>
               <td align=left>$pnode_total</td>
           </tr>
708 709 710
           <tr><td nowrap align=right><b>Vnodes</b></td>
               <td align=left>$vnode_total</td>
           </tr>
711 712
           <tr><td nowrap align=right><b>Pnode Days</b></td>
               <td align=left>$pdays_total</td>
713
           </tr>
714 715
           <tr><td nowrap align=right><b>Expt Days</b></td>
               <td align=left>$edays_total</td>
716
           </tr>
717 718 719
           <tr><td nowrap align=right><b>Starts</b></td>
               <td align=left>$started_total</td>
           </tr>
720 721 722
           <tr><td nowrap align=right><b>Swapins</b></td>
               <td align=left>$swapin_total</td>
           </tr>
723 724
           <tr><td nowrap align=right><b>Total Swapmods</b></td>
               <td align=left>$swapmod_total</td>
725
           </tr>
726 727 728 729 730
           <tr><td nowrap align=right><b>Inactive Swapmods</b></td>
               <td align=left>$inactive_swapmods</td>
           </tr>
           <tr><td nowrap align=right><b>Preloaded</b></td>
               <td align=left>$preload_total</td>
731
           </tr>
732 733 734
          </table>\n";
    SUBMENUEND_2B();
    
735 736 737 738
    echo "<center>
               <b>$title</b><br>
               (includes current experiments (*))
          </center><br>\n";
739 740
    echo "<table align=center border=1 id=sumstats>
	  <thead class='sort'>
Leigh Stoller's avatar
Leigh Stoller committed
741
          <tr>
742 743 744 745
             <th>$which</th>
             <th>Pnodes</th>
             <th>Pnode Days</th>
             <th>Expt Days</th>
746
             <th>Starts</th>
747 748
             <th>Swapins</th>
             <th>Swapmods</th>
749
             <th>Preloaded</th>
750 751
             <th>Vnodes</th>
          </tr></thead>\n";
Leigh Stoller's avatar
Leigh Stoller committed
752 753 754 755

    foreach ($table as $key => $value) {
	$heading = $key;
	$pnodes  = $value["pnodes"];
756
	$vnodes  = $value["vnodes"];
757
	$swapins = $value["swapins"];
758
	$swapmods= $value["swapmods"];
759 760
	$preload = $value["preloaded"];
	$starts  = $value["started"];
761
	$current = $value["current"];
762 763
	$pdays   = sprintf("%.2f", $value["pseconds"] / (3600 * 24));
	$edays   = sprintf("%.2f", $value["eseconds"] / (3600 * 24));
Leigh Stoller's avatar
Leigh Stoller committed
764

765 766 767 768 769
	if ($current)
	    $current = "*";
	else
	    $current = "";

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
	echo "<tr>";

	if ($showby == "users") {
	    # A current, archived, or a deleted user?
	    $status  = $value["status"];
	    $heading = $value["uid"];
	    
	    if (isset($status)) {
		$extra    = "";

		if ($status == TBDB_USERSTATUS_ARCHIVED) {
		    $extra = "(a)";
		}
		echo "<td><A href='$link${key}'>$heading $extra</A></td>";
	    }
	    else {
		echo "<td>$heading</td>";
	    }
	}
	else {
	    echo "<td><A href='$link${heading}'>$heading $current</A></td>";
	}
        echo "  <td>$pnodes</td>
793 794
                <td>$pdays</td>
                <td>$edays</td>
795
                <td>$starts</td>
796
                <td>$swapins</td>
797
                <td>$swapmods</td>
798
                <td>$preload</td>
799
                <td>$vnodes</td>
Leigh Stoller's avatar
Leigh Stoller committed
800 801 802
              </tr>\n";
    }
    echo "</table>\n";
803
    SUBPAGEEND();
Leigh Stoller's avatar
Leigh Stoller committed
804 805 806
}

if ($range == "epoch") {
807
    showsummary($showby);
Leigh Stoller's avatar
Leigh Stoller committed
808 809
}
else {
810
    showrange($showby, $range);
Leigh Stoller's avatar
Leigh Stoller committed
811 812
}

813 814 815
echo "<script type='text/javascript' language='javascript'>
       sorttable.makeSortable(getObjbyName('sumstats'));
      </script>\n";
Leigh Stoller's avatar
Leigh Stoller committed
816 817 818 819 820
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>