diff --git a/api/worker.go b/api/worker.go index 3176b086..53c5b619 100644 --- a/api/worker.go +++ b/api/worker.go @@ -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 { diff --git a/api/xpub.go b/api/xpub.go index b14ba292..94f283b6 100644 --- a/api/xpub.go +++ b/api/xpub.go @@ -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 { diff --git a/server/public.go b/server/public.go index 80115db4..d467552a 100644 --- a/server/public.go +++ b/server/public.go @@ -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() } } diff --git a/server/websocket.go b/server/websocket.go index 358997ae..9f31ebc5 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -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