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

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

job_monarch/libtoga.php,
job_monarch/jobstore.php:

  • more associative array key WARNING fixes
File size: 14.8 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 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
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                        }
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                        }
181                }
182        }
183
184        return $searchresults;
185}
186
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                {
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'];
211
212                                $nodes          = $jobattrs['nodes'];
213
214                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
215
216                                if( $state == 'R' )
217                                {
218                                        $cpus           = count( $nodes ) * $ppn;
219                                }
220                                else
221                                {
222                                        $cpus           = ((int) $nodes ) * $ppn;
223                                }
224                                $queued_time    = (int) $jobattrs['queued_timestamp'];
225                                $start_time     = (int) $jobattrs['start_timestamp'];
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;
261                                        case "nodesct":
262                                                if( $state == 'Q' )
263                                                {
264                                                        $sorted[$jobid] = $nodes;
265                                                }
266                                                else
267                                                {
268                                                        $sorted[$jobid] = count( $nodes );
269                                                }
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;
286                                        case "nodes":
287                                                if( $state == 'R' )
288                                                {
289                                                        $sorted[$jobid] = $nodes[0];
290                                                }
291                                                else
292                                                {
293                                                        $sorted[$jobid] = $nodes;
294                                                }
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
314function filterJobs( $jobs )
315{
316        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
317
318        $filtered_jobs  = array();
319
320        if( isset( $jobs ) && count( $jobs ) > 0 )
321        {
322                foreach( $jobs as $jobid => $jobattrs )
323                {
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'];
330
331                                if( $state == 'R' )
332                                {
333                                        $nodes = count( $jobattrs['nodes'] );
334
335                                        $mynodehosts = array();
336                                        foreach( $jobattrs['nodes'] as $mynode )
337                                        {
338                                                //if( $use_fqdn == 1)
339                                                //{
340                                                //      $mynode = $mynode.".".$jobattrs['domain'];
341                                                //}
342                                                $mynodehosts[]  = $mynode;
343                                        }
344                                        $jobattrs['nodes'] = $mynodehosts;
345                                }
346                                else
347                                {
348                                        $nodes = $jobattrs['nodes'];
349                                }
350
351                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
352                                $cpus           = $nodes * $ppn;
353                                $queued_time    = (int) $jobattrs['queued_timestamp'];
354                                $start_time     = (int) $jobattrs['start_timestamp'];
355                                $runningtime    = $report_time - $start_time;
356
357                                $domain         = $jobattrs['domain'];
358                                $domain_len     = 0 - strlen( $domain );
359
360                                $keepjob        = true;
361
362                                if( $jid )
363                                {
364                                        if( $jobid != $jid )
365                                        {
366                                                $keepjob        = false;
367                                        }
368                                }
369
370                                if( $host )
371                                {
372                                        if( $state == 'R' )
373                                        {
374                                                $jnodes = $jobattrs['nodes'];
375
376                                                $keepjob = false;
377
378                                                foreach( $jnodes as $jnode)
379                                                {
380                                                        if( $jnode == $host )
381                                                        {
382                                                                $keepjob = true;
383                                                        }
384                                                }
385                                        }
386                                        else
387                                        {
388                                                $keepjob = false;
389                                        }
390                                }
391                                if( $owner )
392                                {
393                                        if( $user != $owner )
394                                        {
395                                                $keepjob        = false;
396                                        }
397                                }
398                                if( $queue )
399                                {
400                                        if( $jqueue != $queue )
401                                        {
402                                                $keepjob        = false;
403                                        }
404                                }
405                                if( $status )
406                                {
407                                        if( $state != $status )
408                                        {
409                                                $keepjob        = false;
410                                        }
411                                }
412                                if( $keepjob )
413                                {
414                                        $filtered_jobs[$jobid]  = $jobattrs;
415                                }
416                }
417        }
418
419        return $filtered_jobs;
420}
421
422function getNodes()
423{
424        global $jobs, $jobids, $clustername, $metrics;
425
426        $display_nodes  = array();
427
428        if( !$jobids )
429        {
430                return 1;
431        }
432        foreach( $jobs as $jobid => $jobattrs )
433        {
434                if( in_array( $jobid, $jobids ) )
435                {
436                        foreach( $jobattrs['nodes'] as $jobnode )
437                        {
438                                if( !in_array( $jobnode, $display_nodes) )
439                                {
440                                        $display_nodes[$jobid]  = $jobnode;
441                                }
442                        }
443                }
444        }
445
446        $node_results   = array();
447        $result_count   = count( $display_nodes );
448        foreach( $display_nodes as $jobid => $host )
449        {
450                $nr             = array();
451                $nr['c']        = $clustername;
452                $nr['h']        = $host ;
453                $nr['x']        = '5';
454                $nr['v']        = '0';
455
456                $cpus           = $metrics[$host]['cpu_num']['VAL'];
457
458                if ( !$cpus )
459                {
460                        $cpus           = 1;
461                }
462
463                $load_one       = $metrics[$host]['load_one']['VAL'];
464                $load           = ((float) $load_one) / $cpus;
465                $load_color     = load_color($load);
466
467                $nr['l']        = $load_color;
468
469                $job_runtime    = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
470                $job_window     = intval($job_runtime * 1.2);
471
472                $nr['jr']       = -$job_window;
473                $nr['js']       = (int) $jobs[$jobid]['start_timestamp'];
474
475                $node_results[] = $nr;
476        }
477        $jsonresults    = JEncode( $node_results );
478
479        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
480}
481
482function getJobs() 
483{
484        global $jobs, $hearbeat, $pstart, $pend;
485        global $sortfield, $sortorder, $query, $host;
486        global $jid, $owner, $queue,  $status;
487
488        $job_count              = count( $jobs );
489
490        if( $job_count == 0 )
491        {
492                echo '({"total":"0", "results":""})';
493                return 0;
494        }
495
496        $jobresults             = array();
497
498        $cur_job                = 0;
499
500        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
501
502        if( $query )
503        {
504                $jobs                   = quickSearchJobs( $jobs, $query );
505        }
506        if( $jid || $owner || $queue || $status || $host )
507        {
508                $jobs                   = filterJobs( $jobs );
509        }
510        $result_count           = count( $jobs );
511
512        foreach( $sorted_jobs as $jobid => $jobattrs )
513        {
514                //if( $jobattrs['reported'] != $heartbeat )
515                //{
516                //      continue;
517                //}
518
519                if( ! array_key_exists( $jobid, $jobs ) )
520                {
521                        continue;
522                }
523
524                $jr                     = array();
525                $jr['jid']              = strval( $jobid );
526                $jr['status']           = $jobs[$jobid]['status'];
527                $jr['owner']            = $jobs[$jobid]['owner'];
528                $jr['queue']            = $jobs[$jobid]['queue'];
529                $jr['name']             = $jobs[$jobid]['name'];
530                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
531
532                if( $jr['status'] == 'R' )
533                {
534                        $nodes          = count( $jobs[$jobid]['nodes'] );
535                }
536                else
537                {
538                        $nodes          = (int) $jobs[$jobid]['nodes'];
539                }
540
541                $jr['ppn']              = strval( $jobs[$jobid]['ppn'] ? $jobs[$jobid]['ppn'] : 1 );
542                $jr['nodect']           = strval( $nodes );
543
544                if( $jr['status'] == 'R' )
545                {
546                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
547                }
548                else
549                {
550                        $jr['nodes']    = "";
551                }
552
553                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
554                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
555
556                if( $jr['status'] == 'R' )
557                {
558                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
559                        $jr['runningtime']      = makeTime( $runningtime );
560                }
561                else
562                {
563                        $jr['runningtime']      = "";
564                }
565
566                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
567                {
568                        $cur_job        = $cur_job + 1;
569                        continue;
570                }
571                else
572                {
573                        $cur_job        = $cur_job + 1;
574                }
575
576                $jobresults[]           = $jr;
577        }
578
579        $jsonresults    = JEncode( $jobresults );
580
581        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
582
583        return 0;
584}
585
586// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
587//
588function JEncode( $arr )
589{
590        if( version_compare( PHP_VERSION, "5.2", "<" ) )
591        {   
592                require_once( "./JSON.php" );           //if php<5.2 need JSON class
593
594                $json   = new Services_JSON();          //instantiate new json object
595                $data   = $json->encode( $arr );        //encode the data in json format
596        } 
597        else
598        {
599                $data   = json_encode( $arr );          //encode the data in json format
600        }
601
602        return $data;
603}
604
605?> 
Note: See TracBrowser for help on using the repository browser.