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
RevLine 
[532]1<?php
2
[536]3$c                      = $_POST["c"];
4$clustername            = $c;
5$cluster                = $c;
[532]6
[537]7// Supplied by ExtJS when DataStore has remoteSort: true
8//
[538]9$sortfield              = isset($_POST['sort'] ) ? $_POST["sort"] : "jid";
10$sortorder              = isset($_POST['dir'] ) ? $_POST["dir"] : "ASC"; // ASC or DESC
[537]11
[538]12// Search query from ext.SearchField
13//
14$query                  = isset($_POST['query']) ? $_POST['query'] : null;
15
[543]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
[532]23global $c, $clustername, $cluster;
24
[537]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
[538]32//echo $pend.'p ';
[537]33// Need to fool Ganglia here: or it won't parse XML for our cluster
34//
[536]35$HTTP_POST_VARS["c"]    = $c;
36$_GET["c"]              = $c;
37
38global $c, $clustername, $cluster;
39
[532]40include_once "./libtoga.php";
41
42$ds             = new DataSource();
43$myxml_data     = &$ds->getData();
44
[549]45session_start();
46unset( $_SESSION["data"] );
47$_SESSION["data"]       = &$myxml_data;
48
[532]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
[538]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
[537]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
[543]222function filterJobs( $jobs )
223{
224        global $jid, $owner, $queue,  $status;
[537]225
[543]226        $filtered_jobs  = array();
[537]227
[543]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
[532]294function getList() 
295{
[537]296        global $jobs, $hearbeat, $pstart, $pend;
[538]297        global $sortfield, $sortorder, $query;
[543]298        global $jid, $owner, $queue,  $status;
[532]299
[538]300        $job_count              = count( $jobs );
[532]301
302        if( $job_count == 0 )
303        {
[538]304                echo '({"total":"0", "results":""})';
[532]305                return 0;
306        }
307
[538]308        $jobresults             = array();
[532]309
[538]310        $cur_job                = 0;
[537]311
312        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
313
[543]314        if( $query )
[538]315        {
316                $jobs                   = quickSearchJobs( $jobs, $query );
317        }
[543]318        if( $jid || $owner || $queue || $status )
319        {
320                $jobs                   = filterJobs( $jobs );
321        }
[538]322        $result_count           = count( $jobs );
[537]323
324        foreach( $sorted_jobs as $jobid => $jobattrs )
[532]325        {
[535]326                //if( $jobattrs['reported'] != $heartbeat )
327                //{
328                //      continue;
329                //}
[532]330
[538]331                if( ! array_key_exists( $jobid, $jobs ) )
[537]332                {
333                        continue;
334                }
335
[538]336                $jr                     = array();
[534]337                $jr['jid']              = strval( $jobid );
[537]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'] ) );
[534]343
[535]344                if( $jr['status'] == 'R' )
[532]345                {
[537]346                        $nodes          = count( $jobs[$jobid][nodes] );
[532]347                }
348                else
349                {
[537]350                        $nodes          = (int) $jobs[$jobid][nodes];
[532]351                }
352
[537]353                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
[535]354                $jr['nodect']           = strval( $nodes );
[532]355
[535]356                if( $jr['status'] == 'R' )
[532]357                {
[537]358                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
[532]359                }
[535]360                else
361                {
362                        $jr['nodes']    = "";
363                }
[532]364
[537]365                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
366                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
[534]367
[535]368                if( $jr['status'] == 'R' )
369                {
[537]370                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
[535]371                        $jr['runningtime']      = makeTime( $runningtime );
372                }
373                else
374                {
375                        $jr['runningtime']      = "";
376                }
377
[538]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                }
[537]387
[534]388                $jobresults[]           = $jr;
[532]389        }
390
391        $jsonresults    = JEncode( $jobresults );
392
[537]393        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
[532]394
395        return 0;
396}
397
[538]398// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
399//
[532]400function JEncode( $arr )
401{
[538]402        if( version_compare( PHP_VERSION, "5.2", "<" ) )
[532]403        {   
[538]404                require_once( "./JSON.php" );           //if php<5.2 need JSON class
[532]405
[538]406                $json   = new Services_JSON();          //instantiate new json object
407                $data   = $json->encode( $arr );        //encode the data in json format
[532]408        } 
409        else
410        {
[538]411                $data   = json_encode( $arr );          //encode the data in json format
[532]412        }
413
414        return $data;
415}
416
417?> 
Note: See TracBrowser for help on using the repository browser.