balanceHistory: accept Unix timestamps instead of a date string

This commit is contained in:
Vladyslav Burzakovskyy 2020-01-21 17:55:39 +01:00 committed by Martin
parent 729c35334a
commit 6f06b549df
6 changed files with 47 additions and 49 deletions

View File

@ -801,17 +801,17 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
return r, nil return r, nil
} }
func (w *Worker) balanceHistoryHeightsFromTo(fromTime, toTime time.Time) (uint32, uint32, uint32, uint32) { func (w *Worker) balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp int64) (uint32, uint32, uint32, uint32) {
fromUnix := uint32(0) fromUnix := uint32(0)
toUnix := maxUint32 toUnix := maxUint32
fromHeight := uint32(0) fromHeight := uint32(0)
toHeight := maxUint32 toHeight := maxUint32
if !fromTime.IsZero() { if fromTimestamp != 0 {
fromUnix = uint32(fromTime.Unix()) fromUnix = uint32(fromTimestamp)
fromHeight = w.is.GetBlockHeightOfTime(fromUnix) fromHeight = w.is.GetBlockHeightOfTime(fromUnix)
} }
if !toTime.IsZero() { if toTimestamp != 0 {
toUnix = uint32(toTime.Unix()) toUnix = uint32(toTimestamp)
toHeight = w.is.GetBlockHeightOfTime(toUnix) toHeight = w.is.GetBlockHeightOfTime(toUnix)
} }
return fromUnix, fromHeight, toUnix, toHeight return fromUnix, fromHeight, toUnix, toHeight
@ -932,14 +932,14 @@ func (w *Worker) setFiatRateToBalanceHistories(histories BalanceHistories, fiat
} }
// GetBalanceHistory returns history of balance for given address // GetBalanceHistory returns history of balance for given address
func (w *Worker) GetBalanceHistory(address string, fromTime, toTime time.Time, fiat string, groupBy uint32) (BalanceHistories, error) { func (w *Worker) GetBalanceHistory(address string, fromTimestamp, toTimestamp int64, fiat string, groupBy uint32) (BalanceHistories, error) {
bhs := make(BalanceHistories, 0) bhs := make(BalanceHistories, 0)
start := time.Now() start := time.Now()
addrDesc, _, err := w.getAddrDescAndNormalizeAddress(address) addrDesc, _, err := w.getAddrDescAndNormalizeAddress(address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTime, toTime) fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp)
if fromHeight >= toHeight { if fromHeight >= toHeight {
return bhs, nil return bhs, nil
} }
@ -1208,7 +1208,7 @@ func (w *Worker) GetFiatRatesForBlockID(bid string, currency string) (*db.Result
return result, nil return result, nil
} }
// GetCurrentFiatRates returns current fiat rates // GetCurrentFiatRates returns last available fiat rates
func (w *Worker) GetCurrentFiatRates(currency string) (*db.ResultTickerAsString, error) { func (w *Worker) GetCurrentFiatRates(currency string) (*db.ResultTickerAsString, error) {
ticker, err := w.db.FiatRatesFindLastTicker() ticker, err := w.db.FiatRatesFindLastTicker()
if err != nil { if err != nil {

View File

@ -591,10 +591,10 @@ func (w *Worker) GetXpubUtxo(xpub string, onlyConfirmed bool, gap int) (Utxos, e
} }
// GetXpubBalanceHistory returns history of balance for given xpub // GetXpubBalanceHistory returns history of balance for given xpub
func (w *Worker) GetXpubBalanceHistory(xpub string, fromTime, toTime time.Time, fiat string, gap int, groupBy uint32) (BalanceHistories, error) { func (w *Worker) GetXpubBalanceHistory(xpub string, fromTimestamp, toTimestamp int64, fiat string, gap int, groupBy uint32) (BalanceHistories, error) {
bhs := make(BalanceHistories, 0) bhs := make(BalanceHistories, 0)
start := time.Now() start := time.Now()
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTime, toTime) fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp)
if fromHeight >= toHeight { if fromHeight >= toHeight {
return bhs, nil return bhs, nil
} }

View File

@ -1042,21 +1042,26 @@ func (s *PublicServer) apiUtxo(r *http.Request, apiVersion int) (interface{}, er
func (s *PublicServer) apiBalanceHistory(r *http.Request, apiVersion int) (interface{}, error) { func (s *PublicServer) apiBalanceHistory(r *http.Request, apiVersion int) (interface{}, error) {
var history []api.BalanceHistory var history []api.BalanceHistory
var fromTime, toTime time.Time var fromTimestamp, toTimestamp int64
var err error var err error
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
gap, ec := strconv.Atoi(r.URL.Query().Get("gap")) gap, ec := strconv.Atoi(r.URL.Query().Get("gap"))
if ec != nil { if ec != nil {
gap = 0 gap = 0
} }
t := r.URL.Query().Get("from") from := r.URL.Query().Get("from")
if t != "" { if from != "" {
fromTime, _ = time.Parse("2006-01-02", t) fromTimestamp, err = strconv.ParseInt(from, 10, 64)
if err != nil {
return history, err
}
} }
t = r.URL.Query().Get("to") to := r.URL.Query().Get("to")
if t != "" { if to != "" {
// time.RFC3339 toTimestamp, err = strconv.ParseInt(to, 10, 64)
toTime, _ = time.Parse("2006-01-02", t) if err != nil {
return history, err
}
} }
var groupBy uint64 var groupBy uint64
groupBy, err = strconv.ParseUint(r.URL.Query().Get("groupBy"), 10, 32) groupBy, err = strconv.ParseUint(r.URL.Query().Get("groupBy"), 10, 32)
@ -1064,11 +1069,11 @@ func (s *PublicServer) apiBalanceHistory(r *http.Request, apiVersion int) (inter
groupBy = 3600 groupBy = 3600
} }
fiat := r.URL.Query().Get("fiatcurrency") fiat := r.URL.Query().Get("fiatcurrency")
history, err = s.api.GetXpubBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat, gap, uint32(groupBy)) history, err = s.api.GetXpubBalanceHistory(r.URL.Path[i+1:], fromTimestamp, toTimestamp, fiat, gap, uint32(groupBy))
if err == nil { if err == nil {
s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub-balancehistory"}).Inc() s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub-balancehistory"}).Inc()
} else { } else {
history, err = s.api.GetBalanceHistory(r.URL.Path[i+1:], fromTime, toTime, fiat, uint32(groupBy)) history, err = s.api.GetBalanceHistory(r.URL.Path[i+1:], fromTimestamp, toTimestamp, fiat, uint32(groupBy))
s.metrics.ExplorerViews.With(common.Labels{"action": "api-address-balancehistory"}).Inc() s.metrics.ExplorerViews.With(common.Labels{"action": "api-address-balancehistory"}).Inc()
} }
} }

View File

@ -789,8 +789,8 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
}, },
}, },
{ {
name: "apiBalanceHistory Addr2 v2 from=2018-03-20&to=2018-03-21", name: "apiBalanceHistory Addr2 v2 from=1521504000&to=1521590400",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz?from=2018-03-20&to=2018-03-21"), r: newGetRequest(ts.URL + "/api/v2/balancehistory/mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz?from=1521504000&to=1521590400"),
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
@ -807,8 +807,8 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
}, },
}, },
{ {
name: "apiBalanceHistory xpub v2 from=2018-03-20&to=2018-03-21", name: "apiBalanceHistory xpub v2 from=1521504000&to=1521590400",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=2018-03-20&to=2018-03-21"), r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=1521504000&to=1521590400"),
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
@ -816,8 +816,8 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
}, },
}, },
{ {
name: "apiBalanceHistory xpub v2 from=2018-03-20&to=2018-03-21&fiatcurrency=usd", name: "apiBalanceHistory xpub v2 from=1521504000&to=1521590400&fiatcurrency=usd",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=2018-03-20&to=2018-03-21&fiatcurrency=usd"), r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=1521504000&to=1521590400&fiatcurrency=usd"),
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
@ -825,8 +825,8 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
}, },
}, },
{ {
name: "apiBalanceHistory xpub v2 from=2018-03-21", name: "apiBalanceHistory xpub v2 from=1521590400",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=2018-03-21"), r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=1521590400"),
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
@ -1340,7 +1340,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamp": 1570346615, "timestamp": 1570346615,
}, },
}, },
want: `{"id":"30","data":{"ts":1570346615,"available_currencies":["eur","usd"]}}`, want: `{"id":"30","data":{"ts":1574344800,"available_currencies":["eur","usd"]}}`,
}, },
{ {
name: "websocket getBalanceHistory Addr2", name: "websocket getBalanceHistory Addr2",
@ -1363,13 +1363,13 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
want: `{"id":"32","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0"},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1"}]}`, want: `{"id":"32","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0"},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1"}]}`,
}, },
{ {
name: "websocket getBalanceHistory xpub from=2018-03-20&to=2018-03-21&fiat=usd", name: "websocket getBalanceHistory xpub from=1521504000&to=1521590400&fiat=usd",
req: websocketReq{ req: websocketReq{
Method: "getBalanceHistory", Method: "getBalanceHistory",
Params: map[string]interface{}{ Params: map[string]interface{}{
"descriptor": dbtestdata.Xpub, "descriptor": dbtestdata.Xpub,
"from": "2018-03-20", "from": 1521504000,
"to": "2018-03-21", "to": 1521590400,
"fiat": "usd", "fiat": "usd",
}, },
}, },

View File

@ -260,33 +260,26 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
"getBalanceHistory": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) { "getBalanceHistory": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
r := struct { r := struct {
Descriptor string `json:"descriptor"` Descriptor string `json:"descriptor"`
From string `json:"from"` From int64 `json:"from"`
To string `json:"to"` To int64 `json:"to"`
Fiat string `json:"fiat"` Fiat string `json:"fiat"`
Gap int `json:"gap"` Gap int `json:"gap"`
GroupBy uint32 `json:"groupBy"` GroupBy uint32 `json:"groupBy"`
}{} }{}
err = json.Unmarshal(req.Params, &r) err = json.Unmarshal(req.Params, &r)
if err == nil { if err == nil {
var fromTime, toTime time.Time if r.From <= 0 {
if r.From != "" { r.From = 0
fromTime, err = time.Parse("2006-01-02", r.From)
if err != nil {
return
}
} }
if r.To != "" { if r.To <= 0 {
toTime, err = time.Parse("2006-01-02", r.To) r.To = 0
if err != nil {
return
}
} }
if r.GroupBy <= 0 { if r.GroupBy <= 0 {
r.GroupBy = 3600 r.GroupBy = 3600
} }
rv, err = s.api.GetXpubBalanceHistory(r.Descriptor, fromTime, toTime, strings.ToLower(r.Fiat), r.Gap, r.GroupBy) rv, err = s.api.GetXpubBalanceHistory(r.Descriptor, r.From, r.To, strings.ToLower(r.Fiat), r.Gap, r.GroupBy)
if err != nil { if err != nil {
rv, err = s.api.GetBalanceHistory(r.Descriptor, fromTime, toTime, strings.ToLower(r.Fiat), r.GroupBy) rv, err = s.api.GetBalanceHistory(r.Descriptor, r.From, r.To, strings.ToLower(r.Fiat), r.GroupBy)
} }
} }
return return

View File

@ -163,8 +163,8 @@
function getBalanceHistory() { function getBalanceHistory() {
const descriptor = document.getElementById('getBalanceHistoryDescriptor').value.trim(); const descriptor = document.getElementById('getBalanceHistoryDescriptor').value.trim();
const from = document.getElementById("getBalanceHistoryFrom").value.trim(); const from = parseInt(document.getElementById("getBalanceHistoryFrom").value.trim());
const to = document.getElementById("getBalanceHistoryTo").value.trim(); const to = parseInt(document.getElementById("getBalanceHistoryTo").value.trim());
const fiat = document.getElementById("getBalanceHistoryFiat").value.trim(); const fiat = document.getElementById("getBalanceHistoryFiat").value.trim();
const groupBy = parseInt(document.getElementById("getBalanceHistoryGroupBy").value); const groupBy = parseInt(document.getElementById("getBalanceHistoryGroupBy").value);
const method = 'getBalanceHistory'; const method = 'getBalanceHistory';