experiment_defs.php 46.1 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
<?php
#
3
# Copyright (c) 2006-2018 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
#
#
Leigh Stoller's avatar
Leigh Stoller committed
25
include_once("template_defs.php");
26
include_once("geni_defs.php");
Leigh Stoller's avatar
Leigh Stoller committed
27

Leigh Stoller's avatar
Leigh Stoller committed
28 29
# This class is really just a wrapper around the DB data ...
#
30 31 32 33
# A cache to avoid lookups. Indexed by node_id.
#
$experiment_cache = array();

Leigh Stoller's avatar
Leigh Stoller committed
34 35
class Experiment
{
36 37
    var	$experiment;
    var $group;
38 39
    var $project;
    var $isinstance = false;
40
    var $resources;
Leigh Stoller's avatar
Leigh Stoller committed
41

42 43 44 45 46 47
    #
    # Constructor by lookup on unique index.
    #
    function Experiment($exptidx) {
	$safe_exptidx = addslashes($exptidx);

48 49 50 51
	#
	# We do a join with the instances table simply to determine if this
	# experiment is a current instance. 
	#
52
	$query_result =
53 54 55 56
	    DBQueryWarn("select e.*,i.parent_guid from experiments as e ".
			"left join experiment_template_instances as i on ".
			"     i.exptidx=e.idx ".
			"where e.idx='$safe_exptidx'");
57 58 59 60 61 62 63

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->experiment = null;
	    return;
	}
	$this->experiment = mysql_fetch_array($query_result);

64 65 66 67 68
	# Instance flag.
	if ($this->experiment["parent_guid"]) {
	    $this->isinstance = true;
	}

69 70
	# Load lazily;
	$this->group      = null;
71
	$this->project    = null;
72
	$this->resources  = null;
73 74 75 76 77 78 79
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->experiment);
    }

80
    # Lookup by exptidx, but allow for lookup by pid,eid with variable args.
81
    function Lookup($exptidx) {
82 83
	global $experiment_cache;
	
84 85
	$args = func_get_args();

86 87 88 89
        # Look in cache first
	if (array_key_exists("$exptidx", $experiment_cache))
	    return $experiment_cache["$exptidx"];
	
90 91
	$foo = new Experiment($exptidx);

92 93 94
	if ($foo->IsValid()) {
            # Insert into cache.
	    $experiment_cache["$exptidx"] =& $foo;
95
	    return $foo;
96
	}
97 98 99 100 101 102 103 104

        # Try lookup with pid,eid.
	if (count($args) == 2) {
	    $pid = array_shift($args);
	    $eid = array_shift($args);

	    $foo = Experiment::LookupByPidEid($pid, $eid);

105
	    # Already in the cache from LookupByPidEid() so just return it.
106
	    if ($foo && $foo->IsValid())
107 108
		return $foo;
	}
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	return null;
    }

    # Backwards compatable lookup by pid,eid. Will eventually flush this.
    function LookupByPidEid($pid, $eid) {
	$safe_pid = addslashes($pid);
	$safe_eid = addslashes($eid);

	$query_result =
	    DBQueryWarn("select idx from experiments ".
			"where pid='$safe_pid' and eid='$safe_eid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['idx'];

	return Experiment::Lookup($idx); 
    }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    function LookupByUUID($uuid) {
	$safe_uuid = addslashes($uuid);

	$query_result =
	    DBQueryWarn("select idx from experiments ".
			"where eid_uuid='$safe_uuid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['idx'];

	return Experiment::Lookup($idx); 
    }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$idx = $this->idx();

	$query_result =
	    DBQueryWarn("select * from experiments where idx='$idx'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->experiment = NULL;
159 160
	    $this->group      = null;
	    $this->project    = null;
161
	    $this->resources  = null;
162 163 164 165
	    return -1;
	}
	$this->experiment = mysql_fetch_array($query_result);
	$this->group      = null;
166
	$this->project    = null;
167
	$this->resources  = null;
168 169 170
	return 0;
    }

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    #
    # Class function to change experiment info via XML to a backend script.
    #
    function EditExp($experiment, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

	if (!count($args)) {
	    $errors[] = "No changes to submit.";
	    return null;
	}	    

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "editexp");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
188
	    $errors[] = "Transient error(1); please try again later.";
189 190 191 192
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
193
	    $errors[] = "Transient error(2); please try again later.";
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["experiment"] = $experiment->idx();

	fwrite($fp, "<experiment>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</experiment>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	$retval = SUEXEC("nobody", "nobody", "webeditexp $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
215
		$errors[] = "Transient error(3, $retval); please try again later.";
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
232 233
		    $errors[] = "Transient error(4, $retval); ".
			"please try again later.";
234 235 236 237 238 239 240
	    }
	    return null;
	}

	# There are no return value(s) to parse at the end of the output.

	# Unlink this here, so that the file is left behind in case of error.
Russ Fish's avatar
Russ Fish committed
241
	# We can then edit the experiment by hand from the xmlfile, if desired.
242 243 244 245
	unlink($xmlname);
	return true;
    }

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    #
    # Update fields. Array of "foo=bar" ...
    #
    function UpdateOldStyle($inserts) {
	$idx = $this->idx();

	DBQueryFatal("update experiments set ".
		     implode(",", $inserts) . " ".
		     "where idx='$idx'");

	return $this->Refresh();
    }
    
    #
    # Load the project object for an experiment.
    #
    function Project() {
	$pid = $this->pid();

	if ($this->project)
	    return $this->project;

	$this->project = Project::Lookup($pid);
	if (! $this->project) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $this->project;
    }
274 275 276 277 278 279 280 281
    #
    # Load the group object for an experiment.
    #
    function Group() {
	$pid = $this->pid();
	$gid = $this->gid();

	if ($this->group)
282
	    return $this->group;
283 284 285 286 287 288 289 290

	$this->group = Group::LookupByPidGid($pid, $gid);
	if (! $this->group) {
	    TBERROR("Could not lookup group $pid/$gid!", 1);
	}
	return $this->group;
    }

291 292 293 294 295 296 297 298 299
    #
    # Get the creator for a project.
    #
    function GetCreator() {
	return User::Lookup($this->creator());
    }
    function GetSwapper() {
	return User::Lookup($this->swapper());
    }
300 301 302
    function GetStats() {
	return ExperimentStats::Lookup($this->idx());
    }
303
    function GetResources() {
304 305
	$stats = $this->GetStats();
	return ExperimentResources::Lookup($stats->rsrcidx());
306
    }
307

308 309 310 311 312 313 314 315
    # accessors
    function field($name) {
	return (is_null($this->experiment) ? -1 : $this->experiment[$name]);
    }
    function pid()	    { return $this->field('pid'); }
    function gid()	    { return $this->field('gid'); }
    function eid()	    { return $this->field('eid'); }
    function idx()	    { return $this->field('idx'); }
316
    function uuid()	    { return $this->field('eid_uuid'); }
317
    function description()  { return $this->field('expt_name'); }
318 319 320 321 322 323 324 325 326
    function path()	    { return $this->field('path'); }
    function state()	    { return $this->field('state'); }
    function batchstate()   { return $this->field('batchstate'); }
    function batchmode()    { return $this->field('batchmode'); }
    function creator()      { return $this->field('expt_head_uid');}
    function canceled()     { return $this->field('canceled'); }
    function locked()       { return $this->field('expt_locked'); }
    function elabinelab()   { return $this->field('elab_in_elab');}
    function lockdown()     { return $this->field('lockdown'); }
327
    function skipvlans()    { return $this->field('skipvlans'); }
328 329
    function created()      { return $this->field('expt_created'); }
    function swapper()      { return $this->field('expt_swap_uid');}
330
    function swapper_idx()  { return $this->field('swapper_idx');}
331
    function swapped()      { return $this->field('expt_swapped');}
332 333 334 335 336 337 338 339 340 341 342 343
    function swappable()    { return $this->field('swappable');}
    function idleswap()     { return $this->field('idleswap');}
    function autoswap()     { return $this->field('autoswap');}
    function noswap_reason(){ return $this->field('noswap_reason');}
    function noidleswap_reason(){ return $this->field('noidleswap_reason');}
    function idleswap_timeout() { return $this->field('idleswap_timeout');}
    function autoswap_timeout() { return $this->field('autoswap_timeout');}
    function prerender_pid()    { return $this->field('prerender_pid');}
    function dpdb()		{ return $this->field('dpdb');}
    function dpdbname()         { return $this->field('dpdbname');}
    function dpdbpassword()     { return $this->field('dpdbpassword');}
    function instance_idx()     { return $this->field('instance_idx'); }
344 345 346 347 348 349 350 351 352 353
    function swap_requests()    { return $this->field('swap_requests'); }
    function last_swap_req()    { return $this->field('last_swap_req'); }
    function idle_ignore()      { return $this->field('idle_ignore'); }
    function savedisk()         { return $this->field('savedisk'); }
    function mem_usage()        { return $this->field('mem_usage'); }
    function cpu_usage()        { return $this->field('cpu_usage'); }
    function linktest_level()   { return $this->field('linktest_level'); }
    function linktest_pid()     { return $this->field('linktest_pid'); }
    function logfile()          { return $this->field('logfile'); }
    function keyhash()          { return $this->field('keyhash'); }
354 355
    function paniced()          { return $this->field('paniced'); }
    function panic_date()       { return $this->field('panic_date'); }
356
    function geniflags()        { return $this->field('geniflags'); }
357
    function nonlocal_id()      { return $this->field('nonlocal_id'); }
358
    function virtnode_count()   { return $this->field('virtnode_count'); }
359 360

    #
361 362
    # Access Check. Project level check since this might not be a current
    # experiment.
363
    #
364
    function AccessCheck ($user, $access_type) {
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	global $TB_EXPT_READINFO;
	global $TB_EXPT_MODIFY;
	global $TB_EXPT_DESTROY;
	global $TB_EXPT_UPDATE;
	global $TB_EXPT_MIN;
	global $TB_EXPT_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_PROJROOT;
	$mintrust = $TB_EXPT_READINFO;

	if ($access_type < $TB_EXPT_MIN ||
	    $access_type > $TB_EXPT_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}

        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}

	if ($access_type == $TB_EXPT_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

	$uid = $user->uid();
397
	$pid = $this->pid();
398
	$gid = $this->gid();
399
	$uid = $user->uid();
400
	
401 402 403 404 405 406 407
        #
        # Either proper permission in the group, or group_root in the project.
        # This lets group_roots muck with other peoples experiments, including
        # those in groups they do not belong to.
        #
	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
408 409 410 411 412 413 414 415
    }

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$pid = $this->pid();
	$eid = $this->eid();
416
	$tag = ($this->isinstance ? "Instance" : "Experiment");
417
	
418
	$html = "<font size=+2>$tag <b>".
419 420 421 422 423 424 425
	    "<a href='showproject.php3?pid=$pid'>$pid</a>/".
	    "<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a>".
	    "</b></font>\n";

	return $html;
    }

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    #
    # Flip batch mode, return success to caller. 
    #
    function SetBatchMode($mode) {
	global $TB_EXPTSTATE_SWAPPED;
	
	$reqstate = $TB_EXPTSTATE_SWAPPED;
	$idx      = $this->idx();
	$mode     = ($mode ? 1 : 0);

	DBQueryFatal("lock tables experiments write");

	$query_result =
	    DBQueryFatal("update experiments set ".
			 "   batchmode=$mode ".
			 "where idx='$idx' and ".
			 "     expt_locked is NULL and state='$reqstate'");

	$success = DBAffectedRows();
	DBQueryFatal("unlock tables");

	return ($success ? 0 : -1);
    }

450 451 452 453 454 455 456 457 458 459 460 461 462 463
    #
    # Flip lockdown bit.
    #
    function SetSkipVlans($mode) {
	$idx      = $this->idx();
	$mode     = ($mode ? 1 : 0);

	$query_result =
	    DBQueryFatal("update experiments set skipvlans='$mode' ".
			 "where idx='$idx'");

	return 0;
    }

464 465 466 467 468 469 470 471
    function GetLogfile() {
	$this->Refresh();
	
	if ($this->logfile()) 
	    return Logfile::Lookup($this->logfile());
	return null;
    }

472 473 474 475 476 477 478 479
    #
    # Return the Unix GID (numeric) for an experiments group.
    #
    function UnixGID() {
	$group = $this->Group();

	return $group->unix_gid();
    }
480 481 482

    # Return the stored NS file for an experiment.
    function NSFile() {
483 484 485
	$resources      = $this->GetResources();
	$input_data_idx = $resources->input_data_idx();

486
	$query_result =
487 488
	    DBQueryFatal("select input from experiment_input_data ".
			 "where idx='$input_data_idx'");
489 490 491 492 493

	if (! mysql_num_rows($query_result)) {
	    return null;
	}
	$row    = mysql_fetch_array($query_result);
494
	return $row["input"];
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    }

    #
    # Return number of physical nodes (pcs) in use by experiment.
    #
    function PCCount() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select count(*) as count from reserved as r ".
			 "left join nodes as n on r.node_id=n.node_id ".
			 "left join node_types as nt on n.type=nt.type ".
			 "where nt.class='pc' and pid='$pid' and eid='$eid'");

	if (!mysql_num_rows($query_result))
	    return 0;

	$row = mysql_fetch_array($query_result);
	return $row["count"];
    }

    #
    # Return number of events.
    #
    function EventCount() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select count(*) from eventlist ".
			 "where pid='$pid' and eid='$eid'");

	if (mysql_num_rows($query_result) == 0) {
	    return 0;
	}
	$row = mysql_fetch_row($query_result);
	return $row[0];
    }

    #
    # Return time since swapin
    #
    function SwapSeconds() {
	$idx = $this->idx();

	$query_result =
	    DBQueryFatal("select (UNIX_TIMESTAMP(now()) - ".
			 "        UNIX_TIMESTAMP(expt_swapped)) ".
			 "  from experiments ".
			 "where idx='$idx'");

	$row = mysql_fetch_array($query_result);
	return $row[0];
    }

551 552 553 554 555 556 557 558
    #
    # Return formated time/date string at which autoswap will occur
    # (if enabled).
    #
    function AutoSwapTime() {
	$idx = $this->idx();
	# Let mysql do the work
	$query_result =
559 560
	    DBQueryWarn("select date_add(s.swapin_last,".
			"                interval e.autoswap_timeout minute) ".
561
			"       as autoswap_date ".
562 563
			"  from experiments as e, experiment_stats as s ".
			"where s.exptidx=e.idx and idx='$idx'");
564 565 566 567 568

	$row = mysql_fetch_array($query_result);
	return $row[0];
    }

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    var $lastact_query =
	"greatest(last_tty_act,last_net_act,last_cpu_act,last_ext_act)";

    #
    # Gets the time of last activity for an expt
    #
    function LastAct() {
	$pid = $this->pid();
	$eid = $this->eid();
	$clause = $this->lastact_query;
	
	$query_result =
	    DBQueryWarn("select max($clause) as last_act ".
			"  from node_activity as na ".
			"left join reserved as r on na.node_id=r.node_id ".
			"where pid='$pid' and eid='$eid' ".
			"group by pid,eid");

	if (mysql_num_rows($query_result) == 0) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	return $row["last_act"];
    }

    #
    # Gets the time of idleness for an expt, in hours
    #
    function IdleTime($format = 0) {
	$pid = $this->pid();
	$eid = $this->eid();
	$clause = $this->lastact_query;

	$query_result =
	    DBQueryWarn("select (unix_timestamp(now()) - unix_timestamp( ".
			"        max($clause))) as idle_time ".
			"  from node_activity as na ".
			"left join reserved as r on na.node_id=r.node_id ".
			"where pid='$pid' and eid='$eid' ".
			"group by pid,eid");

	if (mysql_num_rows($query_result) == 0) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$t = $row["idle_time"];
        # if it is less than 5 minutes, it is not idle at all...
	$t = ($t < 300 ? 0 : $t);
	if (!$format) {
	    $t = round($t/3600,2);
	}
	else {
	    $t = date($format,mktime(0,0,$t,1,1));
	}
	return $t;
    }

    #
    # Find out if an expts idle report is stale
    #
    function IdleStale() {
	$pid = $this->pid();
	$eid = $this->eid();

        # We currently have a 5 minute interval for slothd between reports
        # So give some slack in case a node reboots without reporting for
	# a while
	#
	# in minutes
	$staletime = 10; 
	$stalesec  = 60 * $staletime;

	$query_result =
	    DBQueryWarn("select (unix_timestamp(now()) - ".
			"        unix_timestamp(min(last_report) )) as t ".
			"  from node_activity as na ".
			"left join reserved as r on na.node_id=r.node_id ".
			"where pid='$pid' and eid='$eid' ".
			"group by pid,eid");

	if (mysql_num_rows($query_result) == 0) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	return ($row["t"]>$stalesec);
    }

    #
    # Is an experiment firewalled.
    # 
    function Firewalled() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select eid from virt_firewalls ".
			 "where pid='$pid' and eid='$eid' and ".
			 "      type like '%-vlan'");
	
	if (!mysql_num_rows($query_result)) {
	    return 0;
	}
	return 1;
    }
    
    #
    # Show an experiment.
    #
    function Show($short = 0, $sortby = "") {
678
	global $TBDBNAME, $TBDOCBASE, $WIKIDOCURL;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	$pid = $this->pid();
	$eid = $this->eid();
	$nodecounts  = array();

        # Node counts, by class. 
	$query_result =
	    DBQueryFatal("select nt.class,count(*) from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "left join node_types as nt on n.type=nt.type ".
			 "where pid='$pid' and eid='$eid' group by nt.class");

	while ($row = mysql_fetch_array($query_result)) {
	    $class = $row[0];
	    $count = $row[1];
	
	    $nodecounts[$class] = $count;
	}
		
	$query_result =
	    DBQueryFatal("select e.*, s.archive_idx, pl.slicename, ". 
			 "round(e.minimum_nodes+.1,0) as min_nodes, ".
			 "round(e.maximum_nodes+.1,0) as max_nodes ".
			 " from experiments as e ".
			 "left join experiment_stats as s on s.exptidx=e.idx ".
			 "left join plab_slices as pl".
			 " on e.pid = pl.pid and e.eid = pl.eid ".
			 "where e.pid='$pid' and e.eid='$eid'");
    
	if (($exprow = mysql_fetch_array($query_result)) == 0) {
	    TBERROR("Experiment $eid in project $pid is gone!\n", 1);
	}

	$exp_gid     = $exprow["gid"];
	$exp_name    = $exprow["expt_name"];
	$exp_swapped = $exprow["expt_swapped"];
	$exp_swapuid = $exprow["expt_swap_uid"];
	$exp_end     = $exprow["expt_end"];
	$exp_created = $exprow["expt_created"];
717
	$exp_expires = $exprow["expt_expires"];
718
	$exp_head    = $exprow["expt_head_uid"];
719
	$exp_swapper = $exprow["swapper_idx"];
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	$exp_state   = $exprow["state"];
	$exp_shared  = $exprow["shared"];
	$exp_path    = $exprow["path"];
	$batchmode   = $exprow["batchmode"];
	$canceled    = $exprow["canceled"];
	$attempts    = $exprow["attempts"];
	$expt_locked = $exprow["expt_locked"];
	$priority    = $exprow["priority"];
	$swappable   = $exprow["swappable"];
	$noswap_reason = $exprow["noswap_reason"];
	$idleswap    = $exprow["idleswap"];
	$idleswap_timeout  = $exprow["idleswap_timeout"];
	$noidleswap_reason = $exprow["noidleswap_reason"];
	$autoswap    = $exprow["autoswap"];
	$autoswap_timeout  = $exprow["autoswap_timeout"];
	$idle_ignore = $exprow["idle_ignore"];
	$savedisk    = $exprow["savedisk"];
	$swapreqs    = $exprow["swap_requests"];
	$lastswapreq = $exprow["last_swap_req"];
	$minnodes    = $exprow["min_nodes"];
	$maxnodes    = $exprow["max_nodes"];
741
	$virtnodes   = $exprow["virtnode_count"];
742 743 744 745 746 747 748 749 750 751
	$syncserver  = $exprow["sync_server"];
	$mem_usage   = $exprow["mem_usage"];
	$cpu_usage   = $exprow["cpu_usage"];
	$exp_slice   = $exprow["slicename"];
	$linktest_level = $exprow["linktest_level"];
	$linktest_pid   = $exprow["linktest_pid"];
	$usemodelnet = $exprow["usemodelnet"];
	$mnet_cores  = $exprow["modelnet_cores"];
	$mnet_edges  = $exprow["modelnet_edges"];
	$lockdown    = $exprow["lockdown"];
752
	$skipvlans   = $exprow["skipvlans"];
753 754 755 756 757
	$exptidx     = $exprow["idx"];
	$archive_idx = $exprow["archive_idx"];
	$dpdb        = $exprow["dpdb"];
	$dpdbname    = $exprow["dpdbname"];
	$dpdbpassword= $exprow["dpdbpassword"];
758
	$uuid        = $exprow["eid_uuid"];
Leigh Stoller's avatar
Leigh Stoller committed
759 760
	$paniced     = $exprow["paniced"];
	$panic_date  = $exprow["panic_date"];
761 762 763 764 765 766 767 768 769 770 771 772

	$autoswap_hrs= ($autoswap_timeout/60.0);
	$idleswap_hrs= ($idleswap_timeout/60.0);
	$noswap = "($noswap_reason)";
	$noidleswap = "($noidleswap_reason)";
	$autoswap_str= $autoswap_hrs." hour".($autoswap_hrs==1 ? "" : "s");
	$idleswap_str= $idleswap_hrs." hour".($idleswap_hrs==1 ? "":"s");

	if (! ($head_user = User::Lookup($exp_head))) {
	    TBERROR("Error getting object for user $exp_head", 1);
	}
	$showuser_url = CreateURL("showuser", $head_user);
773 774 775 776 777
	if (! ($swapper = User::Lookup($exp_swapper))) {
	    TBERROR("Error getting object for user $exp_swapper", 1);
	}
	$swapper_uid = $swapper->uid();
	$swapper_url = CreateURL("showuser", $swapper);
778 779 780 781 782 783 784 785 786 787 788

	if ($swappable)
	    $swappable = "Yes";
	else
	    $swappable = "<b>No</b> $noswap";

	if ($idleswap)
	    $idleswap = "Yes (after $idleswap_str)";
	else
	    $idleswap = "<b>No</b> $noidleswap";

789 790 791 792 793 794 795
	if ($autoswap) {
	    $autoswap = "<b>Yes</b>";
	    $str = $this->AutoSwapTime();
	    if ($str && $swappable == "Yes" && $exp_state == "active")
		$autoswap .= "<b>, auto-swapout at $str</b>";
	    $autoswap .= " ($autoswap_str from swapin)";
	} else
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	    $autoswap = "No";

	if ($idle_ignore)
	    $idle_ignore = "<b>Yes</b>";
	else
	    $idle_ignore = "No";

	if ($savedisk)
	    $savedisk = "<b>Yes</b>";
	else
	    $savedisk = "No";

	if ($expt_locked)
	    $expt_locked = "($expt_locked)";
	else
	    $expt_locked = "";

	$query_result =
	    DBQueryFatal("select cause_desc ".
			 "from experiment_stats as s,errors,causes ".
			 "where s.exptidx = $exptidx ".
			 "and errors.cause = causes.cause ".
			 "and s.last_error = errors.session");

	if ($row = mysql_fetch_array($query_result)) {
	    $err_cause = $row[0];
	} else {
	    $err_cause = '';
	}

        #
        # Generate the table.
        #
829
	echo "<table align=center border=1>\n";
830 831

	if (!$short) {
832
	    $thisurl = CreateURL("showexp", $this);
833 834
	    echo "<tr>
                <td>Name: </td>
835
                <td class=left><a href='$thisurl'>$eid</a></td>
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
              </tr>\n";

	    echo "<tr>
                <td>Description: </td>
                <td class=\"left\">$exp_name</td>
              </tr>\n";

	    echo "<tr>
                <td>Project: </td>
                <td class=\"left\">
                  <a href='showproject.php3?pid=$pid'>$pid</a></td>
              </tr>\n";

	    echo "<tr>
                <td>Group: </td>
                <td class=\"left\">
                  <a href='showgroup.php3?pid=$pid&gid=$exp_gid'>$exp_gid</a>
                </td>
              </tr>\n";

	    if (isset($exp_slice)) {
		echo "<tr>
                  <td>Planetlab Slice: </td>
                  <td class=\"left\">$exp_slice</td>
                </tr>\n";
	    }
	}

	echo "<tr>
865
            <td>Creator: </td>
866 867 868 869
            <td class=\"left\">
              <a href='$showuser_url'>$exp_head</a></td>
          </tr>\n";

870 871 872 873 874 875 876 877
	if (!$swapper->SameUser($head_user)) {
	    echo "<tr>
                      <td>Swapper: </td>
                      <td class=\"left\">
                          <a href='$swapper_url'>$swapper_uid</a></td>
                  </tr>\n";
	}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	if (!$short) {
	    $instance = TemplateInstance::LookupByExptidx($exptidx);

	    if (! is_null($instance)) {
		$guid   = $instance->guid();
		$vers   = $instance->vers();
	
		echo "<tr>
                    <td>Template: </td>
                    <td class=\"left\">
                       <a href='template_show.php?guid=$guid&version=$vers'>
                          $guid/$vers</a>";

		if ($instance->runidx()) {
		    $runidx = $instance->runidx();
		    $runid  = $instance->GetRunID($runidx);
		    $url    = CreateURL("experimentrun_show", $instance,
					"runidx", $runidx);
		    echo " (Current Run:
                       <a href='$url'>$runid</a>)</td>";
		} else {
		    $runidx = $instance->LastRunIdx();
		    $runid  = $instance->GetRunID($runidx);
		    $url    = CreateURL("experimentrun_show", $instance,
					"runidx", $runidx);
		    echo " (Last Run:
                       <a href='$url'>$runid</a>)</td>";
		}
		echo "</tr>\n";
	    }

	    echo "<tr>
                <td>Created: </td>
                <td class=\"left\">$exp_created</td>
              </tr>\n";

	    if ($exp_swapped) {
		echo "<tr>
                    <td>Last Swap/Modify: </td>
917 918
                    <td class=\"left\">$exp_swapped
                        (<a href='$swapper_url'>$swapper_uid</a>)</td>
919 920 921 922 923
                  </tr>\n";
	    }

	    if (ISADMIN()) {
		echo "<tr>
924
                    <td><a href='$WIKIDOCURL/Swapping#swapping'>Swappable:</a></td>
925 926 927 928 929
                    <td class=\"left\">$swappable</td>
                  </tr>\n";
	    }
    
	    echo "<tr>
930
                  <td><a href='$WIKIDOCURL/Swapping#idleswap'>Idle-Swap:</a></td>
931 932 933 934
                  <td class=\"left\">$idleswap</td>
              </tr>\n";

	    echo "<tr>
935
                <td><a href='$WIKIDOCURL/Swapping#autoswap'>Max. Duration:</a></td>
936 937 938 939
                <td class=\"left\">$autoswap</td>
              </tr>\n";

	    echo "<tr>
940
                <td><a href='$WIKIDOCURL/Swapping#swapstatesave'>Save State:</a></td>
941 942 943 944
                <td class=\"left\">$savedisk</td>
              </tr>\n";

	    if (ISADMIN()) {
945 946 947 948 949 950
	        if ($exp_expires) {
		    echo "<tr>
		          <td>Expires: </td>
		          <td class=\"left\">$exp_expires</td>
		          </tr>\n";
                }
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
		echo "<tr>
                    <td>Idle Ignore:</td>
                    <td class=\"left\">$idle_ignore</td>
                 </tr>\n";
	    }
    
	    echo "<tr>
                <td>Path: </td>
                <td class=left>$exp_path</td>
              </tr>\n";

	    echo "<tr>
                <td>Status: </td>
                <td id=exp_state class=\"left\">$exp_state $expt_locked</td>
              </tr>\n";

	    if ($err_cause) {
		echo "<tr>
                    <td>Last Error: </td>
                    <td class=\"left\"><a href=\"showlasterror.php3?pid=$pid&eid=$eid\">$err_cause</a></td>
                  </tr>\n";
	    }

Leigh Stoller's avatar
Leigh Stoller committed
974 975 976 977 978 979 980 981
	    if ($paniced) {
		echo "<tr>
                    <td>Paniced on: </td>
                    <td class=left>$panic_date</td>
                  </tr>\n";

	    }

982 983 984 985 986 987 988 989
	    if ($linktest_pid) {
		$linktest_running = "<b>(Linktest Running)</b>";
	    }
	    else {
		$linktest_running = "";
	    }

	    echo "<tr>
990
                <td><a href='$WIKIDOCURL/linktest'>".
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
		"Linktest Level</a>: </td>
                <td class=\"left\">$linktest_level $linktest_running</td>
              </tr>\n";
	}

	if (count($nodecounts)) {
	    echo "<tr>
                 <td>Reserved Nodes: </td>
                 <td class=\"left\">\n";
	    while (list ($class, $count) = each($nodecounts)) {
		echo "$count ($class) &nbsp; ";
	    }
	    echo "   </td>
              </tr>\n";
	}
	elseif (!$short) {
	    if ($minnodes!="") {
		echo "<tr>
                      <td>Min/Max Nodes: </td>
                      <td class=\"left\"><font color=green>
                          $minnodes/$maxnodes (estimates)</font></td>
                  </tr>\n";
	    }
	    else {
		echo "<tr>
                      <td>Minumum Nodes: </td>
                      <td class=\"left\"><font color=green>Unknown</font></td>
                  </tr>\n";
	    }
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	    if ($virtnodes) {
		echo "<tr>
                      <td>Virtual Nodes: </td>
                      <td class=\"left\"><font>
                          $virtnodes</font></td>
                  </tr>\n";
	    }
	    else {
		echo "<tr>
                      <td>Virtual Nodes: </td>
                      <td class=\"left\"><font color=green>Unknown</font></td>
                  </tr>\n";
	    }
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	}
	if (!$short) {
	    if ($mem_usage || $cpu_usage) {
		echo "<tr>
                      <td>Mem Usage Est: </td>
                      <td class=\"left\">$mem_usage</td>
                  </tr>\n";

		echo "<tr>
                      <td>CPU Usage Est: </td>
                      <td class=\"left\">$cpu_usage</td>
                  </tr>\n";
	    }

	    $lastact  = $this->LastAct();
	    $idletime = $this->IdleTime();
	    $stale    = $this->IdleStale();
    
	    if ($lastact != -1) {
		echo "<tr>
                      <td>Last Activity: </td>
                      <td class=\"left\">$lastact</td>
                  </tr>\n";

		if ($stale) { $str = "(stale)"; } else { $str = ""; }
	
		echo "<tr>
                      <td>Idle Time: </td>
                      <td class=\"left\">$idletime hours $str</td>
                  </tr>\n";
	    }

	    if (! ($swapreqs=="" || $swapreqs==0)) {
		echo "<tr>
                      <td>Swap Requests: </td>
                      <td class=\"left\">$swapreqs</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last Swap Req.: </td>
                      <td class=\"left\">$lastswapreq</td>
                  </tr>\n";
	    }

1077
	    if (ISADMIN() || STUDLY() || OPSGUY()) {
1078 1079 1080 1081
		$thisflip = ($skipvlans ? 0 : 1);
		$flipval  = ($skipvlans ? "Yes" : "No");
		echo "<tr>
                       <td>Skip Vlans:</td>
1082 1083
                       <td>$flipval (<a href='toggle.php?pid=$pid&eid=$eid".
		           "&type=skipvlans&value=$thisflip'>Toggle</a>)
1084 1085 1086
                       </td>
                      </tr>\n";
	    }
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	}

	if ($batchmode) {
	    echo "<tr>
                    <td>Batch Mode: </td>
                    <td class=\"left\">Yes</td>
                  </tr>\n";

	    echo "<tr>
                    <td>Start Attempts: </td>
                    <td class=\"left\">$attempts</td>
                  </tr>\n";
	}

	if ($canceled) {
	    echo "<tr>
                 <td>Cancel Flag: </td>
                 <td class=\"left\">$canceled</td>
              </tr>\n";
	}

	if (!$short) {
1109 1110 1111 1112 1113 1114
	    if (ISADMIN()) {
		echo "<tr>
                        <td>UUID: </td>
                        <td class=left>$uuid</td>
                      </tr>\n";
	    }
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	    if ($usemodelnet) {
		echo "<tr>
                      <td>Use Modelnet: </td>
                      <td class=\"left\">Yes</td>
                  </tr>\n";
		echo "<tr>
                      <td>Modelnet Phys Core Nodes: </td>
                      <td class=\"left\">$mnet_cores</td>
                  </tr>\n";
		echo "<tr>
                      <td>Modelnet Phys Edge Nodes: </td>
                      <td class=\"left\">$mnet_edges</td>
                  </tr>\n";

	    }
	    if (isset($syncserver)) {
		echo "<tr>
                      <td>Sync Server: </td>
                      <td class=\"left\">$syncserver</td>
                  </tr>\n";
	    }
	    if ($dpdb && $dpdbname && $dpdbpassword) {
		echo "<tr>
                      <td>DataBase Name: </td>
                      <td class=\"left\">$dpdbname</td>
                  </tr>\n";

		echo "<tr>
                      <td>DataBase User: </td>
                      <td class=\"left\">E${exptidx}</td>
                  </tr>\n";

		echo "<tr>
                      <td>DataBase Password: </td>
                      <td class=\"left\">$dpdbpassword</td>
                  </tr>\n";
	    }
	    echo "<tr>
                  <td>Index: </td>
                  <td class=\"left\">$exptidx";
	    if ($archive_idx)
		echo " ($archive_idx) ";
	    echo " </td>
              </tr>\n";
	}
1160
	if (!$short) {
1161 1162
	    if ($this->geniflags()) {
		$slice = GeniSlice::Lookup("geni-cm", $uuid);
1163

1164 1165
		if ($slice) {
		    $slice_hrn = $slice->hrn();
1166
		    $slice_urn = $slice->urn();
1167 1168 1169
		    if (ISADMIN()) {
			$url = CreateURL("showslice", "slice_idx",
					 $slice->idx(), "showtype", "cm");
1170

1171 1172 1173
			echo "<tr>
                                <td>Geni Slice (CM): </td>
                                <td class=\"left\">
1174
                                     <a href='$url'>$slice_urn</a></td>
1175 1176 1177 1178 1179
                              </tr>\n";
		    }
		    else {
			echo "<tr>
                                <td>Geni Slice (CM): </td>
1180
                                <td class=\"left\">$slice_urn</td>
1181 1182
                              </tr>\n";
		    }
1183 1184 1185 1186 1187 1188
		}
	    }
	    else {
		$slice = GeniSlice::LookupByExperiment("geni-sa", $this);
		if ($slice) {
		    $slice_hrn = $slice->hrn();
1189
		    $slice_urn = $slice->urn();
1190 1191 1192
		    if (ISADMIN()) {
			$url = CreateURL("showslice", "slice_idx",
					 $slice->idx(), "showtype", "sa");
1193 1194

			echo "<tr>
1195 1196
                                 <td>Geni Slice (SA): </td>
                                 <td class=\"left\">
1197
                                      <a href='$url'>$slice_urn</a></td>
1198 1199
                             </tr>\n";
		    }
1200 1201 1202
		    else {
			echo "<tr>
                                <td>Geni Slice (SA): </td>
1203
                                <td class=\"left\">$slice_urn</td>
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
                              </tr>\n";
		    }
		    $slice = GeniSlice::Lookup("geni-cm", $slice_hrn);
		    if ($slice) {
			if (ISADMIN()) {
			    $url = CreateURL("showslice", "slice_idx",
					     $slice->idx(), "showtype", "cm");

			    echo "<tr>
                                     <td>Geni Slice (CM): </td>
                                     <td class=\"left\">
1215
                                           <a href='$url'>$slice_urn</a></td>
1216 1217 1218 1219 1220
                                  </tr>\n";
			}
			else {
			    echo "<tr>
                                    <td>Geni Slice (SA): </td>
1221
                                    <td class=\"left\">$slice_urn</td>
1222 1223 1224
                                  </tr>\n";
			}
		    }
1225
		}
1226
	    }
1227
	}
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	echo "</table>\n";
    }

    function ShowStats() {
	$idx = $this->idx();

	$query_result =
	    DBQueryFatal("select s.*,r.* from experiments as e ".
			 "left join experiment_stats as s on s.exptidx=e.idx ".
			 "left join experiment_resources as r on ".
			 "     r.idx=s.rsrcidx ".
			 "where e.idx='$idx'");

	if (! mysql_num_rows($query_result)) {
	    return 0;
	}
	$row = mysql_fetch_assoc($query_result);

        #
        # Not pretty printed yet.
        #
	echo "<table align=center border=1>\n";
    
	foreach($row as $key => $value) {
	    if ($key == "thumbnail")
		continue;
	
	    echo "<tr>
                      <td>$key:</td>
                      <td>$value</td>
                  </tr>\n";
	}
	echo "</table>\n";
	return 0;
    }
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281

    #
    # Find the blockstore details from the virt table. 
    #
    function LookupBlockstore($bsname)
    {
        $safe_bsname = addslashes($bsname);
        $pid = $this->pid();
        $eid = $this->eid();
        
        $query_result =
            DBQueryFatal("select * from virt_blockstores ".
                         "where pid='$pid' and eid='$eid' and ".
                         "       vname='$safe_bsname'");
        if (!$query_result || !mysql_num_rows($query_result)) {
            return null;
        }
        return mysql_fetch_array($query_result);
    }
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
}

class ExperimentStats
{
    var	$stats;

    #
    # Constructor by lookup on unique index.
    #
    function ExperimentStats($exptidx) {
	$safe_exptidx = addslashes($exptidx);

	$query_result =
	    DBQueryWarn("select * from experiment_stats ".
			"where exptidx='$safe_exptidx'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->stats = null;
	    return;
	}
	$this->stats = mysql_fetch_array($query_result);
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->stats);
    }

    # Lookup by exptidx, but allow for lookup by pid,eid with variable args.
    function Lookup($exptidx) {
	$foo = new ExperimentStats($exptidx);

	if ($foo->IsValid())
	    return $foo;

	return null;
    }

    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$idx = $this->exptidx();

	$query_result =
	    DBQueryWarn("select * from experiment_stats ".
			"where exptidx='$idx'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->stats = NULL;
	    return -1;
	}
	$this->stats = mysql_fetch_array($query_result);
	return 0;
    }

    # accessors
    function field($name) {
	return (is_null($this->stats) ? -1 : $this->stats[$name]);
    }
    function pid()	    { return $this->field('pid'); }
    function gid()	    { return $this->field('gid'); }
    function eid()	    { return $this->field('eid'); }
    function idx()	    { return $this->field('exptidx'); }
    function exptidx()	    { return $this->field('exptidx'); }
    function rsrcidx()      { return $this->field('rsrcidx'); }
1351 1352 1353
    function pid_idx()      { return $this->field('pid_idx'); }
    function gid_idx()      { return $this->field('gid_idx'); }
    function archive_idx()  { return $this->field('archive_idx'); }
1354
    function slice_uuid()   { return $this->field('slice_uuid'); }
1355
    function swapin_last()  { return $this->field('swapin_last'); }
1356 1357 1358 1359

    function Project() {
	return Project::Lookup($this->pid_idx());
    }
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$idx = $this->idx();
	
	$html = "<font size=+2>Experiment <b>".
	    "<a href='showstats.php3?showby=expt&exptidx=$idx'>$idx</a> ".
	    "</b></font>\n";

	return $html;
    }

    #
    # Project level check.
    #
    function AccessCheck ($user, $access_type) {
	global $TBDB_TRUST_USER;
1379
	$pid_idx = $this->pid_idx();
1380
	
1381
	if (! ($project = Project::Lookup($pid_idx))) {
1382
	    TBERROR("ExperimentStats::AccessCheck: ".
1383
		    "Cannot map project $pid_idx to its object", 1);
1384 1385 1386
	}
	return $project->AccessCheck($user, $TBDB_TRUST_USER);
    }
Leigh Stoller's avatar
Leigh Stoller committed
1387
}
1388

1389 1390 1391 1392 1393 1394 1395
class ExperimentResources
{
    var	$resources;

    #
    # Constructor by lookup on unique index for current resources
    #
1396 1397
    function ExperimentResources($rsrcidx) {
	$safe_rsrcidx = addslashes($rsrcidx);
1398 1399

	$query_result =
1400 1401
	    DBQueryWarn("select r.* from experiment_resources as r ".
			"where r.idx='$safe_rsrcidx'");
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->resources = null;
	    return;
	}
	$this->resources = mysql_fetch_array($query_result);
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->resources);
    }

1415 1416 1417
    # Lookup by resource record number
    function Lookup($rsrcidx) {
	$foo = new ExperimentResources($rsrcidx);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

	if ($foo->IsValid())
	    return $foo;

	return null;
    }

    # accessors
    function field($name) {
	return (is_null($this->resources) ? -1 : $this->resources[$name]);
    }
1429 1430 1431 1432 1433
    function idx()	      { return $this->field('idx'); }
    function exptidx()	      { return $this->field('exptidx'); }
    function lastidx()        { return $this->field('lastidx'); }
    function wirelesslans()   { return $this->field('wirelesslans'); }
    function input_data_idx() { return $this->field('input_data_idx'); }
1434 1435
    function vnodes()         { return $this->field('vnodes'); }
    function swapin_time()    { return $this->field('swapin_time'); }
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

    function GetStats() {
	return ExperimentStats::Lookup($this->exptidx());
    }

    #
    # Project level check via the stats record.
    #
    function AccessCheck ($user, $access_type) {
	$experiment_stats = $this->GetStats();

	return $experiment_stats->AccessCheck($user, $access_type);
    }
    
    # Return the stored NS file this resource record
    function NSFile() {
	$input_data_idx = $this->input_data_idx();

	if (! $input_data_idx) {
	    return null;
	}

	$query_result =
	    DBQueryFatal("select input from experiment_input_data ".
			 "where idx='$input_data_idx'");

	if (! mysql_num_rows($query_result)) {
	    return null;
	}
	$row    = mysql_fetch_array($query_result);
	return $row["input"];
    }
1468 1469
}

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
#
# Class function to show a listing of experiments by user/pid/gid
#
function ShowExperimentList($type, $this_user, $object) {
    global $EXPOSETEMPLATES;
    
    if ($EXPOSETEMPLATES) {
	ShowExperimentList_internal(1, $type, $this_user, $object);
    }
    ShowExperimentList_internal(0, $type, $this_user, $object);
}

function ShowExperimentList_internal($templates_only,
1483 1484
				     $type, $this_user, $object,
				     $tabledefs = null) {
1485
    global $TB_EXPTSTATE_SWAPPED, $TB_EXPTSTATE_SWAPPING;
1486
    
1487 1488 1489
    $from_idx = $this_user->uid_idx();
    $nopid    = 0;
    $parens   = 0;
1490 1491 1492 1493
    $id       = "explist";
    $class    = "explist";
    $wanthtml = 0;
    $notitle  = FALSE;
1494
    $html     = null;
1495 1496 1497 1498
    
    if ($tabledefs) {
	$id    = (isset($tabledefs['#id']) ? $tabledefs['#id'] : $id);
	$class = (isset($tabledefs['#class']) ? $tabledefs['#class'] : $class);
1499
	$wanthtml = (isset($tabledefs['#html']) ? $tabledefs['#html'] : $html);
1500 1501 1502
	$notitle = (isset($tabledefs['#notitle']) ?
		    $tabledefs['#notitle'] : $notitle);
    }
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

    if ($type == "USER") {
	$uid   = $object->uid();
	$where = "expt_head_uid='$uid'";
	$title = "Current";
    } elseif ($type == "PROJ") {
	$pid   = $object->pid();
	$where = "e.pid='$pid'";
	$title = "Project";
	$nopid = 1;
    } elseif ($type == "GROUP") {
	$pid   = $object->pid();
	$gid   = $object->gid();
	$where = "e.pid='$pid' and e.gid='$gid'";
	$title = "Group";
	$nopid = 1;
    } else {
	TBERROR("ShowExperimentList: Invalid arguments", 1);
    }

    $template_clause = "";
    if ($templates_only) {
	$template_clause = " and i.idx is not null ";
    }

    if (ISADMIN()) {
	$query_result =
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
			 "round(e.minimum_nodes+.1,0) as min_nodes ".
			 "from experiments as e ".
			 "left join experiment_templates as t on ".
			 "     t.pid=e.pid and t.eid=e.eid ".
			 "left join experiment_template_instances as i on ".
			 "     i.exptidx=e.idx " .
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
			 "where ($where) and t.guid is null $template_clause ".
			 "group by e.pid,e.eid order by e.state,e.eid");
    }
    else {
	$query_result =
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
	 		 "round(e.minimum_nodes+.1,0) as min_nodes ".
 			 "from experiments as e ".
			 "left join experiment_templates as t on ".
			 "     t.pid=e.pid and t.eid=e.eid ".
			 "left join experiment_template_instances as i on ".
			 "     i.exptidx=e.idx " .
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
 			 "left join group_membership as g on g.pid=e.pid and ".
	 		 "     g.gid=e.gid and g.uid_idx='$from_idx' ".
			 "where g.uid is not null and ($where) ".
			 "      and t.guid is null $template_clause " .
			 "group by e.pid,e.eid order by e.state,e.eid");
    }
    
    if (mysql_num_rows($query_result)) {
1561
	if ($wanthtml)
1562 1563
	    ob_start();

1564
	echo "<center>
1565 1566 1567 1568
               <h3>$title ".
	        ($templates_only ?
		 "Template Instances" : "Experiments") . "</h3>
              </center>
1569
              <table align=center border=1 class=$class id=$id>\n";
1570 1571 1572 1573 1574 1575 1576

	if ($nopid) {
	    $pidrow="";
	} else {
	    $pidrow="\n<th>PID</th>";
	}
	
1577
	echo "<thead class='sort'>";
1578
	echo "<tr>$pidrow
1579
              <th class='sorttable_alpha'>EID</th>
1580 1581 1582
              <th>State</th>
              <th align=center>Nodes [1]</th>
              <th align=center>Hours Idle [2]</th>
1583
              <th class='sorttable_alpha'>Description</th>
1584
          </tr></thead>\n";
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid  = $row["pid"];
	    $eid  = $row["eid"];
	    $state= $row["state"];
	    $nodes= $row["nodes"];
	    $minnodes = $row["min_nodes"];
	    $ignore = $row["idle_ignore"];
	    $name = $row["expt_name"];

	    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		TBERROR("Could not map $pid/$eid to its object", 1);
	    }
1601 1602 1603 1604 1605 1606 1607 1608
            #
            # No long show Geni experiments here, going to be bad news for
            # mere users.
            #
            if ($experiment->geniflags() && !ISADMIN()) {
                continue;
            }
            
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	    $idlehours = $experiment->IdleTime();
	    $stale     = $experiment->IdleStale();
	    
	    if ($nodes==0) {
		$nodes = "<font color=green>$minnodes</font>";
	    } elseif ($row["swap_requests"] > 0) {
		$nodes .= $idlemark;
	    }

	    if ($nopid) {
		$pidrow="";
	    } else {
		$pidrow="\n<td>".
		    "<a href='showproject.php3?pid=$pid'>$pid</a></td>";
	    }

	    $idlestr = $idlehours;
1626
	    $skey = 0;
1627
	    if ($idlehours > 0) {
1628
		$skey = $idlehours;
1629 1630 1631 1632 1633 1634 1635 1636
		if ($stale) { $idlestr .= $stalemark; }
		if ($ignore) { $idlestr = "($idlestr)"; $parens=1; }
	    } elseif ($idlehours == -1) { $idlestr = "&nbsp;"; }
	    
	    echo "<tr>$pidrow
                 <td><a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a></td>
		 <td>$state</td>
                 <td align=center>$nodes</td>
1637
                 <td align=center sorttable_customkey='$skey'>$idlestr</td>
1638 1639 1640 1641 1642 1643 1644 1645
                 <td>$name</td>
             </tr>\n";
	}
	echo "</table>\n";
	echo "<table align=center cellpadding=0 class=stealth><tr>\n".
	    "<td class=stealth align=left><font size=-1><ol>\n".
	    "<li>Node counts in <font color=green><b>green</b></font>\n".
	    "show a rough estimate of the minimum number of \n".
1646
	    "nodes required to swap in.\n".
1647 1648
	    "They account for delay nodes, but not for node types, etc.\n".
	    "<li>A $stalemark indicates that the data is stale, and ".
1649
	    "at least one node in the experiment has not reported ".
1650 1651 1652 1653 1654 1655 1656 1657
	    "on its proper schedule.\n"; 
	if ($parens) {
            # do not show this unless we did it... most users should not ever
	    # need to know that some expts have their idleness ignored
	    echo "Values are in parenthesis for idle-ignore experiments.\n";
	}
	echo "</ol></font></td></tr></table>\n";

1658 1659 1660
	# Sort initialized later when page fully loaded.
	AddSortedTable($id);

1661
	if ($wanthtml) {
1662 1663 1664
	    $html = ob_get_contents();
	    ob_end_clean();
	}
1665
    }
1666
    return $html;
1667
}
1668
?>