Skip to content

Commit

Permalink
add statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Jul 19, 2024
1 parent 8da7402 commit 96de49c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
54 changes: 48 additions & 6 deletions internal/api/dispute_game_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ func (h DisputeGameHandler) GetCreditDetails(c *gin.Context) {
}

type Overview struct {
DisputeGameProxy string `json:"disputeGameProxy"`
TotalGames int64 `json:"totalGames"`
TotalCredit string `json:"totalCredit"`
DisputeGameProxy string `json:"disputeGameProxy"`
TotalGames int64 `json:"totalGames"`
TotalCredit string `json:"totalCredit"`
InProgressGamesCount int64 `json:"inProgressGamesCount"`
ChallengerWinGamesCount int64 `json:"challengerWinGamesCount"`
DefenderWinWinGamesCount int64 `json:"defenderWinWinGamesCount"`
}

type AmountPerDay struct {
Expand All @@ -93,13 +96,22 @@ type AmountPerDay struct {

func (h DisputeGameHandler) GetOverview(c *gin.Context) {
var gameCount int64
var inProgressGamesCount int64
var challengerWinGamesCount int64
var defenderWinWinGamesCount int64
var totalCredit string
h.DB.Model(&schema.DisputeGame{}).Count(&gameCount)
h.DB.Model(&schema.DisputeGame{}).Where("status=?", 0).Count(&inProgressGamesCount)
h.DB.Model(&schema.DisputeGame{}).Where("status=?", 1).Count(&challengerWinGamesCount)
h.DB.Model(&schema.DisputeGame{}).Where("status=?", 2).Count(&defenderWinWinGamesCount)
h.DB.Model(&schema.GameCredit{}).Select("IFNULL(sum(credit), 0) as totalCredit").Find(&totalCredit)
overview := &Overview{
DisputeGameProxy: "0x05F9613aDB30026FFd634f38e5C4dFd30a197Fa1",
TotalGames: gameCount,
TotalCredit: totalCredit,
DisputeGameProxy: "0x05F9613aDB30026FFd634f38e5C4dFd30a197Fa1",
TotalGames: gameCount,
TotalCredit: totalCredit,
InProgressGamesCount: inProgressGamesCount,
ChallengerWinGamesCount: challengerWinGamesCount,
DefenderWinWinGamesCount: defenderWinWinGamesCount,
}

c.JSON(http.StatusOK, gin.H{
Expand All @@ -121,3 +133,33 @@ func (h DisputeGameHandler) GetAmountPerDays(c *gin.Context) {
"data": res,
})
}

func (h DisputeGameHandler) GetBondInProgressPerDays(c *gin.Context) {
res := make([]AmountPerDay, 0)
h.DB.Raw(" select sum(a.bond) amount, DATE_FORMAT(FROM_UNIXTIME(se.block_time),'%Y-%m-%d') date " +
" from game_claim_data a left join sync_events se on a.event_id = se.id" +
" left join dispute_game dg on a.game_contract = dg.game_contract where dg.status=0 group by date order by date desc").Scan(&res)
c.JSON(http.StatusOK, gin.H{
"data": res,
})
}

type CountGames struct {
Amount string `json:"amount"`
Date string `json:"date"`
Status string `json:"status"`
}

func (h DisputeGameHandler) GetCountDisputeGameGroupByStatus(c *gin.Context) {
days := cast.ToInt(c.Query("days"))
res := make([]CountGames, 0)
if days == 0 || days > 100 {
days = 15
}
h.DB.Raw("select count(1) count, DATE_FORMAT(FROM_UNIXTIME(dg.block_time),'%Y-%m-%d') date, status "+
" from dispute_game dg where DATE_FORMAT(FROM_UNIXTIME(dg.block_time),'%Y-%m-%d') >= "+
" DATE_FORMAT((NOW() - INTERVAL ? DAY),'%Y-%m-%d') group by date, status order by date desc", days).Scan(&res)
c.JSON(http.StatusOK, gin.H{
"data": res,
})
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func main() {
router.GET("/disputegames/:address/credit", disputeGameHandler.GetCreditDetails)
router.GET("/disputegames/overview", disputeGameHandler.GetOverview)
router.GET("/disputegames/overview/amountperdays", disputeGameHandler.GetAmountPerDays)
router.GET("/disputegames/statistics/bond/inprogress", disputeGameHandler.GetBondInProgressPerDays)
router.GET("/disputegames/count", disputeGameHandler.GetCountDisputeGameGroupByStatus)

err := router.Run()
if err != nil {
Expand Down

0 comments on commit 96de49c

Please sign in to comment.