edb_refresh.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package services
  2. import (
  3. "eta/mysteel_watch/global"
  4. "eta/mysteel_watch/utils"
  5. "eta/mysteel_watch/watch"
  6. "fmt"
  7. "strings"
  8. "time"
  9. )
  10. var BaseStartDate = "1991-01-01"
  11. // CheckRefreshConfig
  12. // @Description: 定时检查刷新配置
  13. // @author: Roc
  14. // @datetime 2024-01-11 14:04:08
  15. func CheckRefreshConfig() {
  16. var err error
  17. errMsgList := make([]string, 0)
  18. defer func() {
  19. if err != nil {
  20. global.LOG.Info("定时检查刷新配置失败:", err.Error())
  21. }
  22. if len(errMsgList) > 0 {
  23. global.LOG.Info("定时检查刷新配置异常:\n", strings.Join(errMsgList, "\n"))
  24. }
  25. }()
  26. resp, err := watch.GetRefreshConfigList()
  27. if err != nil {
  28. fmt.Println("获取刷新配置失败:", err.Error())
  29. return
  30. }
  31. list := resp.Data
  32. if len(resp.Data) <= 0 {
  33. return
  34. }
  35. now := time.Now()
  36. //now = time.Date(2024, 6, 30, 15, 10, 59, 0, time.Local)
  37. //now = time.Date(2023, 12, 31, 04, 10, 59, 0, time.Local)
  38. currTimeStr := getPreviousHalfHour(now)
  39. //fmt.Println(currTimeStr)
  40. frequencyMap := make(map[string]string)
  41. for _, item := range list {
  42. // 判断配置的刷新时间与当前时间是否匹配,如果不匹配,那么就过滤
  43. if item.RefreshTime != currTimeStr {
  44. continue
  45. }
  46. // 判断当天是否刷新,如果匹配失败,那么就过滤
  47. if !checkRefreshFrequency(now, item.RefreshFrequency, item.RefreshFrequencyDay) {
  48. continue
  49. }
  50. if _, ok := frequencyMap[item.Frequency]; ok {
  51. continue
  52. }
  53. frequencyMap[item.Frequency] = item.Frequency
  54. startDate := BaseStartDate // 默认全部刷新
  55. endDate := time.Now().Format(utils.FormatDate)
  56. filePre := item.Frequency
  57. switch item.Frequency {
  58. case "日度":
  59. filePre = "day"
  60. endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  61. if item.RefreshAllData == 0 {
  62. startDate = now.AddDate(0, 0, -item.RefreshDataNum).Format(utils.FormatDate)
  63. }
  64. case "周度":
  65. filePre = "week"
  66. endDate = utils.GetNowWeekMonday().Format(utils.FormatDate)
  67. if item.RefreshAllData == 0 {
  68. startDate = now.AddDate(0, 0, -item.RefreshDataNum*7).Format(utils.FormatDate)
  69. }
  70. case "旬度":
  71. filePre = "tendan"
  72. if item.RefreshAllData == 0 {
  73. startDate = now.AddDate(0, 0, -item.RefreshDataNum*10).Format(utils.FormatDate)
  74. }
  75. case "月度":
  76. filePre = "month"
  77. if item.RefreshAllData == 0 {
  78. startDate = now.AddDate(0, -item.RefreshDataNum, 0).Format(utils.FormatDate)
  79. }
  80. case "季度":
  81. filePre = "season"
  82. if item.RefreshAllData == 0 {
  83. startDate = now.AddDate(0, -item.RefreshDataNum*3, 0).Format(utils.FormatDate)
  84. }
  85. case "半年度":
  86. filePre = "half_year"
  87. if item.RefreshAllData == 0 {
  88. startDate = now.AddDate(0, -item.RefreshDataNum*6, 0).Format(utils.FormatDate)
  89. }
  90. case "年度":
  91. filePre = "year"
  92. if item.RefreshAllData == 0 {
  93. startDate = now.AddDate(-item.RefreshDataNum, 0, 0).Format(utils.FormatDate)
  94. }
  95. default:
  96. errMsgList = append(errMsgList, fmt.Sprintf("异常的频度:%s", item.Frequency))
  97. continue
  98. }
  99. // 加入到待刷新列表
  100. err = indexMergeV2(item.Frequency, startDate, endDate, filePre)
  101. if err != nil {
  102. errMsgList = append(errMsgList, fmt.Sprintf("合并刷新%s指标失败,err:%s", item.Frequency, err.Error()))
  103. }
  104. }
  105. fmt.Println("刷新结束")
  106. }
  107. // getPreviousHalfHour
  108. // @Description: 根据传入的时间获取该时间的前整半小时的时间字符串
  109. // @author: Roc
  110. // @datetime 2024-01-09 14:27:34
  111. // @param now time.Time
  112. // @return string
  113. func getPreviousHalfHour(now time.Time) string {
  114. minute := now.Minute()
  115. if minute >= 30 {
  116. return fmt.Sprintf("%02d:%02d", now.Hour(), 30)
  117. }
  118. return fmt.Sprintf("%02d:%02d", now.Hour(), 0)
  119. }
  120. // checkRefreshFrequency
  121. // @Description: 根据当前日期、配置的刷新的频度、配置指定刷新的天数来判断当天是否要刷新
  122. // @author: Roc
  123. // @datetime 2024-03-04 10:01:57
  124. // @param now time.Time
  125. // @param refreshFrequency string
  126. // @param refreshTime string
  127. // @param refreshFrequencyDay int
  128. // @return isHandler bool
  129. func checkRefreshFrequency(now time.Time, refreshFrequency string, refreshFrequencyDay int) (isHandler bool) {
  130. //isHandler = true
  131. var dayNum int
  132. var isLastDay bool
  133. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  134. switch refreshFrequency {
  135. case "每自然日":
  136. // 自然日,直接判断时间能不能匹配上即可
  137. case "每交易日":
  138. // 周六日不处理
  139. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  140. return
  141. }
  142. case "每周":
  143. currWeekDay := now.Weekday()
  144. if currWeekDay == time.Sunday {
  145. currWeekDay = 7
  146. isLastDay = true
  147. }
  148. dayNum = int(currWeekDay)
  149. case "每旬":
  150. currDay := now.Day()
  151. if currDay <= 10 {
  152. dayNum = currDay
  153. // 如果是这旬的最后一天
  154. if currDay == 10 {
  155. isLastDay = true
  156. }
  157. } else if currDay <= 20 {
  158. dayNum = currDay - 10
  159. // 如果是这旬的最后一天
  160. if currDay == 20 {
  161. isLastDay = true
  162. }
  163. } else {
  164. dayNum = currDay - 20
  165. // 当月的最后一天
  166. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  167. // 如果是这旬的最后一天
  168. if currDay == monthLastDay.Day() {
  169. isLastDay = true
  170. }
  171. }
  172. case "每月":
  173. // 当前日期
  174. currDay := now.Day()
  175. dayNum = currDay
  176. // 当期的最后一天
  177. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  178. // 如果是这期的最后一天
  179. if currDay == monthLastDay.Day() {
  180. isLastDay = true
  181. }
  182. case "每季":
  183. // 当期的第一天 ; 当期的最后一天
  184. var startDay, endDay time.Time
  185. currMonth := now.Month()
  186. currDay := now.Day()
  187. if currMonth <= 3 {
  188. // 当季的第一天
  189. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  190. // 当季的最后一天
  191. endDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  192. } else if currMonth <= 6 {
  193. // 当期的第一天
  194. startDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local)
  195. // 当期的最后一天
  196. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  197. } else if currMonth <= 9 {
  198. // 当期的第一天
  199. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  200. // 当期的最后一天
  201. endDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  202. } else {
  203. // 当期的第一天
  204. startDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local)
  205. // 当期的最后一天
  206. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  207. }
  208. // 计算这期的第一天和当日的天数
  209. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  210. // 如果是这期的最后一天
  211. if currMonth == endDay.Month() && currDay == endDay.Day() {
  212. isLastDay = true
  213. }
  214. case "每半年":
  215. // 当期的第一天 ; 当期的最后一天
  216. var startDay, endDay time.Time
  217. currMonth := now.Month()
  218. currDay := now.Day()
  219. if currMonth <= 6 {
  220. // 当期的第一天
  221. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  222. // 当期的最后一天
  223. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  224. } else {
  225. // 当期的第一天
  226. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  227. // 当期的最后一天
  228. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  229. }
  230. // 计算这期的第一天和当日的天数
  231. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  232. // 如果是这期的最后一天
  233. if currMonth == endDay.Month() && currDay == endDay.Day() {
  234. isLastDay = true
  235. }
  236. case "每年":
  237. currMonth := now.Month()
  238. currDay := now.Day()
  239. // 当期的第一天
  240. startDay := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  241. // 当期的最后一天
  242. endDay := time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  243. // 计算这期的第一天和当日的天数
  244. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  245. // 如果是这期的最后一天
  246. if currMonth == endDay.Month() && currDay == endDay.Day() {
  247. isLastDay = true
  248. }
  249. }
  250. // 如果是这期的最后一天,那么就是判断refresh_frequency_day是否配置为0,或者配置的天数大于这期的最大天数
  251. // 如果配置项不是0,且配置项的天数小于当天的天数,那么就不处理
  252. if isLastDay {
  253. if refreshFrequencyDay != 0 && refreshFrequencyDay < dayNum {
  254. return
  255. }
  256. } else {
  257. // 如果不是这期的最后一天,那么就是判断refresh_frequency_day是否等于配置的天数
  258. if refreshFrequencyDay != dayNum {
  259. return
  260. }
  261. }
  262. isHandler = true
  263. return
  264. }