From 9cec5424e77d3f7b1be984a6f5d00fb25edf09bd Mon Sep 17 00:00:00 2001 From: Vladyslav Burzakovskyy Date: Fri, 20 Dec 2019 16:18:11 +0100 Subject: [PATCH] fiatRates: fields to camelCase, update output format and tests --- api/worker.go | 19 +++++++++++++++++++ db/rocksdb.go | 4 ++-- server/public_test.go | 42 +++++++++++++++++++++--------------------- server/websocket.go | 1 - 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/api/worker.go b/api/worker.go index bb40fc70..3176b086 100644 --- a/api/worker.go +++ b/api/worker.go @@ -1145,6 +1145,25 @@ func (w *Worker) GetBlocks(page int, blocksOnPage int) (*Blocks, error) { return r, nil } +// getFiatRatesResults checks if CurrencyRatesTicker contains all necessary data and returns formatted result +func (w *Worker) getFiatRatesResults(currency string, ticker *db.CurrencyRatesTicker) (*db.ResultTickerAsString, error) { + if currency == "" { + return &db.ResultTickerAsString{ + Timestamp: ticker.Timestamp.UTC().Unix(), + Rates: ticker.Rates, + }, nil + } + 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, + Rates: map[string]float64{currency: rate}, + }, nil + } +} + // getFiatRatesResult checks if CurrencyRatesTicker contains all necessary data and returns formatted result func (w *Worker) getFiatRatesResult(currency string, ticker *db.CurrencyRatesTicker) (*db.ResultTickerAsString, error) { if currency == "" { diff --git a/db/rocksdb.go b/db/rocksdb.go index 16840835..098a4404 100644 --- a/db/rocksdb.go +++ b/db/rocksdb.go @@ -42,7 +42,7 @@ type CurrencyRatesTicker struct { // ResultTickerAsString contains formatted CurrencyRatesTicker data type ResultTickerAsString struct { - Timestamp int64 `json:"rate_ts,omitempty"` + Timestamp int64 `json:"rateTs,omitempty"` Rates map[string]float64 `json:"rates,omitempty"` Rate float64 `json:"rate,omitempty"` Error string `json:"error,omitempty"` @@ -55,7 +55,7 @@ type ResultTickersAsString struct { // ResultTickerListAsString contains formatted data about available currency tickers type ResultTickerListAsString struct { - Timestamp int64 `json:"data_timestamp,omitempty"` + Timestamp int64 `json:"dataTimestamp,omitempty"` Tickers []string `json:"available_currencies"` Error string `json:"error,omitempty"` } diff --git a/server/public_test.go b/server/public_test.go index 2e0b8580..61b775c8 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -524,7 +524,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574346615,"rates":{"eur":7134.1,"usd":7914.5}}`, + `{"rateTs":1574346615,"rates":{"eur":7134.1,"usd":7914.5}}`, }, }, { @@ -533,7 +533,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574346615,"rate":7914.5}`, + `{"rateTs":1574346615,"rate":7914.5}`, }, }, { @@ -542,7 +542,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574344800,"rate":7814.5}`, + `{"rateTs":1574344800,"rate":7814.5}`, }, }, { @@ -560,7 +560,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":7980386400,"rate":-1}`, + `{"rateTs":7980386400,"rate":-1}`, }, }, { @@ -569,7 +569,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574344800,"rate":7100}`, + `{"rateTs":1574344800,"rate":7100}`, }, }, { @@ -578,7 +578,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1521511200,"rate":2000}`, + `{"rateTs":1521511200,"rate":2000}`, }, }, { @@ -587,7 +587,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1521611721,"rate":2003}`, + `{"rateTs":1521611721,"rate":2003}`, }, }, { @@ -596,7 +596,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574346615,"rate":7134.1}`, + `{"rateTs":1574346615,"rate":7134.1}`, }, }, { @@ -605,7 +605,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"rate_ts":1574346615,"rate":-1}`, + `{"rateTs":1574346615,"rate":-1}`, }, }, { @@ -614,7 +614,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `{"data_timestamp":1574346615,"available_currencies":["eur","usd"]}`, + `{"dataTimestamp":1574346615,"available_currencies":["eur","usd"]}`, }, }, { @@ -1202,7 +1202,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "": "", }, }, - want: `{"id":"17","data":{"rate_ts":1574346615,"rates":{"eur":7134.1,"usd":7914.5}}}`, + want: `{"id":"17","data":{"rateTs":1574346615,"rates":{"eur":7134.1,"usd":7914.5}}}`, }, { name: "websocket getCurrentFiatRates usd", @@ -1212,7 +1212,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "currency": "usd", }, }, - want: `{"id":"18","data":{"rate_ts":1574346615,"rate":7914.5}}`, + want: `{"id":"18","data":{"rateTs":1574346615,"rate":7914.5}}`, }, { name: "websocket getCurrentFiatRates eur", @@ -1222,7 +1222,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "currency": "eur", }, }, - want: `{"id":"19","data":{"rate_ts":1574346615,"rate":7134.1}}`, + want: `{"id":"19","data":{"rateTs":1574346615,"rate":7134.1}}`, }, { name: "websocket getCurrentFiatRates incorrect currency", @@ -1264,7 +1264,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{7885693815}, }, }, - want: `{"id":"23","data":{"tickers":[{"rate_ts":7885693815,"rate":-1}]}}`, + want: `{"id":"23","data":{"tickers":[{"rateTs":7885693815,"rate":-1}]}}`, }, { name: "websocket getFiatRatesForTimestamps exact date", @@ -1275,7 +1275,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{1574346615}, }, }, - want: `{"id":"24","data":{"tickers":[{"rate_ts":1574346615,"rate":7914.5}]}}`, + want: `{"id":"24","data":{"tickers":[{"rateTs":1574346615,"rate":7914.5}]}}`, }, { name: "websocket getFiatRatesForTimestamps closest date, eur", @@ -1286,7 +1286,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{1521507600}, }, }, - want: `{"id":"25","data":{"tickers":[{"rate_ts":1521511200,"rate":1300}]}}`, + want: `{"id":"25","data":{"tickers":[{"rateTs":1521511200,"rate":1300}]}}`, }, { name: "websocket getFiatRatesForTimestamps multiple timestamps usd", @@ -1297,7 +1297,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{1570346615, 1574346615}, }, }, - want: `{"id":"26","data":{"tickers":[{"rate_ts":1574344800,"rate":7814.5},{"rate_ts":1574346615,"rate":7914.5}]}}`, + want: `{"id":"26","data":{"tickers":[{"rateTs":1574344800,"rate":7814.5},{"rateTs":1574346615,"rate":7914.5}]}}`, }, { name: "websocket getFiatRatesForTimestamps multiple timestamps eur", @@ -1308,7 +1308,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{1570346615, 1574346615}, }, }, - want: `{"id":"27","data":{"tickers":[{"rate_ts":1574344800,"rate":7100},{"rate_ts":1574346615,"rate":7134.1}]}}`, + want: `{"id":"27","data":{"tickers":[{"rateTs":1574344800,"rate":7100},{"rateTs":1574346615,"rate":7134.1}]}}`, }, { 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}, }, }, - want: `{"id":"28","data":{"tickers":[{"rate_ts":1574344800,"rate":7814.5},{"rate_ts":1574346615,"rate":7914.5},{"rate_ts":2000000000,"rate":-1}]}}`, + want: `{"id":"28","data":{"tickers":[{"rateTs":1574344800,"rate":7814.5},{"rateTs":1574346615,"rate":7914.5},{"rateTs":2000000000,"rate":-1}]}}`, }, { name: "websocket getFiatRatesForTimestamps multiple errors", @@ -1330,7 +1330,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamps": []int64{7832854800, 2000000000}, }, }, - want: `{"id":"29","data":{"tickers":[{"rate_ts":7832854800,"rate":-1},{"rate_ts":2000000000,"rate":-1}]}}`, + want: `{"id":"29","data":{"tickers":[{"rateTs":7832854800,"rate":-1},{"rateTs":2000000000,"rate":-1}]}}`, }, { name: "websocket getTickersList", @@ -1340,7 +1340,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "timestamp": 1570346615, }, }, - want: `{"id":"30","data":{"data_timestamp":1570346615,"available_currencies":["eur","usd"]}}`, + want: `{"id":"30","data":{"dataTimestamp":1570346615,"available_currencies":["eur","usd"]}}`, }, { name: "websocket getBalanceHistory Addr2", diff --git a/server/websocket.go b/server/websocket.go index 59d93615..358997ae 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -839,7 +839,6 @@ func (s *WebsocketServer) getFiatRatesForTimestamps(timestamps []int64, currency } func (s *WebsocketServer) getFiatRatesTickersList(timestamp int64) (interface{}, error) { - glog.Errorf("ts: %v", timestamp) ret, err := s.api.GetFiatRatesTickersList(timestamp) return ret, err }