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

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

job_monarch/js/jobgrid.js,
job_monarch/templates/header.tpl:

  • added searchField

job_monarch/jobstore.php:

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