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

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

web2/addons/job_monarch/jobstore.php:

  • fixed/added sorting by node/hostname

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

  • changed button text
File size: 12.7 KB
RevLine 
[532]1<?php
2
[550]3ini_set("memory_limit","100M");
4set_time_limit(0);
5
[536]6$c                      = $_POST["c"];
7$clustername            = $c;
8$cluster                = $c;
[532]9
[537]10// Supplied by ExtJS when DataStore has remoteSort: true
11//
[538]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;
22$owner                  = isset($_POST['owner']) ? $_POST['owner'] : null;
23$status                 = isset($_POST['status']) ? $_POST['status'] : null;
24$queue                  = isset($_POST['queue']) ? $_POST['queue'] : null;
[550]25$host                   = isset($_POST['host']) ? $_POST['host'] : null;
[543]26
[532]27global $c, $clustername, $cluster;
28
[537]29// Grid Paging stuff
30//
31//$pstart       = (int) (isset($_POST['start']) ? $_POST['start'] : $_GET['pstart']);
32$pstart = (int) $_POST['start'];
33//$pend = (int) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['plimit']);
34$pend   = (int) $_POST['limit'];
35
[538]36//echo $pend.'p ';
[537]37// Need to fool Ganglia here: or it won't parse XML for our cluster
38//
[536]39$HTTP_POST_VARS["c"]    = $c;
40$_GET["c"]              = $c;
41
42global $c, $clustername, $cluster;
43
[532]44include_once "./libtoga.php";
45
46$ds             = new DataSource();
47$myxml_data     = &$ds->getData();
48
[549]49session_start();
50unset( $_SESSION["data"] );
51$_SESSION["data"]       = &$myxml_data;
52
[532]53global $jobs;
54
55$data_gatherer  = new DataGatherer( $clustername );
56$data_gatherer->parseXML( &$myxml_data );
57
58$heartbeat      = &$data_gatherer->getHeartbeat();
59$jobs           = &$data_gatherer->getJobs();
60//$gnodes         = $data_gatherer->getNodes();
61$cpus           = &$data_gatherer->getCpus();
62$use_fqdn       = &$data_gatherer->getUsingFQDN();
63
64// The ext grid script will send  a task field which will specify what it wants to do
65//$task = '';
66
67if( isset($_POST['task']) )
68{
69        $task = $_POST['task'];
70}
71if( isset( $HTTP_POST_VARS['task' ] ) )
72{
73        $task = $HTTP_POST_VARS['task'];
74}
75
76switch($task)
77{
78    case "LISTING":
79        getList();
80        break;         
[553]81    case "SUMMARY":
82        getSummary();
83        break;         
[532]84    default:
85        echo "{failure:true}";
86        break;
87}
88
[538]89function quickSearchJobs( $jobs, $query )
90{
91        $searchresults  = array();
92
93        foreach( $jobs as $jobid => $jobattrs )
94        {
95                if( $query != null )
96                {
97                        if( strpos( $jobattrs['jid'], $query ) !== false )
98                        {
99                                $searchresults[$jobid]  = $jobattrs;
100                        }
101                        if( strpos( $jobattrs['owner'], $query ) !== false )
102                        {
103                                $searchresults[$jobid]  = $jobattrs;
104                        }
105                        if( strpos( $jobattrs['queue'], $query ) !== false )
106                        {
107                                $searchresults[$jobid]  = $jobattrs;
108                        }
109                        if( strpos( $jobattrs['name'], $query ) !== false )
110                        {
111                                $searchresults[$jobid]  = $jobattrs;
112                        }
[563]113                        if( is_array( $jobattrs['nodes'] ) )
114                        {
115                                foreach( $jobattrs['nodes'] as $jattr )
116                                {
117                                        if( strpos( $jattr, $query ) !== false )
118                                        {
119                                                $searchresults[$jobid]  = $jobattrs;
120                                        }
121                                }
122                        }
123                        if( strpos( $jobid, $query ) !== false )
124                        {
125                                $searchresults[$jobid]  = $jobattrs;
126                        }
[538]127                }
128        }
129
130        return $searchresults;
131}
132
[537]133function sortJobs( $jobs, $sortby, $sortorder )
134{
135        $sorted = array();
136
137        $cmp    = create_function( '$a, $b',
138                "global \$sortby, \$sortorder;".
139
140                "if( \$a == \$b ) return 0;".
141
142                "if (\$sortorder==\"DESC\")".
143                        "return ( \$a < \$b ) ? 1 : -1;".
144                "else if (\$sortorder==\"ASC\")".
145                        "return ( \$a > \$b ) ? 1 : -1;" );
146
147        if( isset( $jobs ) && count( $jobs ) > 0 )
148        {
149                foreach( $jobs as $jobid => $jobattrs )
150                {
151                                $state          = $jobattrs[status];
152                                $user           = $jobattrs[owner];
153                                $queue          = $jobattrs[queue];
154                                $name           = $jobattrs[name];
155                                $req_cpu        = $jobattrs[requested_time];
156                                $req_memory     = $jobattrs[requested_memory];
157
[564]158                                $nodes          = $jobattrs[nodes];
[537]159
160                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
[564]161
162                                if( $state == 'R' )
163                                {
164                                        $cpus           = count( $nodes ) * $ppn;
165                                }
166                                else
167                                {
168                                        $cpus           = ((int) $nodes ) * $ppn;
169                                }
[537]170                                $queued_time    = (int) $jobattrs[queued_timestamp];
171                                $start_time     = (int) $jobattrs[start_timestamp];
172                                $runningtime    = $report_time - $start_time;
173
174                                switch( $sortby )
175                                {
176                                        case "jid":
177                                                $sorted[$jobid] = $jobid;
178                                                break;
179
180                                        case "status":
181                                                $sorted[$jobid] = $state;
182                                                break;
183
184                                        case "owner":
185                                                $sorted[$jobid] = $user;
186                                                break;
187
188                                        case "queue":
189                                                $sorted[$jobid] = $queue;
190                                                break;
191
192                                        case "name":
193                                                $sorted[$jobid] = $name;
194                                                break;
195
196                                        case "requested_time":
197                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
198                                                break;
199
200                                        case "requested_memory":
201                                                $sorted[$jobid] = $req_memory;
202                                                break;
203
204                                        case "ppn":
205                                                $sorted[$jobid] = $ppn;
206                                                break;
207                                        case "nodesct":
[564]208                                                if( $state == 'Q' )
209                                                {
210                                                        $sorted[$jobid] = $nodes;
211                                                }
212                                                else
213                                                {
214                                                        $sorted[$jobid] = count( $nodes );
215                                                }
[537]216                                                break;
217                                        case "cpus":
218                                                $sorted[$jobid] = $cpus;
219                                                break;
220
221                                        case "queued_timestamp":
222                                                $sorted[$jobid] = $queued_time;
223                                                break;
224
225                                        case "start_timestamp":
226                                                $sorted[$jobid] = $start_time;
227                                                break;
228
229                                        case "runningtime":
230                                                $sorted[$jobid] = $runningtime;
231                                                break;
[564]232                                        case "nodes":
233                                                if( $state == 'R' )
234                                                {
235                                                        $sorted[$jobid] = $nodes[0];
236                                                }
237                                                else
238                                                {
239                                                        $sorted[$jobid] = $nodes;
240                                                }
[537]241
242                                        default:
243                                                break;
244                                }
245                }
246        }
247
248        if( $sortorder == "ASC" )
249        {
250                asort( $sorted );
251        }
252        else if( $sortorder == "DESC" )
253        {
254                arsort( $sorted );
255        }
256
257        return $sorted;
258}
259
[543]260function filterJobs( $jobs )
261{
[550]262        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
[537]263
[543]264        $filtered_jobs  = array();
[537]265
[543]266        if( isset( $jobs ) && count( $jobs ) > 0 )
267        {
268                foreach( $jobs as $jobid => $jobattrs )
269                {
270                                $state          = $jobattrs[status];
271                                $user           = $jobattrs[owner];
272                                $jqueue          = $jobattrs[queue];
273                                $name           = $jobattrs[name];
274                                $req_cpu        = $jobattrs[requested_time];
275                                $req_memory     = $jobattrs[requested_memory];
276
277                                if( $state == 'R' )
278                                {
279                                        $nodes = count( $jobattrs[nodes] );
[550]280
281                                        $mynodehosts = array();
282                                        foreach( $jobattrs[nodes] as $mynode )
283                                        {
[562]284                                                //if( $use_fqdn == 1)
285                                                //{
286                                                //      $mynode = $mynode.".".$jobattrs[domain];
287                                                //}
[550]288                                                $mynodehosts[]  = $mynode;
289                                        }
290                                        $jobattrs[nodes] = $mynodehosts;
[543]291                                }
292                                else
293                                {
294                                        $nodes = $jobattrs[nodes];
295                                }
296
297                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
298                                $cpus           = $nodes * $ppn;
299                                $queued_time    = (int) $jobattrs[queued_timestamp];
300                                $start_time     = (int) $jobattrs[start_timestamp];
301                                $runningtime    = $report_time - $start_time;
[550]302
303                                $domain         = $jobattrs[domain];
304                                $domain_len     = 0 - strlen( $domain );
305
[543]306                                $keepjob        = true;
307
308                                if( $jid )
309                                {
310                                        if( $jobid != $jid )
311                                        {
312                                                $keepjob        = false;
313                                        }
314                                }
[550]315
316                                if( $host )
317                                {
318                                        if( $state == 'R' )
319                                        {
320                                                $jnodes = $jobattrs['nodes'];
321
322                                                $keepjob = false;
323
324                                                foreach( $jnodes as $jnode)
325                                                {
326                                                        if( $jnode == $host )
327                                                        {
328                                                                $keepjob = true;
329                                                        }
330                                                }
331                                        }
332                                        else
333                                        {
334                                                $keepjob = false;
335                                        }
336                                }
[543]337                                if( $owner )
338                                {
339                                        if( $user != $owner )
340                                        {
341                                                $keepjob        = false;
342                                        }
343                                }
344                                if( $queue )
345                                {
346                                        if( $jqueue != $queue )
347                                        {
348                                                $keepjob        = false;
349                                        }
350                                }
351                                if( $status )
352                                {
353                                        if( $state != $status )
354                                        {
355                                                $keepjob        = false;
356                                        }
357                                }
358                                if( $keepjob )
359                                {
360                                        $filtered_jobs[$jobid]  = $jobattrs;
361                                }
362                }
363        }
364
365        return $filtered_jobs;
366}
367
[532]368function getList() 
369{
[537]370        global $jobs, $hearbeat, $pstart, $pend;
[550]371        global $sortfield, $sortorder, $query, $host;
[543]372        global $jid, $owner, $queue,  $status;
[532]373
[538]374        $job_count              = count( $jobs );
[532]375
376        if( $job_count == 0 )
377        {
[538]378                echo '({"total":"0", "results":""})';
[532]379                return 0;
380        }
381
[538]382        $jobresults             = array();
[532]383
[538]384        $cur_job                = 0;
[537]385
386        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
387
[543]388        if( $query )
[538]389        {
390                $jobs                   = quickSearchJobs( $jobs, $query );
391        }
[550]392        if( $jid || $owner || $queue || $status || $host )
[543]393        {
394                $jobs                   = filterJobs( $jobs );
395        }
[538]396        $result_count           = count( $jobs );
[537]397
398        foreach( $sorted_jobs as $jobid => $jobattrs )
[532]399        {
[535]400                //if( $jobattrs['reported'] != $heartbeat )
401                //{
402                //      continue;
403                //}
[532]404
[538]405                if( ! array_key_exists( $jobid, $jobs ) )
[537]406                {
407                        continue;
408                }
409
[538]410                $jr                     = array();
[534]411                $jr['jid']              = strval( $jobid );
[537]412                $jr['status']           = $jobs[$jobid]['status'];
413                $jr['owner']            = $jobs[$jobid]['owner'];
414                $jr['queue']            = $jobs[$jobid]['queue'];
415                $jr['name']             = $jobs[$jobid]['name'];
416                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
[534]417
[535]418                if( $jr['status'] == 'R' )
[532]419                {
[537]420                        $nodes          = count( $jobs[$jobid][nodes] );
[532]421                }
422                else
423                {
[537]424                        $nodes          = (int) $jobs[$jobid][nodes];
[532]425                }
426
[537]427                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
[535]428                $jr['nodect']           = strval( $nodes );
[532]429
[535]430                if( $jr['status'] == 'R' )
[532]431                {
[537]432                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
[532]433                }
[535]434                else
435                {
436                        $jr['nodes']    = "";
437                }
[532]438
[537]439                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
440                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
[534]441
[535]442                if( $jr['status'] == 'R' )
443                {
[537]444                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
[535]445                        $jr['runningtime']      = makeTime( $runningtime );
446                }
447                else
448                {
449                        $jr['runningtime']      = "";
450                }
451
[538]452                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
453                {
454                        $cur_job        = $cur_job + 1;
455                        continue;
456                }
457                else
458                {
459                        $cur_job        = $cur_job + 1;
460                }
[537]461
[534]462                $jobresults[]           = $jr;
[532]463        }
464
465        $jsonresults    = JEncode( $jobresults );
466
[537]467        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
[532]468
469        return 0;
470}
471
[538]472// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
473//
[532]474function JEncode( $arr )
475{
[538]476        if( version_compare( PHP_VERSION, "5.2", "<" ) )
[532]477        {   
[538]478                require_once( "./JSON.php" );           //if php<5.2 need JSON class
[532]479
[538]480                $json   = new Services_JSON();          //instantiate new json object
481                $data   = $json->encode( $arr );        //encode the data in json format
[532]482        } 
483        else
484        {
[538]485                $data   = json_encode( $arr );          //encode the data in json format
[532]486        }
487
488        return $data;
489}
490
491?> 
Note: See TracBrowser for help on using the repository browser.