-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apidata: store completed candles in db (#2443)
* cache api candles
- Loading branch information
Showing
14 changed files
with
343 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This code is available on the terms of the project LICENSE.md file, | ||
// also available online at https://blueoakcouncil.org/license/1.0.0. | ||
|
||
package utils | ||
|
||
func ReverseSlice[T any](s []T) { | ||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build pgonline | ||
|
||
package pg | ||
|
||
import ( | ||
"testing" | ||
|
||
"decred.org/dcrdex/dex/candles" | ||
) | ||
|
||
func TestCandles(t *testing.T) { | ||
if err := cleanTables(archie.db); err != nil { | ||
t.Fatalf("cleanTables: %v", err) | ||
} | ||
|
||
var baseID, quoteID uint32 = 42, 0 | ||
var candleDur uint64 = 5 * 60 * 1000 | ||
|
||
lastCandle, err := archie.LastCandleEndStamp(baseID, quoteID, candleDur) | ||
if err != nil { | ||
t.Fatalf("Initial LastCandleEndStamp error: %v", err) | ||
} | ||
|
||
cands := []*candles.Candle{ | ||
{EndStamp: candleDur}, | ||
{EndStamp: candleDur * 2}, | ||
} | ||
|
||
if err = archie.InsertCandles(baseID, quoteID, candleDur, cands); err != nil { | ||
t.Fatalf("InsertCandles error: %v", err) | ||
} | ||
|
||
lastCandle, err = archie.LastCandleEndStamp(baseID, quoteID, candleDur) | ||
if err != nil { | ||
t.Fatalf("LastCandleEndStamp error: %v", err) | ||
} | ||
|
||
if lastCandle != candleDur*2 { | ||
t.Fatalf("Wrong last candle. Wanted 2, got %d", lastCandle) | ||
} | ||
|
||
// Updating is fine | ||
cands[1].MatchVolume = 1 | ||
if err = archie.InsertCandles(baseID, quoteID, candleDur, []*candles.Candle{cands[1]}); err != nil { | ||
t.Fatalf("InsertCandles (overwrite) error: %v", err) | ||
} | ||
|
||
cache := candles.NewCache(5, candleDur) | ||
if err = archie.LoadEpochStats(baseID, quoteID, []*candles.Cache{cache}); err != nil { | ||
t.Fatalf("LoadEpochStats error: %v", err) | ||
} | ||
|
||
if len(cache.Candles) != 2 { | ||
t.Fatalf("Expected 2 candles, got %d", len(cache.Candles)) | ||
} | ||
|
||
if cache.Last().MatchVolume != 1 { | ||
t.Fatalf("Overwrite failed") | ||
} | ||
} |
Oops, something went wrong.