func GetBuckets(token string, min, max time.Time) ([]*Bucket, error) { var buckets []*Bucket startQuery := time.Now() rows, err := db.PGR.Query("select name, bucket, source, token, vals from metrics where token = $1 and bucket > $2 and bucket <= $3 order by bucket desc", token, min, max) if err != nil { return nil, err } utils.MeasureT(startQuery, "buckets.get-all") startParse := time.Now() defer rows.Close() for rows.Next() { var tmp []byte b := new(Bucket) buckets = append(buckets, b) rows.Scan(&b.Name, &b.Time, &b.Source, &b.Token, &tmp) if len(tmp) == 0 { b.Vals = []float64{} continue } encoding.DecodeArray(tmp, &b.Vals) } utils.MeasureT(startParse, "buckets.vals.decode") return buckets, nil }
func GetMetrics(token, name string, resolution int64, min, max time.Time) ([]*Metric, error) { startQuery := time.Now() rows, err := db.PGR.Query("select * from get_metrics($1, $2, $3, $4, $5)", token, name, resolution, min, max) if err != nil { utils.MeasureE("get-metrics-error", err) return nil, err } utils.MeasureT(startQuery, "get-metrics.query") startParse := time.Now() defer rows.Close() var metrics []*Metric for rows.Next() { startLoop := time.Now() var tmp []byte b := new(Bucket) rows.Scan(&b.Name, &b.Source, &b.Time, &tmp) if len(tmp) == 0 { b.Vals = []float64{} continue } encoding.DecodeArray(tmp, &b.Vals) m := new(Metric) m.Time = b.Time m.Name = b.Name m.Source = b.Source m.Mean = b.Mean() metrics = append(metrics, m) utils.MeasureT(startLoop, "get-metrics.scan-struct-loop") } utils.MeasureT(startParse, "parse.get-metrics") return metrics, nil }
func GetMetrics(token, name string, resolution int64, min, max time.Time) ([]*Metric, error) { defer utils.MeasureT("get-metrics", time.Now()) rows, err := pg.Query("select * from get_buckets($1, $2, $3, $4, $5)", token, name, resolution, min, max) if err != nil { return nil, err } defer rows.Close() var metrics []*Metric for rows.Next() { var tmp []byte k := BKey{} rows.Scan(&k.Name, &k.Source, &k.Time, &tmp) b := Bucket{Key: k} if len(tmp) == 0 { b.Vals = []float64{} continue } encoding.DecodeArray(tmp, &b.Vals, '{', '}', ',') m := new(Metric) m.Time = k.Time m.Name = k.Name m.Source = k.Source m.Mean = b.Mean() metrics = append(metrics, m) } return metrics, nil }
func (b *Bucket) Get() error { defer utils.MeasureT("bucket.get", time.Now()) rc := redisPool.Get() defer rc.Close() //Fill in the vals. reply, err := redis.Values(rc.Do("LRANGE", b.String(), 0, -1)) if err != nil { return err } for _, item := range reply { v, ok := item.([]byte) if !ok { continue } err = encoding.DecodeArray(v, &b.Vals, '[', ']', ' ') } return nil }
func (b *Bucket) Get() { defer utils.MeasureT(time.Now(), "bucket.get") db.PGRLocker.Lock() rows, err := db.PGR.Query("select name, bucket, source, token, vals from metrics where id = $1", b.Id) if err != nil { fmt.Printf("at=error error=%s\n", err) return } rows.Next() var tmp []byte rows.Scan(&b.Name, &b.Time, &b.Source, &b.Token, &tmp) rows.Close() db.PGRLocker.Unlock() if len(tmp) == 0 { b.Vals = []float64{} return } encoding.DecodeArray(tmp, &b.Vals) }