#!/usr/bin/perl
#########################################################################
#                                                                       #
#          This version of Broadband Speed Graph - last day 1.0         #
#                  Copyright (c)2005 future publishing.                 #
#                                                                       #
#            Based upon Broadband Speed Graph - last day 1.0            #
#           Copyright (c)2005 Paul Grosse All Rights Reserved.          #
#                                                                       #
#########################################################################

use warnings;
#png version. You will need to install GD from either your distribution
# disks or from CPAN. This should work on Windows if you change the paths
# to reflect the non-standard way that Windows represents directory paths
use GD;

# look at the last 24 hours-worth of data in the BBSL log file
# samples taken every 10 minutes

# slurp the speed logger's log file
open LF, "</home/paul/bin/perl/bbspeed/bbspeed_log";
  @log = <LF>;
close LF;
chomp @log;

#we need the last 24 hours worth using fence-post logic
$samples = 24 * 6 + 1;
#see how many samples to remove
$sr = $#log - $samples;
my $a;
foreach (0..$sr) {
  $a = shift @log;
};

# now that the array is in order, let's start on the graphics
$barwidth = 4;
$graphheight = 200;
$gdx = $samples * $barwidth + 50;
$gdy = $graphheight + 86;
$im = new GD::Image($gdx, $gdy);
# define some colours
$white = $im->colorAllocate(255,255,255);
$grey50 = $im->colorAllocate(127,127,127);
$grey40 = $im->colorAllocate(165,165,165);
$grey20 = $im->colorAllocate(205,205,205);
$grey10 = $im->colorAllocate(230,230,230);
$red = $im->colorAllocate(210,0,0);
$green = $im->colorAllocate(0,220,0);
$yellow = $im->colorAllocate(200,200,0);
$black = $im->colorAllocate(0,0,0);
# define transparency
$im->transparent($white);
#start the titles
$tt1 = "Broadband Speed Test";
$im->string(gdGiantFont,1,1,$tt1,$grey50);
$im->string(gdGiantFont,0,0,$tt1,$black);
# extract the date from the last sample
$thedate = $log[$#log];
$thedate =~ m/([\d]{10,10})\d\d\d/;
$td = $1;
my @dtar = localtime($td);
my $theday = $dtar[3];
unless (length($theday) == 2) {$theday = "0".$theday};
my $themonth = $dtar[4] + 1;
unless (length($themonth) == 2) {$themonth = "0".$themonth};
$thedate = ($theday)."-".($themonth)."-".($dtar[5] + 1900);
my $year = $dtar[5] + 1900;
$im->string(gdMediumBoldFont,188,2,$thedate,$grey50);

#### LIMITS #########################
# Target is 2M broadband so lets have that as the upper limit
$fsd = 2*1024**2;
$greenlimit = $fsd * 0.85;
$yellowlimit = $fsd * 0.65;

# set offsets so that moving it around is easy
($xo, $yo) = (45, 30);
#Draw graph border shadow (the main frame comes afterwards)
$im->rectangle($xo, $yo, $xo+2+$samples*$barwidth, $yo+2+$graphheight, $grey50);
# draw in Y axis
# lines and numbers
$nobars = 10;
foreach $zy (0..$nobars) {
  # line
  $zyy = $graphheight / $nobars * $zy;
  $im->line($xo-4, $yo+$graphheight-$zyy, $xo+$samples*$barwidth, $yo+$graphheight-$zyy, $grey20);
  #numbers
  $zys = int ($zy / $nobars * $fsd/1024 + 0.5);
  $zys = "    ".$zys;
  $zys = substr ($zys, length($zys)-4, 4);
  $im->string(gdSmallFont,$xo-30,$yo+$graphheight-$zyy-7,$zys,$black);
}
# title
$im->stringUp(gdLargeFont,-2,$yo+$graphheight-40,"DL Speed / kbps",$black);

# draw in X axis
# lines and numbers
foreach $zx (0..$#log) {
  # extract the numbers
  $log[$zx] =~ m/(\d+\.\d\d).+([\d]{10,10})\d\d\d/;
  ($spd, $tim) = ($1, $2);
  $spd = $spd * 8 * 1024;
  #labels
  @tm = localtime($tim);
  $theminute = $tm[1];
  if ($theminute == 5) {
    $thehour = $tm[2];
    $im->line($xo+$zx*$barwidth-1, $yo, $xo+$zx*$barwidth-1, $yo+$graphheight+22, $grey40);
    unless (length($thehour) == 2) {$thehour = "0".$thehour};
    $thehour .= ":00";
    $im->stringUp(gdSmallFont,$xo-2+$zx*$barwidth,$yo+$graphheight+36,$thehour,$black);
  } elsif ($theminute == 35) {
    $thehour = $tm[2];
    $im->line($xo+$zx*$barwidth-1, $yo, $xo+$zx*$barwidth-1, $yo+$graphheight+4, $grey10);
  }
  #bars
  $spdh = $spd / $fsd * $graphheight;
  if ($spd>$greenlimit) {
    $im->rectangle($xo+$zx*$barwidth, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $green);
    $im->rectangle($xo+$zx*$barwidth+1, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $green);
  } elsif ($spd>$yellowlimit) {
    $im->rectangle($xo+$zx*$barwidth, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $yellow);
    $im->rectangle($xo+$zx*$barwidth+1, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $yellow);
  } else {
    $im->rectangle($xo+$zx*$barwidth, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $red);
    $im->rectangle($xo+$zx*$barwidth+1, $yo+$graphheight, $xo+($zx+1)*$barwidth-2, $yo+$graphheight-$spdh, $red);
  }
}
# title
$im->string(gdLargeFont,$xo+$samples*$barwidth/2-60,$yo+$graphheight+38,"Time of day",$black);

#Draw graph border
$im->rectangle($xo-1, $yo-1, $xo+1+$samples*$barwidth, $yo+1+$graphheight, $black);

open (PNG, ">/srv/www/htdocs/bbst/bbstd1.png");
  binmode PNG;
  print PNG $im->png;
close (PNG);
