fiatRates: always return rates as a map, even if the ticker is unavailable

pull/358/head
Vladyslav Burzakovskyy 2020-01-22 14:25:03 +01:00 committed by Martin
parent 357ad63bde
commit 4b63b483e4
4 changed files with 53 additions and 54 deletions

View File

@ -1176,19 +1176,6 @@ func (w *Worker) getFiatRatesResult(currencies []string, ticker *db.CurrencyRate
Rates: ticker.Rates, Rates: ticker.Rates,
}, nil }, nil
} }
if len(currencies) == 1 {
// Return one specific rate
var currency = strings.ToLower(currencies[0])
timestamp := ticker.Timestamp.UTC().Unix()
if rate, found := ticker.Rates[currency]; !found {
return nil, NewAPIError(fmt.Sprintf("Currency %q is not available for timestamp %d.", currency, timestamp), true)
} else {
return &db.ResultTickerAsString{
Timestamp: timestamp,
Rate: rate,
}, nil
}
} else {
// Check if currencies from the list are available in the ticker rates // Check if currencies from the list are available in the ticker rates
rates := make(map[string]float64) rates := make(map[string]float64)
for _, currency := range currencies { for _, currency := range currencies {
@ -1203,7 +1190,6 @@ func (w *Worker) getFiatRatesResult(currencies []string, ticker *db.CurrencyRate
Timestamp: ticker.Timestamp.UTC().Unix(), Timestamp: ticker.Timestamp.UTC().Unix(),
Rates: rates, Rates: rates,
}, nil }, nil
}
} }
// GetFiatRatesForBlockID returns fiat rates for block height or block hash // GetFiatRatesForBlockID returns fiat rates for block height or block hash
@ -1246,6 +1232,16 @@ func (w *Worker) GetCurrentFiatRates(currencies []string) (*db.ResultTickerAsStr
return result, nil return result, nil
} }
// makeErrorRates returns a map of currrencies, with each value equal to -1
// used when there was an error finding ticker
func makeErrorRates(currencies []string) map[string]float64 {
rates := make(map[string]float64)
for _, currency := range currencies {
rates[strings.ToLower(currency)] = -1
}
return rates
}
// GetFiatRatesForTimestamps returns fiat rates for each of the provided dates // GetFiatRatesForTimestamps returns fiat rates for each of the provided dates
func (w *Worker) GetFiatRatesForTimestamps(timestamps []int64, currencies []string) (*db.ResultTickersAsString, error) { func (w *Worker) GetFiatRatesForTimestamps(timestamps []int64, currencies []string) (*db.ResultTickersAsString, error) {
if len(timestamps) == 0 { if len(timestamps) == 0 {
@ -1259,15 +1255,15 @@ func (w *Worker) GetFiatRatesForTimestamps(timestamps []int64, currencies []stri
ticker, err := w.db.FiatRatesFindTicker(&date) ticker, err := w.db.FiatRatesFindTicker(&date)
if err != nil { if err != nil {
glog.Errorf("Error finding ticker for date %v. Error: %v", date, err) glog.Errorf("Error finding ticker for date %v. Error: %v", date, err)
ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rate: -1}) ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rates: makeErrorRates(currencies)})
continue continue
} else if ticker == nil { } else if ticker == nil {
ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rate: -1}) ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rates: makeErrorRates(currencies)})
continue continue
} }
result, err := w.getFiatRatesResult(currencies, ticker) result, err := w.getFiatRatesResult(currencies, ticker)
if err != nil { if err != nil {
ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rate: -1}) ret.Tickers = append(ret.Tickers, db.ResultTickerAsString{Timestamp: date.Unix(), Rates: makeErrorRates(currencies)})
continue continue
} }
ret.Tickers = append(ret.Tickers, *result) ret.Tickers = append(ret.Tickers, *result)

View File

@ -43,8 +43,7 @@ type CurrencyRatesTicker struct {
// ResultTickerAsString contains formatted CurrencyRatesTicker data // ResultTickerAsString contains formatted CurrencyRatesTicker data
type ResultTickerAsString struct { type ResultTickerAsString struct {
Timestamp int64 `json:"ts,omitempty"` Timestamp int64 `json:"ts,omitempty"`
Rates map[string]float64 `json:"rates,omitempty"` Rates map[string]float64 `json:"rates"`
Rate float64 `json:"rate,omitempty"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }

View File

@ -618,10 +618,10 @@ Example response (no parameters):
```javascript ```javascript
{ {
"ts":1574346615, "ts": 1574346615,
"rates": { "rates": {
"eur":7134.1, "eur": 7134.1,
"usd":7914.5 "usd": 7914.5
} }
} }
``` ```
@ -630,8 +630,10 @@ Example response (currency=usd):
```javascript ```javascript
{ {
"ts":1574346615, "ts": 1574346615,
"rate":7914.5 "rates": {
"usd": 7914.5
}
} }
``` ```
@ -639,7 +641,9 @@ Example error response (e.g. rate unavailable, incorrect currency...):
```javascript ```javascript
{ {
"ts":7980386400, "ts":7980386400,
"rate":-1 "rates": {
"usd": -1
}
} }
``` ```

View File

@ -533,7 +533,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1574346615,"rate":7914.5}`, `{"ts":1574346615,"rates":{"usd":7914.5}}`,
}, },
}, },
{ {
@ -542,7 +542,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1574344800,"rate":7814.5}`, `{"ts":1574344800,"rates":{"usd":7814.5}}`,
}, },
}, },
{ {
@ -560,7 +560,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":7980386400,"rate":-1}`, `{"ts":7980386400,"rates":{"usd":-1}}`,
}, },
}, },
{ {
@ -569,7 +569,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1574344800,"rate":7100}`, `{"ts":1574344800,"rates":{"eur":7100}}`,
}, },
}, },
{ {
@ -578,7 +578,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1521511200,"rate":2000}`, `{"ts":1521511200,"rates":{"usd":2000}}`,
}, },
}, },
{ {
@ -587,7 +587,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1521611721,"rate":2003}`, `{"ts":1521611721,"rates":{"usd":2003}}`,
}, },
}, },
{ {
@ -596,7 +596,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1574346615,"rate":7134.1}`, `{"ts":1574346615,"rates":{"eur":7134.1}}`,
}, },
}, },
{ {
@ -605,7 +605,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusOK, status: http.StatusOK,
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
body: []string{ body: []string{
`{"ts":1574346615,"rate":-1}`, `{"ts":1574346615,"rates":{"does_not_exist":-1}}`,
}, },
}, },
{ {
@ -1212,7 +1212,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"currencies": []string{"usd"}, "currencies": []string{"usd"},
}, },
}, },
want: `{"id":"18","data":{"ts":1574346615,"rate":7914.5}}`, want: `{"id":"18","data":{"ts":1574346615,"rates":{"usd":7914.5}}}`,
}, },
{ {
name: "websocket getCurrentFiatRates eur", name: "websocket getCurrentFiatRates eur",
@ -1222,7 +1222,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"currencies": []string{"eur"}, "currencies": []string{"eur"},
}, },
}, },
want: `{"id":"19","data":{"ts":1574346615,"rate":7134.1}}`, want: `{"id":"19","data":{"ts":1574346615,"rates":{"eur":7134.1}}}`,
}, },
{ {
name: "websocket getCurrentFiatRates incorrect currency", name: "websocket getCurrentFiatRates incorrect currency",
@ -1232,7 +1232,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"currencies": []string{"does-not-exist"}, "currencies": []string{"does-not-exist"},
}, },
}, },
want: `{"id":"20","data":{"error":{"message":"Currency \"does-not-exist\" is not available for timestamp 1574346615."}}}`, want: `{"id":"20","data":{"ts":1574346615,"rates":{"does-not-exist":-1}}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps missing date", name: "websocket getFiatRatesForTimestamps missing date",
@ -1264,7 +1264,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{7885693815}, "timestamps": []int64{7885693815},
}, },
}, },
want: `{"id":"23","data":{"tickers":[{"ts":7885693815,"rate":-1}]}}`, want: `{"id":"23","data":{"tickers":[{"ts":7885693815,"rates":{"usd":-1}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps exact date", name: "websocket getFiatRatesForTimestamps exact date",
@ -1275,7 +1275,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{1574346615}, "timestamps": []int64{1574346615},
}, },
}, },
want: `{"id":"24","data":{"tickers":[{"ts":1574346615,"rate":7914.5}]}}`, want: `{"id":"24","data":{"tickers":[{"ts":1574346615,"rates":{"usd":7914.5}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps closest date, eur", name: "websocket getFiatRatesForTimestamps closest date, eur",
@ -1286,7 +1286,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{1521507600}, "timestamps": []int64{1521507600},
}, },
}, },
want: `{"id":"25","data":{"tickers":[{"ts":1521511200,"rate":1300}]}}`, want: `{"id":"25","data":{"tickers":[{"ts":1521511200,"rates":{"eur":1300}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps multiple timestamps usd", name: "websocket getFiatRatesForTimestamps multiple timestamps usd",
@ -1297,7 +1297,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{1570346615, 1574346615}, "timestamps": []int64{1570346615, 1574346615},
}, },
}, },
want: `{"id":"26","data":{"tickers":[{"ts":1574344800,"rate":7814.5},{"ts":1574346615,"rate":7914.5}]}}`, want: `{"id":"26","data":{"tickers":[{"ts":1574344800,"rates":{"usd":7814.5}},{"ts":1574346615,"rates":{"usd":7914.5}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps multiple timestamps eur", name: "websocket getFiatRatesForTimestamps multiple timestamps eur",
@ -1308,7 +1308,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{1570346615, 1574346615}, "timestamps": []int64{1570346615, 1574346615},
}, },
}, },
want: `{"id":"27","data":{"tickers":[{"ts":1574344800,"rate":7100},{"ts":1574346615,"rate":7134.1}]}}`, want: `{"id":"27","data":{"tickers":[{"ts":1574344800,"rates":{"eur":7100}},{"ts":1574346615,"rates":{"eur":7134.1}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps multiple timestamps with an error", name: "websocket getFiatRatesForTimestamps multiple timestamps with an error",
@ -1319,7 +1319,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{1570346615, 1574346615, 2000000000}, "timestamps": []int64{1570346615, 1574346615, 2000000000},
}, },
}, },
want: `{"id":"28","data":{"tickers":[{"ts":1574344800,"rate":7814.5},{"ts":1574346615,"rate":7914.5},{"ts":2000000000,"rate":-1}]}}`, want: `{"id":"28","data":{"tickers":[{"ts":1574344800,"rates":{"usd":7814.5}},{"ts":1574346615,"rates":{"usd":7914.5}},{"ts":2000000000,"rates":{"usd":-1}}]}}`,
}, },
{ {
name: "websocket getFiatRatesForTimestamps multiple errors", name: "websocket getFiatRatesForTimestamps multiple errors",
@ -1330,7 +1330,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"timestamps": []int64{7832854800, 2000000000}, "timestamps": []int64{7832854800, 2000000000},
}, },
}, },
want: `{"id":"29","data":{"tickers":[{"ts":7832854800,"rate":-1},{"ts":2000000000,"rate":-1}]}}`, want: `{"id":"29","data":{"tickers":[{"ts":7832854800,"rates":{"usd":-1}},{"ts":2000000000,"rates":{"usd":-1}}]}}`,
}, },
{ {
name: "websocket getTickersList", name: "websocket getTickersList",