package ticktock

import (
	"time"

	"gitlink.org.cn/cloudream/common/pkgs/logger"
	"gitlink.org.cn/cloudream/common/utils/reflect2"
	scevt "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/mq/scanner/event"
)

type UpdatePackageAccessStatAmount struct {
	*scevt.UpdatePackageAccessStatAmount
}

func (j *UpdatePackageAccessStatAmount) Name() string {
	return reflect2.TypeNameOf[UpdatePackageAccessStatAmount]()
}

func (j *UpdatePackageAccessStatAmount) Execute(t *TickTock) {
	log := logger.WithType[UpdatePackageAccessStatAmount]("TickTock")
	startTime := time.Now()
	log.Debugf("job start")
	defer func() {
		log.Debugf("job end, time: %v", time.Since(startTime))
	}()

	err := t.db.PackageAccessStat().UpdateAllAmount(t.db.DefCtx(), t.cfg.AccessStatHistoryWeight)
	if err != nil {
		log.Warnf("update all package access stat amount: %v", err)
		return
	}
	err = t.db.ObjectAccessStat().UpdateAllAmount(t.db.DefCtx(), t.cfg.AccessStatHistoryWeight)
	if err != nil {
		log.Warnf("update all object access stat amount: %v", err)
		return
	}
}
