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

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

web2/addons/job_monarch/jobstore.php:

  • added getnodes task to retrieve node graphs
  • changed getList to getjobs

web2/addons/job_monarch/js/jobgrid.js:

  • added Graphs window popup
File size: 13.6 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;
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    default:
95        echo "{failure:true}";
96        break;
97}
98
99function quickSearchJobs( $jobs, $query )
100{
101        $searchresults  = array();
102
103        foreach( $jobs as $jobid => $jobattrs )
104        {
105                if( $query != null )
106                {
107                        if( strpos( $jobattrs['jid'], $query ) !== false )
108                        {
109                                $searchresults[$jobid]  = $jobattrs;
110                        }
111                        if( strpos( $jobattrs['owner'], $query ) !== false )
112                        {
113                                $searchresults[$jobid]  = $jobattrs;
114                        }
115                        if( strpos( $jobattrs['queue'], $query ) !== false )
116                        {
117                                $searchresults[$jobid]  = $jobattrs;
118                        }
119                        if( strpos( $jobattrs['name'], $query ) !== false )
120                        {
121                                $searchresults[$jobid]  = $jobattrs;
122                        }
123                        if( is_array( $jobattrs['nodes'] ) )
124                        {
125                                foreach( $jobattrs['nodes'] as $jattr )
126                                {
127                                        if( strpos( $jattr, $query ) !== false )
128                                        {
129                                                $searchresults[$jobid]  = $jobattrs;
130                                        }
131                                }
132                        }
133                        if( strpos( $jobid, $query ) !== false )
134                        {
135                                $searchresults[$jobid]  = $jobattrs;
136                        }
137                }
138        }
139
140        return $searchresults;
141}
142
143function sortJobs( $jobs, $sortby, $sortorder )
144{
145        $sorted = array();
146
147        $cmp    = create_function( '$a, $b',
148                "global \$sortby, \$sortorder;".
149
150                "if( \$a == \$b ) return 0;".
151
152                "if (\$sortorder==\"DESC\")".
153                        "return ( \$a < \$b ) ? 1 : -1;".
154                "else if (\$sortorder==\"ASC\")".
155                        "return ( \$a > \$b ) ? 1 : -1;" );
156
157        if( isset( $jobs ) && count( $jobs ) > 0 )
158        {
159                foreach( $jobs as $jobid => $jobattrs )
160                {
161                                $state          = $jobattrs[status];
162                                $user           = $jobattrs[owner];
163                                $queue          = $jobattrs[queue];
164                                $name           = $jobattrs[name];
165                                $req_cpu        = $jobattrs[requested_time];
166                                $req_memory     = $jobattrs[requested_memory];
167
168                                $nodes          = $jobattrs[nodes];
169
170                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
171
172                                if( $state == 'R' )
173                                {
174                                        $cpus           = count( $nodes ) * $ppn;
175                                }
176                                else
177                                {
178                                        $cpus           = ((int) $nodes ) * $ppn;
179                                }
180                                $queued_time    = (int) $jobattrs[queued_timestamp];
181                                $start_time     = (int) $jobattrs[start_timestamp];
182                                $runningtime    = $report_time - $start_time;
183
184                                switch( $sortby )
185                                {
186                                        case "jid":
187                                                $sorted[$jobid] = $jobid;
188                                                break;
189
190                                        case "status":
191                                                $sorted[$jobid] = $state;
192                                                break;
193
194                                        case "owner":
195                                                $sorted[$jobid] = $user;
196                                                break;
197
198                                        case "queue":
199                                                $sorted[$jobid] = $queue;
200                                                break;
201
202                                        case "name":
203                                                $sorted[$jobid] = $name;
204                                                break;
205
206                                        case "requested_time":
207                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
208                                                break;
209
210                                        case "requested_memory":
211                                                $sorted[$jobid] = $req_memory;
212                                                break;
213
214                                        case "ppn":
215                                                $sorted[$jobid] = $ppn;
216                                                break;
217                                        case "nodesct":
218                                                if( $state == 'Q' )
219                                                {
220                                                        $sorted[$jobid] = $nodes;
221                                                }
222                                                else
223                                                {
224                                                        $sorted[$jobid] = count( $nodes );
225                                                }
226                                                break;
227                                        case "cpus":
228                                                $sorted[$jobid] = $cpus;
229                                                break;
230
231                                        case "queued_timestamp":
232                                                $sorted[$jobid] = $queued_time;
233                                                break;
234
235                                        case "start_timestamp":
236                                                $sorted[$jobid] = $start_time;
237                                                break;
238
239                                        case "runningtime":
240                                                $sorted[$jobid] = $runningtime;
241                                                break;
242                                        case "nodes":
243                                                if( $state == 'R' )
244                                                {
245                                                        $sorted[$jobid] = $nodes[0];
246                                                }
247                                                else
248                                                {
249                                                        $sorted[$jobid] = $nodes;
250                                                }
251
252                                        default:
253                                                break;
254                                }
255                }
256        }
257
258        if( $sortorder == "ASC" )
259        {
260                asort( $sorted );
261        }
262        else if( $sortorder == "DESC" )
263        {
264                arsort( $sorted );
265        }
266
267        return $sorted;
268}
269
270function filterJobs( $jobs )
271{
272        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
273
274        $filtered_jobs  = array();
275
276        if( isset( $jobs ) && count( $jobs ) > 0 )
277        {
278                foreach( $jobs as $jobid => $jobattrs )
279                {
280                                $state          = $jobattrs[status];
281                                $user           = $jobattrs[owner];
282                                $jqueue          = $jobattrs[queue];
283                                $name           = $jobattrs[name];
284                                $req_cpu        = $jobattrs[requested_time];
285                                $req_memory     = $jobattrs[requested_memory];
286
287                                if( $state == 'R' )
288                                {
289                                        $nodes = count( $jobattrs[nodes] );
290
291                                        $mynodehosts = array();
292                                        foreach( $jobattrs[nodes] as $mynode )
293                                        {
294                                                //if( $use_fqdn == 1)
295                                                //{
296                                                //      $mynode = $mynode.".".$jobattrs[domain];
297                                                //}
298                                                $mynodehosts[]  = $mynode;
299                                        }
300                                        $jobattrs[nodes] = $mynodehosts;
301                                }
302                                else
303                                {
304                                        $nodes = $jobattrs[nodes];
305                                }
306
307                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
308                                $cpus           = $nodes * $ppn;
309                                $queued_time    = (int) $jobattrs[queued_timestamp];
310                                $start_time     = (int) $jobattrs[start_timestamp];
311                                $runningtime    = $report_time - $start_time;
312
313                                $domain         = $jobattrs[domain];
314                                $domain_len     = 0 - strlen( $domain );
315
316                                $keepjob        = true;
317
318                                if( $jid )
319                                {
320                                        if( $jobid != $jid )
321                                        {
322                                                $keepjob        = false;
323                                        }
324                                }
325
326                                if( $host )
327                                {
328                                        if( $state == 'R' )
329                                        {
330                                                $jnodes = $jobattrs['nodes'];
331
332                                                $keepjob = false;
333
334                                                foreach( $jnodes as $jnode)
335                                                {
336                                                        if( $jnode == $host )
337                                                        {
338                                                                $keepjob = true;
339                                                        }
340                                                }
341                                        }
342                                        else
343                                        {
344                                                $keepjob = false;
345                                        }
346                                }
347                                if( $owner )
348                                {
349                                        if( $user != $owner )
350                                        {
351                                                $keepjob        = false;
352                                        }
353                                }
354                                if( $queue )
355                                {
356                                        if( $jqueue != $queue )
357                                        {
358                                                $keepjob        = false;
359                                        }
360                                }
361                                if( $status )
362                                {
363                                        if( $state != $status )
364                                        {
365                                                $keepjob        = false;
366                                        }
367                                }
368                                if( $keepjob )
369                                {
370                                        $filtered_jobs[$jobid]  = $jobattrs;
371                                }
372                }
373        }
374
375        return $filtered_jobs;
376}
377
378function getNodes()
379{
380        global $jobs, $jobids, $clustername;
381
382        $display_nodes  = array();
383
384        if( !$jobids )
385        {
386                return 1;
387        }
388        foreach( $jobs as $jobid => $jobattrs )
389        {
390                if( in_array( $jobid, $jobids ) )
391                {
392                        foreach( $jobattrs['nodes'] as $jobnode )
393                        {
394                                if( !in_array( $jobnode, $display_nodes) )
395                                {
396                                        $display_nodes[]        = $jobnode;
397                                }
398                        }
399                }
400        }
401        //print_r( $display_nodes );
402        $node_results   = array();
403        $result_count   = count( $display_nodes );
404        foreach( $display_nodes as $dnode )
405        {
406
407                $nr             = array();
408                $nr['c']        = $clustername;
409                $nr['h']        = $dnode;
410                $nr['x']        = '5';
411                $nr['v']        = '0';
412
413                $node_results[] = $nr;
414        }
415        $jsonresults    = JEncode( $node_results );
416
417        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
418}
419
420function getJobs() 
421{
422        global $jobs, $hearbeat, $pstart, $pend;
423        global $sortfield, $sortorder, $query, $host;
424        global $jid, $owner, $queue,  $status;
425
426        $job_count              = count( $jobs );
427
428        if( $job_count == 0 )
429        {
430                echo '({"total":"0", "results":""})';
431                return 0;
432        }
433
434        $jobresults             = array();
435
436        $cur_job                = 0;
437
438        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
439
440        if( $query )
441        {
442                $jobs                   = quickSearchJobs( $jobs, $query );
443        }
444        if( $jid || $owner || $queue || $status || $host )
445        {
446                $jobs                   = filterJobs( $jobs );
447        }
448        $result_count           = count( $jobs );
449
450        foreach( $sorted_jobs as $jobid => $jobattrs )
451        {
452                //if( $jobattrs['reported'] != $heartbeat )
453                //{
454                //      continue;
455                //}
456
457                if( ! array_key_exists( $jobid, $jobs ) )
458                {
459                        continue;
460                }
461
462                $jr                     = array();
463                $jr['jid']              = strval( $jobid );
464                $jr['status']           = $jobs[$jobid]['status'];
465                $jr['owner']            = $jobs[$jobid]['owner'];
466                $jr['queue']            = $jobs[$jobid]['queue'];
467                $jr['name']             = $jobs[$jobid]['name'];
468                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
469
470                if( $jr['status'] == 'R' )
471                {
472                        $nodes          = count( $jobs[$jobid][nodes] );
473                }
474                else
475                {
476                        $nodes          = (int) $jobs[$jobid][nodes];
477                }
478
479                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
480                $jr['nodect']           = strval( $nodes );
481
482                if( $jr['status'] == 'R' )
483                {
484                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
485                }
486                else
487                {
488                        $jr['nodes']    = "";
489                }
490
491                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
492                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
493
494                if( $jr['status'] == 'R' )
495                {
496                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
497                        $jr['runningtime']      = makeTime( $runningtime );
498                }
499                else
500                {
501                        $jr['runningtime']      = "";
502                }
503
504                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
505                {
506                        $cur_job        = $cur_job + 1;
507                        continue;
508                }
509                else
510                {
511                        $cur_job        = $cur_job + 1;
512                }
513
514                $jobresults[]           = $jr;
515        }
516
517        $jsonresults    = JEncode( $jobresults );
518
519        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
520
521        return 0;
522}
523
524// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
525//
526function JEncode( $arr )
527{
528        if( version_compare( PHP_VERSION, "5.2", "<" ) )
529        {   
530                require_once( "./JSON.php" );           //if php<5.2 need JSON class
531
532                $json   = new Services_JSON();          //instantiate new json object
533                $data   = $json->encode( $arr );        //encode the data in json format
534        } 
535        else
536        {
537                $data   = json_encode( $arr );          //encode the data in json format
538        }
539
540        return $data;
541}
542
543?> 
Note: See TracBrowser for help on using the repository browser.