base_from_wind.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hongze_edb_lib/services/alarm_msg"
  8. "hongze/hongze_edb_lib/utils"
  9. "time"
  10. )
  11. type EdbDataFromWind struct {
  12. Close map[string]float64 `json:"CLOSE"`
  13. Dt map[string]int64 `json:"DT"`
  14. ErrorCode map[string]int64 `json:"ErrorCode"`
  15. ErrMsg string
  16. }
  17. // GetEdbDataFromWind 获取wind数据
  18. func GetEdbDataFromWind(edbCode, startDate, endDate string) (item *EdbDataFromWind, errorCode int, err error) {
  19. windUrl, err := GetWindUrl(edbCode)
  20. if err != nil {
  21. errorCode = 421
  22. go alarm_msg.SendAlarmMsg(fmt.Sprintf("获取wind服务器地址失败,err:%s", err.Error()), 3)
  23. return
  24. }
  25. thsUrl := windUrl + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  26. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  27. utils.FileLog.Info(fmt.Sprintf("windUrl:%s", thsUrl))
  28. body, err := http.Get(thsUrl)
  29. fmt.Println("GetEdbDataByWind body:")
  30. fmt.Println(string(body))
  31. utils.FileLog.Info(fmt.Sprint("wind result:", string(body)))
  32. if err != nil {
  33. return
  34. }
  35. item = new(EdbDataFromWind)
  36. err = json.Unmarshal(body, &item)
  37. //异常的话,需要邮件通知
  38. if len(item.ErrorCode) > 0 {
  39. if item.ErrorCode["0"] != 0 {
  40. if item.ErrorCode["0"] == -40522017 {
  41. // 设置服务器已超限
  42. SetIsLimitEdbCodeInWindUrl(windUrl)
  43. err = DeleteEdbCodeInWindUrl(edbCode)
  44. if err != nil {
  45. return
  46. }
  47. return GetEdbDataFromWind(edbCode, startDate, endDate)
  48. } else {
  49. go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d", edbCode, item.ErrorCode["0"]), 3)
  50. }
  51. }
  52. }
  53. return
  54. }
  55. // GetWindUrl 获取wind的url
  56. func GetWindUrl(edbCode string) (windUrl string, err error) {
  57. defer func() {
  58. if err == nil && windUrl == "" {
  59. err = errors.New("获取wind服务器地址失败,指标超限了")
  60. }
  61. }()
  62. //从缓存中获取
  63. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  64. windUrl, _ = utils.Rc.RedisString(cacheKey)
  65. if windUrl != "" {
  66. return
  67. }
  68. //如果缓存中没有的话,那么从配置中获取
  69. for _, windUrlMap := range utils.Hz_Wind_Data_Url_LIST {
  70. //判断该url是否被占满了
  71. //count, tmpErr := GetCountEdbCodeInWindUrl(windUrlMap.Url)
  72. //if tmpErr != nil && tmpErr.Error() != "nil returned" {
  73. // err = tmpErr
  74. // return
  75. //}
  76. //if count < windUrlMap.Num {
  77. // windUrl = windUrlMap.Url
  78. // AddEdbCodeInWindUrl(windUrlMap.Url, edbCode)
  79. // return
  80. //}
  81. //如果超限了,那么进入下一循环
  82. isLimit, tmpErr := GetIsLimitEdbCodeInWindUrl(windUrlMap.Url)
  83. if isLimit {
  84. err = tmpErr
  85. continue
  86. }
  87. windUrl = windUrlMap.Url
  88. AddEdbCodeInWindUrl(windUrlMap.Url, edbCode)
  89. return
  90. }
  91. return
  92. }
  93. // GetCountEdbCodeInWindUrl 从缓存key中获取已经插入入的指标数
  94. func GetCountEdbCodeInWindUrl(windUrl string) (num int, err error) {
  95. cacheKey := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  96. num, err = utils.Rc.RedisInt(cacheKey)
  97. if err != nil && err.Error() == "redigo: nil returned" {
  98. err = nil
  99. }
  100. return
  101. }
  102. // GetIsLimitEdbCodeInWindUrl 从缓存key中获取是否超限
  103. func GetIsLimitEdbCodeInWindUrl(windUrl string) (isLimit bool, err error) {
  104. cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  105. num, err := utils.Rc.RedisInt(cacheKey)
  106. if err != nil && err.Error() == "redigo: nil returned" {
  107. err = nil
  108. }
  109. if num > 0 {
  110. isLimit = true
  111. }
  112. return
  113. }
  114. // SetIsLimitEdbCodeInWindUrl 设置服务器已超限
  115. func SetIsLimitEdbCodeInWindUrl(windUrl string) {
  116. cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  117. _ = utils.Rc.SetNX(cacheKey, 1, utils.GetTodayLastSecond())
  118. return
  119. }
  120. // AddEdbCodeInWindUrl 将指标插入到缓存key中
  121. // @return isInsert bool 是否插入数据,true时为插入数据,false表示数据已存在
  122. func AddEdbCodeInWindUrl(windUrl, edbCode string) (isInsert bool) {
  123. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  124. isInsert = utils.Rc.SetNX(cacheKey, windUrl, utils.GetTodayLastSecond())
  125. cacheKey2 := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  126. utils.Rc.Incrby(cacheKey2, 1)
  127. return
  128. }
  129. // DeleteEdbCodeInWindUrl 删除指标编码 服务器归属 缓存
  130. func DeleteEdbCodeInWindUrl(edbCode string) (err error) {
  131. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  132. err = utils.Rc.Delete(cacheKey)
  133. return
  134. }