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

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

web2/addons/job_monarch/libtoga.php:

  • once and for all REALLY fixed hostname FQDN detection
  • fixed owner filtering of nodes when multiple jobs present with different owners

web2/addons/job_monarch/jobstore.php:

  • removed domain/hostname FQDN handling: is now all done in libtoga

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

  • test alert
File size: 12.4 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$owner                  = isset($_POST['owner']) ? $_POST['owner'] : null;
23$status                 = isset($_POST['status']) ? $_POST['status'] : null;
24$queue                  = isset($_POST['queue']) ? $_POST['queue'] : null;
25$host                   = isset($_POST['host']) ? $_POST['host'] : null;
26
27global $c, $clustername, $cluster;
28
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
36//echo $pend.'p ';
37// Need to fool Ganglia here: or it won't parse XML for our cluster
38//
39$HTTP_POST_VARS["c"]    = $c;
40$_GET["c"]              = $c;
41
42global $c, $clustername, $cluster;
43
44include_once "./libtoga.php";
45
46$ds             = new DataSource();
47$myxml_data     = &$ds->getData();
48
49session_start();
50unset( $_SESSION["data"] );
51$_SESSION["data"]       = &$myxml_data;
52
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;         
81    case "SUMMARY":
82        getSummary();
83        break;         
84    default:
85        echo "{failure:true}";
86        break;
87}
88
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                        }
113                }
114        }
115
116        return $searchresults;
117}
118
119function sortJobs( $jobs, $sortby, $sortorder )
120{
121        $sorted = array();
122
123        $cmp    = create_function( '$a, $b',
124                "global \$sortby, \$sortorder;".
125
126                "if( \$a == \$b ) return 0;".
127
128                "if (\$sortorder==\"DESC\")".
129                        "return ( \$a < \$b ) ? 1 : -1;".
130                "else if (\$sortorder==\"ASC\")".
131                        "return ( \$a > \$b ) ? 1 : -1;" );
132
133        if( isset( $jobs ) && count( $jobs ) > 0 )
134        {
135                foreach( $jobs as $jobid => $jobattrs )
136                {
137                                $state          = $jobattrs[status];
138                                $user           = $jobattrs[owner];
139                                $queue          = $jobattrs[queue];
140                                $name           = $jobattrs[name];
141                                $req_cpu        = $jobattrs[requested_time];
142                                $req_memory     = $jobattrs[requested_memory];
143
144                                if( $state == 'R' )
145                                {
146                                        $nodes = count( $jobattrs[nodes] );
147                                }
148                                else
149                                {
150                                        $nodes = $jobattrs[nodes];
151                                }
152
153                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
154                                $cpus           = $nodes * $ppn;
155                                $queued_time    = (int) $jobattrs[queued_timestamp];
156                                $start_time     = (int) $jobattrs[start_timestamp];
157                                $runningtime    = $report_time - $start_time;
158
159                                switch( $sortby )
160                                {
161                                        case "jid":
162                                                $sorted[$jobid] = $jobid;
163                                                break;
164
165                                        case "status":
166                                                $sorted[$jobid] = $state;
167                                                break;
168
169                                        case "owner":
170                                                $sorted[$jobid] = $user;
171                                                break;
172
173                                        case "queue":
174                                                $sorted[$jobid] = $queue;
175                                                break;
176
177                                        case "name":
178                                                $sorted[$jobid] = $name;
179                                                break;
180
181                                        case "requested_time":
182                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
183                                                break;
184
185                                        case "requested_memory":
186                                                $sorted[$jobid] = $req_memory;
187                                                break;
188
189                                        case "ppn":
190                                                $sorted[$jobid] = $ppn;
191                                                break;
192                                        case "nodesct":
193                                                $sorted[$jobid] = $nodes;
194                                                break;
195                                        case "cpus":
196                                                $sorted[$jobid] = $cpus;
197                                                break;
198
199                                        case "queued_timestamp":
200                                                $sorted[$jobid] = $queued_time;
201                                                break;
202
203                                        case "start_timestamp":
204                                                $sorted[$jobid] = $start_time;
205                                                break;
206
207                                        case "runningtime":
208                                                $sorted[$jobid] = $runningtime;
209                                                break;
210
211                                        default:
212                                                break;
213                                }
214                }
215        }
216
217        if( $sortorder == "ASC" )
218        {
219                asort( $sorted );
220        }
221        else if( $sortorder == "DESC" )
222        {
223                arsort( $sorted );
224        }
225
226        return $sorted;
227}
228
229function filterJobs( $jobs )
230{
231        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
232
233        $filtered_jobs  = array();
234
235        if( isset( $jobs ) && count( $jobs ) > 0 )
236        {
237                foreach( $jobs as $jobid => $jobattrs )
238                {
239                                $state          = $jobattrs[status];
240                                $user           = $jobattrs[owner];
241                                $jqueue          = $jobattrs[queue];
242                                $name           = $jobattrs[name];
243                                $req_cpu        = $jobattrs[requested_time];
244                                $req_memory     = $jobattrs[requested_memory];
245
246                                if( $state == 'R' )
247                                {
248                                        $nodes = count( $jobattrs[nodes] );
249
250                                        $mynodehosts = array();
251                                        foreach( $jobattrs[nodes] as $mynode )
252                                        {
253                                                //if( $use_fqdn == 1)
254                                                //{
255                                                //      $mynode = $mynode.".".$jobattrs[domain];
256                                                //}
257                                                $mynodehosts[]  = $mynode;
258                                        }
259                                        $jobattrs[nodes] = $mynodehosts;
260                                }
261                                else
262                                {
263                                        $nodes = $jobattrs[nodes];
264                                }
265
266                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
267                                $cpus           = $nodes * $ppn;
268                                $queued_time    = (int) $jobattrs[queued_timestamp];
269                                $start_time     = (int) $jobattrs[start_timestamp];
270                                $runningtime    = $report_time - $start_time;
271
272                                $domain         = $jobattrs[domain];
273                                $domain_len     = 0 - strlen( $domain );
274
275                                $keepjob        = true;
276
277                                if( $jid )
278                                {
279                                        if( $jobid != $jid )
280                                        {
281                                                $keepjob        = false;
282                                        }
283                                }
284
285                                if( $host )
286                                {
287                                        if( $state == 'R' )
288                                        {
289                                                $jnodes = $jobattrs['nodes'];
290
291                                                $keepjob = false;
292
293                                                foreach( $jnodes as $jnode)
294                                                {
295                                                        if( $jnode == $host )
296                                                        {
297                                                                $keepjob = true;
298                                                        }
299                                                }
300                                        }
301                                        else
302                                        {
303                                                $keepjob = false;
304                                        }
305                                }
306                                if( $owner )
307                                {
308                                        if( $user != $owner )
309                                        {
310                                                $keepjob        = false;
311                                        }
312                                }
313                                if( $queue )
314                                {
315                                        if( $jqueue != $queue )
316                                        {
317                                                $keepjob        = false;
318                                        }
319                                }
320                                if( $status )
321                                {
322                                        if( $state != $status )
323                                        {
324                                                $keepjob        = false;
325                                        }
326                                }
327                                if( $keepjob )
328                                {
329                                        $filtered_jobs[$jobid]  = $jobattrs;
330                                }
331                }
332        }
333
334        return $filtered_jobs;
335}
336
337function getList() 
338{
339        global $jobs, $hearbeat, $pstart, $pend;
340        global $sortfield, $sortorder, $query, $host;
341        global $jid, $owner, $queue,  $status;
342
343        $job_count              = count( $jobs );
344
345        if( $job_count == 0 )
346        {
347                echo '({"total":"0", "results":""})';
348                return 0;
349        }
350
351        $jobresults             = array();
352
353        $cur_job                = 0;
354
355        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
356
357        if( $query )
358        {
359                $jobs                   = quickSearchJobs( $jobs, $query );
360        }
361        if( $jid || $owner || $queue || $status || $host )
362        {
363                $jobs                   = filterJobs( $jobs );
364        }
365        $result_count           = count( $jobs );
366
367        foreach( $sorted_jobs as $jobid => $jobattrs )
368        {
369                //if( $jobattrs['reported'] != $heartbeat )
370                //{
371                //      continue;
372                //}
373
374                if( ! array_key_exists( $jobid, $jobs ) )
375                {
376                        continue;
377                }
378
379                $jr                     = array();
380                $jr['jid']              = strval( $jobid );
381                $jr['status']           = $jobs[$jobid]['status'];
382                $jr['owner']            = $jobs[$jobid]['owner'];
383                $jr['queue']            = $jobs[$jobid]['queue'];
384                $jr['name']             = $jobs[$jobid]['name'];
385                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
386
387                if( $jr['status'] == 'R' )
388                {
389                        $nodes          = count( $jobs[$jobid][nodes] );
390                }
391                else
392                {
393                        $nodes          = (int) $jobs[$jobid][nodes];
394                }
395
396                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
397                $jr['nodect']           = strval( $nodes );
398
399                if( $jr['status'] == 'R' )
400                {
401                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
402                }
403                else
404                {
405                        $jr['nodes']    = "";
406                }
407
408                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
409                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
410
411                if( $jr['status'] == 'R' )
412                {
413                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
414                        $jr['runningtime']      = makeTime( $runningtime );
415                }
416                else
417                {
418                        $jr['runningtime']      = "";
419                }
420
421                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
422                {
423                        $cur_job        = $cur_job + 1;
424                        continue;
425                }
426                else
427                {
428                        $cur_job        = $cur_job + 1;
429                }
430
431                $jobresults[]           = $jr;
432        }
433
434        $jsonresults    = JEncode( $jobresults );
435
436        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
437
438        return 0;
439}
440
441// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
442//
443function JEncode( $arr )
444{
445        if( version_compare( PHP_VERSION, "5.2", "<" ) )
446        {   
447                require_once( "./JSON.php" );           //if php<5.2 need JSON class
448
449                $json   = new Services_JSON();          //instantiate new json object
450                $data   = $json->encode( $arr );        //encode the data in json format
451        } 
452        else
453        {
454                $data   = json_encode( $arr );          //encode the data in json format
455        }
456
457        return $data;
458}
459
460?> 
Note: See TracBrowser for help on using the repository browser.