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

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

job_monarch/jobstore.php:

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