base_from_wind.go 7.4 KB

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