Skip to content

Commit

Permalink
Merge pull request #30 from bradzhang717/20240226_allchains
Browse files Browse the repository at this point in the history
20240226 update inds_getAllChains api
  • Loading branch information
max-uxuy authored Feb 27, 2024
2 parents ea69e33 + 5ed8d47 commit 116d313
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 17 deletions.
7 changes: 5 additions & 2 deletions explorer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/uxuycom/indexer/xyerrors"
"github.com/uxuycom/indexer/xylog"
"math/big"
"runtime/debug"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -221,7 +222,8 @@ func (e *Explorer) FlushDB() {
defer func() {
e.cancel()
if err := recover(); err != nil {
xylog.Logger.Panicf("flush db error & quit, err[%v]", err)
stack := string(debug.Stack())
xylog.Logger.Panicf("flush db error & quit, err[%v], stack=%v", err, stack)
}
xylog.Logger.Infof("flush db quit")
}()
Expand All @@ -232,7 +234,8 @@ func (e *Explorer) Index() {
defer func() {
e.cancel()
if err := recover(); err != nil {
xylog.Logger.Panicf("index error & quit, err[%v]", err)
stack := string(debug.Stack())
xylog.Logger.Panicf("index error & quit, err[%v], stack:%v", err, stack)
}
xylog.Logger.Infof("index quit")
}()
Expand Down
4 changes: 3 additions & 1 deletion explorer/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"golang.org/x/sync/errgroup"
"math/big"
"os"
"runtime/debug"
"sync"
"sync/atomic"
"syscall"
Expand Down Expand Up @@ -83,7 +84,8 @@ func (e *Explorer) Scan() {
defer func() {
e.cancel()
if err := recover(); err != nil {
xylog.Logger.Panicf("scan error & quit, err[%v]", err)
stack := string(debug.Stack())
xylog.Logger.Panicf("scan error & quit, err[%v],stack=%v", err, stack)
}
xylog.Logger.Infof("scan quit")
e.quit <- syscall.SIGUSR1
Expand Down
13 changes: 13 additions & 0 deletions jsonrpc/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ type InscriptionInfo struct {
Progress string `json:"progress"`
}

type ChainInfo struct {
ChainId int64 `json:"chain_id"`
Chain string `json:"chain"`
OuterChain string `json:"outer_chain"`
Name string `json:"name"`
Logo string `json:"logo"`
NetworkId int64 `json:"network_id"`
Ext string `json:"ext"`
BlockNumber string `json:"block_number"`
BlockTime time.Time `json:"block_time"`
UpdatedAt time.Time `json:"timestamp"`
}

// IndsGetInscriptionTickCmd defines the inscription JSON-RPC command.
type IndsGetInscriptionTickCmd struct {
Chain string
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var rpcHandlersBeforeInitV2 = map[string]commandHandler{
"inds_getBalancesByAddress": indsGetBalancesByAddress,
"inds_getHoldersByTick": indsGetHoldersByTick,
"inds_getLastBlockNumberIndexed": indsGetLastBlockNumber,
"inds_getTickByCallData": indsGetTxOperate, //
"inds_getInscriptionTxOperate": indsGetTxOperate, //
"inds_getTickByCallData": indsGetTxOperate,
"inds_getInscriptionTxOperate": indsGetTxOperate,
"inds_getAddressBalance": indsGetAddressBalance,
"inds_getTickBriefs": indsGetTickBriefs,
"inds_chainStat": indsChainStat,
Expand Down
46 changes: 35 additions & 11 deletions jsonrpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/shopspring/decimal"
"github.com/uxuycom/indexer/model"
"github.com/uxuycom/indexer/protocol"
"github.com/uxuycom/indexer/utils"
"github.com/uxuycom/indexer/xylog"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -325,11 +325,40 @@ func (s *Service) Search(keyword, chain string) (interface{}, error) {
return result, nil
}
func (s *Service) GetAllChain() (interface{}, error) {

blocksMap := make(map[string]model.Block, 0)
blocks, err := s.rpcServer.dbc.GetAllBlocks()
if err != nil {
return ErrRPCInternal, err
}
for _, v := range blocks {
blocksMap[v.Chain] = v
}

chains, err := s.rpcServer.dbc.GetAllChainInfo()
if err != nil {
return ErrRPCInternal, err
}
return chains, nil

chainsInfo := make([]ChainInfo, 0)
for _, v := range chains {
info := ChainInfo{
ChainId: v.ChainId,
Chain: v.Chain,
OuterChain: v.OuterChain,
Name: v.Name,
Logo: v.Logo,
NetworkId: v.NetworkId,
Ext: v.Ext,
}
if block, ok := blocksMap[v.Chain]; ok {
info.BlockTime = block.BlockTime
info.UpdatedAt = block.UpdatedAt
info.BlockNumber = block.BlockNumber
}
chainsInfo = append(chainsInfo, info)
}
return chainsInfo, nil
}

func (s *Service) GetInscriptionByTick(protocol string, tick string, chain string) (interface{}, error) {
Expand Down Expand Up @@ -696,15 +725,10 @@ func (s *Service) GetTickBriefs(addresses []*TickAddress) (interface{}, error) {
}
func (s *Service) GetChainStat(chain []string) (interface{}, error) {
// get 24H chain stat from chain_stats_hour
now := time.Now().Truncate(time.Hour)
yesterday := now.Add(-24 * time.Hour).Truncate(time.Hour)
dayBeforeYesterday := yesterday.Add(-24 * time.Hour).Truncate(time.Hour)
nowFormat := now.Format("2006010215")
nowUint, _ := strconv.ParseUint(nowFormat, 10, 32)
yesterdayFormat := yesterday.Format("2006010215")
yesterdayUint, _ := strconv.ParseUint(yesterdayFormat, 10, 32)
dayBeforeYesterdayFormat := dayBeforeYesterday.Format("2006010215")
dayBeforeYesterdayUint, _ := strconv.ParseUint(dayBeforeYesterdayFormat, 10, 32)
nowUint := utils.TimeHourInt(time.Now())
yesterdayUint := utils.TimeHourInt(utils.YesterdayHour())
dayBeforeYesterdayUint := utils.TimeHourInt(utils.BeforeYesterdayHour())

todayStat, err := s.rpcServer.dbc.GroupChainStatHourBy24Hour(uint32(nowUint), uint32(yesterdayUint), chain)
if err != nil {
return ErrRPCInternal, err
Expand Down
12 changes: 11 additions & 1 deletion storage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,16 @@ func (conn *DBClient) GetAllChainFromBlock() ([]string, error) {
return chains, nil
}

// GetAllBlocks query all last block from block table
func (conn *DBClient) GetAllBlocks() ([]model.Block, error) {
blocks := make([]model.Block, 0)
err := conn.SqlDB.Model(&model.Block{}).Find(&blocks).Error
if err != nil {
return nil, err
}
return blocks, nil
}

func (conn *DBClient) FindLastBlock(chain string) (*model.Block, error) {
data := &model.Block{}
err := conn.SqlDB.First(data, "chain = ? ", chain).Error
Expand Down Expand Up @@ -827,7 +837,7 @@ func (conn *DBClient) GetChainInfoByChain(chain string) (*model.ChainInfo, error
func (conn *DBClient) GroupChainStatHourBy24Hour(startHour, endHour uint32, chain []string) ([]model.GroupChainStatHour, error) {
stats := make([]model.GroupChainStatHour, 0)
tx := conn.SqlDB.Select("chain,SUM(address_count) as address_count,SUM(inscriptions_count) as inscriptions_count,SUM(balance_sum) as balance_sum").
Where("date_hour >= ? and date_hour <= ?", startHour, endHour)
Where("date_hour >= ? and date_hour <= ?", endHour, startHour)
if len(chain) > 0 {
tx = tx.Where("chain in ?", chain)
}
Expand Down
60 changes: 60 additions & 0 deletions utils/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2023-2024 The UXUY Developer Team
// License:
// MIT License

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE

package utils

import (
"github.com/uxuycom/indexer/xylog"
"strconv"
"time"
)

var (
dateFormat = "20060102"
dateLineFormat = "2006-01-02"
hourFormat = "2006010215"
hourLineFormat = "2006-01-02-15"
timeFormat = "20060102 15:23:26"
timeLineFormat = "2006-01-02 15:23:26"
)

func BeforeYesterdayHour() time.Time {
yesterday := time.Now().Add(-24 * time.Hour)
return Hour(yesterday.Add(-24 * time.Hour))
}

func YesterdayHour() time.Time {
return Hour(time.Now().Add(-24 * time.Hour))
}

func Hour(tm time.Time) time.Time {
return tm.Truncate(time.Hour)
}

func TimeHourInt(tm time.Time) uint64 {
format := tm.Format(hourFormat)
tmInt, err := strconv.ParseUint(format, 10, 32)
if err != nil {
xylog.Logger.Errorf("TimeHourInt err!, err = %v ", err)
}
return tmInt
}
49 changes: 49 additions & 0 deletions utils/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023-2024 The UXUY Developer Team
// License:
// MIT License

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE

package utils

import (
"testing"
"time"
)

func Test_Hour(t *testing.T) {
t.Logf("hour: %v", Hour(time.Now()))
}

func Test_Yesterday(t *testing.T) {
t.Logf("hour: %v", YesterdayHour())
}

func Test_BeforeYesterdayHour(t *testing.T) {
t.Logf("hour: %v", BeforeYesterdayHour())
}

func Test_TimeHour(t *testing.T) {
now := Hour(time.Now())
yesterday := YesterdayHour()
beforeYesterday := BeforeYesterdayHour()
t.Logf("now: %v", TimeHourInt(now))
t.Logf("yesterday: %v", TimeHourInt(yesterday))
t.Logf("beforeYesterday: %v", TimeHourInt(beforeYesterday))
}

0 comments on commit 116d313

Please sign in to comment.