base_from_wind_bridge.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // GetEdbDataFromBridgeJy 从桥接服务里的嘉悦物产数据中心获取数据
  13. func GetEdbDataFromBridgeJy(windUrl, edbCode, startDate, endDate string) (index *models.EdbDataFromWindBridgeIndex, err error) {
  14. if windUrl == `` {
  15. windUrl, err = GetWindBridgeUrl()
  16. if err != nil {
  17. return
  18. }
  19. }
  20. source := "wind"
  21. index, err = getEdbDataFromBridgeJy(windUrl, source, edbCode, startDate, endDate)
  22. return
  23. }
  24. // GetWindBridgeUrl 获取桥接服务地址
  25. func GetWindBridgeUrl() (windUrl string, err error) {
  26. defer func() {
  27. if err == nil && windUrl == "" {
  28. err = errors.New("wind桥接服务器地址为空")
  29. }
  30. }()
  31. // 先配置中获取,如果有指定配置的话,那么取配置中的数据
  32. tmpConfig, err := models.GetConfigDetailByCode("wind_from_bridge")
  33. if err != nil && err.Error() != utils.ErrNoRow() {
  34. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  35. return
  36. }
  37. err = nil
  38. if tmpConfig != nil {
  39. windFrom := tmpConfig.ConfigValue
  40. if windFrom != "" {
  41. tmpConfig, err = models.GetConfigDetailByCode(windFrom)
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  44. return
  45. }
  46. if err == nil {
  47. windUrl = tmpConfig.ConfigValue
  48. }
  49. err = nil
  50. }
  51. return
  52. }
  53. return
  54. }
  55. // GetManualBridgeUrl 获取桥接服务地址
  56. func GetManualBridgeUrl() (manualUrl string, err error) {
  57. defer func() {
  58. if err == nil && manualUrl == "" {
  59. err = errors.New("wind桥接服务器地址为空")
  60. }
  61. }()
  62. // 先配置中获取,如果有指定配置的话,那么取配置中的数据
  63. tmpConfig, err := models.GetConfigDetailByCode("manual_from_bridge")
  64. if err != nil && err.Error() != utils.ErrNoRow() {
  65. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  66. return
  67. }
  68. err = nil
  69. if tmpConfig != nil {
  70. windFrom := tmpConfig.ConfigValue
  71. tmpConfig, err = models.GetConfigDetailByCode(windFrom)
  72. if err != nil && err.Error() != utils.ErrNoRow() {
  73. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  74. return
  75. }
  76. manualUrl = tmpConfig.ConfigValue
  77. return
  78. }
  79. return
  80. }
  81. // GetManualEdbDataFromBridgeJy 从桥接服务里的嘉悦物产数据中心获取数据
  82. func GetManualEdbDataFromBridgeJy(windUrl, edbCode, startDate, endDate string) (list []*models.ManualEdbdata, err error) {
  83. if windUrl == `` {
  84. windUrl, err = GetManualBridgeUrl()
  85. if err != nil {
  86. return
  87. }
  88. }
  89. source := "manual"
  90. index, err := getEdbDataFromBridgeJy(windUrl, source, edbCode, startDate, endDate)
  91. if err != nil {
  92. return
  93. }
  94. for _, v := range index.IndexData {
  95. tmp := new(models.ManualEdbdata)
  96. t, _ := time.ParseInLocation(utils.FormatDateTime, v.UpdateTime, time.Local)
  97. tmp = &models.ManualEdbdata{
  98. TradeCode: index.IndexCode,
  99. Dt: v.DataTime,
  100. Close: fmt.Sprintf("%f", v.Val),
  101. ModifyTime: t,
  102. }
  103. list = append(list, tmp)
  104. }
  105. return
  106. }
  107. // getEdbDataFromBridgeJy 从桥接服务里的嘉悦物产数据中心获取数据
  108. func getEdbDataFromBridgeJy(url, source, edbCode, startDate, endDate string) (index *models.EdbDataFromWindBridgeIndex, err error) {
  109. requestWindUrl := url + `?index_code=%s&start_date=%s&end_date=%s&source=%s`
  110. requestWindUrl = fmt.Sprintf(requestWindUrl, edbCode, startDate, endDate, source)
  111. utils.FileLog.Info(fmt.Sprintf("windUrl:%s", requestWindUrl))
  112. body, err := http.Get(requestWindUrl)
  113. fmt.Println("GetEdbDataFromBridgeJy body:")
  114. fmt.Println(string(body))
  115. utils.FileLog.Info(fmt.Sprint("GetEdbDataFromBridgeJy result:", string(body)))
  116. if err != nil {
  117. return
  118. }
  119. ret := new(models.EdbDataFromWindBridge)
  120. err = json.Unmarshal(body, &ret)
  121. if err != nil {
  122. err = errors.New("返回值格式错误,json解析失败,Err: " + err.Error())
  123. return
  124. }
  125. //异常的话,需要邮件通知
  126. if ret.Code != 200 {
  127. errMsg := fmt.Sprintf(" 桥接服务 指标数据接口报错,地址:%s, Code: %d, Err: %s", requestWindUrl, ret.Code, ret.Msg)
  128. err = errors.New(errMsg)
  129. go alarm_msg.SendAlarmMsg(errMsg, 3)
  130. return
  131. }
  132. index = &ret.Data
  133. return
  134. }