base_from_wind.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, err error) {
  19. windUrl, err := GetWindUrl(edbCode)
  20. if err != nil {
  21. go alarm_msg.SendAlarmMsg(fmt.Sprintf("获取wind服务器地址失败,err:%s", err.Error()), 3)
  22. return
  23. }
  24. thsUrl := windUrl + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  25. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  26. utils.FileLog.Info(fmt.Sprintf("windUrl:%s", thsUrl))
  27. body, err := http.Get(thsUrl)
  28. fmt.Println("GetEdbDataByWind body:")
  29. fmt.Println(string(body))
  30. utils.FileLog.Info(fmt.Sprint("wind result:", string(body)))
  31. if err != nil {
  32. return
  33. }
  34. item = new(EdbDataFromWind)
  35. err = json.Unmarshal(body, &item)
  36. //异常的话,需要邮件通知
  37. if len(item.ErrorCode) > 0 {
  38. if item.ErrorCode["0"] != 0 {
  39. go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d", edbCode, item.ErrorCode["0"]), 3)
  40. }
  41. }
  42. return
  43. }
  44. // GetWindUrl 获取wind的url
  45. func GetWindUrl(edbCode string) (windUrl string, err error) {
  46. defer func() {
  47. if err == nil && windUrl == "" {
  48. err = errors.New("获取wind服务器地址失败,指标超限了")
  49. }
  50. }()
  51. //从缓存中获取
  52. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  53. windUrl, _ = utils.Rc.RedisString(cacheKey)
  54. if windUrl != "" {
  55. return
  56. }
  57. //如果缓存中没有的话,那么从配置中获取
  58. for _, windUrlMap := range utils.Hz_Wind_Data_Url_LIST {
  59. //判断该url是否被占满了
  60. count, tmpErr := GetCountEdbCodeInWindUrl(windUrlMap.Url)
  61. if tmpErr != nil && tmpErr.Error() != "nil returned" {
  62. err = tmpErr
  63. return
  64. }
  65. if count < windUrlMap.Num {
  66. windUrl = windUrlMap.Url
  67. AddEdbCodeInWindUrl(windUrlMap.Url, edbCode)
  68. return
  69. }
  70. }
  71. return
  72. }
  73. // GetCountEdbCodeInWindUrl 从缓存key中获取已经插入入的指标数
  74. func GetCountEdbCodeInWindUrl(windUrl string) (num int, err error) {
  75. cacheKey := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  76. num, err = utils.Rc.RedisInt(cacheKey)
  77. if err != nil && err.Error() == "redigo: nil returned" {
  78. err = nil
  79. }
  80. return
  81. }
  82. // AddEdbCodeInWindUrl 将指标插入到缓存key中
  83. // @return isInsert bool 是否插入数据,true时为插入数据,false表示数据已存在
  84. func AddEdbCodeInWindUrl(windUrl, edbCode string) (isInsert bool) {
  85. cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  86. isInsert = utils.Rc.SetNX(cacheKey, windUrl, utils.GetTodayLastSecond())
  87. cacheKey2 := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
  88. utils.Rc.Incrby(cacheKey2, 1)
  89. return
  90. }