#!/bin/sh  
# \
  exec oagwish "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.3  2001/11/28 18:30:37  borland
# Fixed problems with assignment of disk list.
#
# Revision 1.2  2001/11/09 23:24:21  borland
# Added ability to plot disk totals, suffix totals, and suffix individuals
# for each disk.
#
# Revision 1.1  2001/10/27 04:32:08  borland
# First version.
#
#

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set CVSRevisionAuthor "\$Revision: 1.4 $ \$Author: soliday $"

proc PlotSelection {} {
    global selection
    if ![llength $selection] return
    set optionList ""
    foreach item $selection {
        lappend optionList -column=Time,SpaceUsed $item/spaceUsed.sdds  -title=$item
    }
    eval exec sddsplot  -graph=dot -separate -ticks=xtime -factor=yMultiplier=1e-3 -ylabel=edit=%/kByte/MByte/ $optionList  &
}

proc PlotDiskTotals {} {
    global diskList dirList
    APSSetVarAndUpdate status "Working..."
    set tmpRoot /tmp/[APSTmpString]
    foreach disk $diskList {
        APSAddToTempFileList $tmpRoot.$disk
        set fileList ""
        foreach dir $dirList {
            if [string match */$disk/* $dir] {
                if [file exists  $dir/spaceUsed.sdds] {
                    lappend fileList $dir/spaceUsed.sdds
                }
            }
        }
        APSSetVarAndUpdate status "[llength $fileList] files found for $disk"
        if [llength $fileList] {
            eval exec sddsxref $fileList -pipe=out -equate=Time -edit=col,SpaceUsed,ei/%ld/ -nowarning -fillIn \
              | sddsprocess -pipe -print=parameter,DiskName,$disk \
              | sddsrowstats -pipe=in $tmpRoot.$disk -sum=TotalSpaceUsed,SpaceUsed* 
            lappend plotFileList $tmpRoot.$disk
        }
    }
    if [llength $plotFileList] {
        eval exec sddsplot $plotFileList -title=@DiskName -ticks=xtime -separate -same \
          -column=Time,TotalSpaceUsed &
    }
}

proc FindLargest {} {
    global diskList dirList
    set sizeList ""
    APSSetVarAndUpdate status Working...
    foreach disk $diskList {
        set fileList ""
        foreach dir $dirList {
            if [string match */$disk/* $dir] {
                if [file exists  $dir/spaceUsed.sdds] {
                    lappend fileList $dir/spaceUsed.sdds
                }
            }
        }
        foreach f $fileList {
            if {[catch {exec sddsprocess $f -pipe=out -process=SpaceUsed,last,SpaceUsed | sdds2stream -pipe=in -param=SpaceUsed} size]} {
                continue
            } else {
                lappend sizeList "$size $f"
            }
        }
    }
    set sizeList [lsort -real -index 0 $sizeList]
    foreach data $sizeList {
        set size [lindex $data 0]
        set f [lindex $data 1]
        APSSetVarAndUpdate status "[expr int($size)]000 $f"
    }
}

proc Find1DayChange {} {
    global diskList dirList
    set sizeList ""
    APSSetVarAndUpdate status Working...
    foreach disk $diskList {
        set fileList ""
        foreach dir $dirList {
            if [string match */$disk/* $dir] {
                if [file exists  $dir/spaceUsed.sdds] {
                    lappend fileList $dir/spaceUsed.sdds
                }
            }
        }
        foreach f $fileList {
            if {[catch {exec sddsprocess $f -pipe=out -clip=0,2,invert -process=SpaceUsed,spread,SpaceUsed -process=SpaceUsed,slope,Slope,functionOf=Time | sdds2stream -pipe=in -param=SpaceUsed,Slope} sizeAndSlope]} {
                continue
            } else {
                set size [lindex $sizeAndSlope 0]
                set slope [lindex $sizeAndSlope 1]
                if {$slope < 0} {
                    set size [expr $size * -1]
                }
                lappend sizeList "$size $f"
            }
        }
    }
    set sizeList [lsort -real -index 0 $sizeList]
    foreach data $sizeList {
        set size [lindex $data 0]
        set f [lindex $data 1]
        APSSetVarAndUpdate status "[expr int($size)]000 $f"
    }
}

proc Find1WeekChange {} {
    global diskList dirList
    set sizeList ""
    APSSetVarAndUpdate status Working...
    set s [clock seconds]
    set s2 [expr $s - 60 * 60 * 24 * 8]
    foreach disk $diskList {
        set fileList ""
        foreach dir $dirList {
            if [string match */$disk/* $dir] {
                if [file exists  $dir/spaceUsed.sdds] {
                    lappend fileList $dir/spaceUsed.sdds
                }
            }
        }
        foreach f $fileList {
            if {[catch {exec sddsprocess $f -pipe=out -filter=column,Time,$s2,$s -clip=0,8,invert -process=SpaceUsed,spread,SpaceUsed -process=SpaceUsed,slope,Slope,functionOf=Time | sdds2stream -pipe=in -param=SpaceUsed,Slope} sizeAndSlope]} {
                if {[catch {exec sddsprocess $f -pipe=out -filter=column,Time,$s2,$s -process=SpaceUsed,spread,SpaceUsed -process=SpaceUsed,slope,Slope,functionOf=Time | sdds2stream -pipe=in -param=SpaceUsed,Slope} sizeAndSlope]} {
                    continue
                } else {
                    set size [lindex $sizeAndSlope 0]
                    set slope [lindex $sizeAndSlope 1]
                    if {$slope < 0} {
                        set size [expr $size * -1]
                    }
                    lappend sizeList "$size $f"
                }
            } else {
                set size [lindex $sizeAndSlope 0]
                set slope [lindex $sizeAndSlope 1]
                if {$slope < 0} {
                    set size [expr $size * -1]
                }
                lappend sizeList "$size $f"
            }
        }
    }
    set sizeList [lsort -real -index 0 $sizeList]
    foreach data $sizeList {
        set size [lindex $data 0]
        set f [lindex $data 1]
        APSSetVarAndUpdate status "[expr int($size)]000 $f"
    }
}


proc PlotSuffix {args} {
    set mode total
    APSStrictParseArguments {mode}

    global diskList dirList
    APSSetVarAndUpdate status "Working..."
    set tmpRoot /tmp/[APSTmpString]
    set suffixList ""
    foreach item $dirList {
        set suffix [os editstring 1000Z/ $item]
        lappend suffixItem($suffix) $item/spaceUsed.sdds
        if [lsearch -exact $suffixList $suffix]==-1 {
            lappend suffixList $suffix
        }
    }
    set suffixList [APSChooseItemFromList -itemList [lsort $suffixList] \
                      -returnIndices 0 -multiItem 1]

    if [string compare $mode total]==0 {
        set plotFileList ""
        foreach suffix $suffixList {
            APSAddToTempFileList $tmpRoot.$suffix
            if [llength $suffixItem($suffix)]>1 {
                if [catch {eval exec sddscombine $suffixItem($suffix) -pipe=out \
                             | sddsprocess -pipe -define=parameter,Points,n_rows,type=long \
                             | sddscollapse -pipe \
                             | sddssort -pipe -column=Points,decr -column=Filename \
                             | sdds2stream -pipe -column=Filename} result] {
                    return -code error "$result"
                }
                set fileList ""
                foreach item $result {
                    lappend fileList [string trim $item \n]
                }
                if [catch {eval exec sddsxref $fileList \
                             -pipe=out -equate=Time -edit=col,SpaceUsed,ei/%ld/ -nowarning -fillIn \
                             | sddsprocess -pipe -print=parameter,SuffixName,$suffix \
                             | sddsrowstats -pipe=in $tmpRoot.$suffix -sum=TotalSpaceUsed,SpaceUsed*} result] {
                    return -code error "$result"
                }
            } else {
                if [catch {exec sddsconvert $suffixItem($suffix) -pipe=out \
                             -rename=column,SpaceUsed=TotalSpaceUsed \
                             | sddsprocess -pipe=in $tmpRoot.$suffix -print=parameter,SuffixName,$suffix} result] {
                    return -code error "$result"
                }
            }
            lappend plotFileList $tmpRoot.$suffix
        }
        if [llength $plotFileList] {
            eval exec sddsplot $plotFileList -title=@SuffixName -ticks=xtime -separate -same \
              -column=Time,TotalSpaceUsed &
        }
    } else {
        set plotFileList ""
        foreach suffix $suffixList {
            foreach item $suffixItem($suffix) {
                lappend plotFileList $item
            }
        }
        if [llength $plotFileList] {
            eval exec sddsplot $plotFileList -filename -ticks=xtime -separate -same \
              -column=Time,SpaceUsed &
        }
    }
}

APSApplication . -name reviewSpaceUsed -version $CVSRevisionAuthor 

set status Ready.
APSScrolledStatus .status \
  -parent .userFrame \
  -textVariable status \
  -width 110 \
  -withButtons 1 \
  -height 10 \
  -packOption "-fill x"

eval set diskList [APSGetSDDSParameter\
                     -parameter diskList \
                     -fileName /home/helios/OAG/oagData/dataLoggerConfig/sddsSpaceUsed.config]

set dir [pwd]
set dirList0 [lsort [APSExpandSDDSSpaceUsedDirList -configFile \
                       /home/helios/OAG/oagData/dataLoggerConfig/sddsSpaceUsed.config]]
cd $dir
foreach item $dirList0 {
    if {![file exists $item/spaceUsed.sdds]} { continue }
    lappend dirList $item
}

APSScrolledListWindow .sl \
  -parent .userFrame \
  -height 10 \
  -itemList "$dirList" \
  -selectMode extended \
  -autoAccept 1 \
  -closeButton 0 \
  -clearButton 0 \
  -acceptButton 0 \
  -printButton 0 \
  -selectionVar selection \
  -packOption "-fill both -expand true"

set selection ""

pack [frame .userFrame.buttonRow1] -fill x
APSButton .plot -parent .userFrame.buttonRow1 -text "Plot" -command PlotSelection
APSButton .plotd -parent .userFrame.buttonRow1 -text "Plot Disk Totals" -command PlotDiskTotals
APSButton .plotss -parent .userFrame.buttonRow1 -text "Plot Suffix..." -command "PlotSuffix -mode single"
APSButton .plotst -parent .userFrame.buttonRow1 -text "Plot Suffix Totals..." -command "PlotSuffix -mode total"

pack [frame .userFrame.buttonRow2] -fill x
APSButton .calc -parent .userFrame.buttonRow2 -text "Find Largest" -command FindLargest
APSButton .calc2 -parent .userFrame.buttonRow2 -text "Find 1-Day Change" -command Find1DayChange
APSButton .calc3 -parent .userFrame.buttonRow2 -text "Find 1-Week Change" -command Find1WeekChange
