-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bucky-isempty: returns 0 if given WSP file is completely null
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |