Add paging to explorer
This commit is contained in:
parent
2033dba169
commit
fb0c766ee4
@ -182,7 +182,9 @@ func (w *Worker) GetTransaction(txid string, bestheight uint32, spendingTxs bool
|
|||||||
Vin: vins,
|
Vin: vins,
|
||||||
Vout: vouts,
|
Vout: vouts,
|
||||||
}
|
}
|
||||||
glog.Info("GetTransaction ", txid, " finished in ", time.Since(start))
|
if spendingTxs {
|
||||||
|
glog.Info("GetTransaction ", txid, " finished in ", time.Since(start))
|
||||||
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,6 +299,7 @@ func (w *Worker) txFromTxAddress(txid string, ta *db.TxAddresses, bi *db.BlockIn
|
|||||||
// GetAddress computes address value and gets transactions for given address
|
// GetAddress computes address value and gets transactions for given address
|
||||||
func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids bool) (*Address, error) {
|
func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids bool) (*Address, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
page--
|
||||||
if page < 0 {
|
if page < 0 {
|
||||||
page = 0
|
page = 0
|
||||||
}
|
}
|
||||||
@ -340,7 +343,10 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
}
|
}
|
||||||
// paging
|
// paging
|
||||||
from := page * txsOnPage
|
from := page * txsOnPage
|
||||||
totalPages := len(txc) / txsOnPage
|
totalPages := (len(txc) - 1) / txsOnPage
|
||||||
|
if totalPages < 0 {
|
||||||
|
totalPages = 0
|
||||||
|
}
|
||||||
if from >= len(txc) {
|
if from >= len(txc) {
|
||||||
page = totalPages - 1
|
page = totalPages - 1
|
||||||
if page < 0 {
|
if page < 0 {
|
||||||
@ -370,7 +376,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
} else {
|
} else {
|
||||||
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
|
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
|
||||||
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
|
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
|
||||||
if page > 0 {
|
if page == 0 {
|
||||||
if onlyTxids {
|
if onlyTxids {
|
||||||
txids[txi] = tx.Txid
|
txids[txi] = tx.Txid
|
||||||
} else {
|
} else {
|
||||||
@ -408,7 +414,9 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
}
|
}
|
||||||
txi++
|
txi++
|
||||||
}
|
}
|
||||||
if !onlyTxids {
|
if onlyTxids {
|
||||||
|
txids = txids[:txi]
|
||||||
|
} else {
|
||||||
txs = txs[:txi]
|
txs = txs[:txi]
|
||||||
}
|
}
|
||||||
r := &Address{
|
r := &Address{
|
||||||
@ -421,8 +429,8 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
UnconfirmedTxApperances: len(txm),
|
UnconfirmedTxApperances: len(txm),
|
||||||
Transactions: txs,
|
Transactions: txs,
|
||||||
Txids: txids,
|
Txids: txids,
|
||||||
Page: page,
|
Page: page + 1,
|
||||||
TotalPages: totalPages,
|
TotalPages: totalPages + 1,
|
||||||
TxsOnPage: txsOnPage,
|
TxsOnPage: txsOnPage,
|
||||||
}
|
}
|
||||||
glog.Info("GetAddress ", address, " finished in ", time.Since(start))
|
glog.Info("GetAddress ", address, " finished in ", time.Since(start))
|
||||||
|
|||||||
@ -20,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const blockbookAbout = "Blockbook - blockchain indexer for TREZOR wallet https://trezor.io/. Do not use for any other purpose."
|
const blockbookAbout = "Blockbook - blockchain indexer for TREZOR wallet https://trezor.io/. Do not use for any other purpose."
|
||||||
const txsOnPage = 30
|
const txsOnPage = 25
|
||||||
const txsInAPI = 1000
|
const txsInAPI = 1000
|
||||||
|
|
||||||
// PublicServer is a handle to public http server
|
// PublicServer is a handle to public http server
|
||||||
@ -272,6 +272,8 @@ const (
|
|||||||
errorTpl = tpl(iota)
|
errorTpl = tpl(iota)
|
||||||
txTpl
|
txTpl
|
||||||
addressTpl
|
addressTpl
|
||||||
|
|
||||||
|
tplCount
|
||||||
)
|
)
|
||||||
|
|
||||||
type TemplateData struct {
|
type TemplateData struct {
|
||||||
@ -281,6 +283,10 @@ type TemplateData struct {
|
|||||||
AddrStr string
|
AddrStr string
|
||||||
Tx *api.Tx
|
Tx *api.Tx
|
||||||
Error *api.ApiError
|
Error *api.ApiError
|
||||||
|
Page int
|
||||||
|
PrevPage int
|
||||||
|
NextPage int
|
||||||
|
PagingRange []int
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTemplates() []*template.Template {
|
func parseTemplates() []*template.Template {
|
||||||
@ -290,10 +296,10 @@ func parseTemplates() []*template.Template {
|
|||||||
"setTxToTemplateData": setTxToTemplateData,
|
"setTxToTemplateData": setTxToTemplateData,
|
||||||
"stringInSlice": stringInSlice,
|
"stringInSlice": stringInSlice,
|
||||||
}
|
}
|
||||||
t := make([]*template.Template, 3)
|
t := make([]*template.Template, tplCount)
|
||||||
t[errorTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
t[errorTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
||||||
t[txTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/tx.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
t[txTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/tx.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
||||||
t[addressTpl] = template.Must(template.New("address").Funcs(templateFuncMap).ParseFiles("./static/templates/address.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
t[addressTpl] = template.Must(template.New("address").Funcs(templateFuncMap).ParseFiles("./static/templates/address.html", "./static/templates/txdetail.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,9 +352,65 @@ func (s *PublicServer) explorerAddress(r *http.Request) (tpl, *TemplateData, err
|
|||||||
data := s.newTemplateData()
|
data := s.newTemplateData()
|
||||||
data.AddrStr = address.AddrStr
|
data.AddrStr = address.AddrStr
|
||||||
data.Address = address
|
data.Address = address
|
||||||
|
data.Page = address.Page
|
||||||
|
data.PagingRange, data.PrevPage, data.NextPage = getPagingRange(address.Page, address.TotalPages)
|
||||||
return addressTpl, data, nil
|
return addressTpl, data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPagingRange(page int, total int) ([]int, int, int) {
|
||||||
|
if total < 2 {
|
||||||
|
return nil, 0, 0
|
||||||
|
}
|
||||||
|
pp, np := page-1, page+1
|
||||||
|
if np > total {
|
||||||
|
np = total
|
||||||
|
}
|
||||||
|
if pp < 1 {
|
||||||
|
pp = 1
|
||||||
|
}
|
||||||
|
r := make([]int, 0, 8)
|
||||||
|
if total < 6 {
|
||||||
|
for i := 1; i <= total; i++ {
|
||||||
|
r = append(r, i)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r = append(r, 1)
|
||||||
|
if page > 3 {
|
||||||
|
r = append(r, 0)
|
||||||
|
}
|
||||||
|
if pp == 1 {
|
||||||
|
if page == 1 {
|
||||||
|
r = append(r, np)
|
||||||
|
r = append(r, np+1)
|
||||||
|
r = append(r, np+2)
|
||||||
|
} else {
|
||||||
|
r = append(r, page)
|
||||||
|
r = append(r, np)
|
||||||
|
r = append(r, np+1)
|
||||||
|
}
|
||||||
|
} else if np == total {
|
||||||
|
if page == total {
|
||||||
|
r = append(r, pp-2)
|
||||||
|
r = append(r, pp-1)
|
||||||
|
r = append(r, pp)
|
||||||
|
} else {
|
||||||
|
r = append(r, pp-1)
|
||||||
|
r = append(r, pp)
|
||||||
|
r = append(r, page)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r = append(r, pp)
|
||||||
|
r = append(r, page)
|
||||||
|
r = append(r, np)
|
||||||
|
}
|
||||||
|
if page <= total-3 {
|
||||||
|
r = append(r, 0)
|
||||||
|
}
|
||||||
|
r = append(r, total)
|
||||||
|
}
|
||||||
|
return r, pp, np
|
||||||
|
}
|
||||||
|
|
||||||
type resAboutBlockbookPublic struct {
|
type resAboutBlockbookPublic struct {
|
||||||
Coin string `json:"coin"`
|
Coin string `json:"coin"`
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
|
|||||||
@ -192,4 +192,38 @@ h3 {
|
|||||||
.navbar-nav .nav-link {
|
.navbar-nav .nav-link {
|
||||||
padding-right: 0;
|
padding-right: 0;
|
||||||
padding-left: .25rem;
|
padding-left: .25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.h-container {
|
||||||
|
display: -webkit-box;
|
||||||
|
display: -ms-flexbox;
|
||||||
|
-ms-flex-wrap: wrap;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.h-container-6 {
|
||||||
|
width: 50%;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.h-container ul {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-link {
|
||||||
|
color: #428bca;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-text {
|
||||||
|
display: block;
|
||||||
|
padding: .5rem .3rem;
|
||||||
|
line-height: 1.25;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-link {
|
||||||
|
color: #428bca;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-item.active .page-link {
|
||||||
|
background-color: #428bca;
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{{if $addr.UnconfirmedTxApperances}}
|
{{- if $addr.UnconfirmedTxApperances -}}
|
||||||
<h3>Unconfirmed</h3>
|
<h3>Unconfirmed</h3>
|
||||||
<div class="data-div">
|
<div class="data-div">
|
||||||
<table class="table data-table">
|
<table class="table data-table">
|
||||||
@ -44,9 +44,13 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{{end}} {{if $addr.Transactions}}
|
{{- end}}{{if $addr.Transactions -}}
|
||||||
<h3>Transactions</h3>
|
<div class="h-container">
|
||||||
<div class="data-div">
|
<h3 class="h-container-6">Transactions</h3>
|
||||||
{{range $tx := $addr.Transactions}}{{$data := setTxToTemplateData $data $tx}}{{template "txdetail" $data }}{{end}}
|
<nav class="h-container-6">{{template "paging" $data}}</nav>
|
||||||
</div>
|
</div>
|
||||||
{{end}} {{end}}
|
<div class="data-div">
|
||||||
|
{{- range $tx := $addr.Transactions}}{{$data := setTxToTemplateData $data $tx}}{{template "txdetail" $data}}{{end -}}
|
||||||
|
</div>
|
||||||
|
<nav>{{template "paging" $data }}</nav>
|
||||||
|
{{end}}{{end}}
|
||||||
11
static/templates/paging.html
Normal file
11
static/templates/paging.html
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{{- define "paging"}}{{$data := . -}}{{if $data.PagingRange -}}
|
||||||
|
<ul class="pagination justify-content-end">
|
||||||
|
<li class="page-item"><a class="page-link" href="?page={{$data.PrevPage}}"><</a></li>
|
||||||
|
{{- range $p := $data.PagingRange -}}
|
||||||
|
<li class="page-item{{if eq $data.Page $p}} active{{end}}">
|
||||||
|
{{- if $p}}<a class="page-link" href="?page={{$p}}">{{$p}}</a>
|
||||||
|
{{- else -}}<span class="page-text">...</span>{{- end -}}
|
||||||
|
</li>{{- end -}}
|
||||||
|
<li class="page-item"><a class="page-link" href="?page={{$data.NextPage}}">></a></li>
|
||||||
|
</ul>
|
||||||
|
{{- end -}}{{- end -}}
|
||||||
Loading…
Reference in New Issue
Block a user