source: trunk/web/addons/job_monarch/libtoga.php @ 519

Last change on this file since 519 was 519, checked in by bastiaans, 16 years ago

libtoga.php:

  • massive indentation code cleanup
  • Property svn:keywords set to Id
File size: 47.6 KB
RevLine 
[102]1<?php
[225]2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
[231]22 * SVN $Id: libtoga.php 519 2008-03-09 12:42:01Z bastiaans $
23 *
[225]24 */
25
26
[519]27class HTTPVariables
28{
[103]29        var $clustername, $metricname;
[112]30        var $restvars, $httpvars;
[103]31
[519]32        function HTTPVariables( $httpvars, $getvars )
33        {
34                $this->restvars         = array();
[103]35
[519]36                $this->clustername      = $httpvars["c"] ? $httpvars["c"] : $getvars["c"];
37                $this->metricname       = $httpvars["m"] ? $httpvars["m"] : $getvars["m"];
[110]38
[519]39                foreach( $httpvars as $httpvar => $httpval )
40                {
41                        if( $httpval )
42                        {
[110]43                                $this->restvars[$httpvar] = $httpval;
44                        }
45                }
[117]46
[519]47                foreach( $getvars as $getvar => $getval )
48                {
49                        if( $getval )
50                        {
[117]51                                $this->restvars[$getvar] = $getval;
52                        }
53                }
[103]54        }
55
[519]56        function getClusterName()
57        {
[103]58                return $this->clustername;
59        }
60
[519]61        function getMetricName()
62        {
[103]63                return $this->metricname;
64        }
[110]65
[519]66        function getHttpVar( $var )
67        {
[110]68                if( isset( $this->restvars[$var] ) )
[519]69                {
[112]70                        return $this->restvars[$var];
[519]71                }
[110]72                else
[519]73                {
[110]74                        return null;
[519]75                }
[110]76        }
[103]77}
78
[337]79$CLUSTER_CONFS  = array();
80
[519]81// Monarch's conf
[112]82//
83include_once "./conf.php";
[195]84include_once "./version.php";
[112]85
[117]86global $GANGLIA_PATH;
[206]87global $RRDTOOL;
88global $JOB_ARCHIVE_DIR;
[207]89global $JOB_ARCHIVE_DBASE;
[329]90global $skan_str;
91global $x_first, $y_first;
[337]92global $CLUSTER_CONFS;
[112]93
[117]94$my_dir = getcwd();
[112]95
[117]96// Load Ganglia's PHP
97chdir( $GANGLIA_PATH );
[112]98
[117]99include_once "./conf.php";
100include_once "./functions.php";
101include_once "./ganglia.php";
[145]102include_once "./get_context.php";
[519]103
[145]104$context = 'cluster';
[117]105include_once "./get_ganglia.php";
106
107// Back to our PHP
108chdir( $my_dir );
109
110global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
111$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
112
[112]113// Set cluster context so that Ganglia will
114// provide us with the correct metrics array
115//
[126]116global $context, $clustername, $reports;
[129]117
[126]118global $default_metric;
119
[112]120// Ganglia's array of host metrics
121//
[126]122global $metrics, $hosts_up;
[409]123global $range, $start;
[112]124
[246]125global $DATETIME_FORMAT;
126
[519]127function makeDate( $time )
128{
[246]129        global $DATETIME_FORMAT;
130        return strftime( $DATETIME_FORMAT, $time );
131}
132
[514]133
[519]134class TarchDbase
135{
[138]136        var $ip, $dbase, $conn;
[130]137
[519]138        function TarchDbase( $ip = null, $dbase = null )
139        {
[454]140                global $CLUSTER_CONFS, $clustername;
[207]141                global $JOB_ARCHIVE_DBASE;
142
[454]143                // Import cluster specific settings
144                //
145                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
146                {
147                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
148                        {
149                                include_once $conffile;
150                        }
151                }
152
[207]153                $db_fields = explode( '/', $JOB_ARCHIVE_DBASE );
154
[454]155                $this->ip       = $db_fields[0];
156                $this->dbase    = $db_fields[1];
157                $this->conn     = null;
[130]158        }
159
[519]160        function connect()
161        {
[294]162                if( $this->ip == null )
[138]163                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]164                else
[204]165                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]166        }
[138]167
[519]168        function searchDbase( $id = null, $queue = null, $user = null, $name = null, $start_from_time = null, $start_to_time = null, $end_from_time = null, $end_to_time = null )
169        {
[248]170                global $SEARCH_RESULT_LIMIT;
171
[519]172                if( $id )
173                {
[251]174                        $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
175                        $this->resultcount = 1;
[519]176                }
177                else
178                {
[138]179                        $query_args = array();
180                       
181                        if( $queue )
[519]182                        {
[138]183                                $query_args[] = "job_queue ='$queue'";
[519]184                        }
[138]185                        if( $user )
[519]186                        {
[138]187                                $query_args[] = "job_owner ='$user'";
[519]188                        }
[138]189                        if( $name )
[519]190                        {
[138]191                                $query_args[] = "job_name = '$name'";
[519]192                        }
[138]193                        if( $start_from_time )
[519]194                        {
[138]195                                $query_args[] = "job_start_timestamp >= $start_from_time";
[519]196                        }
[142]197                        if( $start_to_time )
[519]198                        {
[138]199                                $query_args[] = "job_start_timestamp <= $start_to_time";
[519]200                        }
[138]201                        if( $end_from_time )
[519]202                        {
[138]203                                $query_args[] = "job_stop_timestamp >= $end_from_time";
[519]204                        }
[138]205                        if( $end_to_time )
[519]206                        {
[138]207                                $query_args[] = "job_stop_timestamp <= $end_to_time";
[519]208                        }
[138]209
[248]210                        $query = "FROM jobs WHERE job_status = 'F' AND ";
[138]211                        $extra_query_args = '';
212
[519]213                        foreach( $query_args as $myquery )
214                        {
[138]215                                if( $extra_query_args == '' )
[519]216                                {
[138]217                                        $extra_query_args = $myquery;
[519]218                                }
[138]219                                else
[519]220                                {
[138]221                                        $extra_query_args .= " AND ".$myquery;
[519]222                                }
[138]223                        }
224                        $query .= $extra_query_args;
225
[251]226                        $count_result_idname = "COUNT(job_id)";
227                        $select_result_idname = "job_id";
[140]228
[251]229                        $count_query = "SELECT " . $count_result_idname . " " . $query;
[248]230
[251]231                        $count_result = $this->queryDbase( $count_query );
232                        $this->resultcount = (int) $count_result[0][count];
[248]233
[454]234                        $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id DESC LIMIT " . $SEARCH_RESULT_LIMIT;
[251]235                }
236
[248]237                $ids = $this->queryDbase( $select_query );
238
[140]239                $ret = array();
240
241                foreach( $ids as $crow)
[519]242                {
[140]243                        $ret[] = $crow[job_id];
[519]244                }
[140]245
246                return $ret;
[138]247        }
248
[519]249        function getNodesForJob( $jobid )
250        {
[138]251                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
252
253                $nodes = array();
254
255                foreach( $result as $result_row ) 
[519]256                {
[141]257                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[519]258                }
[138]259
260                return $nodes;
261        }
262
[519]263        function getJobsForNode( $nodeid )
264        {
[138]265                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
266
267                $jobs = array();
268
269                foreach( $result as $result_row )
[519]270                {
[141]271                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[519]272                }
[138]273                return $jobs;
274        }
275
[519]276        function getJobArray( $id )
277        {
[138]278                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
279
280                return ( $this->makeArray( $result[0] ) );
281        }
282
[519]283        function getNodeArray( $id )
284        {
[138]285                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
286
287                return ( $this->makeArray( $result[0] ) );
288        }
289
[519]290        function makeArray( $result_row )
291        {
[138]292                $myar = array();
293
[519]294                foreach( $result_row as $mykey => $myval )
295                {
[138]296                        $map_key = explode( '_', $mykey );
297
[141]298                        $rmap_key = array_reverse( $map_key );
299                        array_pop( $rmap_key );
300                        $map_key = array_reverse( $rmap_key );
[138]301                       
[141]302                        $newkey = implode( '_', $map_key );
303                       
[138]304                        $myar[$newkey] = $result_row[$mykey];
305                }
306
307                return $myar;
308        }
309
[519]310        function queryDbase( $query )
311        {
[138]312                $result_rows = array();
313       
314                if( !$this->conn )
[519]315                {
[138]316                        $this->connect();
[519]317                }
[138]318
319                $result = pg_query( $this->conn, $query );
320
321                while ($row = pg_fetch_assoc($result))
[519]322                {
[138]323                        $result_rows[] = $row;
[519]324                }
[138]325
326                return $result_rows;
327        }
[130]328}
329
[519]330class TarchRrdGraph
331{
[130]332        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
333
[519]334        function TarchRrdGraph( $clustername, $hostname )
335        {
[206]336                global $RRDTOOL;
337                global $JOB_ARCHIVE_DIR;
338
[519]339                $this->rrdbin           = $RRDTOOL;
340                $this->rrdvalues        = array();
341                $this->tarchdir         = $JOB_ARCHIVE_DIR;
342                $this->clustername      = $clustername;
343                $this->hostname         = $hostname;
[130]344        }
345
[519]346        function doCmd( $command )
347        {
[130]348                $pipe = popen( $command . ' 2>&1', 'r' );
349
[519]350                if (!$pipe)
351                {
[130]352                        print "pipe failed.";
353                        return "";
354                }
355
356                $output = '';
[519]357
[130]358                while(!feof($pipe))
[519]359                {
[130]360                        $output .= fread($pipe, 1024);
[519]361                }
[130]362
363                pclose($pipe);
364
365                $output = explode( "\n", $output );
[519]366
[130]367                return $output;
368        }
369
[519]370        function dirList( $dir )
371        {
[130]372                $dirlist = array();
373
[519]374                if ($handle = opendir( $dir ))
375                {
376                        while (false !== ($file = readdir($handle)))
377                        {
378                                if ($file != "." && $file != "..")
379                                {
[130]380                                        $dirlist[] = $file;
381                                }
382                        }
383                        closedir($handle);
384                }
385
386                return $dirlist;
387        }
388
[519]389        function getTimePeriods( $start, $end )
390        {
[130]391                $times = array();
392                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]393
[130]394                $first = 0;
395                $last = 9999999999999;
396
[519]397                foreach( $dirlist as $dir )
398                {
[130]399                        if( $dir > $first and $dir <= $start )
[519]400                        {
[130]401                                $first = $dir;
[519]402                        }
[130]403                        if( $dir < $last and $dir >= $end )
[519]404                        {
[130]405                                $last = $dir;
[519]406                        }
[130]407                }
408
[519]409                foreach( $dirlist as $dir )
410                {
411                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) )
412                        {
[130]413                                $times[] = $dir;
[145]414                        }
[130]415                }
416
417                sort( $times );
418
419                return $times;
420        }
421
[519]422        function getRrdDirs( $start, $stop )
423        {
[145]424                $timess = $this->getTimePeriods( $start, $stop );
425
426                $rrd_files = array();
427
[519]428                foreach( $timess as $time )
429                {
[145]430                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
431                }
432
433                return $rrd_files;
434        }
435
[519]436        function getRrdFiles( $metric, $start, $stop )
437        {
[145]438                $times = $this->getTimePeriods( $start, $stop );
439
440                $rrd_files = array();
441
[519]442                foreach( $times as $time )
443                {
[145]444                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
445                }
446
447                return $rrd_files;
448        }
[130]449}
450
[519]451class DataSource
452{
[103]453        var $data, $ip, $port;
454
[519]455        function DataSource()
456        {
[326]457                global $DATA_SOURCE;
458
459                $ds_fields      = explode( ':', $DATA_SOURCE );
460
461                $ds_ip          = $ds_fields[0];
462                $ds_port        = $ds_fields[1];
463
464                $this->ip       = $ds_ip;
465                $this->port     = $ds_port;
466
[103]467        }
468
[519]469        function getData()
470        {
[103]471                $errstr;
472                $errno = 0;
473                $timeout = 3;
474
[313]475                $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout );
[103]476
[519]477                if( !$fp )
478                {
[103]479                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
480                        return;
481                }
482
[177]483                stream_set_timeout( $fp, 30 );
484
[519]485                while ( !feof( $fp ) )
486                {
[103]487                        $data .= fread( $fp, 16384 );
488                }
489
490                fclose( $fp );
491
492                return $data;
493        }
494}
495
[519]496class DataGatherer
497{
[105]498        var $xmlhandler, $data, $httpvars;
[103]499
[519]500        function DataGatherer( $cluster )
501        {
[326]502                $this->cluster  = $cluster;
[112]503                $this->httpvars = $httpvars;
[103]504        }
505
[519]506        function parseXML( $data )
507        {
[326]508                $this->parser           = xml_parser_create();
509                $this->xmlhandler       = new TorqueXMLHandler( $this->cluster );
510
511                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
[519]512
[326]513                if ( !xml_parse( $this->parser, $data ) )
[519]514                {
[313]515                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) );
[519]516                }
[103]517        }
518
[519]519        function printInfo()
520        {
[105]521                $handler = $this->xmlhandler;
522                $handler->printInfo();
523        }
524
[519]525        function getUsingFQDN()
526        {
[463]527                $handler = $this->xmlhandler;
528                return $handler->getUsingFQDN();
529        }
530
[519]531        function getNodes()
532        {
[106]533                $handler = $this->xmlhandler;
534                return $handler->getNodes();
535        }
536
[519]537        function getNode( $node )
538        {
[303]539                $handler = $this->xmlhandler;
540                return $handler->getNode( $node );
541        }
542
[519]543        function getCpus()
544        {
[124]545                $handler = $this->xmlhandler;
546                return $handler->getCpus();
547        }
548
[519]549        function getJobs()
550        {
[106]551                $handler = $this->xmlhandler;
552                return $handler->getJobs();
553        }
554
[519]555        function getJob( $job )
556        {
[303]557                $handler = $this->xmlhandler;
558                return $handler->getJob( $job );
559        }
560
[519]561        function getHeartbeat()
562        {
[114]563                $handler = $this->xmlhandler;
564                return $handler->getHeartbeat();
565        }
[298]566
[519]567        function isJobmonRunning()
568        {
[298]569                $handler = $this->xmlhandler;
570                return $handler->isJobmonRunning();
571        }
[103]572}
573
[519]574class TorqueXMLHandler
575{
[163]576        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]577
[519]578        function TorqueXMLHandler( $clustername )
579        {
[459]580                $jobs                   = array();
581                $clusters               = array();
582                $this->nodes            = array();
583                $heartbeat              = array();
[514]584                $down_nodes             = array();
585                $offline_nodes          = array();
[459]586                $this->clustername      = $clustername;
587                $this->fqdn             = 1;
[103]588        }
589
[519]590        function getUsingFQDN()
591        {
[463]592                return $this->fqdn;
593        }
594
[519]595        function getCpus()
596        {
[124]597                $cpus = 0;
598
[519]599                if( isset( $this->jobs ) && count( $this->jobs ) > 0 )
600                {
601                        foreach( $this->jobs as $jobid=>$jobattrs )
602                        {
603                                $nodes  = count( $jobattrs[nodes] );
604                                $ppn    = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
605                                $mycpus = $nodes * $ppn;
[124]606
[519]607                                $cpus   = $cpus + $mycpus;
[301]608                        }
[124]609                }
610        }
611
[519]612        function isJobmonRunning()
613        {
[298]614                if (isset( $this->heartbeat['time'] ))
[519]615                {
[298]616                        return 1;
[519]617                }
[298]618                else
[519]619                {
[298]620                        return 0;
[519]621                }
[298]622        }
623
[514]624        function makeHostname( $thostname, $tdomain=null )
625        {
626                // Should hostname be FQDN or short w/o domain
627                //
628                $nodes = &$this->nodes;
629
630                $fqdn = 1;
631
632                //$tdomain = explode( '.', $thostname );
[519]633                //
634                // TODO?: extract domain from hostname or something?
[514]635
636                if( $tdomain )
637                {
638                        $domain_len     = 0 - strlen( $tdomain );
639
640                        // Let's see if Ganglia use's FQDN or short hostnames
641                        //
642                        foreach( $nodes as $hostname => $nimage )
643                        {
644                                if( substr( $hostname, $domain_len ) != $tdomain )
645                                {
646                                        $fqdn   = 0;
647                                }
648                        }
649                }
650                else
651                {
652                        $fqdn   = 0;
653                }
654       
655                if( $tdomain && $fqdn )
656                {
657                        if( substr( $thostname, $domain_len ) != $tdomain )
658                        {
659                                $thostname = $thostname . '.'.$tdomain;
660                        } 
661                        else
662                        {
663                                $thostname = $thostname;
664                        }
665                }
666
667                return $thostname;
668        }
669
[519]670        function startElement( $parser, $name, $attrs )
671        {
[262]672                $jobs = $this->jobs;
673                $nodes = $this->nodes;
[105]674
[519]675                if ( $attrs[TN] )
676                {
[103]677                        // Ignore dead metrics. Detect and mask failures.
678                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
[519]679                        {
[103]680                                return;
[519]681                        }
[103]682                }
683
684                $jobid = null;
685
[519]686                if( $name == 'CLUSTER' )
687                {
[163]688                        $this->proc_cluster = $attrs[NAME];
[519]689                }
690                else if( $name == 'HOST' and $this->proc_cluster == $this->clustername)
691                {
[105]692                        $hostname = $attrs[NAME];
[459]693
[105]694                        $location = $attrs[LOCATION];
695
[200]696                        if( !isset( $nodes[$hostname] ) )
[519]697                        {
[326]698                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
[519]699                        }
700                }
701                else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername )
702                {
[514]703                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) )
704                        {
[114]705                                $this->heartbeat['time'] = $attrs[VAL];
[514]706                        }
707                        else if( strstr( $attrs[NAME], 'MONARCH-DOWN' ) )
708                        {
709                                $fields         = explode( ' ', $attrs[VAL] );
[104]710
[514]711                                $nodes_down     = array();
712                                $down_domain    = null;
713
714                                foreach( $fields as $f )
715                                {
716                                        $togavalues     = explode( '=', $f );
717
718                                        $toganame       = $togavalues[0];
719                                        $togavalue      = $togavalues[1];
720
721                                        if( $toganame == 'nodes' )
722                                        {
723                                                $mynodes = explode( ';', $togavalue );
724
725                                                foreach( $mynodes as $node )
726                                                {
727                                                        $nodes_down[] = $node;
728                                                }
729                                        }
730                                        else if( $toganame == 'domain' )
731                                        {
732                                                $down_domain = $togavalue;
733                                        }
734                                        else if( $toganame == 'reported' )
735                                        {
736                                                if( !isset( $this->down_nodes['heartbeat'] ) )
737                                                {
738                                                        $this->down_nodes[$togavalue]   = array( $nodes_down, $down_domain );
739                                                }
740                                        }
741                                }
[519]742                        }
743                        else if( strstr( $attrs[NAME], 'MONARCH-OFFLINE' ) )
744                        {
[514]745                                $fields         = explode( ' ', $attrs[VAL] );
746
747                                $nodes_offline  = array();
748                                $offline_domain = null;
749
750                                foreach( $fields as $f )
751                                {
752                                        $togavalues     = explode( '=', $f );
753
754                                        $toganame       = $togavalues[0];
755                                        $togavalue      = $togavalues[1];
756
757                                        if( $toganame == 'nodes' )
758                                        {
759                                                $mynodes = explode( ';', $togavalue );
760
761                                                foreach( $mynodes as $node )
762                                                {
763                                                        $nodes_offline[] = $node;
764                                                }
765                                        }
766                                        else if( $toganame == 'domain' )
767                                        {
768                                                $offline_domain = $togavalue;
769                                        }
770                                        else if( $toganame == 'reported' )
771                                        {
772                                                if( !isset( $this->offline_nodes['heartbeat'] ) )
773                                                {
774                                                        $this->offline_nodes[$togavalue] = array( $nodes_offline, $offline_domain );
775                                                }
776                                        }
777                                }
[519]778                        }
779                        else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) )
780                        {
[253]781                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
[104]782
783                                if( !isset( $jobs[$jobid] ) )
[519]784                                {
[104]785                                        $jobs[$jobid] = array();
[519]786                                }
[104]787
788                                $fields = explode( ' ', $attrs[VAL] );
789
[519]790                                foreach( $fields as $f )
791                                {
[104]792                                        $togavalues = explode( '=', $f );
793
794                                        $toganame = $togavalues[0];
795                                        $togavalue = $togavalues[1];
796
[519]797                                        if( $toganame == 'nodes' )
798                                        {
799                                                if( $jobs[$jobid][status] == 'R' )
800                                                {
[135]801                                                        if( !isset( $jobs[$jobid][$toganame] ) )
[519]802                                                        {
[135]803                                                                $jobs[$jobid][$toganame] = array();
[519]804                                                        }
[104]805
[135]806                                                        $mynodes = explode( ';', $togavalue );
[103]807
[519]808                                                        foreach( $mynodes as $node )
809                                                        {
810                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) )
811                                                                {
[253]812                                                                        $jobs[$jobid][$toganame][] = $node;
[262]813                                                                }
814                                                        }
[135]815
[519]816                                                }
817                                                else if( $jobs[$jobid][status] == 'Q' )
818                                                {
[135]819                                                        $jobs[$jobid][$toganame] = $togavalue;
820                                                }
[519]821                                        }
822                                        else
823                                        {
[105]824                                                $jobs[$jobid][$toganame] = $togavalue;
[104]825                                        }
[103]826                                }
[111]827
[519]828                                if( isset( $jobs[$jobid][nodes] ) )
829                                {
[112]830                                        $nr_nodes = count( $jobs[$jobid][nodes] );
[300]831               
[492]832                                        if( $jobs[$jobid][status] == 'R' )
833                                        {
[111]834
[495]835                                                if( isset( $jobs[$jobid][domain] ) )
[492]836                                                {
837                                                        $domain         = $jobs[$jobid][domain];
838                                                        $domain_len     = 0 - strlen( $domain );
[461]839
[492]840                                                        // Let's see if Ganglia use's FQDN or short hostnames
841                                                        //
842                                                        foreach( $nodes as $hostname => $nimage )
843                                                        {
[459]844                                       
[492]845                                                                if( substr( $hostname, $domain_len ) != $domain )
846                                                                {
847                                                                        $this->fqdn     = 0;
848                                                                }
[459]849                                                        }
850                                                }
[496]851                                                else
852                                                {
853                                                        $this->fqdn     = 0;
854                                                }
[459]855
[495]856                                                foreach( $jobs[$jobid][nodes] as $node )
857                                                {
[200]858
[461]859                                                        // Only add domain name to the hostname if Ganglia is doing that too
860                                                        //
[495]861                                                        if( $this->fqdn && isset( $jobs[$jobid][domain] ) )
[459]862                                                        {
[492]863                                                                if( substr( $node, $domain_len ) != $domain )
864                                                                {
[459]865                                                                        $host = $node. '.'.$domain;
[492]866                                                                } else
867                                                                {
[459]868                                                                        $host = $node;
869                                                                }
[300]870                                                        }
[459]871                                                        else
872                                                        {
873                                                                $host   = $node;
874                                                        }
[300]875
876                                                        if( !isset( $nodes[$host] ) )
[519]877                                                        {
[326]878                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
[519]879                                                        }
[300]880                                                        else
[519]881                                                        {
[300]882                                                                $my_node = $nodes[$host];
[519]883                                                        }
[111]884
[300]885                                                        if( !$my_node->hasJob( $jobid ) )
[519]886                                                        {
[300]887                                                                if( isset( $jobs[$jobid][ppn] ) )
[519]888                                                                {
[300]889                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
[519]890                                                                }
[300]891                                                                else
[519]892                                                                {
[300]893                                                                        $my_node->addJob( $jobid, 1 );
[519]894                                                                }
895                                                        }
[111]896
[300]897                                                        $nodes[$host] = $my_node;
898                                                }
[111]899                                        }
900                                }
[103]901                        }
902                }
[519]903                $this->jobs     = $jobs;
904                $this->nodes    = $nodes;
[103]905        }
906
[519]907        function stopElement( $parser, $name )
908        {
[514]909                $nodes  = $this->nodes;
910
911                if( $name == "GANGLIA_XML" )
912                {
[518]913                        if( sizeof( $this->down_nodes ) > 0 )
[514]914                        {
[518]915                                foreach( $this->down_nodes as $reported => $dnodes )
[514]916                                {
[518]917                                        if( $reported == $this->heartbeat['time'] )
[514]918                                        {
[518]919                                                $domain = $dnodes[1];
[514]920
[518]921                                                foreach( $dnodes[0] as $downhost )
[514]922                                                {
[518]923                                                        $downhost = $this->makeHostname( $downhost, $domain );
924
925                                                        if( isset( $nodes[$downhost] ) )
926                                                        {
927                                                                // OMG PHP4 is fking stupid!
928                                                                // $nodes[$downhost]->setDown( 1 ) won't work here..
929                                                                //
930                                                                $mynode = $nodes[$downhost];
931                                                                $mynode->setDown( 1 );
932                                                                $nodes[$downhost] = $mynode;
933                                                        }
[514]934                                                }
935                                        }
936                                }
937                        }
938
[518]939                        if( sizeof( $this->offline_nodes ) > 0 )
[514]940                        {
[518]941                                foreach( $this->offline_nodes as $reported => $onodes )
[514]942                                {
[518]943                                        if( $reported == $this->heartbeat['time'] )
[514]944                                        {
[518]945                                                $domain = $onodes[1];
[514]946
[518]947                                                foreach( $onodes[0] as $offlinehost )
[514]948                                                {
[518]949                                                        $offlinehost = $this->makeHostname( $offlinehost, $domain );
950
951                                                        if( isset( $nodes[$offlinehost] ) )
952                                                        {
953                                                                // OMG PHP4 is fking stupid!
954                                                                // $nodes[$offlinehost]->setDown( 1 ) won't work here..
955                                                                //
956                                                                $mynode = $nodes[$offlinehost];
957                                                                $mynode->setOffline( 1 );
958                                                                $nodes[$offlinehost] = $mynode;
959                                                        }
[514]960                                                }
961                                        }
962                                }
963                        }
964                }
965
966                $this->nodes = $nodes;
[103]967        }
[105]968
[519]969        function printInfo()
970        {
[105]971                $jobs = &$this->jobs;
972
973                printf( "---jobs---\n" );
974
[519]975                foreach( $jobs as $jobid => $job )
976                {
[105]977                        printf( "job %s\n", $jobid );
978
[519]979                        if( isset( $job[nodes] ) )
980                        {
981                                foreach( $job[nodes] as $node )
982                                {
[105]983                                        $mynode = $this->nodes[$node];
984                                        $hostname = $mynode->getHostname();
985                                        $location = $mynode->getLocation();
986
987                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
988                                }
989                        }
990                }
991
992                printf( "---nodes---\n" );
993
994                $nodes = &$this->nodes;
995
[519]996                foreach( $nodes as $node )
997                {
[105]998                        $hostname = $node->getHostname();
999                        $location = $node->getLocation();
1000                        $jobs = implode( ' ', $node->getJobs() );
1001                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
1002                }
1003        }
[106]1004
[519]1005        function getNodes()
1006        {
[106]1007                return $this->nodes;
1008        }
1009
[519]1010        function getNode( $node )
1011        {
1012                $nodes = &$this->nodes;
[303]1013
1014                if( isset( $nodes[$node] ) )
[519]1015                {
[303]1016                        return $nodes[$node];
[519]1017                }
[303]1018                else
[519]1019                {
[303]1020                        return NULL;
[519]1021                }
[303]1022        }
1023
[519]1024        function getJobs()
1025        {
[106]1026                return $this->jobs;
1027        }
[114]1028
[519]1029        function getJob( $job )
1030        {
1031                $jobs = &$this->jobs;
[303]1032
1033                if( isset( $jobs[$job] ) )
[519]1034                {
[303]1035                        return $jobs[$job];
[519]1036                }
[303]1037                else
[519]1038                {
[303]1039                        return NULL;
[519]1040                }
[303]1041        }
1042
[519]1043        function getHeartbeat()
1044        {
[114]1045                return $this->heartbeat['time'];
1046        }
[103]1047}
1048
[519]1049class NodeImage
1050{
[122]1051        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]1052
[519]1053        function NodeImage( $cluster, $hostname )
1054        {
[305]1055                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1056
[519]1057                $this->jobs             = array();
1058                $this->tasks            = 0;
1059                $this->hostname         = $hostname;
1060                $this->cpus             = $this->determineCpus();
1061                $this->clustername      = $cluster;
1062                $this->showinfo         = 1;
1063                $this->size             = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1064                $this->down             = 0;
1065                $this->offline          = 0;
[102]1066        }
1067
[519]1068        function addJob( $jobid, $cpus )
1069        {
1070                $jobs           = &$this->jobs;
1071                $jobs[]         = $jobid;
1072                $this->jobs     = $jobs;
[111]1073
1074                $this->addTask( $cpus );
1075        }
1076
[519]1077        function hasJob( $jobid )
1078        {
[111]1079                $jobfound = 0;
1080
1081                if( count( $this->jobs ) > 0 )
[519]1082                {
[111]1083                        foreach( $this->jobs as $job )
[519]1084                        {
[111]1085                                if( $job == $jobid )
[519]1086                                {
[111]1087                                        $jobfound = 1;
[519]1088                                }
1089                        }
1090                }
[111]1091
1092                return $jobfound;
1093        }
1094
[519]1095        function addTask( $cpus )
1096        {
[111]1097                $this->tasks = $this->tasks + $cpus;
1098        }
1099
[519]1100        function setDown( $down )
1101        {
[514]1102                $this->down = $down;
1103        }
1104
[519]1105        function isDown()
1106        {
[514]1107                return $this->down;
1108        }
[519]1109        function setOffline( $offline )
1110        {
[514]1111                $this->offline = $offline;
1112        }
1113
[519]1114        function isOffline()
1115        {
[514]1116                return $this->offline;
1117        }
1118
[519]1119        function setImage( $image )
1120        {
[111]1121                $this->image = $image;
1122        }
1123
[519]1124        function setCoords( $x, $y )
1125        {
[106]1126                $this->x = $x;
1127                $this->y = $y;
1128        }
1129
[519]1130        function getImagemapArea()
1131        {
[326]1132                $area_topleft           = $this->x . "," . $this->y;
1133                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
1134                $area_coords            = $area_topleft . "," . $area_bottomright;
1135
1136                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
1137
[514]1138                $area_tooltip           = $this->hostname;
1139
1140                if( $this->down)
1141                {
1142                        $area_tooltip           = $area_tooltip . ": DOWN";
1143                }
1144                else if( $this->offline )
1145                {
1146                        $area_tooltip           = $area_tooltip . ": OFFLINE";
1147                }
1148
1149                $area_tooltip           = $area_tooltip . ": " . implode( " ", $this->jobs );
1150
[326]1151                $tag_href               = "HREF=\"" . $area_href . "\"";
1152                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
1153                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
1154                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
1155
1156                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
1157        }
1158
[519]1159        function colorHex( $color )
1160        {
[109]1161                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]1162
1163                return $my_color;
1164        }
1165
[519]1166        function setLoad( $load )
1167        {
[106]1168                $this->load = $load;
1169        }
1170
[519]1171        function setHostname( $hostname )
1172        {
[108]1173                $this->hostname = $hostname;
1174        }
1175
[519]1176        function getHostname()
1177        {
[122]1178                return $this->hostname;
1179        }
1180
[519]1181        function getJobs()
1182        {
[114]1183                return $this->jobs;
1184        }
1185
[519]1186        function setShowinfo( $showinfo )
1187        {
[122]1188                $this->showinfo = $showinfo;
1189        }
1190
[519]1191        function drawSmall()
1192        {
[305]1193                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1194
1195                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1196
1197                $this->draw();
[122]1198        }
[110]1199
[519]1200        function drawBig()
1201        {
[305]1202                global $BIG_CLUSTERIMAGE_NODEWIDTH;
1203
1204                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
1205
1206                $this->draw();
[122]1207        }
[106]1208
[519]1209        function draw()
1210        {
[514]1211                global $JOB_NODE_MARKING, $NODE_DOWN_MARKING, $NODE_OFFLINE_MARKING;
[102]1212
[122]1213                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[305]1214                $size = $this->size;
[107]1215
[110]1216                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
1217
[519]1218                if( $this->showinfo)
1219                {
[122]1220                        $this->load = $this->determineLoad();
[111]1221
[519]1222                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) )
1223                        {
[122]1224                                printf( "aborting\n" );
1225                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
1226                                return;
1227                        }
[111]1228
[122]1229                        // Convert Ganglias Hexadecimal load color to a Decimal one
1230                        //
1231                        $load = $this->determineLoad(); 
1232                        $usecolor = $this->colorHex( load_color($load) );
1233                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
[514]1234                        if( $this->down )
1235                        {
1236                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_DOWN_MARKING, $black_color );
1237                        }
1238                        else if( $this->offline )
1239                        {
1240                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_OFFLINE_MARKING, $black_color );
1241                        }
1242                        else if( count( $this->jobs ) > 0 )
1243                        {
[280]1244                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
[514]1245                        }
[519]1246                }
1247                else
1248                {
[122]1249                        // White
1250                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
1251                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
1252                }
[102]1253        }
1254
[519]1255        function determineCpus()
1256        {
[108]1257                global $metrics;
1258
1259                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[111]1260
[519]1261                if (!$cpus)
1262                {
1263                        $cpus=1;
1264                }
1265
[111]1266                return $cpus;
1267        }
1268
[519]1269        function determineLoad()
1270        {
[111]1271                global $metrics;
1272
[519]1273                $load_one       = $metrics[$this->hostname][load_one][VAL];
1274                $load           = ((float) $load_one)/$this->cpus;
[102]1275
[111]1276                return $load;
[106]1277        }
1278}
1279
[519]1280class ClusterImage
1281{
[111]1282        var $dataget, $image, $clustername;
[124]1283        var $filtername, $filters;
[106]1284
[519]1285        function ClusterImage( $data, $clustername )
1286        {
[305]1287                $this->dataget          = new DataGatherer( $clustername );
[326]1288                $this->data             = $data;
[305]1289                $this->clustername      = $clustername;
1290                $this->filters          = array();
1291                $this->size             = 's';
[326]1292                $this->width            = 0;
1293                $this->height           = 0;
1294                $this->output           = 1;
[106]1295        }
1296
[519]1297        function getWidth()
1298        {
[326]1299                return $this->width;
1300        }
[519]1301        function getHeight()
1302        {
[326]1303                return $this->height;
1304        }
[519]1305        function setSmall()
1306        {
[305]1307                $this->size     = 's';
1308        }
[519]1309        function setBig()
1310        {
[305]1311                $this->size     = 'b';
1312        }
[519]1313        function setNoimage()
1314        {
[326]1315                $this->output   = 0;
1316        }
[519]1317        function isSmall()
1318        {
[305]1319                return ($this->size == 's');
1320        }
[519]1321        function isBig()
1322        {
[305]1323                return ($this->size == 'b');
1324        }
[519]1325        function setFilter( $filtername, $filtervalue )
1326        {
[124]1327                $this->filters[$filtername] = $filtervalue;
[122]1328        }
1329
[519]1330        function filterNodes( $jobs, $nodes )
1331        {
[122]1332                $filtered_nodes = array();
1333
[519]1334                foreach( $nodes as $node )
1335                {
[122]1336                        $hostname = $node->getHostname();
1337
[124]1338                        $addhost = 1;
[122]1339
[519]1340                        if( count( $this->filters ) > 0 )
1341                        {
[124]1342                                $mynjobs = $node->getJobs();
1343
[519]1344                                if( count( $mynjobs ) > 0 )
1345                                {
1346                                        foreach( $mynjobs as $myjob )
1347                                        {
1348                                                foreach( $this->filters as $filtername => $filtervalue )
1349                                                {
1350                                                        if( $filtername!=null && $filtername!='' )
1351                                                        {
1352                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) )
1353                                                                {
[124]1354                                                                        $addhost = 0;
[519]1355                                                                }
1356                                                                else if( $filtername != 'jobid' )
1357                                                                {
1358                                                                        if( $jobs[$myjob][$filtername] != $filtervalue )
1359                                                                        {
[124]1360                                                                                $addhost = 0;
1361                                                                        }
1362                                                                }
1363                                                        }
1364                                                }
1365                                        }
[519]1366                                }
1367                                else
1368                                {
[124]1369                                        $addhost = 0;
[519]1370                                }
[124]1371                        }
1372
1373                        if( $addhost )
[519]1374                        {
[122]1375                                $filtered_nodes[] = $hostname;
[519]1376                        }
[122]1377                }
1378
1379                return $filtered_nodes;
1380        }
1381
[519]1382        function draw()
1383        {
[110]1384                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
[305]1385                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
[339]1386                global $CLUSTER_CONFS, $confcluster;
[329]1387
[339]1388                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
[329]1389                global $x_first, $y_first;
[331]1390
[337]1391                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1392                {
[339]1393                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
[337]1394                        {
1395                                include_once $conffile;
1396                        }
1397                }
1398
[107]1399                $mydatag = $this->dataget;
[326]1400                $mydatag->parseXML( $this->data );
[106]1401
[519]1402                if( $this->isSmall() )
1403                {
1404                        $max_width      = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1405                        $node_width     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[305]1406                }
[519]1407                else if( $this->isBig() )
1408                {
1409                        $max_width      = $BIG_CLUSTERIMAGE_MAXWIDTH;
1410                        $node_width     = $BIG_CLUSTERIMAGE_NODEWIDTH;
1411                }
[106]1412
[519]1413                $nodes          = $mydatag->getNodes();
1414                $nodes_hosts    = array_keys( $nodes );
[106]1415
[519]1416                $nodes_nr       = count( $nodes );
[106]1417
[519]1418                $nodes_size     = $nodes_nr*$node_width;
1419                $node_rows      = 0;
[106]1420
[519]1421                if( $nodes_size > $max_width )
1422                {
[107]1423                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[519]1424                }
1425                else
1426                {
[106]1427                        $nodes_per_row = $nodes_size;
1428                        $node_rows = 1;
1429                }
1430
[519]1431                if( $nodes_per_row < $nodes_nr )
1432                {
[107]1433                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1434                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
[519]1435
1436                        if( $node_rest > 0 )
1437                        {
[106]1438                                $node_rows++;
1439                        }
1440                }
1441
[306]1442                $y_offset       = 0;
1443                $font           = 2;
[329]1444                $fontwidth      = ImageFontWidth( $font );
[306]1445                $fontheight     = ImageFontHeight( $font );
1446                $fontspaceing   = 2;
1447                $y_offset       = $fontheight + (2 * $fontspaceing);
1448
[326]1449                $this->width    = $max_width;
1450                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1451
[122]1452                $jobs = $mydatag->getJobs();
1453                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1454
[329]1455                if( $SORTBY_HOSTNAME != "" )
1456                {
1457
1458                        $sorted         = array();
1459
1460                        $x_first        = 0;
1461                        $y_first        = 0;
1462
[339]1463                        $skan_str       = $SORTBY_HOSTNAME;
[329]1464
[339]1465                        global $x_present, $y_present;
1466                        $x_present      = false;
1467                        $y_present      = false;
1468
[464]1469                        // Should we scan by X, Y or both
1470                        //
1471                        if(strpos( $SORTBY_HOSTNAME, "{x}" ) != false )
[329]1472                        {
[464]1473                                $x_str          = "{x}";
[339]1474                                $x_present      = true;
1475                        }
[464]1476                        else if(strpos( $SORTBY_HOSTNAME, "{X}" ) != false )
[339]1477                        {
[464]1478                                $x_str          = "{X}";
1479                                $x_present      = true;
1480                        }
1481                        if(strpos( $SORTBY_HOSTNAME, "{y}" ) != false )
1482                        {
1483                                $y_str          = "{y}";
[339]1484                                $y_present      = true;
1485                        }
[464]1486                        else if(strpos( $SORTBY_HOSTNAME, "{Y}" ) != false )
1487                        {
1488                                $y_str          = "{Y}";
1489                                $y_present      = true;
1490                        }
[339]1491
[464]1492                        // If we should scan for both X and Y: see which one is first
1493                        //
1494                        if(( strpos( $SORTBY_HOSTNAME, $x_str ) < strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
[339]1495                        {
[106]1496                       
[329]1497                                $x_first        = 1;
1498                        }
[464]1499                        else if(( strpos( $SORTBY_HOSTNAME, $x_str ) > strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
[329]1500                        {
1501                                $y_first        = 1;
1502               
1503                        }
[339]1504                        else if( $x_present )
1505                        {
1506                                $x_first        = 1;
1507                        }
1508                        else if( $y_present )
1509                        {
1510                                $y_first        = 1;
1511                        }
[329]1512
[464]1513                        // Now replace our {x} and {y} with %d for sscanf parsing
1514                        //
[339]1515                        if(( $x_first ) && ( $x_present && $y_present ) )
1516                        {
[464]1517                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1518                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
[339]1519                        } 
1520                        else if( $x_present)
1521                        {
[464]1522                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
[339]1523                        }
1524                        else if( $y_present)
1525                        {
[464]1526                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
[339]1527                        }
[329]1528
1529                        $x_min          = null;
1530                        $x_max          = null;
1531                        $y_min          = null;
1532                        $y_max          = null;
1533
[464]1534                        // Now let's walk through all our nodes and see which one are valid for our scan pattern
1535                        //
[329]1536                        foreach( $nodes as $hostname => $node )
1537                        {
[404]1538                                $x      = null;
1539                                $y      = null;
[339]1540
1541                                if( $x_present && $y_present )
[329]1542                                {
[339]1543                                        if( $x_first )
1544                                        {
1545                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1546                                        }
1547                                        else if( $y_first )
1548                                        {
1549                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1550                                        }
[463]1551
[339]1552                                        // Remove nodes that don't match
1553                                        //
1554                                        if( $n < 2 )
1555                                        {
[463]1556                                                // This node hostname has no match for: {x} and {y}
1557                                                //
[339]1558                                                unset( $nodes[$hostname] );
1559                                        }
[329]1560                                }
[339]1561                                else if( $x_present && !$y_present )
[329]1562                                {
[339]1563                                        $n = sscanf( $hostname, $skan_str, $x );
[463]1564
[339]1565                                        // Remove nodes that don't match
1566                                        //
1567                                        if( $n < 1 )
1568                                        {
[463]1569                                                // This node hostname has no match for: {x}
1570                                                //
[339]1571                                                unset( $nodes[$hostname] );
1572                                        }
1573                                        $y      = 1;
[329]1574                                }
[339]1575                                else if( $y_present && !$x_present )
1576                                {
1577                                        $n = sscanf( $hostname, $skan_str, $y );
[463]1578
[339]1579                                        // Remove nodes that don't match
1580                                        //
1581                                        if( $n < 1 )
1582                                        {
[463]1583                                                // This node hostname has no match for: {y}
1584                                                //
[339]1585                                                unset( $nodes[$hostname] );
1586                                        }
1587                                        $x      = 1;
1588                                }
[329]1589
[463]1590                                // Determine the lowest value of {x} that exists in all node hostnames
1591                                //
[404]1592                                if( !$x_min && $x != null )
[329]1593                                {
1594                                        $x_min  = $x;
1595                                }
[404]1596                                else if( $x < $x_min && $x != null )
[329]1597                                {
1598                                        $x_min  = $x;
1599                                }
[463]1600
1601                                // Determine the highest value of {x} that exists in all node hostnames
1602                                //
[404]1603                                if( !$x_max && $x != null )
[329]1604                                {
1605                                        $x_max  = $x;
1606                                }
[404]1607                                else if( $x > $x_max && $x != null )
[329]1608                                {
1609                                        $x_max  = $x;
1610                                }
[463]1611
1612                                // Determine the lowest value of {y} that exists in all node hostnames
1613                                //
[404]1614                                if( !$y_min && $y != null )
[329]1615                                {
1616                                        $y_min  = $y;
1617                                }
[404]1618                                else if( $y < $y_min && $y != null )
[329]1619                                {
1620                                        $y_min  = $y;
1621                                }
[463]1622
1623                                // Determine the highest value of {y} that exists in all node hostnames
1624                                //
[404]1625                                if( !$y_max && $y != null )
[329]1626                                {
1627                                        $y_max  = $y;
1628                                }
[404]1629                                else if( $y > $y_max && $y != null )
[329]1630                                {
1631                                        $y_max  = $y;
1632                                }
1633                        }
1634
[463]1635                        // Sort all the nodes (alpha and numerically)
1636                        // 1: gb-r1n1, 2: gb-r1n2, 3: gb-r2n1, etc
1637                        //
[329]1638                        $sorted_nodes   = usort( $nodes, "cmp" );
1639
1640                        $cur_node       = 0;
1641
[331]1642                        $x_offset       = 0;
1643                        $y_offset       = 0;
1644                        $font           = 2;
1645                        $fontwidth      = ImageFontWidth( $font );
1646                        $fontheight     = ImageFontHeight( $font );
1647                        $fontspaceing   = 2;
1648
[333]1649                        if( $this->isSmall() ) 
1650                        {
1651                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1652                        }
1653
[331]1654                        if( $this->isBig() ) 
1655                        {
1656
1657                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1658                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1659
1660                        }
1661
[330]1662                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
[428]1663
1664                        if( $this->isSmall() ) 
[340]1665                        {
[428]1666                                $image_width    = $max_width;
[519]1667                        }
1668                        else if( $this->isBig() ) 
[340]1669                        {
[428]1670                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
[340]1671                        }
[330]1672                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1673
1674                        $this->width    = $image_width;
1675                        $this->heigth   = $image_heigth;
1676
1677                        $image          = imageCreateTrueColor( $image_width, $image_height );
[329]1678                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1679
1680                        imageFill( $image, 0, 0, $colorwhite );
1681
[519]1682                        if( $this->isSmall() )
1683                        {
[463]1684                                // Draw a fancy little header text to explain what it is
1685                                //
[333]1686                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1687
1688                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1689                        }
1690
[339]1691                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
[331]1692                        {
1693                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[329]1694
[339]1695                                if( isset( $SORT_XLABEL ) )
1696                                {
[463]1697                                        // Print the {x} label: rack
1698                                        //
[339]1699                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1700                                }
[329]1701
[339]1702                                if( isset( $SORT_YLABEL ) )
1703                                {
[463]1704                                        // Stupid php without imageStringDown function... we'll make one ourself
[339]1705                                        //
[463]1706
1707                                        // Print the {y} label: node
1708                                        //
[339]1709                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1710                                }
[331]1711                        }
[329]1712
1713                        for( $n = $x_min; $n <= $x_max; $n++ )
1714                        {
1715                                for( $m = $y_min; $m <= $y_max; $m++ )
1716                                {
[463]1717
[329]1718                                        if( $x_min > 0 )
1719                                        {
1720                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1721                                        }
1722                                        if( $y_min > 0 )
1723                                        {
1724                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1725                                        }
1726
[463]1727                                        if( $this->isBig() ) 
1728                                        {
1729                                                // Draw y(node) column number header
1730                                                //
1731                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1732                                                {
1733                                                        $mfontspacing   = 1;
1734
1735                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1736                                                        $ylabel_y       = $y;
1737
1738                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1739
1740                                                        $xmin_hit[$n]   = true;
1741                                                }
1742
1743                                                // Draw x(rack) column number header
1744                                                //
1745                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1746                                                {
1747                                                        $mfontspacing   = 2;
1748                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1749                                                        $xlabel_x       = $x + $mfontspacing; 
1750
1751                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1752                                                }
1753                                        }
1754
[330]1755                                        if( isset( $nodes[$cur_node] ) ) 
1756                                        {
[329]1757                                                $host   = $nodes[$cur_node]->getHostname();
1758
[339]1759                                                if( $x_present && $y_present )
[329]1760                                                {
[339]1761                                                        if( $x_first )
1762                                                        {
1763                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1764                                                        }
1765                                                        else if( $y_first )
1766                                                        {
1767                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1768                                                        }
1769                                                        if ( $nn < 2 )
1770                                                        {
[404]1771                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
[339]1772                                                                continue;
1773                                                        }
[404]1774                                                        if( intval( $rx ) > $n )
[339]1775                                                        {
[463]1776                                                                // If x(rack) is higher than current x, skip to next x(rack)
1777                                                                //
1778                                                                $m              = $y_max + 1;
1779
[339]1780                                                                continue;
1781                                                        }
[404]1782                                                        if( intval( $ry ) > $m )
1783                                                        {
[463]1784                                                                // If y(node) is higher than current y, skip to next y(node)
1785                                                                //
[404]1786                                                                continue;
1787                                                        }
[329]1788                                                }
[339]1789                                                else if( $x_present )
[329]1790                                                {
[339]1791                                                        $nn = sscanf( $host, $skan_str, $rx );
[329]1792                                                }
[339]1793                                                else if( $y_present )
[329]1794                                                {
[339]1795                                                        $nn = sscanf( $host, $skan_str, $ry );
[329]1796                                                }
1797
1798                                                if( !in_array( $host, $filtered_nodes ) )
[404]1799                                                {
[463]1800                                                        // This node has been filtered out: we only want to see certain nodes
1801                                                        //
[329]1802                                                        $nodes[$cur_node]->setShowinfo( 0 );
[404]1803                                                }
[329]1804
1805                                                $nodes[$cur_node]->setCoords( $x, $y );
1806                                                $nodes[$cur_node]->setImage( $image );
1807
1808                                                if( $this->isSmall() )
[519]1809                                                {
[329]1810                                                        $nodes[$cur_node]->drawSmall();
[519]1811                                                }
[329]1812                                                else if( $this->isBig() )
[519]1813                                                {
[329]1814                                                        $nodes[$cur_node]->drawBig();
[519]1815                                                }
[404]1816
1817                                                $cur_node++;
[329]1818                                        }
1819                                }
1820                        }
1821
1822                }
1823                else
1824                {
[519]1825                        if( $this->isSmall() )
1826                        {
[428]1827                                $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
[519]1828                        }
1829                        else if( $this->isBig() )
1830                        {
[428]1831                                $image_width    = ($node_width * $nodes_nr) + 2;
1832                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1833                                $image          = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1834                        }
[329]1835                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1836
1837                        imageFill( $image, 0, 0, $colorwhite );
1838
[519]1839                        if( $this->isSmall() )
1840                        {
[329]1841                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1842
1843                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1844                        }
1845
[519]1846                        for( $n = 0; $n < $node_rows; $n++ )
1847                        {
1848                                for( $m = 0; $m < $nodes_per_row; $m++ )
1849                                {
[329]1850                                        $x = ($m * $node_width);
1851                                        $y = $y_offset + ($n * $node_width);
[107]1852
[329]1853                                        $cur_node = ($n * $nodes_per_row) + ($m);
1854                                        $host = $nodes_hosts[$cur_node];
[107]1855
[519]1856                                        if( isset( $nodes[$host] ) )
1857                                        {
[329]1858                                                $nodes[$host]->setCoords( $x, $y );
1859                                                $nodes[$host]->setImage( $image );
[122]1860
[329]1861                                                if( !in_array( $host, $filtered_nodes ) )
[404]1862                                                {
[329]1863                                                        $nodes[$host]->setShowinfo( 0 );
[404]1864                                                }
[122]1865
[329]1866                                                if( $this->isSmall() )
[519]1867                                                {
[329]1868                                                        $nodes[$host]->drawSmall();
[519]1869                                                }
[329]1870                                                else if( $this->isBig() )
[519]1871                                                {
[329]1872                                                        $nodes[$host]->drawBig();
[519]1873                                                }
[329]1874                                        }
[111]1875                                }
[106]1876                        }
1877                }
[326]1878       
1879                $this->nodes    = &$nodes;
1880
[519]1881                if ($this->output)
1882                {
[326]1883                        header( 'Content-type: image/png' );
1884                        imagePNG( $image );
1885                        imageDestroy( $image );
1886                }
[102]1887        }
[326]1888
[519]1889        function getImagemapArea()
1890        {
[326]1891                $clusterimage_map       = "";
1892
[519]1893                foreach( $this->nodes as $hostname => $node )
1894                {
[326]1895                        $node_map               = $node->getImagemapArea();
1896                        $clusterimage_map       .= $node_map;
1897                }
1898
1899                return $clusterimage_map;
1900        }
[102]1901}
1902
[519]1903class EmptyImage
1904{
1905        function draw()
1906        {
[298]1907                $image          = imageCreateTrueColor( 1, 1 );
1908                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1909                imageFill( $image, 0, 0, $colorwhite );                         
1910
1911                header( 'Content-type: image/png' );
1912                imagePNG( $image );
1913                imageDestroy( $image );
1914        }
1915}
1916
[519]1917class HostImage
1918{
[303]1919        var $data_gather, $cluster, $host, $node, $image;
1920        var $headerstrlen;
1921
[519]1922        function HostImage( $data_gather, $cluster, $host )
1923        {
[303]1924                $this->data_gather      = $data_gather;
1925                $this->cluster          = $cluster;
1926                $this->host             = $host;
1927                $this->y_offset         = 0;
1928                $this->font             = 2;
1929                $this->fontspaceing     = 2;
1930                $this->headerstrlen     = array();
1931
1932                $this->fontheight       = ImageFontHeight( $this->font );
1933                $this->fontwidth        = ImageFontWidth( $this->font );
1934
1935                $dg                     = &$this->data_gather;
1936                $this->node             = &$dg->getNode( $this->host );
1937                $n                      = &$this->node;
1938                $this->njobs            = $n->getJobs();
1939        }
1940
[519]1941        function drawJobs()
1942        {
[303]1943                $dg                     = &$this->data_gather;
1944                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1945
[519]1946                for( $n = 0; $n < count( $this->njobs ); $n++ )
1947                {
[303]1948                        $jobid                  = $this->njobs[$n];
1949                        $jobinfo                = $dg->getJob( $jobid );
1950
1951                        $xoffset                = 5;
1952                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1953
[519]1954                        foreach( $this->headerstrlen as $headername => $headerlen )
1955                        {
1956                                if( $headername == 'nodes' )
1957                                {
[303]1958                                        $attrval        = strval( count( $jobinfo[nodes] ) );
[519]1959                                }
1960                                else if( $headername == 'cpus' )
1961                                {
[303]1962                                        if( !isset( $jobinfo[ppn] ) )
[519]1963                                        {
[303]1964                                                $jobinfo[ppn] = 1;
[519]1965                                        }
[303]1966
1967                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
[519]1968                                }
1969                                else if( $headername == 'runningtime' )
1970                                {
[303]1971                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
[519]1972                                }
1973                                else
1974                                {
[303]1975                                        $attrval        = strval( $jobinfo[$headername] );
1976                                }
1977
1978                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1979               
1980                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1981                        }
1982                       
1983                        $this->newLineOffset();
1984                }
1985        }
1986
[519]1987        function drawHeader()
1988        {
[303]1989                $dg                     = &$this->data_gather;
1990
[519]1991                for( $n = 0; $n < count( $this->njobs ); $n++ )
1992                {
[303]1993                        $jobid                  = $this->njobs[$n];
1994                        $jobinfo                = $dg->getJob( $jobid );
1995
1996                        if( !isset( $this->headerstrlen[id] ) )
[519]1997                        {
[303]1998                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
[519]1999                        }
2000                        else if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
2001                        {
2002                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
2003                        }
[303]2004
2005                        if( !isset( $this->headerstrlen[owner] ) )
[519]2006                        {
[303]2007                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
[519]2008                        }
2009                        else if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
2010                        {
2011                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
2012                        }
[303]2013
2014                        if( !isset( $this->headerstrlen[queue] ) )
[519]2015                        {
[303]2016                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
[519]2017                        }
2018                        else if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
2019                        {
2020                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
2021                        }
[303]2022
2023                        if( !isset( $jobinfo[ppn] ) )
[519]2024                        {
[303]2025                                $jobinfo[ppn] = 1;
[519]2026                        }
[303]2027
2028                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
2029
2030                        if( !isset( $this->headerstrlen[cpus] ) )
[519]2031                        {
[303]2032                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
[519]2033                        }
2034                        else if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
2035                        {
2036                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
2037                        }
[303]2038
2039                        $nodes                  = count( $jobinfo[nodes] );
2040
2041                        if( !isset( $this->headerstrlen[nodes] ) )
[519]2042                        {
[303]2043                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
[519]2044                        }
2045                        else if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
2046                        {
2047                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
2048                        }
[303]2049
2050                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
2051
2052                        if( !isset( $this->headerstrlen[runningtime] ) )
[519]2053                        {
[303]2054                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
[519]2055                        }
2056                        else if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
2057                        {
2058                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
2059                        }
[303]2060
2061                        if( !isset( $this->headerstrlen[name] ) )
[519]2062                        {
[303]2063                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
[519]2064                        }
2065                        else if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
2066                        {
2067                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
2068                        }
[303]2069                }
2070
2071                $xoffset        = 5;
2072
[519]2073                foreach( $this->headerstrlen as $headername => $headerlen )
2074                {
[303]2075                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
2076
2077                        if( $headerlen < strlen( $headername ) )
[519]2078                        {
[350]2079                                $this->headerstrlen[$headername]        = strlen( $headername );
[519]2080                        }
[303]2081
[426]2082                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
[303]2083
[426]2084                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
[303]2085                }
2086                $this->newLineOffset();
2087        }
2088
[519]2089        function newLineOffset()
2090        {
[303]2091                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
2092        }
2093
[519]2094        function draw()
2095        {
[303]2096                $xlen           = 450;
2097                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
2098
2099                $this->image    = imageCreateTrueColor( $xlen, $ylen );
2100                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
2101                imageFill( $this->image, 0, 0, $colorwhite );                         
2102
2103                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
2104
2105                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
2106                $this->newLineOffset();
2107
2108                $this->drawHeader();
2109                $this->drawJobs();
2110
2111                header( 'Content-type: image/png' );
2112                imagePNG( $this->image );
2113                imageDestroy( $this->image );
2114        }
2115}
2116
[331]2117function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
2118{
2119        $fw     = imagefontwidth( $font);
2120        $fh     = imagefontheight( $font);
2121       
2122        $fontspacing = 0;
2123
2124        $fx     = $x;
2125        $fy     = $y;
2126
2127        for( $n=0; $n<strlen( $s ); $n++ )
2128        {
2129                $myc    = $s{$n};
2130
2131                imagestring( $image, $font, $fx, $fy, $myc, $col );
2132
2133                $fy     += ($fontspacing + $fh );
2134        }
2135}
2136
[329]2137function array_rem( $val, &$arr )
2138{
2139        // Delete val from arr
2140        //
2141        $i      = array_search( $val, $arr );
2142
2143        if( $i == false ) return false;
2144
2145        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
2146
2147        return true;
2148}
2149
2150function cmp( $a, $b ) 
2151{
2152        global $SORT_ORDER;
2153        global $skan_str;
2154        global $x_first, $y_first;
[339]2155        global $x_present, $y_present;
[329]2156
2157        $a_node         = $a;
2158        $b_node         = $b;
2159        $a              = $a_node->getHostname();
2160        $b              = $b_node->getHostname();
2161
2162        if( $a == $b ) return 0;
2163
[339]2164        $a_x            = 0;
2165        $b_x            = 0;
2166        $a_y            = 0;
2167        $b_y            = 0;
2168
2169        if( $x_present && $y_present )
[329]2170        {
[339]2171                if( $x_first )
2172                {
2173                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
2174                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
2175                }
2176                else if( $y_first )
2177                {
2178                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
2179                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
2180                }
2181        } 
2182        else if( $x_present && !$y_present )
2183        {
2184                $n = sscanf( $a, $skan_str, $a_x );
2185                $n = sscanf( $b, $skan_str, $b_x );
[329]2186        }
[339]2187        else if( $y_present && !$x_present )
[329]2188        {
[339]2189                $n = sscanf( $a, $skan_str, $a_y );
2190                $n = sscanf( $b, $skan_str, $b_y );
[329]2191        }
2192
2193        if ( $SORT_ORDER=="desc" )
2194        {
2195
[339]2196                if( $x_present && $y_present )
[329]2197                {
[339]2198                        // 1  = a < b
2199                        // -1 = a > b
2200                        //
2201                        if ($a_x == $b_x)
[329]2202                        {
[339]2203                                if ($a_y < $b_y)
2204                                {
2205                                        return 1;
2206                                }
2207                                else if ($a_y > $b_y)
2208                                {
2209                                        return -1;
2210                                }
2211                        }
2212                        else if ($a_x < $b_x)
2213                        {
[329]2214                                return 1;
2215                        }
[339]2216                        else if ($a_x > $b_x)
[329]2217                        {
2218                                return -1;
2219                        }
[339]2220                } 
2221                else if( $x_present && !$y_present )
[329]2222                {
[339]2223                        if ($a_x < $b_x)
2224                        {
2225                                return 1;
2226                        }
2227                        else if ($a_x > $b_x)
2228                        {
2229                                return -1;
2230                        }
[329]2231                }
[339]2232                else if( $y_present && !$x_present )
[329]2233                {
[339]2234                        if ($a_y < $b_y)
2235                        {
2236                                return 1;
2237                        }
2238                        else if ($a_y > $b_y)
2239                        {
2240                                return -1;
2241                        }
[329]2242                }
2243        }
2244        else if ( $SORT_ORDER == "asc" )
2245        {
2246
[339]2247                if( $x_present && $y_present )
[329]2248                {
[339]2249                        // 1  = a > b
2250                        // -1 = a < b
2251                        //
2252                        if ($a_x == $b_x)
[329]2253                        {
[339]2254                                if ($a_y > $b_y)
2255                                {
2256                                        return 1;
2257                                }
2258                                else if ($a_y < $b_y)
2259                                {
2260                                        return -1;
2261                                }
2262                        }
2263                        else if ($a_x > $b_x)
2264                        {
[329]2265                                return 1;
2266                        }
[339]2267                        else if ($a_x < $b_x)
[329]2268                        {
2269                                return -1;
2270                        }
2271                }
[339]2272                else if( $x_present && !$y_present )
[329]2273                {
[339]2274                        if ($a_x > $b_x)
2275                        {
2276                                return 1;
2277                        }
2278                        else if ($a_x < $b_x)
2279                        {
2280                                return -1;
2281                        }
[329]2282                }
[339]2283                else if( $y_present && !$x_present )
[329]2284                {
[339]2285                        if ($a_y > $b_y)
2286                        {
2287                                return 1;
2288                        }
2289                        else if ($a_y < $b_y)
2290                        {
2291                                return -1;
2292                        }
[329]2293                }
2294        }
2295}
[519]2296function makeTime( $time )
2297{
[303]2298        $days = intval( $time / 86400 );
2299        $time = ($days>0) ? $time % ($days * 86400) : $time;
2300
2301        $date_str = '';
2302        $day_str = '';
2303
[519]2304        if( $days > 0 )
2305        {
[303]2306                if( $days > 1 )
[519]2307                {
[303]2308                        $day_str .= $days . ' days';
[519]2309                }
[303]2310                else
[519]2311                {
[303]2312                        $day_str .= $days . ' day';
[519]2313                }
[303]2314        }
2315
2316        $hours = intval( $time / 3600 );
2317        $time = $hours ? $time % ($hours * 3600) : $time;
2318
[519]2319        if( $hours > 0 )
2320        {
[303]2321                $date_str .= $hours . ':';
2322                $date_unit = 'hours'; 
2323        }
2324
2325        $minutes = intval( $time / 60 );
2326        $seconds = $minutes ? $time % ($minutes * 60) : $time;
2327
[519]2328        if( $minutes > 0 )
2329        {
[303]2330                if( $minutes >= 10 )
[519]2331                {
[303]2332                        $date_str .= $minutes . ':';
[519]2333                }
[303]2334                else
[519]2335                {
[303]2336                        $date_str .= '0' . $minutes . ':';
[519]2337                }
[303]2338
2339                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
[519]2340        }
2341        else
2342        {
2343                if($hours > 0 )
2344                {
[303]2345                        $date_str .= '00:';
2346                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2347                }
2348        }
2349
2350        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
2351
[519]2352        if( $seconds > 0 )
2353        {
[303]2354                if( $seconds >= 10 )
[519]2355                {
[303]2356                        $date_str .= $seconds . ' ' . $date_unit;
[519]2357                }
[303]2358                else
[519]2359                {
[303]2360                        $date_str .= '0' . $seconds . ' ' . $date_unit;
[519]2361                }
2362        }
2363        else if ( $hours > 0 or $minutes > 0 )
2364        {
[303]2365                $date_str .= '00 ' . $date_unit;
[519]2366        }
[303]2367
[519]2368        if( $days > 0)
2369        {
[303]2370                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
[519]2371                {
[303]2372                        $date_str = $day_str . ' - ' . $date_str;
[519]2373                }
[303]2374                else
[519]2375                {
[303]2376                        $date_str = $day_str;
[519]2377                }
[303]2378        }
2379        return $date_str;
2380}
[102]2381?>
Note: See TracBrowser for help on using the repository browser.