Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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]; }

In reply to Quick 'n' dirty plotting by Mur

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found