package com.red.circle.console.app.scheduler;

import com.red.circle.component.redis.annotation.TaskCacheLock;
import com.red.circle.console.app.service.app.datav.CountryDashboardDailyMetricService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 国家数据大屏活跃和留存增量聚合任务.
 */
@Slf4j
@Component
@ConditionalOnProperty(
    name = "red-circle.country-dashboard.active-retention-refresh-enabled",
    havingValue = "true",
    matchIfMissing = true)
@RequiredArgsConstructor
public class CountryDashboardActiveRetentionMetricTask {

  private final CountryDashboardDailyMetricService countryDashboardDailyMetricService;

  @Value("${red-circle.country-dashboard.active-retention-refresh-days:32}")
  private int refreshDays;

  @Scheduled(cron = "${red-circle.country-dashboard.active-retention-refresh-cron:0 */10 * * * ?}",
      zone = "Asia/Shanghai")
  @TaskCacheLock(key = "COUNTRY_DASHBOARD_ACTIVE_RETENTION_REFRESH", expireSecond = 60 * 8)
  public void refreshActiveRetention() {
    long start = System.currentTimeMillis();
    countryDashboardDailyMetricService.refreshActiveRetentionRecentDays(refreshDays);
    log.info("country dashboard active retention refresh task finished, days={}, costMs={}",
        refreshDays, System.currentTimeMillis() - start);
  }
}
