getData(); global $jobs; $data_gatherer = new DataGatherer( $clustername ); $data_gatherer->parseXML( &$myxml_data ); $heartbeat = &$data_gatherer->getHeartbeat(); $jobs = &$data_gatherer->getJobs(); //$gnodes = $data_gatherer->getNodes(); $cpus = &$data_gatherer->getCpus(); $use_fqdn = &$data_gatherer->getUsingFQDN(); // The ext grid script will send a task field which will specify what it wants to do //$task = ''; if( isset($_POST['task']) ) { $task = $_POST['task']; } if( isset( $HTTP_POST_VARS['task' ] ) ) { $task = $HTTP_POST_VARS['task']; } switch($task) { case "LISTING": getList(); break; default: echo "{failure:true}"; break; } function quickSearchJobs( $jobs, $query ) { $searchresults = array(); foreach( $jobs as $jobid => $jobattrs ) { if( $query != null ) { if( strpos( $jobattrs['jid'], $query ) !== false ) { $searchresults[$jobid] = $jobattrs; } if( strpos( $jobattrs['owner'], $query ) !== false ) { $searchresults[$jobid] = $jobattrs; } if( strpos( $jobattrs['queue'], $query ) !== false ) { $searchresults[$jobid] = $jobattrs; } if( strpos( $jobattrs['name'], $query ) !== false ) { $searchresults[$jobid] = $jobattrs; } } } return $searchresults; } function sortJobs( $jobs, $sortby, $sortorder ) { $sorted = array(); $cmp = create_function( '$a, $b', "global \$sortby, \$sortorder;". "if( \$a == \$b ) return 0;". "if (\$sortorder==\"DESC\")". "return ( \$a < \$b ) ? 1 : -1;". "else if (\$sortorder==\"ASC\")". "return ( \$a > \$b ) ? 1 : -1;" ); if( isset( $jobs ) && count( $jobs ) > 0 ) { foreach( $jobs as $jobid => $jobattrs ) { $state = $jobattrs[status]; $user = $jobattrs[owner]; $queue = $jobattrs[queue]; $name = $jobattrs[name]; $req_cpu = $jobattrs[requested_time]; $req_memory = $jobattrs[requested_memory]; if( $state == 'R' ) { $nodes = count( $jobattrs[nodes] ); } else { $nodes = $jobattrs[nodes]; } $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1; $cpus = $nodes * $ppn; $queued_time = (int) $jobattrs[queued_timestamp]; $start_time = (int) $jobattrs[start_timestamp]; $runningtime = $report_time - $start_time; switch( $sortby ) { case "jid": $sorted[$jobid] = $jobid; break; case "status": $sorted[$jobid] = $state; break; case "owner": $sorted[$jobid] = $user; break; case "queue": $sorted[$jobid] = $queue; break; case "name": $sorted[$jobid] = $name; break; case "requested_time": $sorted[$jobid] = timeToEpoch( $req_cpu ); break; case "requested_memory": $sorted[$jobid] = $req_memory; break; case "ppn": $sorted[$jobid] = $ppn; break; case "nodesct": $sorted[$jobid] = $nodes; break; case "cpus": $sorted[$jobid] = $cpus; break; case "queued_timestamp": $sorted[$jobid] = $queued_time; break; case "start_timestamp": $sorted[$jobid] = $start_time; break; case "runningtime": $sorted[$jobid] = $runningtime; break; default: break; } } } if( $sortorder == "ASC" ) { asort( $sorted ); } else if( $sortorder == "DESC" ) { arsort( $sorted ); } return $sorted; } function getList() { global $jobs, $hearbeat, $pstart, $pend; global $sortfield, $sortorder, $query; $job_count = count( $jobs ); if( $job_count == 0 ) { echo '({"total":"0", "results":""})'; return 0; } $jobresults = array(); $cur_job = 0; $sorted_jobs = sortJobs( $jobs, $sortfield, $sortorder ); if( $query != null ) { $jobs = quickSearchJobs( $jobs, $query ); } $result_count = count( $jobs ); foreach( $sorted_jobs as $jobid => $jobattrs ) { //if( $jobattrs['reported'] != $heartbeat ) //{ // continue; //} if( ! array_key_exists( $jobid, $jobs ) ) { continue; } $jr = array(); $jr['jid'] = strval( $jobid ); $jr['status'] = $jobs[$jobid]['status']; $jr['owner'] = $jobs[$jobid]['owner']; $jr['queue'] = $jobs[$jobid]['queue']; $jr['name'] = $jobs[$jobid]['name']; $jr['requested_time'] = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) ); if( $jr['status'] == 'R' ) { $nodes = count( $jobs[$jobid][nodes] ); } else { $nodes = (int) $jobs[$jobid][nodes]; } $jr['ppn'] = strval( $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1 ); $jr['nodect'] = strval( $nodes ); if( $jr['status'] == 'R' ) { $jr['nodes'] = implode( ",", $jobs[$jobid]['nodes'] ); } else { $jr['nodes'] = ""; } $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] ); $jr['start_timestamp'] = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : ""); if( $jr['status'] == 'R' ) { $runningtime = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp']; $jr['runningtime'] = makeTime( $runningtime ); } else { $jr['runningtime'] = ""; } if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) ) { $cur_job = $cur_job + 1; continue; } else { $cur_job = $cur_job + 1; } $jobresults[] = $jr; } $jsonresults = JEncode( $jobresults ); echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}'; return 0; } // Encodes a SQL array into a JSON formated string: so that Javascript may understand it // function JEncode( $arr ) { if( version_compare( PHP_VERSION, "5.2", "<" ) ) { require_once( "./JSON.php" ); //if php<5.2 need JSON class $json = new Services_JSON(); //instantiate new json object $data = $json->encode( $arr ); //encode the data in json format } else { $data = json_encode( $arr ); //encode the data in json format } return $data; } ?>