package aggregate

import (
	"time"

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

func AggMaxRuWaitDuration() Aggregator {
	return &MaxRuWaitDuration{}
}

type MaxRuWaitDuration struct {
	max time.Duration
}

var _ Aggregator = (*MaxRuWaitDuration)(nil)

func (a *MaxRuWaitDuration) Add(record *load.StmtRecord) {
	if a.max < record.MaxRUWaitDuration {
		a.max = record.MaxRUWaitDuration
	}
}

func (a *MaxRuWaitDuration) Merge(agg Aggregator) {
	if agg, ok := agg.(*MaxRuWaitDuration); ok {
		if a.max < agg.max {
			a.max = agg.max
		}
	}
}

func (a *MaxRuWaitDuration) Output(model *api.Model) {
	model.MaxRUWaitDuration = int64(a.max)
}
