ASNMTAP

 view release on metacpan or  search on metacpan

applications/htmlroot/cgi-bin/generateChart.pl  view on Meta::CPAN

#!/usr/bin/env perl
# ---------------------------------------------------------------------------------------------------------
# © Copyright 2003-2011 Alex Peeters [alex.peeters@citap.be]
# ---------------------------------------------------------------------------------------------------------
# 2011/mm/dd, v3.002.003, generateChart.pl for ASNMTAP::Asnmtap::Applications::CGI
# ---------------------------------------------------------------------------------------------------------

use strict;
use warnings;           # Must be used in test mode only. This reduces a little process speed
#use diagnostics;       # Must be used in test mode only. This reduces a lot of process speed

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

BEGIN { if ( $ENV{ASNMTAP_PERL5LIB} ) { eval 'use lib ( "$ENV{ASNMTAP_PERL5LIB}" )'; } }

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

use CGI;
use DBI;
use Time::Local;
use Date::Calc qw(Add_Delta_Days Date_to_Text_Long Delta_Days);

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

use ASNMTAP::Asnmtap::Applications::CGI v3.002.003;
use ASNMTAP::Asnmtap::Applications::CGI qw(:APPLICATIONS :CGI :REPORTS :DBREADONLY :DBTABLES);

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

use lib ( "$CHARTDIRECTORLIB" );
use perlchartdir;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

use vars qw($PROGNAME);

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$PROGNAME       = "generateChart.pl";
my $prgtext     = "Generate Chart";
my $version     = do { my @r = (q$Revision: 3.002.003$ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r }; # must be all on one line or MakeMaker will get confused.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

my ($rv, $dbh, $sth, $sql, $sqlPeriode, $debugMessage, $errorMessage, $dbiErrorCode, $dbiErrorString);
my ($background, $forGround, $axisColor, $numberOfDays, $numberOfLabels, $dummy, $trendvalue, $chartTitle);
my ($endDateIN, $i, $j, $yearFrom, $monthFrom, $dayFrom, $yearTo, $monthTo, $dayTo, $goodDate, $slaWindow);

my (@avg1, @avg2, @avg3, @data, @dataOK, @dataWarning, @dataCritical, @dataUnknown, @dataNoTest, @dataOffline);
my (@icons, @labels, @labels1, @labels2, @labels3, $applicationTitle1, $applicationTitle2, $applicationTitle3);

my ($currentYear, $currentMonth, $currentDay) = (((localtime)[5] + 1900), ((localtime)[4] + 1), (localtime)[3]);

$applicationTitle1 = $applicationTitle2 = $applicationTitle3 = '<NIHIL>';
my @arrMonths      = qw(January Februari March April May June July August September October November December);

# Chart Parameters
my $width		= 1000; 	 # graph width
my $hight       = 380;       # graph height
my $xOffset     = 74;        # x offset grapharea
my $yOffset     = 28;        # y offset grapharea
my $AreaBOffset = 78;
my $trendZone   = 0xFFFF99;

# URL Access Parameters
my $cgi = new CGI;
my $pagedir      = (defined $cgi->param('pagedir'))      ? $cgi->param('pagedir')      : 'index';    $pagedir =~ s/\+/ /g;
my $pageset      = (defined $cgi->param('pageset'))      ? $cgi->param('pageset')      : 'index-cv'; $pageset =~ s/\+/ /g;
my $debug        = (defined $cgi->param('debug'))        ? $cgi->param('debug')        : 'F';
my $sessionID    = (defined $cgi->param('CGISESSID'))    ? $cgi->param('CGISESSID')    : '';
my $selChart     = (defined $cgi->param('chart'))        ? $cgi->param('chart')        : 'ErrorDetails';
my $CcatalogID   = (defined $cgi->param('catalogID'))    ? $cgi->param('catalogID')    : $CATALOGID;
my $uKey1        = (defined $cgi->param('uKey1'))        ? $cgi->param('uKey1')        : 'none';
my $uKey2        = (defined $cgi->param('uKey2'))        ? $cgi->param('uKey2')        : 'none';
my $uKey3        = (defined $cgi->param('uKey3'))        ? $cgi->param('uKey3')        : 'none';
my $startDateIN  = (defined $cgi->param('startDate'))    ? $cgi->param('startDate')    : 'none';
my $inputType    = (defined $cgi->param('inputType'))    ? $cgi->param('inputType')    : 'none';
my $selQuarter   = (defined $cgi->param('quarter'))      ? $cgi->param('quarter')      : 0;
my $selMonth     = (defined $cgi->param('month'))        ? $cgi->param('month')        : 0;
my $selWeek      = (defined $cgi->param('week'))         ? $cgi->param('week')         : 0;
my $selYear      = (defined $cgi->param('year'))         ? $cgi->param('year')         : 0;
my $timeperiodID = (defined $cgi->param('timeperiodID')) ? $cgi->param('timeperiodID') : 1;
my $pf           = (defined $cgi->param('pf'))           ? $cgi->param('pf')           : 'off';

# set: endDate
$endDateIN = $cgi->param('endDate') if ( $cgi->param('endDate') ne '' );

# set: debug
if ( $debug eq 'T' ) {
  $debugMessage = "chart: $selChart, uKey1: $uKey1, uKey2: $uKey2, uKey3: $uKey3, startDate: $startDateIN, endDate: $endDateIN, inputType: $inputType, selMonth: $selMonth, selWeek: $selWeek, selYear: $selYear.";
  $AreaBOffset += 18;
}

# set: colors
if ($pf eq 'on') {
  $background = 0xF7F7F7;
  $forGround  = 0x000000;
  $axisColor  = 0x0C0C0C;
} else {
  $background = 0x000000;
  $forGround  = 0xF7F7F7;
  $axisColor  = 0x0000FF;
}

# set: forceIndex
my $forceIndex = "force index (key_startDate)"; $forceIndex = '';

# Init return value to true
$rv = 1;

# Chart specific settings
if ( $selChart eq "Status" ) {
  $yOffset     = 0;
  $AreaBOffset = 0;
} elsif ( $selChart eq "ErrorDetails" ) {
  $hight       = 500;
  $yOffset     = 0;
  $AreaBOffset = 0;
} elsif ( $selChart eq "HourlyAverage" ) {
  my ($tmpStartDate, $tmpEndDate);
  ($goodDate, $tmpStartDate, $tmpEndDate, $numberOfDays) = get_sql_startDate_sqlEndDate_numberOfDays_test ($STRICTDATE, $FIRSTSTARTDATE, $inputType, $selYear, $selQuarter, $selMonth, $selWeek, $startDateIN, $endDateIN, $currentYear, $currentMonth, $c...

  unless ( $goodDate ) {
    $rv = 0; $errorMessage = "Wrong Startdate and/or Enddate";
  } elsif ( $numberOfDays > 7 ) {
    $rv = 0; $errorMessage = "Hourly Average Not Available";
  } else {
    ($yearFrom, $monthFrom, $dayFrom) = split (/-/, $tmpStartDate) if (defined $tmpStartDate);
    ($yearTo, $monthTo, $dayTo) = split (/-/, $tmpEndDate) if (defined $tmpEndDate);
  }
}

if ( $rv ) {
  # open connection to database and query data
  $dbh = DBI->connect("DBI:mysql:$DATABASE:$SERVERNAMEREADONLY:$SERVERPORTREADONLY", "$SERVERUSERREADONLY", "$SERVERPASSREADONLY" ) or ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = error_trap_DBI("Cannot connect to the database", $debug, '',...

  if ( $dbh and $rv ) {
    if ( $uKey1 eq 'none' ) {
      $rv = 0; $errorMessage = "URL Access Parameters Error: Application 1";
    } else {
      ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString, $applicationTitle1, $trendvalue, undef) = get_title( $dbh, $rv, $CcatalogID, $uKey1, $debug, 0, $sessionID );
    }

    if ( $rv ) { if ( $uKey2 ne 'none' ) { ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString, $applicationTitle2, undef, undef) = get_title( $dbh, $rv, $CcatalogID, $uKey2, $debug, 0, $sessionID ); } }
    if ( $rv ) { if ( $uKey3 ne 'none' ) { ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString, $applicationTitle3, undef, undef) = get_title( $dbh, $rv, $CcatalogID, $uKey3, $debug, 0, $sessionID ); } }

    if ( $rv ) {
      if ( $uKey1 eq 'none' and $applicationTitle1 eq '<NIHIL>' ) {
        $rv = 0; $errorMessage = "URL Access Parameters Error: Application 1: $applicationTitle1";
      } else {
        if ( $uKey2 eq 'none' and $applicationTitle2 eq '<NIHIL>' ) {
          if ( $selChart eq "Status" ) {
            $chartTitle = "Status";
          } elsif ( $selChart eq "ErrorDetails" ) {
            $chartTitle = "Error Details";
          } elsif ( $selChart eq "Bar" ) {
            $chartTitle = "Bar";
          } elsif ( $selChart eq "HourlyAverage" ) {
            $chartTitle = "Hourly Average";
          } elsif ( $selChart eq "DailyAverage" ) {

applications/htmlroot/cgi-bin/generateChart.pl  view on Meta::CPAN

              }

              $sth->finish() or ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = error_trap_DBI("", "Cannot sth->finish", $debug, '', "", '', "", 0, '', $sessionID);
            }
          } elsif ( $selChart eq "HourlyAverage" or $selChart eq "DailyAverage" ) {
            ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = getAverage( 1, $dbh, $rv, $uKey1, $sqlStartDate, $sqlEndDate, $sqlPeriode, $selChart, $debug );
          }
        }

        if ( $rv and $uKey2 ne 'none' ) {
          if ( $selChart eq "HourlyAverage" or $selChart eq "DailyAverage" ) {
            ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = getAverage( 2, $dbh, $rv, $uKey2, $sqlStartDate, $sqlEndDate, $sqlPeriode, $selChart, $debug );
          }
        }

        if ( $rv and $uKey3 ne 'none' ) {
          if ( $selChart eq "HourlyAverage" or $selChart eq "DailyAverage" ) {
            ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = getAverage( 3, $dbh, $rv, $uKey3, $sqlStartDate, $sqlEndDate, $sqlPeriode, $selChart, $debug );
          }
        }

        if ( $rv ) {
          if ( $selChart eq "HourlyAverage" ) {
            my @refenentieLabels;
	
            for($i = 0; $i < $numberOfDays; ++$i) {
	          for ($j = 0; $j <= 23; ++$j) {
                my ($Tyear, $Tmonth, $Tday) = Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, $i);
	            push (@refenentieLabels, "$Tyear-$Tmonth-$Tday:$j");
                push (@labels,  $j);
              }
            }

            $width = ( $numberOfDays > 1 ) ? 24 * $numberOfDays * 25 : $width;

            @avg1 = rebuildDataArrayHourlyAverage (\@refenentieLabels, \@labels1, \@avg1);
            @avg2 = rebuildDataArrayHourlyAverage (\@refenentieLabels, \@labels2, \@avg2) if ($uKey2 ne 'none');
            @avg3 = rebuildDataArrayHourlyAverage (\@refenentieLabels, \@labels3, \@avg3) if ($uKey3 ne 'none');
          } elsif ( $selChart eq "DailyAverage" ) {
            my $prev = "2003-10-12";
            @labels = sort(@labels1, @labels2, @labels3);
            @labels = grep($_ ne $prev && ($prev = $_), @labels);
            $numberOfLabels = scalar(@labels);
            $width = ( $numberOfLabels > 40 ) ? $numberOfLabels * 25 : $width;

            @avg1 = rebuildDataArrayDailyAverage (\@labels, \@labels1, \@avg1);
            @avg2 = rebuildDataArrayDailyAverage (\@labels, \@labels2, \@avg2) if ($uKey2 ne 'none');
            @avg3 = rebuildDataArrayDailyAverage (\@labels, \@labels3, \@avg3) if ($uKey3 ne 'none');
          }
        }
	    }
    }

    $dbh->disconnect or ($rv, $errorMessage, $dbiErrorCode, $dbiErrorString) = error_trap_DBI("Sorry, the database was unable to disconnect", $debug, '', "", '', "", '', 0, '', $sessionID);
  }
}

my $c;

if ( $selChart eq "Status" or $selChart eq "ErrorDetails" ) {
  # Create Piehart object with: width, hight, backgroundcolor, ...
  $c = new PieChart($width, $hight, $background, -1, 0);

  # Set the center of the pie at (<>, <>) and the radius to <> pixels
  $c->setPieSize($width/2, $hight/2, 130);

  # Draw the pie in 3D
  $c->set3D();
} else {
  # Create XYChart object with: width, hight, backgroundcolor, bordercolor, pxp-3d borden
  $c = new XYChart($width, $hight, $background, $background, 0);

  # Set the plotarea at (xOffset, yOffset) and of size $width - 95 x $hight - 78 pixels, with white background. Set border and grid line colors to 0xa08040.
  $c->setPlotArea($xOffset, $yOffset, $width - 95, $hight - $AreaBOffset, 0xffffff, -1, 0xaCCCCCC, 0xaCCCCCC, 0xaCCCCCC);

  # Set the axes width to 1 pixels
  $c->xAxis()->setWidth(1);
  $c->yAxis()->setWidth(1);

  # Set the axis colors
  $c->xAxis()->setColors($axisColor);
  $c->yAxis()->setColors($axisColor);

  # Add a title on the the y axis
  my $yAxisTitle = ($selChart eq 'Bar') ? 'Response time' : 'Average response time';
  $c->yAxis()->setTitle($yAxisTitle, "arial.ttf", 9, $forGround);

  # Add a title on the the x axis
  $c->xAxis()->setTitle("<*block,valign=absmiddle*>Date/Time<*/*>")->setFontColor($forGround);
}

# Add a title box to the chart using 10 pts Arial Bold Italic font.
$chartTitle = "Error for '$prgtext'" unless ( defined $chartTitle and $rv );
$c->addText($width/2, 14, $chartTitle, "arialbi.ttf", 10, $forGround, 5, 0);

# Add debugMessage and errorMessage
$c->addText($width - 18, $hight - 33, $debugMessage, "arial.ttf", 8, $forGround, 6, 0) if ( defined $debugMessage );
$c->addText($width/2, (($hight - $yOffset - $AreaBOffset)/2) + $yOffset + 16, $errorMessage, "arial.ttf", 12, 0xFF0000, 5, 0) if ( defined $errorMessage );
$c->addText($width/2, (($hight - $yOffset - $AreaBOffset)/2) + $yOffset - 16, $dbiErrorCode, "arial.ttf", 10, 0xFF0000, 5, 0) if ( defined $dbiErrorCode );
$c->addText($width/2, (($hight - $yOffset - $AreaBOffset)/2) + $yOffset + 48, $dbiErrorString, "arial.ttf", 10, 0xFF0000, 5, 0) if ( defined $dbiErrorString );

# Add a custom CDML text at the bottom right of the plot area as the logo
$c->addText($width - 3, 92, $APPLICATION . " @ " . $BUSINESS, "arial.ttf", 8, $forGround, 6, 270);
$c->addText($width - 18, $hight - 18, $DEPARTMENT . " @ " . $BUSINESS . ", created on: " . scalar(localtime()) . ".", "arial.ttf", 8, $forGround, 6, 0);

if ( $rv ) {
  if ( $selChart eq "Status" or $selChart eq "ErrorDetails" ) {
    # use given color array as the data colors (sector colors)
    if ( $selChart eq "Status" ) {
      my (@colors);
      foreach my $label (@labels) { push (@colors, $COLORSPIE {$label} ); }
      $c->setColors2($perlchartdir::DataColor, \@colors);

      # Add icons to the chart as a custom field
      $c->addExtraField(\@icons);
    } elsif ( $selChart eq "ErrorDetails" ) {
      my $colors = [0xb8bc9c, 0xecf0b9, 0x999966, 0x333366, 0xc3c3e6, 0x594330,0xa0bdc4];
      $c->setColors2($perlchartdir::DataColor, $colors);
    }

    # Use the side label layout method
    $c->setLabelLayout($perlchartdir::SideLayout);
    $c->setLabelStyle("tahoma.ttf", 8, $forGround)->setBackground($perlchartdir::Transparent, $perlchartdir::Transparent, 0);
    $c->setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} (#{value} - {percent}%)");

    # Set the border color of the sector the same color as the fill color. Set the line color of the join line to forgroundcolor
    $c->setLineColor($perlchartdir::SameAsMainColor, $forGround);

    # Set the start angle to 135 degrees
    $c->setStartAngle(135);

    # Set the pie data and the pie labels
    $c->setData(\@data, \@labels);



( run in 0.888 second using v1.01-cache-2.11-cpan-d8267643d1d )