Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Quick 'n' dirty plotting

by Mur (Pilgrim)
on Nov 12, 2002 at 18:34 UTC ( #212348=snippet: print w/replies, xml ) Need Help??
Description: Recently I needed to plot the output of another program, which was recording minute-by-minute values in a CSV file. What I wanted to do was to 'tail' this file and display a plot of the data. Having recently "mastered" Term::Screen for another application involving a continuously updated display, I figured this would be a piece of cake. The code is very simple, and not entirely complete (it still burps some sort of error message as it starts and as it exits, but I haven't had an inspiration as to what's causing the error). It plots data that scrolls down the page with new values appearing at the top (not the natural way to do things, but Term::Screen only has a method to insert a line which moves the rest of the screen down).
#!/usr/bin/perl -w
#
# Quick 'n' dirty plotting of the free.csv report.

use strict;
use English;

BEGIN {
  # Argument processing.
  if (@ARGV == 2 and $ARGV[1] =~ /\D/) {
    # free_plot.pl 123 file.csv
    # Yuck, because the 'missing' value could be either top limit.
    die "Arguments '@ARGV' too hard to interpret:  use 'NNN NNN file.c
+sv'\n"

  } elsif (@ARGV == 1 and $ARGV[0] =~ /\D/) {
    # free_plot.pl file.csv
    # Stick in a couple of empty strings to get the TOP_* constants to
+ work.
    unshift @ARGV, '','';
  }
}

my $screen;            # screen control panel

use constant TERM_SIZE => 75;    # how big is the screen?
use constant TIME_FLD  =>  6;      # how wide is the time display?
use constant TOP_LOAD => shift || 20;        # scale factor for load a
+verage
use constant TOP_BUFF => shift || 20000;    # scale factor for buffer 
+size

INIT {
  {
    no warnings;
    require Term::Screen;
  }

  # Set up the screen.
  $screen = Term::Screen->new() or die "Something's wrong with Term::S
+creen";
  $screen->clrscr();
  $screen->at(0,TIME_FLD)->puts(join('',
                     '|----' x (TERM_SIZE/5),
                     '*/L' . TOP_LOAD,
                     ' o/F+/B' . TOP_BUFF,
                    ) );
}

END {
  # Cleanup.
  $screen->at($screen->{ROWS}-1, 0);
}

$|++;                # no buffered output

# Ctrl-C handler.
$SIG{INT} = sub { exit };

# Do we have a filename to read?
if (my $file = shift) {
  open(INPUT, $file) or die $!;
} else {
  open(INPUT, "<&STDIN") or die $!;
}

while (<INPUT>) {        # process all the input

                # CSV file has 'time,loadavg,junk,buff,...'
  my($time,$load,$buff,$free) = (split /,/)[0,1,3,10];
  next if ($load . $buff . $free) =~ /[^\d\.]/;

  $screen->at(1,0)->il;        # new line after the axis

  $time =~ s/\D//g;        # reduce time to HHMMSS
  $time =~ s/.*(\d{6})$/$1/;
  $screen->at(1,0)->puts($time);

  $_ ||= 0E0              # for old format CSV with missing fields
    foreach ($load,$buff,$free);

                # create scaling
  my $load_sp = min(TERM_SIZE, TERM_SIZE * ($load/TOP_LOAD));
  my $buff_sp = min(TERM_SIZE, TERM_SIZE * ($buff/TOP_BUFF));
  my $free_sp = min(TERM_SIZE, TERM_SIZE * ($free/TOP_BUFF));

  if ($load_sp == $buff_sp) {
    # The overlap case.
    $screen->at(1,TIME_FLD+$load_sp)->puts('X');
  } else {
    # The normal case.
    $screen->at(1,TIME_FLD+$load_sp)->puts('*');
    if ($buff_sp == $free_sp) {
      $screen->at(1,TIME_FLD+$buff_sp)->puts('x');
    } else {
      $screen->at(1,TIME_FLD+$buff_sp)->puts('+');
      $screen->at(1,TIME_FLD+$free_sp)->puts('o');
    }
  }
}

sub min {
  my @list = grep(defined,@_);
  return $list[0] unless @list > 1;
  return (sort {$a <=> $b} @list)[0];
}
Replies are listed 'Best First'.
Re: Quick 'n' dirty plotting
by zentara (Archbishop) on Nov 13, 2002 at 14:52 UTC
    This needs a way to quit the program, even control-c dosn't kill it.
      That's odd, it works for me.
      $SIG{INT} = sub { exit };
      is the intended method.
      --
      Jeff Boes
      Database Engineer
      Nexcerpt, Inc.
      vox 269.226.9550 ext 24
      fax 269.349.9076
       http://www.nexcerpt.com
      ...Nexcerpt...Connecting People With Expertise
Re: Quick 'n' dirty plotting
by petral (Curate) on Nov 13, 2002 at 17:27 UTC
    Of course, if the display should ever need to wait for the data recorder, you'll also want something like File::Tail or Event::File::Tail.

      p
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://212348]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-07-12 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?