github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/stats/stats.go (about)

     1  package stats
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  )
     8  
     9  type Stat struct {
    10  	Period int64
    11  	Volume float64
    12  }
    13  
    14  func FromRaw(raw []interface{}) (*Stat, error) {
    15  	if len(raw) < 2 {
    16  		return nil, fmt.Errorf("data slice too short (len=%d) for Stat: %#v", len(raw), raw)
    17  	}
    18  
    19  	return &Stat{
    20  		Period: convert.I64ValOrZero(raw[0]),
    21  		Volume: convert.F64ValOrZero(raw[1]),
    22  	}, nil
    23  }
    24  
    25  func SnapshotFromRaw(raw []interface{}) (snap []*Stat, err error) {
    26  	if len(raw) == 0 {
    27  		return snap, fmt.Errorf("data slice too short for stats: %#v", raw)
    28  	}
    29  
    30  	stats := make([]*Stat, 0)
    31  	for _, v := range raw {
    32  		if v, ok := v.([]interface{}); ok {
    33  			s, err := FromRaw(v)
    34  			if err != nil {
    35  				return snap, err
    36  			}
    37  			stats = append(stats, s)
    38  		} else {
    39  			return nil, fmt.Errorf("Invalid stats snapshot")
    40  		}
    41  	}
    42  
    43  	return stats, nil
    44  }