showstats.php3 9.15 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2013 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/>.
# 
# }}}
23 24 25 26 27 28
#
include("defs.php3");

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

33 34 35 36 37 38 39
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("showby",	      PAGEARG_STRING,
				 "target_user",	      PAGEARG_USER,
				 "target_project",    PAGEARG_PROJECT,
				 "exptidx",           PAGEARG_INTEGER,
40 41 42
				 "records",           PAGEARG_INTEGER,
				 "verbose",           PAGEARG_BOOLEAN,
				 "startwith",         PAGEARG_INTEGER);
43 44 45 46 47 48

#
# Standard Testbed Header
#
PAGEHEADER("Testbed Wide Stats");

49
if (! isset($verbose)) {
50 51
     $verbose = 0;
}
52 53 54 55 56 57 58
if (! isset($showby)) {
    if ($isadmin) 
	$showby = "all";
    else
	$showby = "user";
}
# Show just the last N records unless request is different.
59 60 61 62 63
if (!isset($records) || $records == 0) {
    $records = 20;
}
if (!isset($startwith)) {
    $startwith    = 0;
64 65
}

66
echo "<b>Show: <a class='static' href='showexpstats.php3'>
Leigh Stoller's avatar
Leigh Stoller committed
67 68
                  Experiment Stats</a>";
if ($isadmin) {
69
    echo "<a class='static' href='showsumstats.php3'>, Summary Stats</a>";
70 71
    echo ", <a class='static' href='shownodehistory.php3'>Node History</a>";
    echo ", <a class='static' href='showvlantaghistory.php'>VLan History</a>\n";
Leigh Stoller's avatar
Leigh Stoller committed
72 73
}
echo "</b><br>\n";
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

# Set up where clause and menu.
$wclause = "";

echo "<b>Show by: ";

# By User. A specific user, or the current uid.
if ($showby != "user") {
    echo "<a class='static' href='showstats.php3?showby=user'>User</a>, ";
}
else {
    echo "User, ";
}
# Or All. For mere users, "all" would be confusing, so change the tag
# to "projects" to indicate its really just projects they are members of.
if ($showby != "all") {
    echo "<a class='static' href='showstats.php3?showby=all'>";
    if ($isadmin)
	echo "All</a>.";
    else
	echo "Project</a>.";
}
else {
    if ($isadmin)
	echo "All</a>.";
    else
	echo "Project</a>.";
}
102 103
echo "</b><br>\n";
echo "<br>\n";
104 105 106

# Determine what to do.
if ($showby == "user") {
107
    if (!isset($target_user)) {
108
	$target_user = $this_user;
109
    }
110 111 112
    elseif (! $target_user->AccessCheck($this_user, $TB_USERINFO_READINFO)) {
	USERERROR("You do not have permission to view ${user}'s stats!", 1);
    }
113
    $wclause = "where (t.uid_idx='" . $target_user->uid_idx() . "')";
114 115
}
elseif ($showby == "project") {
116
    if (!isset($target_project)) {
117 118
	USERERROR("Must supply a project to view!", 1);
    }
119
    $pid = $target_project->pid();
120
    $pid_idx = $target_project->pid_idx();
121 122 123 124
    
    if (!$target_project->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
	USERERROR("You do not have permission to view stats for ".
		  "project $pid!", 1);
125
    }
126
    $wclause = "where (s.pid_idx='$pid_idx')";
127
}
128 129
elseif ($showby == "expt") {
    #
130 131 132
    # We get an index, which may or may not map to a current experiment.
    # If not a current experiment, then need to check in the stats table
    # so we can map it to a pid/eid to do the permission check. Not
133 134 135
    # sure I like this so I am not going to permit it for mere users
    # just yet.
    #
136 137
    if (!isset($exptidx)) {
	USERERROR("Must supply an experiment to view!", 1);
138
    }
139 140 141 142 143 144 145 146 147
    if (!TBvalid_integer($exptidx)) {
	USERERROR("Invalid characters in $exptidx!", 1);
    }
    if (! ($experiment = Experiment::Lookup($exptidx))) {
	if (! ($stats = ExperimentStats::Lookup($exptidx))) {
	    USERERROR("No such experiment index $exptidx!", 1);
	}
	if (!$isadmin) {
	    # XXX
148
	    USERERROR("You do not have permission to view stats for ".
149
		      "historical experiment $exptidx!", 1);
150 151
	}
    }
152 153 154 155
    elseif (! $experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view stats for ".
		  "experiment $exptidx!", 1);
    }
156
    $wclause = "where (t.exptidx='$exptidx')";
157

158 159 160 161 162 163 164 165
    echo "<center>";
    if ($experiment) {
	echo $experiment->PageHeader();
    }
    echo "<font size=+2>";
    echo "(<a href='showexpstats.php3?record=$exptidx'>$exptidx</a>) ";
    echo "</font>";
    echo "</center>\n";
166
    echo "<br>\n";
167
}
168
elseif ($showby == "all") {
169 170
    if (isset($target_project)) {
	$pid = $target_project->pid();
171
	$pid_idx = $target_project->pid_idx();
172 173
    
	if (!$target_project->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
174
	    USERERROR("You do not have permission to view stats for ".
175
		      "project $pid!", 1);
176
	}
177
	$wclause = "where (s.pid_idx='$pid_idx')";
178 179 180 181 182
    }
    elseif (!$isadmin) {
        #
        # Get a project list for which the user has read permission.
        #
183
	$orclause = "";
184
        $projlist = $this_user->ProjectAccessList($TB_PROJECT_READINFO);
185 186 187 188 189
	if (! count($projlist)) {
	    USERERROR("You do not have permission to view stats for any ".
		      "project!", 1);
	}
	while (list($project, $grouplist) = each($projlist)) {
190
	    $orclause .= "s.pid='$project' or ";
191
	}
192
	$wclause = "where ($wclause 0)";
193 194 195 196 197
    }
}
else {
    USERERROR("Bad page arguments!", 1);
}
198

Leigh Stoller's avatar
Leigh Stoller committed
199
# Do the endwith to ensure the query does not take too long
200
$startclause = ($startwith ? "(t.idx<$startwith)" : "");
201 202 203 204 205 206

if (strlen($wclause)) {
    if (strlen($startclause)) {
	$wclause = "$wclause and $startclause";
    }
}
Leigh Stoller's avatar
Leigh Stoller committed
207 208 209
else if (strlen($startclause)) {
    $wclause = "where ($startclause)";
}
210
else {
Leigh Stoller's avatar
Leigh Stoller committed
211
    $wclause = "";
212 213
}

214
$query_result =
215
    DBQueryFatal("select t.exptidx,s.pid,s.eid,t.action,t.exitcode,t.uid, ".
216
                 "       r.pnodes,t.idx as statno,t.start_time,t.end_time, ".
217
		 "       s.archive_idx,r.archive_tag,t.uid_idx ".
218
		 "  from testbed_stats as t ".
219
		 "left join experiment_stats as s on s.exptidx=t.exptidx ".
220
		 "left join experiment_resources as r on r.idx=t.rsrcidx ".
221
		 "$wclause ".
222
		 "order by t.end_time desc,t.idx desc limit $records");
223 224 225 226

if (mysql_num_rows($query_result) == 0) {
    USERERROR("No testbed stats records in the system!", 1);
}
227

228
echo "<table align=center border=1>
229 230 231 232 233 234 235 236 237 238 239 240 241 242
      <tr>";
if ($verbose) {
    echo "<th>#</th>";
}
if ($EXPOSEARCHIVE) {
    echo "  <th>Run</th>";
}
if ($verbose || $showby != "expt") {
    echo "  <th>Uid</th>
            <th>Pid</th>
            <th>Eid</th>
            <th>ExptIdx</th>";
}
echo "  <th>Start</th>
243
        <th>End</th>
244
        <th>Action (Nodes)</th>
245 246 247 248 249
        <th>ECode</th>";
if ($EXPOSEARCHIVE) {
        echo "<th>Archive</th>";
}
echo " </tr>\n";
250 251

while ($row = mysql_fetch_assoc($query_result)) {
252
    $idx     = $row["statno"];
253
    $eidx    = $row["exptidx"];
254 255 256
    $pid     = $row["pid"];
    $eid     = $row["eid"];
    $uid     = $row["uid"];
257
    $uid_idx = $row["uid_idx"];
258 259 260 261 262 263 264
    $start   = $row["start_time"];
    $end     = $row["end_time"];
    $action  = $row["action"];
    $ecode   = $row["exitcode"];
    $pnodes  = $row["pnodes"];
    $archive_idx = $row["archive_idx"];
    $archive_tag = $row["archive_tag"];
265
    $startwith   = $idx;
266 267 268

    if (!isset($end))
	$end = "&nbsp";
269
	
270 271 272 273 274
    echo "<tr>";
    if ($verbose)
	echo "<td>$idx</td>";
    if ($EXPOSEARCHIVE) {
	if ($archive_idx && $archive_tag &&
275
	    ($action == "swapout" || $action == "swapmod")) {
276
	    echo "  <td align=center>
277
                       <a href=beginexp.php?copyid=$eidx:$archive_tag>
278 279 280 281 282 283 284
                       <img border=0 alt=Run src=greenball.gif></a></td>";
	}
	else {
	    echo "<td>&nbsp</td>\n";
	}
    }
    if ($verbose || $showby != "expt") {
285
	echo "<td>$uid ($uid_idx)</td>
286 287
              <td>$pid</td>
              <td>$eid</td>
288
              <td><a href=showexpstats.php3?record=$eidx>$eidx</a></td>";
289 290
    }
    echo "  <td>$start</td>
291
            <td>$end</td>\n";
292 293 294 295 296 297 298
    if (!$ecode && (strcmp($action, "preload") &&
		    strcmp($action, "destroy"))) {
	echo "<td>$action ($pnodes)</td>\n";
    }
    else {
	echo "<td>$action</td>\n";
    }
299 300 301 302 303 304
    echo " <td>$ecode</td>\n";
    if ($EXPOSEARCHIVE) {
	if ($archive_idx && $archive_tag &&
	    (strcmp($action, "swapout") == 0 ||
	     strcmp($action, "swapmod") == 0)) {
	    echo "<td>".
305 306
		 "<a href='cvsweb/cvswebwrap.php3/$eidx/history/".
		          "$archive_tag/?exptidx=$eidx'>$archive_tag</a>".
307 308 309 310 311 312 313
		 "</td>\n";
	}
	else {
	    echo "<td>&nbsp</td>\n";
	}
    }
    echo "</tr>\n";
314 315 316
}
echo "</table>\n";

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
if (1) {
    $args  = "?showby=$showby&startwith=$startwith&records=$records";
    if (isset($target_user)) {
	$args .= "&user=" . $target_user->uid_idx();
    }
    if (isset($target_project)) {
	$args .= "&project=" . $target_project->pid_idx();
    }
    if (isset($exptidx)) {
	$args .= "&exptidx=$exptidx";
    }
    if ($verbose) {
	$args .= "&verbose=$verbose";
    }
    echo "<center>".
	"<a href='showstats.php3${args}'>".
	"More Entries</a></center><br>\n";
}

336 337 338 339 340
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>