pcsg_bloomberg.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package index_data
  2. import (
  3. "encoding/json"
  4. "eta/eta_bridge/global"
  5. "eta/eta_bridge/models/pcsg"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. )
  10. // GetPCSGBloombergGeneralIndex 获取指标信息
  11. func GetPCSGBloombergGeneralIndex(taskKey, frequency string) (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  12. apiData, e := CurlPCSGBloombergGeneralDataApi(taskKey)
  13. if e != nil {
  14. err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
  15. return
  16. }
  17. if len(apiData) == 0 {
  18. return
  19. }
  20. for _, v := range apiData {
  21. switch taskKey {
  22. case "IDpcsgDailyRun11":
  23. tList := pcsg.FormatPythonBloombergDailyRun11Data2Base(v, frequency, taskKey)
  24. indexes = append(indexes, tList...)
  25. case "IDpcsgDailyRunHistV1":
  26. t := pcsg.FormatPythonBloombergDailyRunHistV1Data2Base(v, frequency, taskKey)
  27. if t.IndexCode != "" {
  28. indexes = append(indexes, t)
  29. }
  30. default:
  31. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency, taskKey)
  32. if t.IndexCode != "" {
  33. indexes = append(indexes, t)
  34. }
  35. }
  36. /*if taskKey == "IDpcsgDailyRun11" {
  37. tList := pcsg.FormatPythonBloombergDailyRun11Data2Base(v, frequency, taskKey)
  38. indexes = append(indexes, tList...)
  39. } else {
  40. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency, taskKey)
  41. if t.IndexCode != "" {
  42. indexes = append(indexes, t)
  43. }
  44. }*/
  45. }
  46. return
  47. }
  48. // CurlPCSGBloombergGeneralDataApi 请求通用格式数据接口
  49. func CurlPCSGBloombergGeneralDataApi(taskKey string) (resultData []pcsg.PythonBloombergGeneralData, err error) {
  50. if taskKey == "" {
  51. err = fmt.Errorf("任务Key为空")
  52. return
  53. }
  54. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  55. err = fmt.Errorf("服务地址为空")
  56. return
  57. }
  58. url := fmt.Sprintf("%s/api/bloomberg/general_data?task_key=%s", global.CONFIG.PCSG.BloombergApiUrl, taskKey)
  59. resp, e := http.Get(url)
  60. if e != nil {
  61. err = fmt.Errorf("http post err: %s", e.Error())
  62. return
  63. }
  64. defer resp.Body.Close()
  65. b, e := ioutil.ReadAll(resp.Body)
  66. if e != nil {
  67. err = fmt.Errorf("resp body read err: %s", e.Error())
  68. return
  69. }
  70. if len(b) == 0 {
  71. err = fmt.Errorf("resp body is empty")
  72. return
  73. }
  74. //str := `{"Code":200,"Msg":"","Data":[{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-09T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B001","RC":0,"DATE":"2024-05-09","PX_LAST":1183.88},{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-10T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B001","RC":0,"DATE":"2024-05-10","PX_LAST":1182.79},{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-09T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B002","RC":0,"DATE":"2024-05-10","PX_LAST":1162.63}]}`
  75. //b := []byte(str)
  76. result := new(pcsg.PythonBloombergGeneralResult)
  77. if e = json.Unmarshal(b, &result); e != nil {
  78. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  79. return
  80. }
  81. if result.Code != 200 {
  82. err = fmt.Errorf("result: %s", string(b))
  83. return
  84. }
  85. resultData = result.Data
  86. return
  87. }