balanceHistory: add "groupBy" parameter

pull/345/head
Vladyslav Burzakovskyy 2020-01-07 14:10:53 +01:00 committed by Martin
parent d162348d36
commit 918e032bfe
4 changed files with 15 additions and 8 deletions

View File

@ -932,7 +932,7 @@ func (w *Worker) setFiatRateToBalanceHistories(histories BalanceHistories, fiat
}
// GetBalanceHistory returns history of balance for given address
func (w *Worker) GetBalanceHistory(address string, fromTime, toTime time.Time, fiat string) (BalanceHistories, error) {
func (w *Worker) GetBalanceHistory(address string, fromTime, toTime time.Time, fiat string, groupBy uint32) (BalanceHistories, error) {
bhs := make(BalanceHistories, 0)
start := time.Now()
addrDesc, _, err := w.getAddrDescAndNormalizeAddress(address)
@ -956,7 +956,7 @@ func (w *Worker) GetBalanceHistory(address string, fromTime, toTime time.Time, f
bhs = append(bhs, *bh)
}
}
bha := bhs.SortAndAggregate(3600)
bha := bhs.SortAndAggregate(groupBy)
if fiat != "" {
err = w.setFiatRateToBalanceHistories(bha, fiat)
if err != nil {

View File

@ -591,7 +591,7 @@ func (w *Worker) GetXpubUtxo(xpub string, onlyConfirmed bool, gap int) (Utxos, e
}
// GetXpubBalanceHistory returns history of balance for given xpub
func (w *Worker) GetXpubBalanceHistory(xpub string, fromTime, toTime time.Time, fiat string, gap int) (BalanceHistories, error) {
func (w *Worker) GetXpubBalanceHistory(xpub string, fromTime, toTime time.Time, fiat string, gap int, groupBy uint32) (BalanceHistories, error) {
bhs := make(BalanceHistories, 0)
start := time.Now()
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTime, toTime)
@ -622,7 +622,7 @@ func (w *Worker) GetXpubBalanceHistory(xpub string, fromTime, toTime time.Time,
}
}
}
bha := bhs.SortAndAggregate(3600)
bha := bhs.SortAndAggregate(groupBy)
if fiat != "" {
err = w.setFiatRateToBalanceHistories(bha, fiat)
if err != nil {

View File

@ -1058,12 +1058,18 @@ func (s *PublicServer) apiBalanceHistory(r *http.Request, apiVersion int) (inter
// time.RFC3339
toTime, _ = time.Parse("2006-01-02", t)
}
var groupBy uint32
i, err := strconv.ParseUint(r.URL.Query().Get("groupBy"), 10, 32)
if err == nil || i <= 0 {
groupBy = 3600
}
fiat := r.URL.Query().Get("fiatcurrency")
history, err = s.api.GetXpubBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat, gap)
history, err = s.api.GetXpubBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat, gap, groupBy)
if err == nil {
s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub-balancehistory"}).Inc()
} else {
history, err = s.api.GetBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat)
history, err = s.api.GetBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat, groupBy)
s.metrics.ExplorerViews.With(common.Labels{"action": "api-address-balancehistory"}).Inc()
}
}

View File

@ -263,6 +263,7 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
To string `json:"to"`
Fiat string `json:"fiat"`
Gap int `json:"gap"`
GroupBy uint32 `json:"groupBy"`
}{}
err = json.Unmarshal(req.Params, &r)
if err == nil {
@ -279,9 +280,9 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
return
}
}
rv, err = s.api.GetXpubBalanceHistory(r.Descriptor, fromTime, toTime, r.Fiat, r.Gap)
rv, err = s.api.GetXpubBalanceHistory(r.Descriptor, fromTime, toTime, r.Fiat, r.Gap, r.GroupBy)
if err != nil {
rv, err = s.api.GetBalanceHistory(r.Descriptor, fromTime, toTime, r.Fiat)
rv, err = s.api.GetBalanceHistory(r.Descriptor, fromTime, toTime, r.Fiat, r.GroupBy)
}
}
return