Skip to content

Commit

Permalink
bucky-isempty: returns 0 if given WSP file is completely null
Browse files Browse the repository at this point in the history
  • Loading branch information
jjneely committed Apr 16, 2015
1 parent 51ff6d9 commit 730e8ba
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
59 changes: 59 additions & 0 deletions bucky-isempty/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"flag"
"fmt"
"os"
)

import "github.com/jjneely/buckytools"
import "github.com/jjneely/buckytools/whisper"

func usage() {
fmt.Printf("%s <wsp_file>\n", os.Args[0])
fmt.Printf("Version: %s\n", buckytools.Version)
fmt.Printf("\tReturns successfully if there are no non-null data points in\n")
fmt.Printf("\tthe given Whsiper DB file.\n\n")
flag.PrintDefaults()
}

func main() {
flag.Usage = usage
version := flag.Bool("version", false, "Display version information.")
quiet := flag.Bool("quiet", false, "Silence output.")
flag.BoolVar(quiet, "q", false, "Silence output.")
flag.Parse()

if *version {
fmt.Printf("Buckytools version: %s\n", buckytools.Version)
os.Exit(0)
}
if flag.NArg() != 1 {
usage()
os.Exit(1)
}

wsp, err := whisper.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer wsp.Close()

ts, count, err := buckytools.FindValidDataPoints(wsp)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}

if !*quiet {
fmt.Printf("%d data points used out of %d in %s\n",
len(ts), count, flag.Arg(0))
}

if len(ts) == 0 {
os.Exit(0)
} else {
os.Exit(2)
}
}
43 changes: 43 additions & 0 deletions datapoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package buckytools

import (
"math"
"sort"
"time"
)

import "github.com/jjneely/buckytools/whisper"

// FindValidDataPoints does a backwards walk through time to examine the
// highest resolution data for each archive / time period. We collect valid
// data points and return them in a *[]TimeSeriesPoint. The second value
// return is an int containing the total number of points examined. This
// allows one to calculate the percentage of used and unused points stored
// in the Whisper database.
func FindValidDataPoints(wsp *whisper.Whisper) ([]*whisper.TimeSeriesPoint, int, error) {
points := make([]*whisper.TimeSeriesPoint, 0)
count := 0
retentions := whisper.RetentionsByPrecision{wsp.Retentions()}
sort.Sort(retentions)

start := int(time.Now().Unix())
from := 0
for _, r := range retentions.Iterator() {
from = int(time.Now().Unix()) - r.MaxRetention()

ts, err := wsp.Fetch(from, start)
if err != nil {
return make([]*whisper.TimeSeriesPoint, 0, 0), 0, err
}
count = count + len(ts.Values())
for _, v := range ts.Points() {
if !math.IsNaN(v.Value) {
points = append(points, v)
}
}

start = from
}

return points, count, nil
}

0 comments on commit 730e8ba

Please sign in to comment.