balanceHistory: accept Unix timestamps instead of a date string

pull/358/head
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
}
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)
toUnix := maxUint32
fromHeight := uint32(0)
toHeight := maxUint32
if !fromTime.IsZero() {
fromUnix = uint32(fromTime.Unix())
if fromTimestamp != 0 {
fromUnix = uint32(fromTimestamp)
fromHeight = w.is.GetBlockHeightOfTime(fromUnix)
}
if !toTime.IsZero() {
toUnix = uint32(toTime.Unix())
if toTimestamp != 0 {
toUnix = uint32(toTimestamp)
toHeight = w.is.GetBlockHeightOfTime(toUnix)
}
return fromUnix, fromHeight, toUnix, toHeight
@ -932,14 +932,14 @@ 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, groupBy uint32) (BalanceHistories, error) {
func (w *Worker) GetBalanceHistory(address string, fromTimestamp, toTimestamp int64, fiat string, groupBy uint32) (BalanceHistories, error) {
bhs := make(BalanceHistories, 0)
start := time.Now()
addrDesc, _, err := w.getAddrDescAndNormalizeAddress(address)
if err != nil {
return nil, err
}
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTime, toTime)
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp)
if fromHeight >= toHeight {
return bhs, nil
}
@ -1208,7 +1208,7 @@ func (w *Worker) GetFiatRatesForBlockID(bid string, currency string) (*db.Result
return result, nil
}
// GetCurrentFiatRates returns current fiat rates
// GetCurrentFiatRates returns last available fiat rates
func (w *Worker) GetCurrentFiatRates(currency string) (*db.ResultTickerAsString, error) {
ticker, err := w.db.FiatRatesFindLastTicker()
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
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)
start := time.Now()
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTime, toTime)
fromUnix, fromHeight, toUnix, toHeight := w.balanceHistoryHeightsFromTo(fromTimestamp, toTimestamp)
if fromHeight >= toHeight {
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) {
var history []api.BalanceHistory
var fromTime, toTime time.Time
var fromTimestamp, toTimestamp int64
var err error
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
gap, ec := strconv.Atoi(r.URL.Query().Get("gap"))
if ec != nil {
gap = 0
}
t := r.URL.Query().Get("from")
if t != "" {
fromTime, _ = time.Parse("2006-01-02", t)
from := r.URL.Query().Get("from")
if from != "" {
fromTimestamp, err = strconv.ParseInt(from, 10, 64)
if err != nil {
return history, err
}
}
t = r.URL.Query().Get("to")
if t != "" {
// time.RFC3339
toTime, _ = time.Parse("2006-01-02", t)
to := r.URL.Query().Get("to")
if to != "" {
toTimestamp, err = strconv.ParseInt(to, 10, 64)
if err != nil {
return history, err
}
}
var groupBy uint64
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
}
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 {
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, 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()
}
}

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",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz?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=1521504000&to=1521590400"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
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",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?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=1521504000&to=1521590400"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
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",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?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=1521504000&to=1521590400&fiatcurrency=usd"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
@ -825,8 +825,8 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
},
},
{
name: "apiBalanceHistory xpub v2 from=2018-03-21",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=2018-03-21"),
name: "apiBalanceHistory xpub v2 from=1521590400",
r: newGetRequest(ts.URL + "/api/v2/balancehistory/" + dbtestdata.Xpub + "?from=1521590400"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
@ -1340,7 +1340,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"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",
@ -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"}]}`,
},
{
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{
Method: "getBalanceHistory",
Params: map[string]interface{}{
"descriptor": dbtestdata.Xpub,
"from": "2018-03-20",
"to": "2018-03-21",
"from": 1521504000,
"to": 1521590400,
"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) {
r := struct {
Descriptor string `json:"descriptor"`
From string `json:"from"`
To string `json:"to"`
From int64 `json:"from"`
To int64 `json:"to"`
Fiat string `json:"fiat"`
Gap int `json:"gap"`
GroupBy uint32 `json:"groupBy"`
}{}
err = json.Unmarshal(req.Params, &r)
if err == nil {
var fromTime, toTime time.Time
if r.From != "" {
fromTime, err = time.Parse("2006-01-02", r.From)
if err != nil {
return
}
if r.From <= 0 {
r.From = 0
}
if r.To != "" {
toTime, err = time.Parse("2006-01-02", r.To)
if err != nil {
return
}
if r.To <= 0 {
r.To = 0
}
if r.GroupBy <= 0 {
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 {
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

View File

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