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

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

web2/addons/job_monarch/image.php:

  • added query search option for node filtering

web2/addons/job_monarch/jobstore.php:

  • also search nodes and jobid with query

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

  • added load event to datastore to trigger clusterimage reload and include any searchfield query

web2/addons/job_monarch/libtoga.php:

  • added query filter for clusterimage node filtering
File size: 12.7 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                        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                        }
127                }
128        }
129
130        return $searchresults;
131}
132
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
158                                if( $state == 'R' )
159                                {
160                                        $nodes = count( $jobattrs[nodes] );
161                                }
162                                else
163                                {
164                                        $nodes = $jobattrs[nodes];
165                                }
166
167                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
168                                $cpus           = $nodes * $ppn;
169                                $queued_time    = (int) $jobattrs[queued_timestamp];
170                                $start_time     = (int) $jobattrs[start_timestamp];
171                                $runningtime    = $report_time - $start_time;
172
173                                switch( $sortby )
174                                {
175                                        case "jid":
176                                                $sorted[$jobid] = $jobid;
177                                                break;
178
179                                        case "status":
180                                                $sorted[$jobid] = $state;
181                                                break;
182
183                                        case "owner":
184                                                $sorted[$jobid] = $user;
185                                                break;
186
187                                        case "queue":
188                                                $sorted[$jobid] = $queue;
189                                                break;
190
191                                        case "name":
192                                                $sorted[$jobid] = $name;
193                                                break;
194
195                                        case "requested_time":
196                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
197                                                break;
198
199                                        case "requested_memory":
200                                                $sorted[$jobid] = $req_memory;
201                                                break;
202
203                                        case "ppn":
204                                                $sorted[$jobid] = $ppn;
205                                                break;
206                                        case "nodesct":
207                                                $sorted[$jobid] = $nodes;
208                                                break;
209                                        case "cpus":
210                                                $sorted[$jobid] = $cpus;
211                                                break;
212
213                                        case "queued_timestamp":
214                                                $sorted[$jobid] = $queued_time;
215                                                break;
216
217                                        case "start_timestamp":
218                                                $sorted[$jobid] = $start_time;
219                                                break;
220
221                                        case "runningtime":
222                                                $sorted[$jobid] = $runningtime;
223                                                break;
224
225                                        default:
226                                                break;
227                                }
228                }
229        }
230
231        if( $sortorder == "ASC" )
232        {
233                asort( $sorted );
234        }
235        else if( $sortorder == "DESC" )
236        {
237                arsort( $sorted );
238        }
239
240        return $sorted;
241}
242
243function filterJobs( $jobs )
244{
245        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
246
247        $filtered_jobs  = array();
248
249        if( isset( $jobs ) && count( $jobs ) > 0 )
250        {
251                foreach( $jobs as $jobid => $jobattrs )
252                {
253                                $state          = $jobattrs[status];
254                                $user           = $jobattrs[owner];
255                                $jqueue          = $jobattrs[queue];
256                                $name           = $jobattrs[name];
257                                $req_cpu        = $jobattrs[requested_time];
258                                $req_memory     = $jobattrs[requested_memory];
259
260                                if( $state == 'R' )
261                                {
262                                        $nodes = count( $jobattrs[nodes] );
263
264                                        $mynodehosts = array();
265                                        foreach( $jobattrs[nodes] as $mynode )
266                                        {
267                                                //if( $use_fqdn == 1)
268                                                //{
269                                                //      $mynode = $mynode.".".$jobattrs[domain];
270                                                //}
271                                                $mynodehosts[]  = $mynode;
272                                        }
273                                        $jobattrs[nodes] = $mynodehosts;
274                                }
275                                else
276                                {
277                                        $nodes = $jobattrs[nodes];
278                                }
279
280                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
281                                $cpus           = $nodes * $ppn;
282                                $queued_time    = (int) $jobattrs[queued_timestamp];
283                                $start_time     = (int) $jobattrs[start_timestamp];
284                                $runningtime    = $report_time - $start_time;
285
286                                $domain         = $jobattrs[domain];
287                                $domain_len     = 0 - strlen( $domain );
288
289                                $keepjob        = true;
290
291                                if( $jid )
292                                {
293                                        if( $jobid != $jid )
294                                        {
295                                                $keepjob        = false;
296                                        }
297                                }
298
299                                if( $host )
300                                {
301                                        if( $state == 'R' )
302                                        {
303                                                $jnodes = $jobattrs['nodes'];
304
305                                                $keepjob = false;
306
307                                                foreach( $jnodes as $jnode)
308                                                {
309                                                        if( $jnode == $host )
310                                                        {
311                                                                $keepjob = true;
312                                                        }
313                                                }
314                                        }
315                                        else
316                                        {
317                                                $keepjob = false;
318                                        }
319                                }
320                                if( $owner )
321                                {
322                                        if( $user != $owner )
323                                        {
324                                                $keepjob        = false;
325                                        }
326                                }
327                                if( $queue )
328                                {
329                                        if( $jqueue != $queue )
330                                        {
331                                                $keepjob        = false;
332                                        }
333                                }
334                                if( $status )
335                                {
336                                        if( $state != $status )
337                                        {
338                                                $keepjob        = false;
339                                        }
340                                }
341                                if( $keepjob )
342                                {
343                                        $filtered_jobs[$jobid]  = $jobattrs;
344                                }
345                }
346        }
347
348        return $filtered_jobs;
349}
350
351function getList() 
352{
353        global $jobs, $hearbeat, $pstart, $pend;
354        global $sortfield, $sortorder, $query, $host;
355        global $jid, $owner, $queue,  $status;
356
357        $job_count              = count( $jobs );
358
359        if( $job_count == 0 )
360        {
361                echo '({"total":"0", "results":""})';
362                return 0;
363        }
364
365        $jobresults             = array();
366
367        $cur_job                = 0;
368
369        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
370
371        if( $query )
372        {
373                $jobs                   = quickSearchJobs( $jobs, $query );
374        }
375        if( $jid || $owner || $queue || $status || $host )
376        {
377                $jobs                   = filterJobs( $jobs );
378        }
379        $result_count           = count( $jobs );
380
381        foreach( $sorted_jobs as $jobid => $jobattrs )
382        {
383                //if( $jobattrs['reported'] != $heartbeat )
384                //{
385                //      continue;
386                //}
387
388                if( ! array_key_exists( $jobid, $jobs ) )
389                {
390                        continue;
391                }
392
393                $jr                     = array();
394                $jr['jid']              = strval( $jobid );
395                $jr['status']           = $jobs[$jobid]['status'];
396                $jr['owner']            = $jobs[$jobid]['owner'];
397                $jr['queue']            = $jobs[$jobid]['queue'];
398                $jr['name']             = $jobs[$jobid]['name'];
399                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
400
401                if( $jr['status'] == 'R' )
402                {
403                        $nodes          = count( $jobs[$jobid][nodes] );
404                }
405                else
406                {
407                        $nodes          = (int) $jobs[$jobid][nodes];
408                }
409
410                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
411                $jr['nodect']           = strval( $nodes );
412
413                if( $jr['status'] == 'R' )
414                {
415                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
416                }
417                else
418                {
419                        $jr['nodes']    = "";
420                }
421
422                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
423                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
424
425                if( $jr['status'] == 'R' )
426                {
427                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
428                        $jr['runningtime']      = makeTime( $runningtime );
429                }
430                else
431                {
432                        $jr['runningtime']      = "";
433                }
434
435                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
436                {
437                        $cur_job        = $cur_job + 1;
438                        continue;
439                }
440                else
441                {
442                        $cur_job        = $cur_job + 1;
443                }
444
445                $jobresults[]           = $jr;
446        }
447
448        $jsonresults    = JEncode( $jobresults );
449
450        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
451
452        return 0;
453}
454
455// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
456//
457function JEncode( $arr )
458{
459        if( version_compare( PHP_VERSION, "5.2", "<" ) )
460        {   
461                require_once( "./JSON.php" );           //if php<5.2 need JSON class
462
463                $json   = new Services_JSON();          //instantiate new json object
464                $data   = $json->encode( $arr );        //encode the data in json format
465        } 
466        else
467        {
468                $data   = json_encode( $arr );          //encode the data in json format
469        }
470
471        return $data;
472}
473
474?> 
Note: See TracBrowser for help on using the repository browser.