Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monitor API /api/stats to expose information #163

Merged
merged 2 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/exchange/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ type DepositInfo struct {
Deposit scanner.Deposit
}

type DepositStats struct {
TotalBTCReceived int64 `json:"total_btc_received"`
TotalSKYSent int64 `json:"total_sky_sent"`
}

// ValidateForStatus does a consistency check of the data based upon the Status value
func (di DepositInfo) ValidateForStatus() error {

Expand Down
12 changes: 12 additions & 0 deletions src/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Exchanger interface {
GetDepositStatuses(skyAddr string) ([]DepositStatus, error)
GetDepositStatusDetail(flt DepositFilter) ([]DepositStatusDetail, error)
GetBindNum(skyAddr string) (int, error)
GetDepositStats() (*DepositStats, error)
}

// Exchange manages coin exchange between deposits and skycoin
Expand Down Expand Up @@ -626,3 +627,14 @@ func (s *Exchange) GetBindNum(skyAddr string) (int, error) {
addrs, err := s.store.GetSkyBindBtcAddresses(skyAddr)
return len(addrs), err
}

func (s *Exchange) GetDepositStats() (stats *DepositStats, err error) {
tbr, tss, err := s.store.GetDepositStats()
if err != nil {
return nil, err
}
return &DepositStats{
TotalBTCReceived: tbr,
TotalSKYSent: tss,
}, nil
}
26 changes: 26 additions & 0 deletions src/exchange/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Storer interface {
UpdateDepositInfo(string, func(DepositInfo) DepositInfo) (DepositInfo, error)
UpdateDepositInfoCallback(string, func(DepositInfo) DepositInfo, func(DepositInfo) error) (DepositInfo, error)
GetSkyBindBtcAddresses(string) ([]string, error)
GetDepositStats() (int64, int64, error)
}

// Store storage for exchange
Expand Down Expand Up @@ -473,3 +474,28 @@ func (s *Store) getSkyBindBtcAddressesTx(tx *bolt.Tx, skyAddr string) ([]string,

return addrs, nil
}

func (s *Store) GetDepositStats() (int64, int64, error) {
var totalBTCReceived int64
var totalSKYSent int64

if err := s.db.View(func(tx *bolt.Tx) error {
return dbutil.ForEach(tx, depositInfoBkt, func(k, v []byte) error {
var dpi DepositInfo
if err := json.Unmarshal(v, &dpi); err != nil {
return err
}

if dpi.CoinType == scanner.CoinTypeBTC {
totalBTCReceived += dpi.DepositValue
}
totalSKYSent += int64(dpi.SkySent)

return nil
})
}); err != nil {
return -1, -1, err
}

return totalBTCReceived, totalSKYSent, nil
}
5 changes: 5 additions & 0 deletions src/exchange/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (m *MockStore) GetSkyBindBtcAddresses(skyAddr string) ([]string, error) {
return btcAddrs.([]string), args.Error(1)
}

func (m *MockStore) GetDepositStats() (int64, int64, error) {
args := m.Called()
return args.Get(0).(int64), args.Get(1).(int64), args.Error(2)
}

func newTestStore(t *testing.T) (*Store, func()) {
db, shutdown := testutil.PrepareDB(t)

Expand Down
32 changes: 31 additions & 1 deletion src/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type AddrManager interface {
// DepositStatusGetter interface provides api to access exchange resource
type DepositStatusGetter interface {
GetDepositStatusDetail(flt exchange.DepositFilter) ([]exchange.DepositStatusDetail, error)
GetDepositStats() (*exchange.DepositStats, error)
}

// ScanAddressGetter get scanning address interface
Expand Down Expand Up @@ -101,6 +102,7 @@ func (m *Monitor) setupMux() *http.ServeMux {

mux.Handle("/api/address", httputil.LogHandler(m.log, m.addressHandler()))
mux.Handle("/api/deposit_status", httputil.LogHandler(m.log, m.depositStatus()))
mux.Handle("/api/stats", httputil.LogHandler(m.log, m.statsHandler()))
return mux
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func (m *Monitor) addressHandler() http.HandlerFunc {
}
}

// depostStatus returns all deposit status
// depositStatus returns all deposit status
// Method: GET
// URI: /api/deposit_status
// Args:
Expand Down Expand Up @@ -210,3 +212,31 @@ func (m *Monitor) depositStatus() http.HandlerFunc {
}
}
}

// stats returns all deposit stats, including total BTC received and total SKY sent.
// Method: GET
// URI: /api/stats
func (m *Monitor) statsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := logger.FromContext(ctx)

if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
httputil.ErrResponse(w, http.StatusMethodNotAllowed)
return
}

ts, err := m.GetDepositStats()
if err != nil {
log.WithError(err).Error("GetDepositStats failed")
httputil.ErrResponse(w, http.StatusInternalServerError)
return
}

if err := httputil.JSONResponse(w, ts); err != nil {
log.WithError(err).Error("Write json response failed")
return
}
}
}
16 changes: 16 additions & 0 deletions src/monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/skycoin/teller/src/exchange"
"github.com/skycoin/teller/src/scanner"
"github.com/skycoin/teller/src/util/testutil"
)

Expand Down Expand Up @@ -43,6 +44,21 @@ func (dps dummyDepositStatusGetter) GetDepositStatusDetail(flt exchange.DepositF
return ds, nil
}

func (dps dummyDepositStatusGetter) GetDepositStats() (*exchange.DepositStats, error) {
var totalBTCReceived int64
var totalSKYSent int64
for _, dpi := range dps.dpis {
if dpi.CoinType == scanner.CoinTypeBTC {
totalBTCReceived += dpi.DepositValue
}
totalSKYSent += int64(dpi.SkySent)
}
return &exchange.DepositStats{
TotalBTCReceived: totalBTCReceived,
TotalSKYSent: totalSKYSent,
}, nil
}

type dummyScanAddrs struct {
addrs []string
}
Expand Down