Skip to content

Commit

Permalink
format overview
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Jul 7, 2024
1 parent 1553c78 commit 0e34efd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 139 deletions.
134 changes: 0 additions & 134 deletions deploy/mysql.sql

This file was deleted.

11 changes: 6 additions & 5 deletions docs/sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ CREATE TABLE IF NOT EXISTS dispute_game
DROP TABLE IF EXISTS game_claim_data;
CREATE TABLE IF NOT EXISTS game_claim_data
(
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`game_contract` varchar(42) NOT NULL,
`data_index` int NOT NULL,
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`game_contract` varchar(42) NOT NULL,
`data_index` int NOT NULL,
`parent_index` bigint NOT NULL,
`countered_by` varchar(42) NOT NULL,
`claimant` varchar(64) NOT NULL,
Expand All @@ -114,6 +114,7 @@ CREATE TABLE IF NOT EXISTS game_claim_data
`position` bigint NOT NULL,
`clock` bigint NOT NULL,
`output_block` bigint NOT NULL,
`event_id` bigint NOT NULL,
PRIMARY KEY (`id`),
KEY `credit_index` (`game_contract`, `data_index`)
);
Expand Down
1 change: 1 addition & 0 deletions docs/sql/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ CREATE TABLE IF NOT EXISTS game_claim_data
position bigint NOT NULL,
clock bigint NOT NULL,
output_block bigint NOT NUll,
event_id bigint NOT NULL
);
CREATE INDEX if not exists dispute_game_data_index ON game_claim_data (game_contract, data_index);

Expand Down
21 changes: 21 additions & 0 deletions internal/api/dispute_game_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ type Overview struct {
TotalCredit string `json:"totalCredit"`
}

type AmountPerDay struct {
Amount string `json:"amount"`
Date string `json:"date"`
}

func (h DisputeGameHandler) GetOverview(c *gin.Context) {
var gameCount int64
var totalCredit string
Expand All @@ -96,7 +101,23 @@ func (h DisputeGameHandler) GetOverview(c *gin.Context) {
TotalGames: gameCount,
TotalCredit: totalCredit,
}

c.JSON(http.StatusOK, gin.H{
"data": overview,
})
}

func (h DisputeGameHandler) GetAmountPerDays(c *gin.Context) {
days := cast.ToInt(c.Query("days"))
res := make([]AmountPerDay, 0)
if days == 0 || days > 100 {
days = 15
}
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 "+
" where DATE_FORMAT(FROM_UNIXTIME(se.block_time),'%Y-%m-%d') >= DATE_FORMAT((NOW() - INTERVAL ? DAY),'%Y-%m-%d') "+
" group by date", days).Scan(&res)
c.JSON(http.StatusOK, gin.H{
"data": res,
})
}
2 changes: 2 additions & 0 deletions internal/handler/disputeGame.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (r *RetryDisputeGameClient) ProcessDisputeGameMove(ctx context.Context, evt
Position: data.Position.Uint64(),
Clock: data.Clock.Int64(),
OutputBlock: outputblock,
EventID: evt.ID,
}
err = r.DB.Transaction(func(tx *gorm.DB) error {
err = tx.Save(claimData).Error
Expand Down Expand Up @@ -175,6 +176,7 @@ func (r *RetryDisputeGameClient) addDisputeGame(ctx context.Context, evt *schema
Position: claimData.Position.Uint64(),
Clock: claimData.Clock.Int64(),
OutputBlock: l2Block.Uint64(),
EventID: evt.ID,
}

game := &schema.DisputeGame{
Expand Down
1 change: 1 addition & 0 deletions internal/schema/game_claim_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type GameClaimData struct {
Position uint64 `json:"position"`
Clock int64 `json:"clock"`
OutputBlock uint64 `json:"output_block"`
EventID int64 `json:"event_id"`
}

func (GameClaimData) TableName() string {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
router.GET("/disputegames/credit/rank", disputeGameHandler.GetCreditRank)
router.GET("/disputegames/:address/credit", disputeGameHandler.GetCreditDetails)
router.GET("/disputegames/overview", disputeGameHandler.GetOverview)
router.GET("/disputegames/overview/amountperdays", disputeGameHandler.GetAmountPerDays)

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

0 comments on commit 0e34efd

Please sign in to comment.