template_defs.php 63.8 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
#
8 9
# The template class is really just a wrapper around the DB data, plus
# some access routines and printing functions. 
10
#
11
class Template
12
{
13
    var	$template;
14
    var $experiment;
15
    
16 17 18 19 20 21 22
    function Template($guid, $vers) {
	$guid = addslashes($guid);
	$vers = addslashes($vers);
	
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");
23

24 25 26 27 28
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return;
	}
	$this->template = mysql_fetch_array($query_result);
29 30

	# 
31
	# Underlying experiment for easy access.
32 33 34 35
	#
	$pid = $this->pid();
	$eid = $this->eid();

36
	if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
37 38 39
	    $this->template = NULL;
	    return;
	}
40
	$this->experiment = $experiment;
41 42
    }

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

48 49 50
    # Do class level lookup.
    function Lookup($guid, $vers) {
	$foo = new Template($guid, $vers);
51

52 53 54 55 56 57 58
	if ($foo->IsValid())
	    return $foo;
	return null;
    }
    # Do class level lookup for the root template.
    function LookupRoot($guid) {
	$foo = new Template($guid, 1); 
59

60 61 62
	if ($foo->IsValid())
	    return $foo;
	return null;
63
    }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    # Look up by pid,eid is which also unique across templates.
    function LookupbyEid($pid, $eid) {
	$query_result =
	    DBQueryWarn("select guid,vers from experiment_templates  ".
			"where pid='$pid' and eid='$eid'");

	if (!$query_result || !mysql_num_rows($query_result))
	    return null;
	
	$row = mysql_fetch_array($query_result);
	$guid = $row['guid'];
	$vers = $row['vers'];
	
	$foo = new Template($guid, $vers); 

	if ($foo->IsValid())
	    return $foo;
	return null;
    }
83 84

    #
85 86 87
    # Refresh a template instance by reloading from the DB.
    #
    function Refresh() {
88
	if (! $this->IsValid())
89
	    return -1;
90

91 92
	$guid = $this->guid();
	$vers = $this->vers();
93
    
94 95 96 97 98 99 100 101 102
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return -1;
	}
	$this->template = mysql_fetch_array($query_result);
103 104 105
	return 0;
    }

106 107 108
    # accessors
    function guid() {
	return (is_null($this->template) ? -1 : $this->template['guid']);
109
    }
110 111
    function vers() {
	return (is_null($this->template) ? -1 : $this->template['vers']);
112
    }
113 114
    function pid() {
	return (is_null($this->template) ? -1 : $this->template['pid']);
115
    }
116 117
    function gid() {
	return (is_null($this->template) ? -1 : $this->template['gid']);
118
    }
119 120
    function eid() {
	return (is_null($this->template) ? -1 : $this->template['eid']);
121
    }
122 123
    function tid() {
	return (is_null($this->template) ? -1 : $this->template['tid']);
124
    }
125 126
    function uid() {
	return (is_null($this->template) ? -1 : $this->template['uid']);
127
    }
128
    function path() {
129 130 131 132
	if (!$this->experiment)
	    return -1;
	$experiment = $this->experiment;
	return $experiment->path();
133
    }
134 135
    function IsHidden() {
	return (is_null($this->template) ? -1 : $this->template['hidden']);
136
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137 138 139
    function IsActive() {
	return (is_null($this->template) ? -1 : $this->template['active']);
    }
140 141 142 143 144 145 146 147 148 149 150 151
    function created() {
	return (is_null($this->template) ? -1 : $this->template['created']);
    }
    function description() {
	return (is_null($this->template) ? -1 :
		$this->template['description']);
    }
    function parent_guid() {
	return (is_null($this->template) ? -1 :$this->template['parent_guid']);
    }
    function parent_vers() {
	return (is_null($this->template) ? -1 :$this->template['parent_vers']);
152
    }
153 154 155
    function child_vers() {
	return (is_null($this->template) ? -1 :$this->template['child_vers']);
    }
156

157 158 159 160
    # The root template has no parent guid.
    function IsRoot() {
	if (is_null($this->template))
	    return -1;
161

162
	return is_null($this->template['parent_guid']);
163 164
    }

165 166 167 168 169
    # Return the underlying experiment for this template.
    function GetExperiment() {
	return $this->experiment;
    }

170 171 172 173 174 175 176 177 178
    function GetProject() {
	$pid = $this->pid();

	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $project;
    }

179 180 181 182 183 184 185 186
    # Return the unixgid for operating on this template.
    function UnixGID() {
	$experiment = $this->experiment;

	return $experiment->UnixGID();
    }

    function AccessCheck($user, $access_type) {
187 188 189 190 191 192 193 194 195 196
	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;
197
	$mintrust = $TB_EXPT_READINFO;
198 199 200 201 202

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

204 205 206 207 208 209 210 211
        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}
	$pid = $this->pid();
	$gid = $this->gid();
212
	$uid = $user->uid();
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

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

        #
        # 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);
228
    }
229

230
    #
231
    # Display a template in its own table.
232
    #
233 234 235 236 237 238 239 240 241
    function Show() {
	$guid        = $this->guid();
	$vers        = $this->vers();
	$pid         = $this->pid();
	$gid         = $this->gid();
	$uid         = $this->uid();
	$tid         = $this->tid();
	$created     = $this->created();
	$description = $this->description();
242
	$path        = $this->path();
243

244 245 246 247 248
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
	
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        #
        # We need the metadata guid/version for the TID and description since
        # they are mungable metadata.
        #
	$tid_metadata  = $this->LookupMetadataByName("TID");
	$desc_metadata = $this->LookupMetadataByName("description");
	
	if ($tid_metadata == NULL) {
	    TBERROR("Could not find Metadata 'TID' for $guid/$vers", 1);
	}
	if ($desc_metadata == NULL) {
	    TBERROR("Could not find Metadata 'description' for $guid/$vers",1);
	}
	$tid_guid  = $tid_metadata->guid();
	$tid_vers  = $tid_metadata->vers();
	$desc_guid = $desc_metadata->guid();
	$desc_vers = $desc_metadata->vers();
266
    
267 268 269 270 271 272 273 274
        #
        # Generate the table.
        #
	echo "<center>
               <h3>Details</h3>
              </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
275
    
276
	ShowItem("GUID",
277
		 MakeLink("template",
278 279 280
			  "guid=$guid&version=$vers", "$guid/$vers"));
	
	ShowItem("ID",
281 282 283
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${tid_guid}/${tid_vers}", $tid));
284 285 286
	
	ShowItem("Project", MakeLink("project", "pid=$pid", $pid));
	ShowItem("Group",   $gid);
287
	ShowItem("Creator", MakeAnchor($showuser_url, $uid));
288
	ShowItem("Created", $created);
289

290 291 292 293
	$onmouseover = MakeMouseOver($description);
	if (strlen($description) > 40) {
	    $description = substr($description, 0, 40) . " <b>... </b>";
	}
294 295 296 297 298
	ShowItem("Description",
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${desc_guid}/${desc_vers}",
			    $description, $onmouseover));
299
    
300 301 302 303 304 305 306 307 308 309 310 311
	if (! $this->IsRoot()) {
	    $parent_guid = $this->parent_guid();
	    $parent_vers = $this->parent_vers();
	    
	    ShowItem("Parent Template",
		     MakeLink("template",
			      "guid=$parent_guid&version=$parent_vers",
			      "$parent_guid/$parent_vers"));
	}
	echo "</table>\n";
    }

312 313 314 315 316 317 318 319 320 321 322 323 324 325
    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$guid = $this->guid();
	$vers = $this->vers();
	$html = "<font size=+2>Template <b>" .
	    MakeLink("template",
		     "guid=$guid&version=$vers", "$guid/$vers") . 
	    "</b></font>";

	return $html;
    }

326 327 328 329 330 331 332 333
    #
    # Display template parameters and default values in a table
    #
    function ShowParameters() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
334 335 336 337 338 339 340 341 342 343
	    DBQueryFatal("select p.name,p.value, ".
			 "       p.metadata_guid,p.metadata_vers, ".
			 "       m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
344 345 346
	if (!$query_result ||
	    !mysql_num_rows($query_result))
	    return 0;
347 348

	echo "<center>
349
               <h3>Parameters</h3>
350 351 352 353 354 355
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Default Value</th>
356
                <th>Description</th>
357 358 359
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
360 361 362 363 364 365
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['metadata_guid'];
	    $metadata_vers = $row['metadata_vers'];
	    $description   = $row['description'];
	    
366 367 368 369 370 371
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
372 373 374
                   <td>$value</td>";

	    if (is_null($description)) {
375 376
		$url = CreateURL("template_metadata", $this);
		
377
		echo "<td>".
378 379 380
		     "<a href='${url}&action=add&".
		     "metadata_type=parameter_description&".
		     "formfields[name]=${name}'>Click to add</a></td>\n";
381 382 383 384 385 386 387
	    }
	    else {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description =
			substr($description, 0, 30) . " <b>... </b>";
		}
388 389 390 391 392
		$url = CreateURL("template_metadata", $this);
		
		echo "<td><a href='${url}&action=modify".
		     "&metadata=${metadata_guid}/${metadata_vers}' " .
		     "$onmouseover>$description</a></td>\n";
393 394 395
	    }
	    echo "</tr>\n";
	    }
396 397 398
	echo "</table>\n";
	return 1;
    }
399
	
400 401 402 403 404 405 406 407 408
    #
    # Display template metadata and values in a table
    #
    function ShowMetadata() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select i.* from experiment_template_metadata as m ".
409 410 411
		     "left join experiment_template_metadata_items as i on ".
		     "     m.metadata_guid=i.guid and m.metadata_vers=i.vers ".
		     "where m.parent_guid='$guid' and ".
412 413
		     "      m.parent_vers='$vers' and ".
		     "      m.internal=0 and m.hidden=0");
414 415 416

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

	echo "<center>
419
               <h3>Metadata</h3>
420 421 422 423 424
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Edit</th>
425
                <th>Delete</th>
426 427 428 429 430 431 432 433 434
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['guid'];
	    $metadata_vers = $row['vers'];
435 436 437
	    $onmouseover   = "";
	    
	    if (!isset($value) || $value == "") {
438 439
		$value = "&nbsp";
	    }
440 441 442 443 444 445
	    else {
		$onmouseover = MakeMouseOver($value);
		
		if (strlen($value) > 40) {
		    $value = substr($value, 0, 40) . "<b> ... </b></a>";
		}
446
	    }
447

448 449 450
	    $meta_url = CreateURL("template_metadata", $this) .
		"&metadata=${metadata_guid}/${metadata_vers}";
		
451 452
	    echo "<tr>
   	           <td align=center>
453 454
                     <a href='${meta_url}&action=modify'>
                       <img border=0 alt='modify' src='greenball.gif'></A></td>
455
   	           <td align=center>
456 457
                     <a href='${meta_url}&action=delete'>
                       <img border=0 alt='delete' src='redball.gif'></A></td>
458
                   <td>$name</td>
459
                   <td $onmouseover>$value</td>
460 461 462 463 464 465
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

466 467 468 469 470 471
    #
    # Show the instance list for a template.
    #
    function ShowInstances() {
	$guid = $this->guid();
	$vers = $this->vers();
472 473

	$query_result =
474 475 476 477 478 479 480 481 482 483 484 485 486 487
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
			 "    round(minimum_nodes+.1,0) as min_nodes ".
			 "from experiment_template_instances as i ".
			 "left join experiments as e on e.idx=i.exptidx ".
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
			 "where e.pid is not null and ".
			 "      (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "group by e.pid,e.eid order by e.state,e.eid");

	if (! mysql_num_rows($query_result))
	    return;
	
488
	echo "<center>
489
               <h3>Template Instances</h3>
490 491 492 493 494 495
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
               <th>EID</th>
               <th>State</th>
496
               <th align=center>Nodes</th>
497 498 499 500 501 502 503 504 505 506 507 508 509
              </tr>\n";

	$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'];
510 511 512 513 514 515

	    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		TBERROR("Could not map $pid/$eid to its object", 1);
	    }
	    $idlehours = $experiment->IdleTime();
	    $stale     = $experiment->IdleStale();
516 517 518 519
	    
	    if ($nodes == 0) {
		$nodes = "<font color=green>$minnodes</font>";
	    }
520
	    elseif ($row["swap_requests"] > 0) {
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
		$nodes .= $idlemark;
	    }

	    $idlestr = $idlehours;
	    
	    if ($idlehours > 0) {
		if ($stale) {
		    $idlestr .= $stalemark;
		}
		if ($ignore) {
		    $idlestr = "($idlestr)";
		}
	    }
	    elseif ($idlehours == -1) {
		$idlestr = "&nbsp;";
	    }
	    
	    echo "<tr>
                   <td><A href='showexp.php3?pid=$pid&eid=$eid'>$eid</A></td>
  		   <td>$state</td>
                   <td align=center>$nodes</td>
                 </tr>\n";
	}
	echo "</table>\n";
    }

547 548 549 550 551 552
    #
    # Show the historical instance list for a template.
    #
    function ShowHistory($expand) {
	$guid = $this->guid();
	$vers = $this->vers();
553

554
	$query_result =
555 556 557 558 559 560
	    DBQueryFatal("select i.*,r.archive_tag ".
			 "  from experiment_template_instances as i ".
			 "left join experiment_stats as s on ".
			 "     s.exptidx=i.exptidx ".
			 "left join experiment_resources as r on ".
			 "     r.idx=s.rsrcidx ".
561 562 563 564 565 566 567
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "order by i.start_time");

	if (! mysql_num_rows($query_result))
	    return 0;
	
568
	echo "<center>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
569
               <h3>Template History (Swapins)</h3>
570 571 572 573
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
574 575
               <th align=center>&nbsp</th>
               <th>ID</th>
576 577
               <th>Start Time</th>
               <th>Stop Time</th>
578
	       <th>Description</th>
579 580 581 582 583 584
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
585 586 587 588 589 590 591 592 593
	    $pid        = $row['pid'];
	    $eid        = $row['eid'];
	    $start      = $row['start_time'];
	    $stop       = $row['stop_time'];
	    $exptidx    = $row['exptidx'];
	    $idx        = $row['idx'];
	    $tag        = $row['archive_tag'];
	    $description= $row['description'];
	    $onmouseover= "";
594 595 596 597

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
598 599

	    $expandit = ((isset($expand) && $expand == $idx) ? 1 : 0);
600 601 602 603 604 605 606 607 608 609 610

	    if (isset($description) && $description != "") {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description = substr($description, 0, 30) . " <b>...</b>";
		}
	    }
	    else {
		$description = "&nbsp ";
	    }

611
	    echo "<tr>\n";
612 613 614
	    echo " <td align=center>";
	    if ($expandit) {
		echo "<a href=template_history.php".
615
                         "?guid=$guid&version=$vers>".
616 617 618 619
		         "<img border=0 alt='c' src='/icons/down.png'></a>\n";
	    }
	    else {
 		echo "<a href=template_history.php".
620
                         "?guid=$guid&version=$vers&expand=$idx#$idx>".
621 622
		         "<img border=0 alt='e' src='/icons/right.png'></a>\n";
	    }
623
	    echo " </td>";
624

625
	    echo " <td align=center>".
626
		      MakeLink("instance", "instance=$exptidx", $eid);
627
	    echo " </td>\n";
628

629 630 631
	    echo " <td>$start</td>
                   <td>$stop</td>
                   <td $onmouseover>$description</td>\n";
632

633
	    echo "</tr>\n";
634 635

	    if ($expandit) {
636 637
		$instance = new TemplateInstance($exptidx);
		
638 639 640
		echo "<tr>\n";
		echo " <a NAME=$idx></a>\n";
		echo "<td>&nbsp</td>\n";
641
		echo " <td colspan=4>\n";
642
		$instance->ShowRunList(0, 0);
643 644 645
		echo " </td>\n";
		echo "</tr>\n";
	    }
646 647 648
	}
	echo "</table>\n";
    }
649 650 651 652 653 654

    #
    # Dump the image map for a template to the output.
    #
    function ShowGraph() {
	$guid = $this->guid();
655
	$vers = $this->vers();
656
	# Make the link unique to force reload on the client side.
657
	$now  = time();
658 659 660 661 662 663 664 665 666 667 668 669 670

	$query_result =
	    DBQueryFatal("select imap from experiment_template_graphs ".
			 "where parent_guid='$guid'");

	if (!mysql_num_rows($query_result)) {
	    USERERROR("Experiment Template $guid is no longer in the DB!", 1);
	}
	$row  = mysql_fetch_array($query_result);
	$imap = $row['imap'];

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
671 672
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
673
	echo "<div id=\"mygraphdiv\" style='position:relative;'>\n";
674

675 676 677 678 679
	echo "<div id='CurrentTemplate' style='display: block; opacity: 1; ".
	    "visibility: hidden; ".
	    "position: absolute; z-index:1020; left: 0px; top: 0px;".
	    "height: 0px; width: 0px; border: 2px solid red;'></div>\n";

680
	echo $imap;
681 682
	echo "<img id=\"mygraphimg\" border=0 usemap=\"#TemplateGraph\" ";
	echo "      onLoad=\"setTimeout('ShowGraphInit();', 10);\" ";
683
	echo "      style='cursor: move;' ";
684
	echo "      src='template_graph.php?guid=$guid&version=1&now=$now'>\n";
685 686 687 688
	echo "</div>\n";
	echo "</div>\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702
    #
    # Dump the NS file into its own iframe.
    #
    function ShowNS() {
	$guid = $this->guid();
	$vers = $this->vers();

	echo "<center>";
	echo "<iframe width=700 height=400 scrolling=auto
                      src='spitnsdata.php3?guid=$guid&version=$vers'
                      border=2></iframe>\n";
	echo "</center>";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
703 704 705 706 707 708 709
    #
    # Dump the visualization into its own iframe.
    #
    function ShowVis($zoom = 1.25, $detail = 1) {
	$guid = $this->guid();
	$pid  = $this->pid();
	$eid  = $this->eid();
710 711
	# Make the link unique to force reload on the client side.
	$now  = time();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
712 713 714 715 716

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
717
	echo "<div id=\"myvisdiv\" style='position:relative;'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
718

719
	echo "<img id=\"myvizimg\" border=0 style='cursor: move;' ";
720
	echo "      onLoad=\"setTimeout('ShowVisInit();', 100);\" ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
721
	echo "      src='top2image.php3?pid=$pid&eid=$eid".
722
	    "&zoom=$zoom&detail=$detail&now=$now'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	echo "</div>\n";
	echo "</div>\n";
    }

    #
    # Grab the zoom and detail for the current viz picture, as a default.
    #
    function CurrentVisDetails() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select zoom,detail from vis_graphs ".
			 "where pid='$pid' and eid='$eid'");

	if (!mysql_num_rows($query_result)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
739
	    return array(1.25, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
740 741 742 743 744 745 746
	}
	$row    = mysql_fetch_array($query_result);
	$zoom   = $row['zoom'];
	$detail = $row['detail'];
	return array($zoom, $detail);
    }

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    #
    # Grab array of input files for a template, indexed by input_idx.
    #
    function InputFiles() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$input_list = array();

	$query_result =
	    DBQueryFatal("select * from experiment_template_inputs ".
			 "where parent_guid='$guid' and parent_vers='$vers'");

	while ($row = mysql_fetch_array($query_result)) {
	    $input_idx = $row['input_idx'];

	    $input_query =
		DBQueryFatal("select input ".
			     "  from experiment_template_input_data ".
			     "where idx='$input_idx'");

	    $input_row = mysql_fetch_array($input_query);
	    $input_list[] = $input_row['input'];
	}
	return $input_list;
    }

    #
    # Return number of parameters.
    #
    function ParameterCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers'");

	return mysql_num_rows($query_result);
    }

    #
    # Return number of metadata items
    #
    function MetadataCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select internal ".
			 "  from experiment_template_metadata as m ".
			 "where m.parent_guid='$guid' and ".
801 802
			 "      m.parent_vers='$vers' and ".
			 "      m.internal=0 and m.hidden=0");
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 829 830 831 832 833 834 835 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

	return mysql_num_rows($query_result);
    }

    #
    # Return number of instances
    #
    function InstanceCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select * from experiment_template_instances as i ".
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers')");

	return mysql_num_rows($query_result);
    }

    #
    # Look for a metadata item in a template, by the guid/vers of the
    # metadata. Returns a class instance (see below).
    #
    function LookupMetadataByGUID($metadata_guid, $metadata_vers) {
	return TemplateMetadata::TemplateLookupByGUID($this,
						      $metadata_guid,
						      $metadata_vers);
    }
    # Ditto by name,
    function LookupMetadataByName($metadata_name) {
	return TemplateMetadata::TemplateLookupByName($this, $metadata_name);
    }

    # Grab the graph data.
    function GraphImage(&$image) {
	$guid = $this->guid();

	$query_result =
	    DBQueryWarn("select image from experiment_template_graphs ".
			"where parent_guid='$guid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$image = $row['image'];
	return 0;
    }

    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameters(&$parameters) {
	$parameters = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
864 865
			 "      parent_vers='$vers' ".
			 "order by name");
866 867 868 869 870 871 872 873 874

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];

	    $parameters[$name] = $value;
	}
	return 0;
    }
875

876 877 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
    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameterMouseOvers(&$mouseovers) {
	$mouseovers = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select p.name,m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
	while ($row = mysql_fetch_array($query_result)) {
	    $name	 = $row['name'];
	    $description = $row['description'];

	    if (isset($description) && $description != "") {
		$mouseovers[$name] = MakeMouseOver($description);
	    }
	}
	return 0;
    }

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    #
    # Find next candidate for a template (modify) TID
    #
    function NextTID() {
	$tid  = $this->tid();
	$guid = $this->guid();

	$query_result =
	    DBQueryFatal("select MAX(vers) from experiment_templates ".
			 "where guid='$guid'");

	if (mysql_num_rows($query_result) == 0) {
	    return "T" . substr(md5(uniqid($foo, true)), 0, 10);
	}
	$row = mysql_fetch_array($query_result);
	$foo = $row[0] + 1;
	return "${tid}-V${foo}"; 
    }
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

    #
    # Return array of template events, ordered by time.
    #
    function EventList(&$eventlist) {
	$eventlist = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select * from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ".
			 "order by time");

	$i = 0;
	while ($row = mysql_fetch_array($query_result)) {
	    $eventlist[$i++] = $row;
	}
	return 0;
    }
    function EventCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select count(*) from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ");

	$row   = mysql_fetch_array($query_result);
	$count = $row[0];
	return $count;
    }
    function DeleteEvent($vname) {
	$guid = $this->guid();
	$vers = $this->vers();
	
	DBQueryFatal("delete from experiment_template_events ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
    function ModifyEvent($vname, $changes) {
	$guid = $this->guid();
	$vers = $this->vers();
	$sets = array();
	
	while (list ($key, $value) = each ($changes)) {
	    $value  = addslashes($value);
	    $sets[] = "$key='$value'";
	}
	
	DBQueryFatal("update experiment_template_events set ".
		     implode(",", $sets) . " ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
985 986
}

987
#
988
# This is the class for a template instance (swapin).
989
#
990
class TemplateInstance
991
{
992 993
    var	$template;
    var $instance;
994
    var $experiment;
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    #
    # Instances are found by their experiment index. 
    #
    function TemplateInstance($exptidx) {
	$exptidx = addslashes($exptidx);

	$query_result =
	    DBQueryFatal("select * ".
			 "  from experiment_template_instances ".
			 "where exptidx='$exptidx'");
	
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    $this->instance = NULL;
	    return;
	}
	$this->instance = mysql_fetch_array($query_result);
	$this->template = new Template($this->instance['parent_guid'],
				       $this->instance['parent_vers']);
1015
	$this->experiment = Experiment::Lookup($exptidx);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    }
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->instance);
    }

    # Do class level lookup.
    function LookupByExptidx($exptidx) {
	$foo = new TemplateInstance($exptidx);

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

    # accessors
    function idx() {
	return (is_null($this->instance) ? -1 : $this->instance['idx']);
    }
    function exptidx() {
	return (is_null($this->instance) ? -1 : $this->instance['exptidx']);
    }
    function runidx() {
	return (is_null($this->instance) ? -1 : $this->instance['runidx']);
    }
    function pid() {
	return (is_null($this->instance) ? -1 : $this->instance['pid']);
    }
1045 1046 1047 1048 1049 1050
    function gid() {
	if (is_null($this->instance))
	    return -1;
	$template = $this->GetTemplate();
	return $template->gid();
    }
1051 1052 1053
    function eid() {
	return (is_null($this->instance) ? -1 : $this->instance['eid']);
    }
1054 1055 1056 1057
    # This is what I should have called this:
    function id() {
	return $this->eid();
    }
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    function uid() {
	return (is_null($this->instance) ? -1 : $this->instance['uid']);
    }
    function guid() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_guid']);
    }
    function vers() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_vers']);
    }
    function start_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['start_time']);
    }
    function stop_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['stop_time']);
    }
1077 1078 1079 1080
    function pause_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['pause_time']);
    }
1081 1082 1083 1084
    function description() {
	return (is_null($this->instance) ? -1 :
		$this->instance['description']);
    }
1085 1086 1087
    function template() {
	return (is_null($this->instance) ? -1 : $this->template);
    }
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    function GetTemplate() {
	return $this->template;
    }
    function GetExperiment() {
	return $this->experiment;
    }

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

	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $project;
    }
1106

1107 1108
    # Is instance actually running (current experiment).
    function Instantiated() {
1109
	return ($this->experiment ? 1 : 0);
1110 1111
    }

1112 1113 1114 1115 1116 1117
    function AccessCheck($user, $access_type) {
	$template = $this->template;

	return $template->AccessCheck($user, $access_type);
    }

1118 1119 1120
    #
    # Show an instance.
    #
1121
    function Show($detailed, $withanno = 0, $showhidden = 0) {
1122
	$exptidx = $this->exptidx();
1123
	$runidx  = $this->runidx();
1124 1125 1126 1127 1128 1129
	$guid    = $this->guid();
	$vers    = $this->vers();
	$pid     = $this->pid();
	$uid     = $this->uid();
	$start   = $this->start_time();
	$stop    = $this->stop_time();
1130 1131 1132 1133 1134 1135 1136 1137
	$description = $this->description();
	$template    = $this->template();
	$pcount      = $template->ParameterCount();

	# Instance descriptions are metatdata that can be changed.
	# But use a naming convention. 
	$desc_metaname = "__instance_description_$exptidx";
	$desc_metadata = $template->LookupMetadataByName($desc_metaname);
1138

1139 1140 1141 1142
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
1143 1144 1145 1146 1147 1148

	# If the instance is swapped in, we can provide a link to
	# experiment.
	if ($this->experiment) {
	    $showexp_url  = CreateURL("showexp", $this->experiment);
	}
1149

1150 1151 1152 1153 1154 1155 1156
	# If there are hidden runs, then add an option to show them
	$query_result =
	    DBQueryFatal("select count(*) from experiment_runs ".
			 "where exptidx='$exptidx' and hidden!=0");
	$row = mysql_fetch_array($query_result);
	$numhidden = $row[0];
	
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	if ($detailed) {
	    SUBPAGESTART();
	    SUBMENUSTART("Options");
	    WRITESUBMENUBUTTON("Export Instance",
			       CreateURL("template_export", $this));

	    WRITESUBMENUBUTTON("Load Database",
			       CreateURL("template_analyze", $this));
	
	    WRITESUBMENUBUTTON("View Archive",
			       CreateURL("archive_view", $this));
	    
	    WRITESUBMENUBUTTON("Replay",
			       CreateURL("swapin", $template, 
					 "replay_instance_idx", $exptidx));
1172 1173 1174 1175 1176 1177 1178 1179 1180

	    if ($numhidden) {
		$tag = ($showhidden ? "Hide" : "Show");
		$val = ($showhidden ? 0 : 1);
		
		WRITESUBMENUBUTTON("$tag Hidden Runs",
				   CreateURL("instance_show", $this,
					     "showhidden", $val));
	    }
1181 1182
	    SUBMENUEND();
	}
1183
	
1184
	echo "<center>\n";
1185 1186 1187 1188 1189 1190
	if ($detailed && $pcount) {
	    echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
		 " cellpadding=0 cellspacing=0 align=center>\n";
            echo "<tr valign=top>";
	    echo "<td class=stealth align=center>\n";
	}
1191
	echo "<center>
1192
               <h3>Instance Details</h3>
1193 1194 1195 1196 1197 1198
             </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
    
	ShowItem("Template",
		 MakeLink("template",
1199 1200
			  "guid=$guid&version=$vers", "$guid/$vers"));
	ShowItem("ID",          $exptidx);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231

	if ($description && $description != "") {
	    $onmouseover = MakeMouseOver($description);
	    if (strlen($description) > 40) {
		$description = substr($description, 0, 40) . " <b>... </b>";
	    }
	}
	else {
	    $onmouseover = "";
	    $description = "Click to Add";
	}

	# If no metadata, assume its for an instance prior to modifiable
	# instance descriptions.
	if ($desc_metadata) {
	    $desc_anchor = 
		MakeAnchor(CreateURL("template_metadata",
				     $template, $desc_metadata) .
			   "&action=modify",
			   $description, $onmouseover);
	}
	else {
	    $desc_anchor = 
		MakeAnchor(CreateURL("template_metadata", $template) .
			   "&action=add".
			   "&metadata_type=instance_description".
			   "&formfields[name]=${desc_metaname}",
			   $description, $onmouseover);
	}
        ShowItem("Description", $desc_anchor);
	
1232 1233 1234
	if ($this->experiment) {
	    ShowItem("Experiment",  MakeAnchor($showexp_url, $this->eid()));
	}
1235
	ShowItem("Project",     MakeLink("project", "pid=$pid", $pid));
1236
	ShowItem("Creator",     MakeAnchor($showuser_url, $uid));
1237 1238
	ShowItem("Started",     $start);
	ShowItem("Stopped",     (isset($stop) ? $stop : "&nbsp"));
1239
	ShowItem("Current Run", (isset($runidx) ? $runidx : "&nbsp"));
1240 1241
	echo "</table>\n";

1242 1243 1244 1245 1246 1247 1248 1249
	if ($detailed && $pcount) {
	    echo "</td>";
	    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	    echo "<td class=stealth align=center>\n";
	    $this->ShowBindings();
	    echo "</tr>";
	    echo "</table>\n";
	}
1250 1251 1252
	if ($detailed) {
	    SUBPAGEEND();
	}
1253

1254 1255 1256 1257
	if ($withanno) {
	    $this->ShowAnnotation();
	}

1258
	if ($detailed) {
1259
	    $this->ShowRunList(1, $showhidden);
1260
	}
1261

1262 1263
	echo "</center>\n";
	
1264
    }
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
1278
	    MakeLink("instance", "instance=$exptidx", $eid) .
1279 1280 1281 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
	    "</b></font>";
	return $html;
    }

    #
    # A variant that points to the active experiment.
    #
    function ExpPageHeader() {
	$template = $this->template();	
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$pid      = $this->pid();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
	    MakeLink("project", "pid=$pid", $pid) . "/" .
	    MakeLink("experiment", "pid=$pid&eid=$eid", $eid) . 
	    "</b></font>";

	return $html;
    }
    
    #
    # Ditto for a run, although run should its own class.
    #
    function RunPageHeader($runidx) {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $this->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();
	$runid    = $this->GetRunID($runidx);

	$html .= "<font size=+2>, Run <b>" .
1315 1316
	    MakeAnchor(CreateURL("experimentrun_show", $this,
				 "runidx", $runidx), $runid) .
1317 1318 1319
	    "</b></font>";
	return $html;
    }
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

    #
    # Show the current bindings. This depends on the state of the instance.
    # If not swapped in, the instance bindings are current. If swapped in,
    # the most recent run bindings are current (current run, or last run).
    #
    function ShowCurrentBindings() {
	global $TB_EXPTSTATE_SWAPPED;
	
	if ($this->experiment) {
	    $experiment = $this->experiment;

	    if ($experiment->state() == $TB_EXPTSTATE_SWAPPED) {
		# Show the instance bindings is swapped.
		return $this->ShowBindings();
	    }
	    elseif ($this->runidx()) {
		return $this->ShowRunBindings();
	    }
	    return $this->ShowLastRunBindings();
	}
	return $this->ShowBindings();
    }
1343 1344 1345 1346 1347 1348
    
    #
    # Display instance bindings in a table
    #
    function ShowBindings() {
	$instance_idx = $this->