showsumstats.php3 12 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include("showstuff.php3");

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

#
# Only known and logged in users can end experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

# Summary data for admins only.
if (!$isadmin) {
    USERERROR("You are not allowed to view this page!", 1);
}

# Page args,
if (! isset($showby)) {
    $showby = "users";
}
if (! isset($sortby)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
    $sortby = "pdays";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}
if (! isset($range)) {
    $range = "epoch";
}

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

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

echo "<b>Range: ";
if ($range != "epoch") {
    echo "<a class='static'
            href='showsumstats.php3?showby=$showby&sortby=$sortby&range=epoch'>
            Epoch</a>, ";
}
else {
    echo "Epoch, ";
}
if ($range != "day") {
    echo "<a class='static'
            href='showsumstats.php3?showby=$showby&sortby=$sortby&range=day'>
            Day</a>, ";
}
else {
    echo "Day, ";
}
if ($range != "week") {
    echo "<a class='static'
            href='showsumstats.php3?showby=$showby&sortby=$sortby&range=week'>
            Week</a>, ";
}
else {
    echo "Week, ";
}
if ($range != "month") {
    echo "<a class='static'
            href='showsumstats.php3?showby=$showby&sortby=$sortby&range=month'>
            Month</a>, ";
}
else {
    echo "Month. ";
}
echo "</b><br><br>\n";


#
# This version prints out the simple summary info for the entire table.
# No ranges, just ordered. 
#
function showsummary ($showby, $sortby) {
    switch ($showby) {
        case "projects":
	    $which = "pid";
	    $table = "project_stats";
	    $title = "Project Summary Stats (Epoch)";
	    $link  = "showproject.php3?pid=";
	    break;
        case "users":
	    $which = "uid";
	    $table = "user_stats";
	    $title = "User Summary Stats (Epoch)";
	    $link  = "showuser.php3?target_uid=";
	    break;
        default:
	    USERERROR("Invalid showby argument: $showby!", 1);
    }
    switch ($sortby) {
        case "pid":
	    $order = "pid";
	    break;
        case "uid":
	    $order = "uid";
	    break;
        case "pnodes":
	    $order = "allexpt_pnodes desc";
	    break;
        case "pdays":
	    $order = "pnode_days desc";
	    break;
        case "edays":
	    $order = "expt_days desc";
	    break;
        default:
	    USERERROR("Invalid sortby argument: $sortby!", 1);
    }

    $query_result =
	DBQueryFatal("select $which, allexpt_pnodes, ".
		     "allexpt_pnode_duration / (24 * 3600) as pnode_days, ".
		     "allexpt_duration / (24 * 3600) as expt_days ".
		     "from $table where allexpt_pnodes!=0 ".
		     "order by $order");

    echo "<center><b>$title</b></center><br>\n";
    echo "<table align=center border=1>
          <tr>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=$which'>
                    $which</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=pnodes'>
                    Pnodes</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=pdays'>
                    Pnode Days</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=edays'>
                    Expt Days</th>
          </tr>\n";

    while ($row = mysql_fetch_assoc($query_result)) {
	$heading = $row[$which];
	$pnodes  = $row["allexpt_pnodes"];
	$phours  = $row["pnode_days"];
	$ehours  = $row["expt_days"];

	echo "<tr>
                <td><A href='$link${heading}'>$heading</A></td>
                <td>$pnodes</td>
                <td>$phours</td>
                <td>$ehours</td>
              </tr>\n";
    }
    echo "</table>\n";
}

#
# COmparison functions for sort.
#
function intcmp ($a, $b) {
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
}
function pnodecmp ($a, $b) {
    return intcmp($a["pnodes"], $b["pnodes"]);
}
function pdaycmp ($a, $b) {
    return intcmp($a["pseconds"], $b["pseconds"]);
}
function edaycmp ($a, $b) {
    return intcmp($a["eseconds"], $b["eseconds"]);
}

function showrange ($showby, $sortby, $range) {
198
199
    global $TBOPSPID;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
200
201
    switch ($range) {
        case "day":
202
	    $span = 3600 * 24 * 1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203
204
	    break;
        case "week":
205
	    $span = 3600 * 24 * 7;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
207
	    break;
        case "month":
208
	    $span = 3600 * 24 * 31;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
209
210
211
212
	    break;
        default:
	    USERERROR("Invalid range argument: $range!", 1);
    }
213
214
215
    $wclause   = "($span)";
    $now       = time();
    $spanstart = $now - $span;
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

    # Summary info, indexed by pid and uid. Each entry is an array of the
    # summary info.
    $pid_summary = array();
    $uid_summary = array();

    #
    # First get current swapped in experiments.
    #
    $query_result =
	DBQueryFatal("select r.pid,r.eid,e.expt_head_uid as creator, ".
		     "  UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(e.expt_swapped) ".
		     "   as swapseconds, ".
		     "  count(*) as pnodes ".
		     " from reserved as r ".
		     "left join experiments as e on ".
		     "     e.pid=r.pid and e.eid=r.eid ".
		     "where r.pid!='$TBOPSPID' and ".
		     "      (r.pid!='ron' and r.eid!='all') ".
		     "group by r.pid,r.eid");

    while ($row = mysql_fetch_assoc($query_result)) {
	$pid         = $row["pid"];
	$eid         = $row["eid"];
	$uid         = $row["creator"];
	$swapseconds = $row["swapseconds"];
	$pnodes      = $row["pnodes"];

	#echo "$pid $eid $uid $swapseconds $pnodes<br>\n";

	if ($swapseconds > $span)
	    $swapseconds = $span;

	$pid_summary[$pid] = array('pnodes'   => $pnodes,
				   'pseconds' => $pnodes * $swapseconds,
				   'eseconds' => $swapseconds,
				   'current'  => 1);
	$uid_summary[$uid] = array('pnodes'   => $pnodes,
				   'pseconds' => $pnodes * $swapseconds,
				   'eseconds' => $swapseconds,
				   'current'  => 1);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258
259

    $query_result =
260
261
262
263
264
265
266
267
268
269
	DBQueryFatal("select s.pid,s.eid,s.creator,t.action, ".
		     "  r1.pnodes as pnodes1,r2.pnodes as pnodes2, ".
		     "  UNIX_TIMESTAMP(t.tstamp) as ttstamp ".
		     " from testbed_stats as t ".
		     "left join experiment_stats as s on ".
		     "  s.exptidx=t.exptidx ".
		     "left join experiment_resources as r1 on ".
		     "  r1.idx=t.rsrcidx ".
		     "left join experiment_resources as r2 on ".
		     "  r2.idx=s.lastrsrc and s.lastrsrc is not null ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
		     "where t.exitcode = 0 && ".
271
272
		     "    ((UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(t.tstamp)) ".
		     "     < $wclause) ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
273
274
275
276
277
278
279
280
281
282
283
		     "order by t.tstamp");

    # Experiment start time, indexed by pid:eid.
    $expt_start = array();

    while ($row = mysql_fetch_assoc($query_result)) {
	$pid     = $row["pid"];
	$eid     = $row["eid"];
	$uid     = $row["creator"];
	$tstamp  = $row["ttstamp"];
	$action  = $row["action"];
284
285
	$pnodes  = $row["pnodes1"];
	$pnodes2 = $row["pnodes2"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
286
287
288
289
290
291
292

	if ($pnodes == 0)
	    continue;

	if (!isset($pid_summary[$pid])) {
	    $pid_summary[$pid] = array('pnodes'   => 0,
				       'pseconds' => 0,
293
294
				       'eseconds' => 0,
				       'current'  => 0);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
295
296
	    $uid_summary[$uid] = array('pnodes'   => 0,
				       'pseconds' => 0,
297
298
				       'eseconds' => 0,
				       'current'  => 0);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	}

	#echo "$pid $eid $uid $tstamp $action $pnodes<br>\n";

	switch ($action) {
        case "start":
        case "swapin":
	    $expt_start["$pid:$eid"] = array('pnodes' => $pnodes,
					     'uid'    => $uid,
					     'pid'    => $pid,
					     'stamp'  => $tstamp);
	    break;
        case "swapout":
        case "swapmod":
	    if (isset($expt_start["$pid:$eid"])) {
314
		# Use the original data, especially pnodes since if this
Leigh B. Stoller's avatar
Leigh B. Stoller committed
315
316
317
318
319
320
		# was a swapmod, the nodes are for the new config, not
		# the old config. 
		$uid    = $expt_start["$pid:$eid"]["uid"];
		$pnodes = $expt_start["$pid:$eid"]["pnodes"];
		$diff = $tstamp - $expt_start["$pid:$eid"]["stamp"];
	    }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	    else {
		#
                # The start was before the time span being looked at, so
                # no start/swapin event was returned. Add a record for it.
	        #
		$diff = $tstamp - $spanstart;
		#echo "$pid $eid $uid $action $diff $pnodes $pnodes2<br>\n";
		if ($action == "swapmod") {
                    # A pain. We need the number of pnodes for the original
		    # version of the experiment, not the new version.
		    $pnodes = $pnodes2;
		}
	    }
	    $pid_summary[$pid]["pnodes"]   += $pnodes;
	    $pid_summary[$pid]["pseconds"] += $pnodes * $diff;
	    $pid_summary[$pid]["eseconds"] += $diff;
	    $uid_summary[$uid]["pnodes"]   += $pnodes;
	    $uid_summary[$uid]["pseconds"] += $pnodes * $diff;
	    $uid_summary[$uid]["eseconds"] += $diff;
	    unset($expt_start["$pid:$eid"]);
	    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
	    # Basically, start the clock ticking again with the new
	    # number of pnodes.
	    if ($action == "swapmod") {
		$expt_start["$pid:$eid"] = array('pnodes' => $pnodes,
						 'uid'    => $uid,
						 'pid'    => $pid,
						 'stamp'  => $tstamp);
	    }
	    break;
	case "preload":
	case "destroy":
	    break;
        default:
	    TBERROR("Invalid action: $action!", 1);
	}
    }
358

Leigh B. Stoller's avatar
Leigh B. Stoller committed
359
    #
360
361
    # Anything still in the expt_start array is obviously still running,
    # but we caught those in the first query above.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
362
363
364
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
397
    #
    
    switch ($showby) {
        case "projects":
	    $which = "pid";
	    $table = $pid_summary;
	    $title = "Project Summary Stats ($range)";
	    $link  = "showproject.php3?pid=";
	    break;
        case "users":
	    $which = "uid";
	    $table = $uid_summary;
	    $title = "User Summary Stats ($range)";
	    $link  = "showuser.php3?target_uid=";
	    break;
        default:
	    USERERROR("Invalid showby argument: $showby!", 1);
    }
    switch ($sortby) {
        case "pid":
        case "uid":
	    ksort($table);
	    break;
        case "pnodes":
	    uasort($table, "pnodecmp");
	    break;
        case "pdays":
	    uasort($table, "pdaycmp");
	    break;
        case "edays":
	    uasort($table, "edaycmp");
	    break;
        default:
	    USERERROR("Invalid sortby argument: $sortby!", 1);
    }

398
399
400
401
    echo "<center>
               <b>$title</b><br>
               (includes current experiments (*))
          </center><br>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    echo "<table align=center border=1>
          <tr>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=$which&range=$range'>
                    $which</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=pnodes&range=$range'>
                    Pnodes</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=pdays&range=$range'>
                    Pnode Days</th>
             <th><a class='static'
                    href='showsumstats.php3?showby=$showby&sortby=edays&range=$range'>
                    Expt Days</th>
          </tr>\n";

    foreach ($table as $key => $value) {
	$heading = $key;
	$pnodes  = $value["pnodes"];
421
	$current = $value["current"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
422
423
424
425
426
427
428
429
	$phours  = sprintf("%.2f", $value["pseconds"] / (3600 * 24));
	$ehours  = sprintf("%.2f", $value["eseconds"] / (3600 * 24));

	# We caught a swapout, where the swapin was before the interval
	# being looked at.
	if (!$pnodes)
	    continue;

430
431
432
433
434
	if ($current)
	    $current = "*";
	else
	    $current = "";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
435
	echo "<tr>
436
                <td><A href='$link${heading}'>$heading $current</A></td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
                <td>$pnodes</td>
                <td>$phours</td>
                <td>$ehours</td>
              </tr>\n";
    }
    echo "</table>\n";
}

if ($range == "epoch") {
    showsummary($showby, $sortby);
}
else {
    showrange($showby, $sortby, $range);
}

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>