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

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

job_monarch/image.php:

  • fix filter typos

job_monarch/js/jobgrid.js:

  • fixed filter cell click enable/disable check
  • reloading is now a function
  • wil reload both grid and clusterimage now when filters are changed

job_monarch/jobstore.php:

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