package schedulers

import (
	"context"
	"time"

	"github.com/pingcap/test-infra/services/tps/pkg/common"
)

type LeakyScheduler struct {
	last time.Time
}

func (s *LeakyScheduler) Name() string {
	return "LeakyScheduler"
}

const MaxJobPerMin = 2

func (s *LeakyScheduler) Filter(ctx context.Context, jobs []*common.ArgoJob) (result []*common.ArgoJob, err error) {
	now := time.Now()
	if now.Sub(s.last) < time.Minute {
		result = jobs[0:0]
	} else {
		if len(jobs) > MaxJobPerMin {
			result = jobs[:MaxJobPerMin]
		} else {
			result = jobs
		}
		s.last = now
	}
	return
}
