#!/usr/local/bin/wish8.4
#
#
#  Usage: TkPortHistory.tcl filename
#
package require BLT
#
set filename [lindex $argv 0 ]
#
set oldTime 0
#
proc fTime { w val } {
    return [ clock format [expr int($val)] -format "%H:%M:%S" ]
}
#
blt::vector create timeVector inBytesVector outBytesVector inUncPacketsVector outUncPacketsVector \
    inDropsVector outDropsVector inErrorsVector outErrorsVector
#
proc LoadFile { fname } {
    global timeVector inBytesVector outBytesVector inUncPacketsVector outUncPacketsVector \
	inDropsVector outDropsVector inErrorsVector outErrorsVector
    set oldTime 0
    timeVector 		set {} 
    inBytesVector	set {} 
    outBytesVector	set {} 
    inUncPacketsVector	set {} 
    outUncPacketsVector set {} 
    inDropsVector	set {} 
    outDropsVector	set {} 
    inErrorsVector	set {} 
    outErrorsVector	set {} 
#
    if { $fname=="" } {
	set fname [tk_getOpenFile -title "Select file to display" ]
    }
    if { $fname=="" } {
	destroy .
	exit
    }    
#	
    if { [ catch { open $fname r } fd ] } {
	error $fd
	exit
    }
    set body [read $fd]
    close $fd    
    wm title . "SOURCE: $fname"
    foreach str [ split $body "\n"] {
	if { [regexp {\d+} [lindex $str 4 ] ] } {
	    set newTime [ clock scan [lindex $str 2 ] ]
	    if { $oldTime > 0 } {
		timeVector append  $newTime
		set interval [ expr $newTime - $oldTime ]
	    	inBytesVector	append  [expr [ lindex $str 4 ]/$interval ]
		outBytesVector	append  [expr [ lindex $str 5 ]/$interval ]
		inUncPacketsVector	append  [expr [ lindex $str 6 ]/$interval ]
		outUncPacketsVector append  [expr [ lindex $str 7 ]/$interval ]
		inDropsVector	append  [ lindex $str 8 ]
		outDropsVector	append  [ lindex $str 9 ]
		inErrorsVector	append  [ lindex $str 10 ]
		outErrorsVector	append  [ lindex $str 11 ]
	    }
	    set oldTime $newTime
        }
    }
}
#
proc SaveGraph { } {
}
#    
blt::graph .graph -width 1024 -fg black \
    -plotbackground black 

scrollbar .xbar -command { .graph axis view x } -orient horizontal
scrollbar .ybar -command { .graph axis view y } -orient vertical
scrollbar .y2bar -command { .graph axis view y2 } -orient vertical

.graph axis create errs
.graph y2axis use [list y2 errs]
.graph axis configure x -scrollcommand { .xbar set } -logscale no -loose no
.graph axis configure y -scrollcommand { .ybar set } -logscale no -loose no
.graph axis configure y2 -scrollcommand { .y2bar set } -logscale no -loose no

frame .chooser
checkbutton .chooser.bytes -text "Bytes In/Out" -variable ::byt \
    -onvalue 0 -offvalue 1 \
    -command {
	.graph element configure line1 -hide $::byt
	.graph element configure line2 -hide $::byt
    }
checkbutton .chooser.pkt -text "Packets In/Out" -variable ::pkt \
    -onvalue 0 -offvalue 1 \
    -command {
	.graph element configure line3 -hide $::pkt
	.graph element configure line4 -hide $::pkt
    }
checkbutton .chooser.drop -text "Drops In/Out"  -variable ::drop \
    -onvalue 0 -offvalue 1 \
    -command {
	.graph element configure line5 -hide $::drop
	.graph element configure line6 -hide $::drop
    }
checkbutton .chooser.errs -text "Errors In/Out" -variable ::err \
    -onvalue 0 -offvalue 1 \
    -command {
	.graph element configure line7 -hide $::err
	.graph element configure line8 -hide $::err
    }    
checkbutton .chooser.legend -text "Legend" -variable ::lgnd \
    -onvalue 0 -offvalue 1 \
    -command {	.graph legend configure  -hide $::lgnd    }
pack .chooser.bytes  .chooser.pkt .chooser.drop  .chooser.errs .chooser.legend \
    -side left -padx 5 
    
button .chooser.quit -text "Quit" -command "destroy ." 
pack .chooser.quit -side right -pady 2
     
set ::byt 0
set ::pkt 0
set ::drop 0
set ::err 0
set ::lgnd 0

grid rowconfig . 0 -weight 1 -minsize 0
grid columnconfig . 1 -weight 1 -minsize 0

grid .ybar -in . -column 0 -row 0 \
    -columnspan 1 -rowspan 1 -sticky news
grid .graph -in . \
    -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
grid .y2bar -in . -column 2 -row 0 \
    -columnspan 1 -rowspan 1 -sticky news
grid .xbar -in . -column 1 -row 1 \
    -columnspan 1 -rowspan 1 -sticky news
grid .chooser -in . -column 1 -row 2 \
    -columnspan 1 -rowspan 1 -sticky news

#
if { [file isdirectory $filename] } {
    LoadFile [tk_getOpenFile -title "Select file to display" -initialdir $filename ]
} elseif { [file readable $filename] } {
    LoadFile $filename 
} else {
    LoadFile [tk_getOpenFile -title "Select file to display" ]
}
#
.graph element create line1 \
    -label "inBytes/sec" -color cyan -fill green -ydata inBytesVector -xdata timeVector \
    -symbol ""
.graph element create line2 \
    -label "outBytes/sec" -color blue -fill green -ydata outBytesVector -xdata timeVector \
    -symbol ""
#
.graph element create line3 \
    -label "inPackets/sec" -color green -fill green -ydata inUncPacketsVector -xdata timeVector \
    -symbol "" -mapy y2
.graph element create line4 \
    -label "outPackets/sec" -color magenta -fill green -ydata outUncPacketsVector -xdata timeVector \
    -symbol "" -mapy y2
#
.graph element create line5 \
    -label "inDrops/5min" -color yellow -fill green -ydata inDropsVector -xdata timeVector \
    -symbol "" -mapy errs
.graph element create line6 \
    -label "outDrop/5min" -color red -fill green -ydata outDropsVector -xdata timeVector \
    -symbol "" -mapy errs
#
.graph element create line7 \
    -label "inErrors/5min" -color goldenrod -fill green -ydata inErrorsVector -xdata timeVector \
    -symbol "" -mapy errs
.graph element create line8 \
    -label "outErrors/5min" -color DarkRed -fill green -ydata outErrorsVector -xdata timeVector \
    -symbol "" -mapy errs
#    
.graph grid configure -hide no -color grey48  -dashes { 2 4 2 }
.graph xaxis configure -command fTime  -stepsize 7200 -subdivisions 5
.graph yaxis configure -hide no -color blue -rotate 90 \
    -title "Bytes/sec" -titlecolor blue -subdivisions 5 -min 0
.graph y2axis configure -hide no -color magenta -rotate 270 \
    -title ""  -titlecolor cyan -subdivisions 5 -min 0
.graph axis configure errs -hide no -color red -rotate 270 \
    -title ""  -titlecolor red -subdivisions 5 -min 0
#
.graph legend configure -position plotarea -anchor nw -foreground white
#
#
Blt_ZoomStack .graph
Blt_Crosshairs .graph
# Blt_ActiveLegend .graph
Blt_ClosestPoint .graph
Blt_PrintKey .graph
#
bind . <Control-n> "LoadFile {} "
#
bind . <F3> {
    set f [ tk_getSaveFile -title "Save Graph to GIF file" -defaultextension .gif  ]
    if { $f != "" } { 
	image create photo .photo_graph
	.graph snap .photo_graph
	.photo_graph write $f -format gif 
	image delete .photo_graph
    }
}
bind . <F4> {
    set f [ tk_getSaveFile -title "Save Graph to POSTSCRIPT file" -defaultextension .ps  ]
    if { $f != "" } { .graph postscript output $f }
}
bind . <Alt-F4> "destroy . "
bind . <F10> "destroy . "
#