Skip to content

Commit

Permalink
Merge pull request #9 from huyngopt1994/chore/only-get-pending-failed…
Browse files Browse the repository at this point in the history
…-tasks-in-recent-one-week

chore: only getting pending tasks in recent one week
  • Loading branch information
huyngopt1994 authored Apr 2, 2024
2 parents c5a815f + 45d5d0c commit 5a4adf6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions stats/bridge_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

const bridgeVersion = "v0.2.9"
const timeRangeCheck = 60 * 60 * 24 * 7 // 1 Week

type NodeInfo struct {
Organization string `json:"organization,omitempty" mapstructure:"organization"`
Expand Down Expand Up @@ -268,15 +269,16 @@ func (s *Service) report(conn *connWrapper) error {
ProcessedBlock: s.processedBlock,
}

timestamp := time.Now().Unix() - timeRangeCheck // 1 week
// count pending/failed tasks from database
pending, err := s.store.CountTasks(s.chainId, "pending")
pending, err := s.store.CountTasks(s.chainId, "pending", timestamp)
if err != nil {
log.Error("error while getting pending tasks", "err", err)
} else {
info.PendingTasks = int(pending)
}

failed, err := s.store.CountTasks(s.chainId, "failed")
failed, err := s.store.CountTasks(s.chainId, "failed", timestamp)
if err != nil {
log.Error("error while getting failed tasks", "err", err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion stores/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TaskStore interface {
DeleteTasks([]string, uint64) error
Count() int64
ResetTo(ids []string, status string) error
CountTasks(chain, status string) (int64, error)
CountTasks(chain, status string, before int64) (int64, error)
}

type DepositStore interface {
Expand Down
4 changes: 2 additions & 2 deletions stores/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func (t *taskStore) GetTasks(chain, status string, limit, retrySeconds int, befo
return tasks, err
}

func (t *taskStore) CountTasks(chain, status string) (int64, error) {
func (t *taskStore) CountTasks(chain, status string, before int64) (int64, error) {
var count int64
err := t.Model(&models.Task{}).Where("chain_id = ? AND status = ?", chain, status).Count(&count).Error
err := t.Model(&models.Task{}).Where("chain_id = ? AND status = ? AND created_at >= ?", chain, status, before).Count(&count).Error
return count, err
}

Expand Down

0 comments on commit 5a4adf6

Please sign in to comment.