Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 168 lines (131 sloc) 3.758 kb
#!/opt/third-party/bin/perl -w
use strict;
use Getopt::Long;
use Time::ParseDate;
use Data::Dumper;
$Data::Dumper::Indent = 1;
my $VERSION = "1.0.0";
my %option;
GetOptions(\%option,
'config=s',
'debug',
'help',
'keep',
'log',
'min=i',
'max=i',
'save=s',
'small',
'title=s',
'verbose',
'view',
);
if ($option{help}) {
&usage;
exit;
}
$option{config} = glob('~') . '/.zengraphrc'
unless ($option{config});
unless (-f $option{config}) {
print "Need a --config or ~/.zengraphrc must exist\n\n";
&usage;
exit(1);
}
$option{save} = ''
unless $option{save};
$option{title} = ''
unless $option{title};
$option{title} .= ($option{title} ? ' \n' : '') . "Generated: " . localtime;
my $date = '';
my $data = {};
my $total = {};
my $first_date = 0;
my $config = `cat $option{config}`;
print "$config\n\n" if $option{debug};
eval $config;
die "Error in eval: $@" if $@;
foreach my $file (@ARGV) {
open IN, $file || die "Couldn't open file '$file': $!";
&process_file($file);
while (<IN>) {
&process_line($_);
}
}
my @top_keys = (sort {$total->{$b} <=> $total->{$a}} keys %{$total});
$option{min} ||= 0;
$option{max} ||= $#top_keys;
@top_keys = @top_keys[$option{min}..$option{max}];
my @titles = ();
foreach my $date (sort {$a cmp $b} keys %{$data}) {
foreach my $key (sort @top_keys) {
unless (grep $_ eq $key, @titles) {
unshift @titles, $key;
}
}
}
@titles = sort @titles;
print Dumper(\@titles) if $option{debug};
print Dumper($data) if $option{debug};
open OUT, ">temp.$$.dat" || die "temp.$$.dat: $!";
foreach ('#Day', @titles) {
printf OUT "%8s ", $_;
}
print OUT "\n";
foreach my $date (sort {$a cmp $b} keys %{$data}) {
my $record = $data->{$date};
printf OUT "%s ", $date;
foreach my $tag (@titles) {
printf OUT "%8d ", $record->{$tag} || 0;
}
print OUT "\n";
}
close OUT;
open OUT, ">temp.$$.dem" || die "temp.$$.dem: $!";
print OUT "set terminal png small\n";
print OUT "set output 'temp.$$.png'\n";
print OUT "set timefmt '%Y-%m-%d'\n";
print OUT "set xdata time\n";
print OUT "set logscale y\n" if $option{log};
print OUT "set ylabel 'Count'\n";
print OUT "set xlabel 'Date'\n";
print OUT "set title \"$option{title}\"\n";
if ($option{small}) {
print OUT "set size .5,.5\n";
print OUT "set xtics rotate\n";
print OUT "set format x \"%m-%d\"\n";
} else {
print OUT "set format x \"%m-%d\\n%Y\"\n";
}
print OUT "plot ";
my $i = 1;
foreach my $title (@titles) {
print OUT ", " if $i++ > 1;
print( OUT "'temp.$$.dat' using 1:$i t '$title' with linespoints");
}
print OUT "\n";
close OUT;
`/usr/local/bin/gnuplot temp.$$.dem`;
`cp temp.$$.png $option{save}`
if $option{save};
`xv temp.$$.png`
if $option{view};
unlink "temp.$$.dem", "temp.$$.dat", "temp.$$.png" unless $option{keep};
############################################################
# Subroutines:
sub usage {
my $prog = $0;
$prog =~ s|^[^/]*||;
print "Usage: $prog [options] files\n";
print " --config <file> - Use <file> for configuration. Default ~/.zengraphrc.\n";
print " --debug - Provide extra debugging information.\n";
print " --help - Display this usage.\n";
print " --keep - Do not delete the tempfiles.\n";
print " --log - Use a logarithmic scale.\n";
print " --min <N> - Display at least N plots.\n";
print " --max <M> - Display at most M plots.\n";
print " --save <file> - Saves the graph in <file>.\n";
print " --small - Create smaller graphs.\n";
print " --title <title> - Name the graph <title>.\n";
print " --verbose - Output extra warnings as needed.\n";
print " --view - View the graph in xv.\n";
}
Jump to Line
Something went wrong with that request. Please try again.