base_from_wind_wsd.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, apiErrMsg string, 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. apiErrMsg = fmt.Sprintf("ErrorCode:%d, wind数据接口超限", item.ErrorCode["0"])
  52. return GetEdbDataFromWind(edbCode, startDate, endDate)
  53. } else if item.ErrorCode["0"] == -40520005 {
  54. //.ErrorCode=-40520005
  55. //.Data=[No Python API Authority
  56. SetIsLimitEdbCodeInWindUrl(windUrl)
  57. err = DeleteEdbCodeInWindUrl(edbCode)
  58. if err != nil {
  59. return
  60. }
  61. apiErrMsg = fmt.Sprintf("ErrorCode:%d, 无API接口权限", item.ErrorCode["0"])
  62. go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
  63. return GetEdbDataFromWind(edbCode, startDate, endDate)
  64. } else if item.ErrorCode["0"] == WindNoAuthCode {
  65. // 指标下架, 无权限
  66. apiErrMsg = fmt.Sprintf("ErrorCode:%d, 指标已下架", item.ErrorCode["0"])
  67. return nil, WindNoAuthCode, apiErrMsg, nil
  68. } else {
  69. apiErrMsg = fmt.Sprintf("ErrorCode:%d, wind数据服务异常", item.ErrorCode["0"])
  70. go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
  71. }
  72. }
  73. }
  74. return
  75. }
  76. // GetEdbDataFromWindUrlWsd 通过url获取wind wsd数据
  77. func GetEdbDataFromWindUrlWsd(windUrl, stockCode, edbCode, startDate, endDate string) (item map[string]map[string]interface{}, errorCode int, apiErrMsg string, err error) {
  78. requestWindUrl := windUrl + `edbInfo/wind/wsd?StockCode=%s&EdbCode=%s&StartDate=%s&EndDate=%s`
  79. requestWindUrl = fmt.Sprintf(requestWindUrl, stockCode, edbCode, startDate, endDate)
  80. utils.FileLog.Info(fmt.Sprintf("windUrl:%s", requestWindUrl))
  81. body, err := http.Get(requestWindUrl)
  82. utils.FileLog.Info(fmt.Sprint("wind result:", string(body)))
  83. err = json.Unmarshal(body, &item)
  84. if err != nil {
  85. apiErrMsg = fmt.Sprintf("服务异常")
  86. return
  87. }
  88. return
  89. }
  90. //
  91. //// GetWindUrl 获取wind的url
  92. //func GetWindUrl(edbCode string) (windUrl string, err error) {
  93. // defer func() {
  94. // if err == nil && windUrl == "" {
  95. // err = errors.New("获取wind服务器地址失败,指标超限了")
  96. // }
  97. // }()
  98. // //从缓存中获取
  99. // cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  100. // windUrl, _ = utils.Rc.RedisString(cacheKey)
  101. // if windUrl != "" {
  102. // return
  103. // }
  104. // //如果缓存中没有的话,那么从配置中获取
  105. // for _, windUrlMap := range utils.Hz_Wind_Data_Url_LIST {
  106. // //判断该url是否被占满了
  107. // //count, tmpErr := GetCountEdbCodeInWindUrl(windUrlMap.Url)
  108. // //if tmpErr != nil && tmpErr.Error() != "nil returned" {
  109. // // err = tmpErr
  110. // // return
  111. // //}
  112. // //if count < windUrlMap.Num {
  113. // // windUrl = windUrlMap.Url
  114. // // AddEdbCodeInWindUrl(windUrlMap.Url, edbCode)
  115. // // return
  116. // //}
  117. // //如果超限了,那么进入下一循环
  118. // isLimit, tmpErr := GetIsLimitEdbCodeInWindUrl(windUrlMap.Url)
  119. // if isLimit {
  120. // err = tmpErr
  121. // continue
  122. // }
  123. //
  124. // windUrl = windUrlMap.Url
  125. // AddEdbCodeInWindUrl(windUrlMap.Url, edbCode)
  126. // return
  127. // }
  128. // return
  129. //}
  130. //
  131. //// GetCountEdbCodeInWindUrl 从缓存key中获取已经插入入的指标数
  132. //func GetCountEdbCodeInWindUrl(windUrl string) (num int, err error) {
  133. // cacheKey := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  134. // num, err = utils.Rc.RedisInt(cacheKey)
  135. // if err != nil && err.Error() == "redigo: nil returned" {
  136. // err = nil
  137. // }
  138. // return
  139. //}
  140. //
  141. //// GetIsLimitEdbCodeInWindUrl 从缓存key中获取是否超限
  142. //func GetIsLimitEdbCodeInWindUrl(windUrl string) (isLimit bool, err error) {
  143. // cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  144. //
  145. // num, err := utils.Rc.RedisInt(cacheKey)
  146. // if err != nil && err.Error() == "redigo: nil returned" {
  147. // err = nil
  148. // }
  149. // if num > 0 {
  150. // isLimit = true
  151. // }
  152. // return
  153. //}
  154. //
  155. //// SetIsLimitEdbCodeInWindUrl 设置服务器已超限
  156. //func SetIsLimitEdbCodeInWindUrl(windUrl string) {
  157. // cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  158. // _ = utils.Rc.SetNX(cacheKey, 1, utils.GetTodayLastSecond())
  159. // return
  160. //}
  161. //
  162. //// AddEdbCodeInWindUrl 将指标插入到缓存key中
  163. //// @return isInsert bool 是否插入数据,true时为插入数据,false表示数据已存在
  164. //func AddEdbCodeInWindUrl(windUrl, edbCode string) (isInsert bool) {
  165. // cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  166. // isInsert = utils.Rc.SetNX(cacheKey, windUrl, utils.GetTodayLastSecond())
  167. //
  168. // cacheKey2 := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  169. // utils.Rc.Incrby(cacheKey2, 1)
  170. // return
  171. //}
  172. //
  173. //// DeleteEdbCodeInWindUrl 删除指标编码 服务器归属 缓存
  174. //func DeleteEdbCodeInWindUrl(edbCode string) (err error) {
  175. // cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  176. // err = utils.Rc.Delete(cacheKey)
  177. // return
  178. //}