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

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

job_monarch/templates/header.tpl:

  • load Grid with paging limits

job_monarch/jobstore.php:

  • sort jobs
  • only return jobs within Ext's paging start/limit

job_monarch/js/jobgrid.js:

  • add paging toolbar to Grid
File size: 8.4 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              = $_POST["sort"];
10$sortorder              = $_POST["dir"]; // ASC or DESC
11
12global $c, $clustername, $cluster;
13
14// Grid Paging stuff
15//
16//$pstart       = (int) (isset($_POST['start']) ? $_POST['start'] : $_GET['pstart']);
17$pstart = (int) $_POST['start'];
18//$pend = (int) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['plimit']);
19$pend   = (int) $_POST['limit'];
20
21// Need to fool Ganglia here: or it won't parse XML for our cluster
22//
23$HTTP_POST_VARS["c"]    = $c;
24$_GET["c"]              = $c;
25
26global $c, $clustername, $cluster;
27
28include_once "./libtoga.php";
29
30$ds             = new DataSource();
31$myxml_data     = &$ds->getData();
32
33global $jobs;
34
35$data_gatherer  = new DataGatherer( $clustername );
36$data_gatherer->parseXML( &$myxml_data );
37
38$heartbeat      = &$data_gatherer->getHeartbeat();
39$jobs           = &$data_gatherer->getJobs();
40//$gnodes         = $data_gatherer->getNodes();
41$cpus           = &$data_gatherer->getCpus();
42$use_fqdn       = &$data_gatherer->getUsingFQDN();
43
44// The ext grid script will send  a task field which will specify what it wants to do
45//$task = '';
46
47if( isset($_POST['task']) )
48{
49        $task = $_POST['task'];
50}
51if( isset( $HTTP_POST_VARS['task' ] ) )
52{
53        $task = $HTTP_POST_VARS['task'];
54}
55
56//getList();
57
58switch($task)
59{
60    case "LISTING":
61        getList();
62        break;         
63    default:
64        echo "{failure:true}";
65        break;
66}
67
68function sortJobs( $jobs, $sortby, $sortorder )
69{
70        $sorted = array();
71
72        $cmp    = create_function( '$a, $b',
73                "global \$sortby, \$sortorder;".
74
75                "if( \$a == \$b ) return 0;".
76
77                "if (\$sortorder==\"DESC\")".
78                        "return ( \$a < \$b ) ? 1 : -1;".
79                "else if (\$sortorder==\"ASC\")".
80                        "return ( \$a > \$b ) ? 1 : -1;" );
81
82        if( isset( $jobs ) && count( $jobs ) > 0 )
83        {
84                foreach( $jobs as $jobid => $jobattrs )
85                {
86                                $state          = $jobattrs[status];
87                                $user           = $jobattrs[owner];
88                                $queue          = $jobattrs[queue];
89                                $name           = $jobattrs[name];
90                                $req_cpu        = $jobattrs[requested_time];
91                                $req_memory     = $jobattrs[requested_memory];
92
93                                if( $state == 'R' )
94                                {
95                                        $nodes = count( $jobattrs[nodes] );
96                                }
97                                else
98                                {
99                                        $nodes = $jobattrs[nodes];
100                                }
101
102                                $ppn            = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
103                                $cpus           = $nodes * $ppn;
104                                $queued_time    = (int) $jobattrs[queued_timestamp];
105                                $start_time     = (int) $jobattrs[start_timestamp];
106                                $runningtime    = $report_time - $start_time;
107
108                                switch( $sortby )
109                                {
110                                        case "jid":
111                                                $sorted[$jobid] = $jobid;
112                                                break;
113
114                                        case "status":
115                                                $sorted[$jobid] = $state;
116                                                break;
117
118                                        case "owner":
119                                                $sorted[$jobid] = $user;
120                                                break;
121
122                                        case "queue":
123                                                $sorted[$jobid] = $queue;
124                                                break;
125
126                                        case "name":
127                                                $sorted[$jobid] = $name;
128                                                break;
129
130                                        case "requested_time":
131                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
132                                                break;
133
134                                        case "requested_memory":
135                                                $sorted[$jobid] = $req_memory;
136                                                break;
137
138                                        case "ppn":
139                                                $sorted[$jobid] = $ppn;
140                                                break;
141                                        case "nodesct":
142                                                $sorted[$jobid] = $nodes;
143                                                break;
144                                        case "cpus":
145                                                $sorted[$jobid] = $cpus;
146                                                break;
147
148                                        case "queued_timestamp":
149                                                $sorted[$jobid] = $queued_time;
150                                                break;
151
152                                        case "start_timestamp":
153                                                $sorted[$jobid] = $start_time;
154                                                break;
155
156                                        case "runningtime":
157                                                $sorted[$jobid] = $runningtime;
158                                                break;
159
160                                        default:
161                                                break;
162                                }
163                }
164        }
165
166        if( $sortorder == "ASC" )
167        {
168                asort( $sorted );
169        }
170        else if( $sortorder == "DESC" )
171        {
172                arsort( $sorted );
173        }
174
175        return $sorted;
176}
177
178
179
180function getList() 
181{
182        global $jobs, $hearbeat, $pstart, $pend;
183        global $sortfield, $sortorder;
184
185        $job_count      = count( $jobs );
186
187        if( $job_count == 0 )
188        {
189                echo 'crap({"total":"0", "results":""})';
190                return 0;
191        }
192
193        $jobresults     = array();
194
195        $cur_job        = 0;
196
197
198        // sorteer jobs op sortorder en sortfield
199
200        //if( $pstart > 0 ) {
201        //echo $pstart;
202        //echo $pend; }
203
204
205        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
206
207        $result_count           = count( $sorted_jobs );
208
209        foreach( $sorted_jobs as $jobid => $jobattrs )
210        {
211                //if( $jobattrs['reported'] != $heartbeat )
212                //{
213                //      continue;
214                //}
215
216                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
217                {
218                        $cur_job = $cur_job + 1;
219                        continue;
220                }
221
222                $jr['jid']              = strval( $jobid );
223                $jr['status']           = $jobs[$jobid]['status'];
224                $jr['owner']            = $jobs[$jobid]['owner'];
225                $jr['queue']            = $jobs[$jobid]['queue'];
226                $jr['name']             = $jobs[$jobid]['name'];
227                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
228
229                if( $jr['status'] == 'R' )
230                {
231                        $nodes          = count( $jobs[$jobid][nodes] );
232                }
233                else
234                {
235                        $nodes          = (int) $jobs[$jobid][nodes];
236                }
237
238                $jr['ppn']              = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 );
239                $jr['nodect']           = strval( $nodes );
240
241                if( $jr['status'] == 'R' )
242                {
243                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
244                }
245                else
246                {
247                        $jr['nodes']    = "";
248                }
249
250                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
251                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
252
253                if( $jr['status'] == 'R' )
254                {
255                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
256                        $jr['runningtime']      = makeTime( $runningtime );
257                }
258                else
259                {
260                        $jr['runningtime']      = "";
261                }
262
263                $cur_job                = $cur_job + 1;
264
265                $jobresults[]           = $jr;
266        }
267
268        $jsonresults    = JEncode( $jobresults );
269
270        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
271
272        return 0;
273}
274
275// Encodes a SQL array into a JSON formated string
276function JEncode( $arr )
277{
278        if (version_compare(PHP_VERSION,"5.2","<"))
279        {   
280                require_once("./JSON.php"); //if php<5.2 need JSON class
281
282                $json   = new Services_JSON();//instantiate new json object
283                $data   = $json->encode($arr);  //encode the data in json format
284        } 
285        else
286        {
287                $data   = json_encode($arr);  //encode the data in json format
288        }
289
290        return $data;
291}
292
293?> 
Note: See TracBrowser for help on using the repository browser.