123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- package services
- import (
- "eta/mysteel_watch/global"
- "eta/mysteel_watch/utils"
- "eta/mysteel_watch/watch"
- "fmt"
- "strings"
- "time"
- )
- var BaseStartDate = "1991-01-01"
- // CheckRefreshConfig
- // @Description: 定时检查刷新配置
- // @author: Roc
- // @datetime 2024-01-11 14:04:08
- func CheckRefreshConfig() {
- var err error
- errMsgList := make([]string, 0)
- defer func() {
- if err != nil {
- global.LOG.Info("定时检查刷新配置失败:", err.Error())
- }
- if len(errMsgList) > 0 {
- global.LOG.Info("定时检查刷新配置异常:\n", strings.Join(errMsgList, "\n"))
- }
- }()
- resp, err := watch.GetRefreshConfigList()
- if err != nil {
- fmt.Println("获取刷新配置失败:", err.Error())
- return
- }
- list := resp.Data
- if len(resp.Data) <= 0 {
- return
- }
- now := time.Now()
- //now = time.Date(2024, 6, 30, 15, 10, 59, 0, time.Local)
- //now = time.Date(2023, 12, 31, 04, 10, 59, 0, time.Local)
- currTimeStr := getPreviousHalfHour(now)
- //fmt.Println(currTimeStr)
- frequencyMap := make(map[string]string)
- for _, item := range list {
- // 判断配置的刷新时间与当前时间是否匹配,如果不匹配,那么就过滤
- if item.RefreshTime != currTimeStr {
- continue
- }
- // 判断当天是否刷新,如果匹配失败,那么就过滤
- if !checkRefreshFrequency(now, item.RefreshFrequency, item.RefreshFrequencyDay) {
- continue
- }
- if _, ok := frequencyMap[item.Frequency]; ok {
- continue
- }
- frequencyMap[item.Frequency] = item.Frequency
- startDate := BaseStartDate // 默认全部刷新
- endDate := time.Now().Format(utils.FormatDate)
- filePre := item.Frequency
- switch item.Frequency {
- case "日度":
- filePre = "day"
- endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, 0, -item.RefreshDataNum).Format(utils.FormatDate)
- }
- case "周度":
- filePre = "week"
- endDate = utils.GetNowWeekMonday().Format(utils.FormatDate)
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, 0, -item.RefreshDataNum*7).Format(utils.FormatDate)
- }
- case "旬度":
- filePre = "tendan"
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, 0, -item.RefreshDataNum*10).Format(utils.FormatDate)
- }
- case "月度":
- filePre = "month"
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, -item.RefreshDataNum, 0).Format(utils.FormatDate)
- }
- case "季度":
- filePre = "season"
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, -item.RefreshDataNum*3, 0).Format(utils.FormatDate)
- }
- case "半年度":
- filePre = "half_year"
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(0, -item.RefreshDataNum*6, 0).Format(utils.FormatDate)
- }
- case "年度":
- filePre = "year"
- if item.RefreshAllData == 0 {
- startDate = now.AddDate(-item.RefreshDataNum, 0, 0).Format(utils.FormatDate)
- }
- default:
- errMsgList = append(errMsgList, fmt.Sprintf("异常的频度:%s", item.Frequency))
- continue
- }
- // 加入到待刷新列表
- err = indexMergeV2(item.Frequency, startDate, endDate, filePre)
- if err != nil {
- errMsgList = append(errMsgList, fmt.Sprintf("合并刷新%s指标失败,err:%s", item.Frequency, err.Error()))
- }
- }
- fmt.Println("刷新结束")
- }
- // getPreviousHalfHour
- // @Description: 根据传入的时间获取该时间的前整半小时的时间字符串
- // @author: Roc
- // @datetime 2024-01-09 14:27:34
- // @param now time.Time
- // @return string
- func getPreviousHalfHour(now time.Time) string {
- minute := now.Minute()
- if minute >= 30 {
- return fmt.Sprintf("%02d:%02d", now.Hour(), 30)
- }
- return fmt.Sprintf("%02d:%02d", now.Hour(), 0)
- }
- // checkRefreshFrequency
- // @Description: 根据当前日期、配置的刷新的频度、配置指定刷新的天数来判断当天是否要刷新
- // @author: Roc
- // @datetime 2024-03-04 10:01:57
- // @param now time.Time
- // @param refreshFrequency string
- // @param refreshTime string
- // @param refreshFrequencyDay int
- // @return isHandler bool
- func checkRefreshFrequency(now time.Time, refreshFrequency string, refreshFrequencyDay int) (isHandler bool) {
- //isHandler = true
- var dayNum int
- var isLastDay bool
- //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
- switch refreshFrequency {
- case "每自然日":
- // 自然日,直接判断时间能不能匹配上即可
- case "每交易日":
- // 周六日不处理
- if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
- return
- }
- case "每周":
- currWeekDay := now.Weekday()
- if currWeekDay == time.Sunday {
- currWeekDay = 7
- isLastDay = true
- }
- dayNum = int(currWeekDay)
- case "每旬":
- currDay := now.Day()
- if currDay <= 10 {
- dayNum = currDay
- // 如果是这旬的最后一天
- if currDay == 10 {
- isLastDay = true
- }
- } else if currDay <= 20 {
- dayNum = currDay - 10
- // 如果是这旬的最后一天
- if currDay == 20 {
- isLastDay = true
- }
- } else {
- dayNum = currDay - 20
- // 当月的最后一天
- monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- // 如果是这旬的最后一天
- if currDay == monthLastDay.Day() {
- isLastDay = true
- }
- }
- case "每月":
- // 当前日期
- currDay := now.Day()
- dayNum = currDay
- // 当期的最后一天
- monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- // 如果是这期的最后一天
- if currDay == monthLastDay.Day() {
- isLastDay = true
- }
- case "每季":
- // 当期的第一天 ; 当期的最后一天
- var startDay, endDay time.Time
- currMonth := now.Month()
- currDay := now.Day()
- if currMonth <= 3 {
- // 当季的第一天
- startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
- // 当季的最后一天
- endDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- } else if currMonth <= 6 {
- // 当期的第一天
- startDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- } else if currMonth <= 9 {
- // 当期的第一天
- startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- } else {
- // 当期的第一天
- startDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- }
- // 计算这期的第一天和当日的天数
- dayNum = utils.GetTimeSubDay(startDay, now) + 1
- // 如果是这期的最后一天
- if currMonth == endDay.Month() && currDay == endDay.Day() {
- isLastDay = true
- }
- case "每半年":
- // 当期的第一天 ; 当期的最后一天
- var startDay, endDay time.Time
- currMonth := now.Month()
- currDay := now.Day()
- if currMonth <= 6 {
- // 当期的第一天
- startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- } else {
- // 当期的第一天
- startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- }
- // 计算这期的第一天和当日的天数
- dayNum = utils.GetTimeSubDay(startDay, now) + 1
- // 如果是这期的最后一天
- if currMonth == endDay.Month() && currDay == endDay.Day() {
- isLastDay = true
- }
- case "每年":
- currMonth := now.Month()
- currDay := now.Day()
- // 当期的第一天
- startDay := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
- // 当期的最后一天
- endDay := time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- // 计算这期的第一天和当日的天数
- dayNum = utils.GetTimeSubDay(startDay, now) + 1
- // 如果是这期的最后一天
- if currMonth == endDay.Month() && currDay == endDay.Day() {
- isLastDay = true
- }
- }
- // 如果是这期的最后一天,那么就是判断refresh_frequency_day是否配置为0,或者配置的天数大于这期的最大天数
- // 如果配置项不是0,且配置项的天数小于当天的天数,那么就不处理
- if isLastDay {
- if refreshFrequencyDay != 0 && refreshFrequencyDay < dayNum {
- return
- }
- } else {
- // 如果不是这期的最后一天,那么就是判断refresh_frequency_day是否等于配置的天数
- if refreshFrequencyDay != dayNum {
- return
- }
- }
- isHandler = true
- return
- }
|