diff --git a/api/internal/logic/cacherefresh.go b/api/internal/logic/cacherefresh.go index ff6f53c..16eb639 100644 --- a/api/internal/logic/cacherefresh.go +++ b/api/internal/logic/cacherefresh.go @@ -7,6 +7,7 @@ import ( "github.com/zeromicro/go-zero/core/stores/sqlx" "oasisscan-backend/api/internal/svc" "oasisscan-backend/api/internal/types" + "time" ) func SignStatsCacheJob(ctx context.Context, svcCtx *svc.ServiceContext) { @@ -54,6 +55,19 @@ func SignStatsCacheJob(ctx context.Context, svcCtx *svc.ServiceContext) { continue } + validatorSigns, err := svcCtx.BlockSignatureModel.ValidatorSignStats(ctx, signAddresses, 11) + if err != nil { + logc.Errorf(ctx, "FindBlockCountDays error: %v", err) + continue + } + if validatorSigns == nil { + continue + } + signMap := make(map[time.Time]int64, 0) + for _, sign := range validatorSigns { + signMap[sign.Day] = sign.Count + } + var timeResp []int64 for i := len(days) - 1; i > 0; i-- { startDay := days[i].Day @@ -61,16 +75,12 @@ func SignStatsCacheJob(ctx context.Context, svcCtx *svc.ServiceContext) { timeResp = append(timeResp, endDay.Unix()) - signs, err := svcCtx.BlockSignatureModel.CountSigns(ctx, signAddresses, 0, &startDay, &endDay) - if err != nil && !errors.Is(err, sqlx.ErrNotFound) { - logc.Errorf(ctx, "CountSigns error: %v", err) - continue - } + signCount := signMap[startDay] stats = append(stats, &types.ValidatorSignStatsInfo{ - DateTime: uint64(endDay.Unix()), + DateTime: uint64(startDay.Unix()), Expected: uint64(days[i].Count), - Actual: uint64(signs), + Actual: uint64(signCount), }) } diff --git a/api/oasisscan.go b/api/oasisscan.go index f3a2501..804f4da 100644 --- a/api/oasisscan.go +++ b/api/oasisscan.go @@ -37,8 +37,12 @@ func main() { //cache cron job cr := cron.New(cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger))) - ////init - //logic.SignStatsCacheJob(context.Background(), svcCtx) + //init + go func() { + ctx := context.Background() + logc.Infof(ctx, "Initial signStatsCacheJob start...") + logic.SignStatsCacheJob(ctx, svcCtx) + }() /** validator sign stats **/ _, err = cr.AddFunc("@every 10m", func() { diff --git a/job/model/blocksignaturemodel.go b/job/model/blocksignaturemodel.go index f6d84a9..6d2d73c 100644 --- a/job/model/blocksignaturemodel.go +++ b/job/model/blocksignaturemodel.go @@ -21,6 +21,7 @@ type ( SessionInsert(ctx context.Context, session sqlx.Session, data *BlockSignature) (sql.Result, error) BatchSessionInsert(ctx context.Context, session sqlx.Session, data []*BlockSignature) (sql.Result, error) CountSigns(ctx context.Context, signAddresses []string, from int64, startTime *time.Time, endTime *time.Time) (int64, error) + ValidatorSignStats(ctx context.Context, signAddresses []string, days int64) ([]*BlockCountDay, error) FindBlocksByHeight(ctx context.Context, signAddresses []string, startHeight int64) ([]*BlockSignature, error) RefreshBlockCountDaysView(ctx context.Context) error FindBlockCountDays(ctx context.Context) ([]*BlockCountDay, error) @@ -120,6 +121,33 @@ func (m *customBlockSignatureModel) CountSigns(ctx context.Context, signAddresse } } +func (m *customBlockSignatureModel) ValidatorSignStats(ctx context.Context, signAddresses []string, days int64) ([]*BlockCountDay, error) { + query := fmt.Sprintf("select DATE_TRUNC('day', timestamp) AS day, count(distinct height) from %s where validator_address in (", m.table) + vars := make([]interface{}, 0) + for i, signAddress := range signAddresses { + query += fmt.Sprintf("$%d,", i+1) + vars = append(vars, signAddress) + } + query = query[:len(query)-1] + ")" + + paramIndex := len(signAddresses) + if days > 0 { + query += fmt.Sprintf(" and timestamp >= now() - interval '$%d days'", paramIndex+1) + vars = append(vars, days) + paramIndex = paramIndex + 1 + } + var resp []*BlockCountDay + err := m.conn.QueryRowCtx(ctx, &resp, query, vars...) + switch err { + case nil: + return resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + func (m *customBlockSignatureModel) FindBlocksByHeight(ctx context.Context, signAddresses []string, startHeight int64) ([]*BlockSignature, error) { var resp []*BlockSignature query := fmt.Sprintf("select %s from %s where validator_address in (", blockSignatureRows, m.table)