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

Last change on this file since 543 was 543, checked in by ramonb, 16 years ago

job_monarch/jobstore.php:

  • filter jobs

js/jobgrid.js:

  • add and remove proper CSS class to cells for filters

job_monarch/css/styles.css:

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