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

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

job_monarch/libtoga.php:

  • bugfix to host filtering in clusterimage

job_monarch/js/jobgrid.js:

  • added metric select combobox

job_monarch/jobstore.php:

  • added getmetrics task to fill metrics combobox
File size: 14.3 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;
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;
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[]        = $jobnode;
441                                }
442                        }
443                }
444        }
445        //print_r( $display_nodes );
446        $node_results   = array();
447        $result_count   = count( $display_nodes );
448        foreach( $display_nodes as $dnode )
449        {
450
451                $nr             = array();
452                $nr['c']        = $clustername;
453                $nr['h']        = $dnode;
454                $nr['x']        = '5';
455                $nr['v']        = '0';
456
457                $node_results[] = $nr;
458        }
459        $jsonresults    = JEncode( $node_results );
460
461        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
462}
463
464function getJobs() 
465{
466        global $jobs, $hearbeat, $pstart, $pend;
467        global $sortfield, $sortorder, $query, $host;
468        global $jid, $owner, $queue,  $status;
469
470        $job_count              = count( $jobs );
471
472        if( $job_count == 0 )
473        {
474                echo '({"total":"0", "results":""})';
475                return 0;
476        }
477
478        $jobresults             = array();
479
480        $cur_job                = 0;
481
482        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
483
484        if( $query )
485        {
486                $jobs                   = quickSearchJobs( $jobs, $query );
487        }
488        if( $jid || $owner || $queue || $status || $host )
489        {
490                $jobs                   = filterJobs( $jobs );
491        }
492        $result_count           = count( $jobs );
493
494        foreach( $sorted_jobs as $jobid => $jobattrs )
495        {
496                //if( $jobattrs['reported'] != $heartbeat )
497                //{
498                //      continue;
499                //}
500
501                if( ! array_key_exists( $jobid, $jobs ) )
502                {
503                        continue;
504                }
505
506                $jr                     = array();
507                $jr['jid']              = strval( $jobid );
508                $jr['status']           = $jobs[$jobid]['status'];
509                $jr['owner']            = $jobs[$jobid]['owner'];
510                $jr['queue']            = $jobs[$jobid]['queue'];
511                $jr['name']             = $jobs[$jobid]['name'];
512                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
513
514                if( $jr['status'] == 'R' )
515                {
516                        $nodes          = count( $jobs[$jobid][nodes] );
517                }
518                else
519                {
520                        $nodes          = (int) $jobs[$jobid][nodes];
521                }
522
523                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
524                $jr['nodect']           = strval( $nodes );
525
526                if( $jr['status'] == 'R' )
527                {
528                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
529                }
530                else
531                {
532                        $jr['nodes']    = "";
533                }
534
535                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
536                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
537
538                if( $jr['status'] == 'R' )
539                {
540                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
541                        $jr['runningtime']      = makeTime( $runningtime );
542                }
543                else
544                {
545                        $jr['runningtime']      = "";
546                }
547
548                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
549                {
550                        $cur_job        = $cur_job + 1;
551                        continue;
552                }
553                else
554                {
555                        $cur_job        = $cur_job + 1;
556                }
557
558                $jobresults[]           = $jr;
559        }
560
561        $jsonresults    = JEncode( $jobresults );
562
563        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
564
565        return 0;
566}
567
568// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
569//
570function JEncode( $arr )
571{
572        if( version_compare( PHP_VERSION, "5.2", "<" ) )
573        {   
574                require_once( "./JSON.php" );           //if php<5.2 need JSON class
575
576                $json   = new Services_JSON();          //instantiate new json object
577                $data   = $json->encode( $arr );        //encode the data in json format
578        } 
579        else
580        {
581                $data   = json_encode( $arr );          //encode the data in json format
582        }
583
584        return $data;
585}
586
587?> 
Note: See TracBrowser for help on using the repository browser.