package aggregate

import (
	"github.com/pingcap-inc/clinic/pkg/ngm/statements/api"
	"github.com/pingcap-inc/clinic/pkg/ngm/statements/load"
)

func AggAvgTxnRetry() Aggregator {
	return &AvgTxnRetry{}
}

type AvgTxnRetry struct {
	sum int64
	cnt int64
}

var _ Aggregator = (*AvgTxnRetry)(nil)

func (a *AvgTxnRetry) Add(record *load.StmtRecord) {
	a.sum += record.SumTxnRetry
	a.cnt += record.ExecCount
}

func (a *AvgTxnRetry) Merge(agg Aggregator) {
	if agg, ok := agg.(*AvgTxnRetry); ok {
		a.sum += agg.sum
		a.cnt += agg.cnt
	}
}

func (a *AvgTxnRetry) Output(model *api.Model) {
	if a.cnt > 0 {
		model.AvgTxnRetry = int(a.sum / a.cnt)
	}
}
