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

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

job_monarch/overview.php:

job_monarch/libtoga.php:

job_monarch/js/jobgrid.js:

job_monarch/jobstore.php:

  • use session

job_monarch/templates/header.tpl:

job_monarch/image.php:

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