
package Support::LoadHistory::DailyLoadGraph;
use strict;

use lib '/app/tools/common/lib';
use Common::XMLObject;
use Common::Assert;

use lib '/usr/local/chart_director/lib';
use perlchartdir;

use base 'Common::XMLObject';

use constant kLoadReportPath => '/app/data/load_logs';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    my $file = $args{file};
    assert($file);

    my @data;
    my @labels;

    open( LOGFILE, kLoadReportPath . "/$file" ) or die "ERROR - could not open load file $file";

    while ( my $line = <LOGFILE> ) {
        chomp $line;

        # All I want is the time and the 1 minute load avg.
        if ( $line =~ /^ (\d+:\d+:\d+).*average: (\d+\.\d+)/ ) {
            my $time = $1;
            my $load = $2;

            push @data, $load;
            my ( $h, $m, $s ) = split( ':', $time );

            if ( '00' eq $m ) {
                push @labels, $time;
            } else {
                push @labels, '';
            }
        }
    }

    my $c = new XYChart( 1000, 600 );
    $c->setPlotArea( 30, 20, 950, 500 );

    #Add a bar chart layer using the given data
    $c->addBarLayer( \@data );

    #Set the labels on the x axis.
    $c->xAxis()->setLabelStyle( "arial.ttf", 8 )->setFontAngle(90);
    $c->xAxis()->setLabels( \@labels );

    #output the chart
    $self->{_chart} = $c;

    return $self;
}

sub draw {
    my ( $self, $dir, $file ) = @_;

    assert($dir);

    my $chart = $self->{_chart};

    my $filename;
    if ( !$file ) {
        $filename = $chart->makeTmpFile($dir);
    }

    return $filename;
}

1;
