source: trunk/web2/addons/job_monarch/jobstore.php @ 596

Last change on this file since 596 was 596, checked in by ramonb, 15 years ago

job_monarch/jobstore.php:

  • some fixes for tabbed graph view

job_monarch/templates/header.tpl:

  • enabled ext debugging
File size: 15.0 KB
Line 
1<?php
2
3ini_set("memory_limit","100M");
4set_time_limit(0);
5
6$c                      = $_POST['c'];
7$clustername            = $c;
8$cluster                = $c;
9
10// Supplied by ExtJS when DataStore has remoteSort: true
11//
12$sortfield              = isset($_POST['sort'] ) ? $_POST['sort'] : "jid";
13$sortorder              = isset($_POST['dir'] ) ? $_POST['dir'] : "ASC"; // ASC or DESC
14
15// Search query from ext.SearchField
16//
17$query                  = isset($_POST['query']) ? $_POST['query'] : null;
18
19// Filter values
20//
21$jid                    = isset($_POST['jid']) ? $_POST['jid'] : null;
22$jids                   = isset($_POST['jids']) ? $_POST['jids'] : null;
23$owner                  = isset($_POST['owner']) ? $_POST['owner'] : null;
24$status                 = isset($_POST['status']) ? $_POST['status'] : null;
25$queue                  = isset($_POST['queue']) ? $_POST['queue'] : null;
26$host                   = isset($_POST['host']) ? $_POST['host'] : null;
27
28if( $jids != null )
29{
30        $jobids = explode( ",", $jids );
31}
32else
33{
34        $jobids = null;
35}
36
37global $c, $clustername, $cluster, $metrics;
38
39// Grid Paging stuff
40//
41//$pstart       = (int) (isset($_POST['start']) ? $_POST['start'] : $_GET['pstart']);
42$pstart = (int) $_POST['start'];
43//$pend = (int) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['plimit']);
44$pend   = (int) $_POST['limit'];
45
46//echo $pend.'p ';
47// Need to fool Ganglia here: or it won't parse XML for our cluster
48//
49$HTTP_POST_VARS['c']    = $c;
50$_GET['c']              = $c;
51
52global $c, $clustername, $cluster;
53
54include_once "./libtoga.php";
55
56$ds             = new DataSource();
57$myxml_data     = &$ds->getData();
58
59session_start();
60unset( $_SESSION['data'] );
61$_SESSION['data']       = &$myxml_data;
62
63global $jobs, $metrics;
64
65$data_gatherer  = new DataGatherer( $clustername );
66$data_gatherer->parseXML( &$myxml_data );
67
68$heartbeat      = &$data_gatherer->getHeartbeat();
69$jobs           = &$data_gatherer->getJobs();
70//$gnodes         = $data_gatherer->getNodes();
71$cpus           = &$data_gatherer->getCpus();
72$use_fqdn       = &$data_gatherer->getUsingFQDN();
73
74// The ext grid script will send  a task field which will specify what it wants to do
75//$task = '';
76
77if( isset($_POST['task']) )
78{
79        $task = $_POST['task'];
80}
81if( isset( $HTTP_POST_VARS['task' ] ) )
82{
83        $task = $HTTP_POST_VARS['task'];
84}
85
86switch($task)
87{
88    case "GETJOBS":
89        getJobs();
90        break;         
91    case "GETNODES":
92        getNodes();
93        break;         
94    case "GETMETRICS":
95        getMetrics();
96        break;         
97    default:
98        echo "{failure:true}";
99        break;
100}
101
102function printCacheHeaders()
103{
104        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
105        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
106        header ("Cache-Control: no-cache, must-revalidate");
107        header ("Pragma: no-cache");
108}
109
110function getMetrics( $host=null )
111{
112        global $metrics;
113
114        reset($metrics);
115        if( !$host)
116        {
117          $firsthost = key($metrics);
118        }
119        else
120        {
121          $firsthost = $host;
122        }
123
124        $first_metrics = $metrics[$firsthost];
125
126        $metric_list    = array();
127
128        $metric_count   = 0;
129
130        foreach( $first_metrics as $metricname => $metricval )
131        {
132                $metric         = array();
133                $metric['id']   = $metricname;
134                $metric['name'] = $metricname;
135
136                $metric_list[]  = $metric;
137                $metric_count   = $metric_count + 1;
138        }
139       
140        $results                = array();
141        $results['names']       = $metric_list;
142        $results['total']       = $metric_count;
143
144        printCacheHeaders();
145
146        $jsonresults    = JEncode( $results );
147
148        echo $jsonresults;
149
150        return 0;
151}
152
153function quickSearchJobs( $jobs, $query )
154{
155        $searchresults  = array();
156
157        foreach( $jobs as $jobid => $jobattrs )
158        {
159                if( $query != null )
160                {
161                        if( strpos( $jobattrs['jid'], $query ) !== false )
162                        {
163                                $searchresults[$jobid]  = $jobattrs;
164                        }
165                        if( strpos( $jobattrs['owner'], $query ) !== false )
166                        {
167                                $searchresults[$jobid]  = $jobattrs;
168                        }
169                        if( strpos( $jobattrs['queue'], $query ) !== false )
170                        {
171                                $searchresults[$jobid]  = $jobattrs;
172                        }
173                        if( strpos( $jobattrs['name'], $query ) !== false )
174                        {
175                                $searchresults[$jobid]  = $jobattrs;
176                        }
177                        if( is_array( $jobattrs['nodes'] ) )
178                        {
179                                foreach( $jobattrs['nodes'] as $jattr )
180                                {
181                                        if( strpos( $jattr, $query ) !== false )
182                                        {
183                                                $searchresults[$jobid]  = $jobattrs;
184                                        }
185                                }
186                        }
187                        if( strpos( $jobid, $query ) !== false )
188                        {
189                                $searchresults[$jobid]  = $jobattrs;
190                        }
191                }
192        }
193
194        return $searchresults;
195}
196
197function sortJobs( $jobs, $sortby, $sortorder )
198{
199        $sorted = array();
200
201        $cmp    = create_function( '$a, $b',
202                "global \$sortby, \$sortorder;".
203
204                "if( \$a == \$b ) return 0;".
205
206                "if (\$sortorder==\"DESC\")".
207                        "return ( \$a < \$b ) ? 1 : -1;".
208                "else if (\$sortorder==\"ASC\")".
209                        "return ( \$a > \$b ) ? 1 : -1;" );
210
211        if( isset( $jobs ) && count( $jobs ) > 0 )
212        {
213                foreach( $jobs as $jobid => $jobattrs )
214                {
215                                $state          = $jobattrs['status'];
216                                $user           = $jobattrs['owner'];
217                                $queue          = $jobattrs['queue'];
218                                $name           = $jobattrs['name'];
219                                $req_cpu        = $jobattrs['requested_time'];
220                                $req_memory     = $jobattrs['requested_memory'];
221
222                                $nodes          = $jobattrs['nodes'];
223
224                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
225
226                                if( $state == 'R' )
227                                {
228                                        $cpus           = count( $nodes ) * $ppn;
229                                }
230                                else
231                                {
232                                        $cpus           = ((int) $nodes ) * $ppn;
233                                }
234                                $queued_time    = (int) $jobattrs['queued_timestamp'];
235                                $start_time     = (int) $jobattrs['start_timestamp'];
236                                $runningtime    = $report_time - $start_time;
237
238                                switch( $sortby )
239                                {
240                                        case "jid":
241                                                $sorted[$jobid] = $jobid;
242                                                break;
243
244                                        case "status":
245                                                $sorted[$jobid] = $state;
246                                                break;
247
248                                        case "owner":
249                                                $sorted[$jobid] = $user;
250                                                break;
251
252                                        case "queue":
253                                                $sorted[$jobid] = $queue;
254                                                break;
255
256                                        case "name":
257                                                $sorted[$jobid] = $name;
258                                                break;
259
260                                        case "requested_time":
261                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
262                                                break;
263
264                                        case "requested_memory":
265                                                $sorted[$jobid] = $req_memory;
266                                                break;
267
268                                        case "ppn":
269                                                $sorted[$jobid] = $ppn;
270                                                break;
271                                        case "nodect":
272                                                if( $state == 'Q' )
273                                                {
274                                                        $sorted[$jobid] = $nodes;
275                                                }
276                                                else
277                                                {
278                                                        $sorted[$jobid] = count( $nodes );
279                                                }
280                                                break;
281                                        case "cpus":
282                                                $sorted[$jobid] = $cpus;
283                                                break;
284
285                                        case "queued_timestamp":
286                                                $sorted[$jobid] = $queued_time;
287                                                break;
288
289                                        case "start_timestamp":
290                                                $sorted[$jobid] = $start_time;
291                                                break;
292
293                                        case "runningtime":
294                                                $sorted[$jobid] = $runningtime;
295                                                break;
296                                        case "nodes":
297                                                if( $state == 'R' )
298                                                {
299                                                        $sorted[$jobid] = $nodes[0];
300                                                }
301                                                else
302                                                {
303                                                        $sorted[$jobid] = $nodes;
304                                                }
305
306                                        default:
307                                                break;
308                                }
309                }
310        }
311
312        if( $sortorder == "ASC" )
313        {
314                asort( $sorted );
315        }
316        else if( $sortorder == "DESC" )
317        {
318                arsort( $sorted );
319        }
320
321        return $sorted;
322}
323
324function filterJobs( $jobs )
325{
326        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
327
328        $filtered_jobs  = array();
329
330        if( isset( $jobs ) && count( $jobs ) > 0 )
331        {
332                foreach( $jobs as $jobid => $jobattrs )
333                {
334                                $state          = $jobattrs['status'];
335                                $user           = $jobattrs['owner'];
336                                $jqueue          = $jobattrs['queue'];
337                                $name           = $jobattrs['name'];
338                                $req_cpu        = $jobattrs['requested_time'];
339                                $req_memory     = $jobattrs['requested_memory'];
340
341                                if( $state == 'R' )
342                                {
343                                        $nodes = count( $jobattrs['nodes'] );
344
345                                        $mynodehosts = array();
346                                        foreach( $jobattrs['nodes'] as $mynode )
347                                        {
348                                                //if( $use_fqdn == 1)
349                                                //{
350                                                //      $mynode = $mynode.".".$jobattrs['domain'];
351                                                //}
352                                                $mynodehosts[]  = $mynode;
353                                        }
354                                        $jobattrs['nodes'] = $mynodehosts;
355                                }
356                                else
357                                {
358                                        $nodes = $jobattrs['nodes'];
359                                }
360
361                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
362                                $cpus           = $nodes * $ppn;
363                                $queued_time    = (int) $jobattrs['queued_timestamp'];
364                                $start_time     = (int) $jobattrs['start_timestamp'];
365                                $runningtime    = $report_time - $start_time;
366
367                                $domain         = $jobattrs['domain'];
368                                $domain_len     = 0 - strlen( $domain );
369
370                                $keepjob        = true;
371
372                                if( $jid )
373                                {
374                                        if( $jobid != $jid )
375                                        {
376                                                $keepjob        = false;
377                                        }
378                                }
379                                else if( $host )
380                                {
381                                        if( $state == 'R' )
382                                        {
383                                                $jnodes         = $jobattrs['nodes'];
384
385                                                $keepjob        = false;
386
387                                                foreach( $jnodes as $jnode)
388                                                {
389                                                        if( $jnode == $host )
390                                                        {
391                                                                $keepjob = true;
392                                                        }
393                                                }
394                                        }
395                                        else
396                                        {
397                                                $keepjob        = false;
398                                        }
399                                }
400                                if( $owner )
401                                {
402                                        if( $user != $owner )
403                                        {
404                                                $keepjob        = false;
405                                        }
406                                }
407                                if( $queue )
408                                {
409                                        if( $jqueue != $queue )
410                                        {
411                                                $keepjob        = false;
412                                        }
413                                }
414                                if( $status )
415                                {
416                                        if( $state != $status )
417                                        {
418                                                $keepjob        = false;
419                                        }
420                                }
421                                if( $keepjob )
422                                {
423                                        $filtered_jobs[$jobid]  = $jobattrs;
424                                }
425                }
426        }
427
428        return $filtered_jobs;
429}
430
431function getNodes()
432{
433        global $jobs, $jobids, $clustername, $metrics, $jid;
434
435        $display_nodes  = array();
436
437        printCacheHeaders();
438
439        if( !$jobids && !$jid )
440        {
441                printf("no jobid(s)\n");
442                return 1;
443        }
444        foreach( $jobs[$jid]['nodes'] as $jobnode )
445        {
446                if( !in_array( $jobnode, $display_nodes) )
447                {
448                        $display_nodes[$jobid]  = $jobnode;
449                }
450        }
451
452        $node_results   = array();
453        $result_count   = count( $display_nodes );
454
455        foreach( $display_nodes as $jobid => $host )
456        {
457                $nr             = array();
458                $nr['c']        = $clustername;
459                $nr['h']        = $host ;
460                $nr['x']        = '5';
461                $nr['v']        = '0';
462
463                $cpus           = $metrics[$host]['cpu_num']['VAL'];
464
465                if ( !$cpus )
466                {
467                        $cpus           = 1;
468                }
469
470                $load_one       = $metrics[$host]['load_one']['VAL'];
471                $load           = ((float) $load_one) / $cpus;
472                $load_color     = load_color($load);
473
474                $nr['l']        = $load_color;
475
476                $job_runtime    = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
477                $job_window     = intval($job_runtime * 1.2);
478
479                $nr['jr']       = -$job_window;
480                $nr['js']       = (int) $jobs[$jobid]['start_timestamp'];
481
482                $node_results[] = $nr;
483        }
484
485
486        $jsonresults    = JEncode( $node_results );
487
488        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
489}
490
491function getJobs() 
492{
493        global $jobs, $hearbeat, $pstart, $pend;
494        global $sortfield, $sortorder, $query, $host;
495        global $jid, $owner, $queue,  $status;
496
497        $job_count              = count( $jobs );
498
499        printCacheHeaders();
500
501        if( $job_count == 0 )
502        {
503                echo '({"total":"0", "results":""})';
504                return 0;
505        }
506
507        $jobresults             = array();
508
509        $cur_job                = 0;
510
511        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
512
513        if( $query )
514        {
515                $jobs                   = quickSearchJobs( $jobs, $query );
516        }
517        if( $jid || $owner || $queue || $status || $host )
518        {
519                $jobs                   = filterJobs( $jobs );
520        }
521        $result_count           = count( $jobs );
522
523        foreach( $sorted_jobs as $jobid => $jobattrs )
524        {
525                //if( $jobattrs['reported'] != $heartbeat )
526                //{
527                //      continue;
528                //}
529
530                if( ! array_key_exists( $jobid, $jobs ) )
531                {
532                        continue;
533                }
534
535                $jr                     = array();
536                $jr['jid']              = strval( $jobid );
537                $jr['status']           = $jobs[$jobid]['status'];
538                $jr['owner']            = $jobs[$jobid]['owner'];
539                $jr['queue']            = $jobs[$jobid]['queue'];
540                $jr['name']             = $jobs[$jobid]['name'];
541                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
542
543                if( $jr['status'] == 'R' )
544                {
545                        $nodes          = count( $jobs[$jobid]['nodes'] );
546                }
547                else
548                {
549                        $nodes          = (int) $jobs[$jobid]['nodes'];
550                }
551
552                $jr['ppn']              = strval( $jobs[$jobid]['ppn'] ? $jobs[$jobid]['ppn'] : 1 );
553                $jr['nodect']           = strval( $nodes );
554
555                if( $jr['status'] == 'R' )
556                {
557                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
558                }
559                else
560                {
561                        $jr['nodes']    = "";
562                }
563
564                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
565                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
566
567                if( $jr['status'] == 'R' )
568                {
569                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
570                        $jr['runningtime']      = makeTime( $runningtime );
571                }
572                else
573                {
574                        $jr['runningtime']      = "";
575                }
576
577                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
578                {
579                        $cur_job        = $cur_job + 1;
580                        continue;
581                }
582                else
583                {
584                        $cur_job        = $cur_job + 1;
585                }
586
587                $jobresults[]           = $jr;
588        }
589
590        $jsonresults    = JEncode( $jobresults );
591
592
593        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
594
595        return 0;
596}
597
598// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
599//
600function JEncode( $arr )
601{
602        if( version_compare( PHP_VERSION, "5.2", "<" ) )
603        {   
604                require_once( "./JSON.php" );           //if php<5.2 need JSON class
605
606                $json   = new Services_JSON();          //instantiate new json object
607                $data   = $json->encode( $arr );        //encode the data in json format
608        } 
609        else
610        {
611                $data   = json_encode( $arr );          //encode the data in json format
612        }
613
614        return $data;
615}
616
617?> 
Note: See TracBrowser for help on using the repository browser.