From 45d5d0c36cfeb34b2f70d5b6ea83fd8be0b467c5 Mon Sep 17 00:00:00 2001 From: Huy Ngo Date: Mon, 1 Apr 2024 17:04:52 +0700 Subject: [PATCH] chore: only getting pending tasks in recent one week --- stats/bridge_stats.go | 6 ++++-- stores/main.go | 2 +- stores/task.go | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/stats/bridge_stats.go b/stats/bridge_stats.go index 1dc7ab7..ac8c501 100644 --- a/stats/bridge_stats.go +++ b/stats/bridge_stats.go @@ -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"` @@ -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 { diff --git a/stores/main.go b/stores/main.go index 98cc576..2c864af 100644 --- a/stores/main.go +++ b/stores/main.go @@ -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 { diff --git a/stores/task.go b/stores/task.go index 4f57ca0..6ee1775 100644 --- a/stores/task.go +++ b/stores/task.go @@ -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 }