base_from_wind_wsd.go 5.8 KB

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