/
home_page_statistics.go
62 lines (56 loc) · 1.48 KB
/
home_page_statistics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package handle
import (
"github.com/gin-gonic/gin"
"github.com/gogf/gf/v2/util/gconv"
"github.com/inscription-c/explorer-api/constants"
"github.com/inscription-c/explorer-api/handle/api_code"
"github.com/shopspring/decimal"
"golang.org/x/sync/errgroup"
"net/http"
)
type HomePageStatisticsResp struct {
Inscriptions string `json:"inscriptions"`
StoredData string `json:"stored_data"`
TotalFees string `json:"total_fees"`
}
func (h *Handler) HomePageStatistics(ctx *gin.Context) {
if err := h.doHomePageStatistics(ctx); err != nil {
ctx.JSON(http.StatusInternalServerError, api_code.NewResponse(api_code.InternalServerErr, err.Error()))
return
}
}
func (h *Handler) doHomePageStatistics(ctx *gin.Context) error {
resp := &HomePageStatisticsResp{}
errWg := &errgroup.Group{}
errWg.Go(func() error {
total, err := h.IndexerDB().InscriptionsNum()
if err != nil {
return err
}
resp.Inscriptions = gconv.String(total)
return nil
})
errWg.Go(func() error {
storedData, err := h.IndexerDB().InscriptionsStoredData()
if err != nil {
return err
}
resp.StoredData = gconv.String(storedData)
return nil
})
errWg.Go(func() error {
totalFees, err := h.IndexerDB().InscriptionsTotalFees()
if err != nil {
return err
}
btc := decimal.NewFromInt(int64(totalFees)).
Div(decimal.NewFromInt(int64(constants.OneBtc)))
resp.TotalFees = btc.String()
return nil
})
if err := errWg.Wait(); err != nil {
return err
}
ctx.JSON(http.StatusOK, resp)
return nil
}