Skip to content

Commit

Permalink
insight api sync status and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
papacarp authored and chappjc committed Aug 28, 2018
1 parent ba0f9ee commit aac0885
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
1 change: 1 addition & 0 deletions api/insight/apirouter.go
Expand Up @@ -58,6 +58,7 @@ func NewInsightApiRouter(app *insightApiContext, userRealIP bool) ApiMux {

// Status and Utility
mux.With(app.StatusInfoCtx).Get("/status", app.getStatusInfo)
mux.Get("/sync", app.getSyncInfo)
mux.With(app.NbBlocksCtx).Get("/utils/estimatefee", app.getEstimateFee)
mux.Get("/peer", app.GetPeerStatus)

Expand Down
76 changes: 32 additions & 44 deletions api/insight/apiroutes.go
Expand Up @@ -83,12 +83,6 @@ func writeJSON(w http.ResponseWriter, thing interface{}, indent string) {
}
}

func writeText(w http.ResponseWriter, str string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
io.WriteString(w, str)
}

// Insight API error response for a BAD REQUEST. This means the request was
// malformed in some way or the request HASH, ADDRESS, BLOCK was not valid.
func writeInsightError(w http.ResponseWriter, str string) {
Expand Down Expand Up @@ -652,51 +646,45 @@ func (c *insightApiContext) getAddressBalance(w http.ResponseWriter, r *http.Req
writeJSON(w, addressInfo.TotalUnspent, c.getIndentQuery(r))
}

func (c *insightApiContext) getAddressTotalReceived(w http.ResponseWriter, r *http.Request) {
address := m.GetAddressCtx(r)
if address == "" {
http.Error(w, http.StatusText(422), 422)
return
}

addressInfo := c.BlockData.ChainDB.GetAddressBalance(address, 20, 0)
if addressInfo == nil {
http.Error(w, http.StatusText(422), 422)
return
}
totalReceived := addressInfo.TotalSpent + addressInfo.TotalUnspent
func (c *insightApiContext) getSyncInfo(w http.ResponseWriter, r *http.Request) {

writeText(w, strconv.Itoa(int(totalReceived)))
}
blockChainHeight, err := c.nodeClient.GetBlockCount()

func (c *insightApiContext) getAddressUnconfirmedBalance(w http.ResponseWriter, r *http.Request) {
address := m.GetAddressCtx(r)
if address == "" {
http.Error(w, http.StatusText(422), 422)
return
// To insure JSON encodes an error properly as a string or no error as null
// its easiest to use a pointer to a string.
var errorString *string
if err != nil {
s := err.Error()
errorString = &s
} else {
errorString = nil
}

addressInfo := c.BlockData.ChainDB.GetAddressBalance(address, 20, 0)
if addressInfo == nil {
http.Error(w, http.StatusText(422), 422)
return
}
writeText(w, string(addressInfo.TotalUnspent))
}
height := c.BlockData.GetHeight()

func (c *insightApiContext) getAddressTotalSent(w http.ResponseWriter, r *http.Request) {
address := m.GetAddressCtx(r)
if address == "" {
http.Error(w, http.StatusText(422), 422)
return
}
syncPercentage := int((float64(height) / float64(blockChainHeight)) * 100)

addressInfo := c.BlockData.ChainDB.GetAddressBalance(address, 20, 0)
if addressInfo == nil {
http.Error(w, http.StatusText(422), 422)
return
st := "syncing"
if syncPercentage == 100 {
st = "finished"
}
writeText(w, strconv.Itoa(int(addressInfo.TotalSpent)))

syncInfo := struct {
Status string `json:"status"`
BlockChainHeight int64 `json:"blockChainHeight"`
SyncPercentage int `json:"syncPercentage"`
Height int `json:"height"`
Error *string `json:"error"`
Type string `json:"type"`
}{
st,
blockChainHeight,
syncPercentage,
height,
errorString,
"from RPC calls",
}
writeJSON(w, syncInfo, c.getIndentQuery(r))
}

func (c *insightApiContext) getStatusInfo(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit aac0885

Please sign in to comment.