Skip to content

Commit

Permalink
Add Fetch().
Browse files Browse the repository at this point in the history
  • Loading branch information
hnakamur committed Mar 30, 2013
1 parent becd288 commit 12e7675
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,13 @@ func (g *Grapher) SaveGraph(filename string, start, end time.Time) (GraphInfo, e
gi, _, err := g.graph(filename, start, end)
return gi, err
}

type FetchResult struct {
Filename string
Cf string
Start time.Time
End time.Time
Step uint64
DsNames []string
Values [][]float64
}
44 changes: 44 additions & 0 deletions rrd_c.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,47 @@ func Info(filename string) (map[string]interface{}, error) {
}
return parseRRDInfo(i), nil
}

// Fetch retrieves data from RRD file.
func Fetch(filename, cf string, start, end time.Time, step uint64) (FetchResult, error) {
fn := C.CString(filename)
defer freeCString(fn)
cCf := C.CString(cf)
defer freeCString(cCf)
var ret C.int
var cStart C.time_t
var cEnd C.time_t
var cStep C.ulong
var cDsCnt C.ulong
var cDsNames **C.char
var cData *C.double
cStart = C.time_t(start.Unix())
cEnd = C.time_t(end.Unix())
cStep = C.ulong(step)
err := makeError(C.rrdFetch(&ret, fn, cCf, &cStart, &cEnd, &cStep, &cDsCnt, &cDsNames, &cData))
if err != nil {
return FetchResult{filename, cf, start, end, step, nil, nil}, err
}

start = time.Unix(int64(cStart), 0)
end = time.Unix(int64(cEnd), 0)
step = uint64(cStep)
dsCnt := int(cDsCnt)

rowLen := (int(cEnd) - int(cStart)) / int(cStep) + 1
dsNames := make([]string, dsCnt)
rows := make([][]float64, dsCnt)
for i := 0; i < dsCnt; i++ {
dsName := C.arrayGetCString(cDsNames, C.int(i))
dsNames[i] = C.GoString(dsName)
C.free(unsafe.Pointer(dsName))

rows[i] = make([]float64, rowLen)
for j := 0; j < rowLen; j++ {
rows[i][j] = float64(C.arrayGetDouble(cData, C.int(i * rowLen + j)))
}
}
C.free(unsafe.Pointer(cDsNames))
C.free(unsafe.Pointer(cData))
return FetchResult{filename, cf, start, end, step, dsNames, rows}, nil
}
14 changes: 14 additions & 0 deletions rrd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ func TestAll(t *testing.T) {
if err != nil {
t.Fatal(err)
}

// Fetch
now := time.Now()
res, err := Fetch(dbfile, "AVERAGE", now.Add(-20*time.Second), now, 1)
if err != nil {
t.Fatal(err)
}
for i, dsName := range res.DsNames {
fmt.Printf("datasource: %s\n", dsName)
for j, value := range res.Values[i] {
t := res.Start.Add(time.Duration(int(res.Step) * j) * time.Second)
fmt.Printf("%s %e\n", t, value)
}
}
}
14 changes: 14 additions & 0 deletions rrdfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ char *rrdInfo(rrd_info_t **ret, char *filename) {
*ret = rrd_info_r(filename);
return rrdError();
}

char *rrdFetch(int *ret, char *filename, const char *cf, time_t *start, time_t *end, unsigned long *step, unsigned long *ds_cnt, char ***ds_namv, double **data) {
rrd_clear_error();
*ret = rrd_fetch_r(filename, cf, start, end, step, ds_cnt, ds_namv, data);
return rrdError();
}

char *arrayGetCString(char **values, int i) {
return values[i];
}

double arrayGetDouble(double *values, int i) {
return values[i];
}
3 changes: 3 additions & 0 deletions rrdfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ extern char *rrdCreate(const char *filename, unsigned long step, time_t start, i
extern char *rrdUpdate(const char *filename, const char *template, int argc, const char **argv);
extern char *rrdGraph(rrd_info_t **ret, int argc, char **argv);
extern char *rrdInfo(rrd_info_t **ret, char *filename);
extern char *rrdFetch(int *ret, char *filename, const char *cf, time_t *start, time_t *end, unsigned long *step, unsigned long *ds_cnt, char ***ds_namv, double **data);
extern char *arrayGetCString(char **values, int i);
extern double arrayGetDouble(double *values, int i);

1 comment on commit 12e7675

@ziutek
Copy link
Owner

@ziutek ziutek commented on 12e7675 May 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you review #13 ?

Please sign in to comment.