base_edb_lib.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // AddEdbData 新增指标数据
  13. func AddEdbData(source int, edbCode string) (resp *models.BaseResponse, err error) {
  14. param := make(map[string]interface{})
  15. param["EdbCode"] = edbCode
  16. urlStr := ``
  17. switch source {
  18. case utils.DATA_SOURCE_THS:
  19. urlStr = "ths/add"
  20. case utils.DATA_SOURCE_WIND:
  21. urlStr = "wind/add"
  22. case utils.DATA_SOURCE_PB:
  23. urlStr = "pb/add"
  24. case utils.DATA_SOURCE_PB_FINANCE:
  25. urlStr = "pb_finance/add"
  26. case utils.DATA_SOURCE_MANUAL:
  27. urlStr = "manual/add"
  28. case utils.DATA_SOURCE_LZ:
  29. urlStr = "lz/add"
  30. case utils.DATA_SOURCE_YS:
  31. urlStr = "smm/add"
  32. case utils.DATA_SOURCE_GL:
  33. urlStr = "mysteel/add"
  34. case utils.DATA_SOURCE_ZZ:
  35. urlStr = "zz/add"
  36. case utils.DATA_SOURCE_DL:
  37. urlStr = "dl/add"
  38. case utils.DATA_SOURCE_SH:
  39. urlStr = "sh/add"
  40. case utils.DATA_SOURCE_CFFEX:
  41. urlStr = "cffex/add"
  42. case utils.DATA_SOURCE_SHFE:
  43. urlStr = "shfe/add"
  44. case utils.DATA_SOURCE_GIE:
  45. urlStr = "gie/add"
  46. case utils.DATA_SOURCE_LT:
  47. urlStr = "lt/add"
  48. case utils.DATA_SOURCE_COAL:
  49. urlStr = "coal/add"
  50. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  51. urlStr = "google_travel/add"
  52. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  53. urlStr = "mysteel_chemical/add"
  54. case utils.DATA_SOURCE_EIA_STEO:
  55. urlStr = "eia_steo/add"
  56. case utils.DATA_SOURCE_COM_TRADE:
  57. urlStr = "com_trade/add"
  58. case utils.DATA_SOURCE_SCI:
  59. urlStr = "sci/add"
  60. case utils.DATA_SOURCE_BAIINFO:
  61. urlStr = "baiinfo/add"
  62. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  63. urlStr = "national_statistics/add"
  64. case utils.DATA_SOURCE_FUBAO:
  65. urlStr = "fubao/add"
  66. default:
  67. }
  68. if urlStr == "" {
  69. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  70. return
  71. }
  72. resp, err = postRefreshEdbData(param, urlStr)
  73. return
  74. }
  75. type AddPredictEdbDataResponse struct {
  76. Ret int
  77. Msg string
  78. ErrMsg string
  79. ErrCode string
  80. Data data_manage.AddEdbInfoResp
  81. Success bool `description:"true 执行成功,false 执行失败"`
  82. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  83. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  84. }
  85. // SaveBasePredictEdbData 新增/编辑预测指标运算
  86. func SaveBasePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  87. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict/save")
  88. err = json.Unmarshal(resultByte, &resp)
  89. if err != nil {
  90. return
  91. }
  92. return
  93. }
  94. // SavePredictEdbData 新增/编辑预测指标运算
  95. func SavePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  96. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/save")
  97. err = json.Unmarshal(resultByte, &resp)
  98. if err != nil {
  99. return
  100. }
  101. return
  102. }
  103. // BatchSavePredictEdbData 新增/编辑 预测指标运算(同比、同差)
  104. func BatchSavePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  105. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/batch/save")
  106. if err != nil {
  107. return
  108. }
  109. err = json.Unmarshal(resultByte, &resp)
  110. if err != nil {
  111. return
  112. }
  113. return
  114. }
  115. // RefreshEdbData 刷新指标数据
  116. func RefreshEdbData(edbInfoId, source int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  117. param := make(map[string]interface{})
  118. param["EdbCode"] = edbCode
  119. param["EdbInfoId"] = edbInfoId
  120. param["StartDate"] = startDate
  121. urlStr := ``
  122. switch source {
  123. case utils.DATA_SOURCE_THS:
  124. urlStr = "ths/refresh"
  125. case utils.DATA_SOURCE_WIND:
  126. urlStr = "wind/refresh"
  127. case utils.DATA_SOURCE_PB:
  128. urlStr = "pb/refresh"
  129. case utils.DATA_SOURCE_PB_FINANCE:
  130. urlStr = "pb_finance/refresh"
  131. case utils.DATA_SOURCE_MANUAL:
  132. urlStr = "manual/refresh"
  133. case utils.DATA_SOURCE_LZ:
  134. urlStr = "lz/refresh"
  135. case utils.DATA_SOURCE_YS:
  136. urlStr = "smm/refresh"
  137. case utils.DATA_SOURCE_GL:
  138. urlStr = "mysteel/refresh"
  139. case utils.DATA_SOURCE_ZZ:
  140. urlStr = "zz/refresh"
  141. case utils.DATA_SOURCE_DL:
  142. urlStr = "dl/refresh"
  143. case utils.DATA_SOURCE_SH:
  144. urlStr = "sh/refresh"
  145. case utils.DATA_SOURCE_CFFEX:
  146. urlStr = "cffex/refresh"
  147. case utils.DATA_SOURCE_SHFE:
  148. urlStr = "shfe/refresh"
  149. case utils.DATA_SOURCE_GIE:
  150. urlStr = "gie/refresh"
  151. case utils.DATA_SOURCE_LT:
  152. urlStr = "lt/refresh"
  153. case utils.DATA_SOURCE_COAL:
  154. urlStr = "coal/refresh"
  155. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  156. urlStr = "google_travel/refresh"
  157. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  158. urlStr = "mysteel_chemical/refresh"
  159. case utils.DATA_SOURCE_EIA_STEO:
  160. urlStr = "eia_steo/refresh"
  161. case utils.DATA_SOURCE_PREDICT:
  162. urlStr = "predict/refresh"
  163. case utils.DATA_SOURCE_COM_TRADE:
  164. urlStr = "com_trade/refresh"
  165. case utils.DATA_SOURCE_SCI:
  166. urlStr = "sci/refresh"
  167. case utils.DATA_SOURCE_BAIINFO:
  168. urlStr = "baiinfo/refresh"
  169. case utils.DATA_SOURCE_STOCK_PLANT:
  170. urlStr = "stock_plant/refresh"
  171. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  172. urlStr = "national_statistics/refresh"
  173. case utils.DATA_SOURCE_FUBAO:
  174. urlStr = "fubao/refresh"
  175. }
  176. if urlStr == "" {
  177. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  178. return
  179. }
  180. resp, err = postRefreshEdbData(param, urlStr)
  181. return
  182. }
  183. // RefreshEdbCalculateData 刷新普通计算指标数据请求
  184. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  185. param := make(map[string]interface{})
  186. param["EdbCode"] = edbCode
  187. param["EdbInfoId"] = edbInfoId
  188. param["StartDate"] = startDate
  189. resp, err = postRefreshEdbData(param, "calculate/refresh")
  190. return
  191. }
  192. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  193. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  194. param := make(map[string]interface{})
  195. param["EdbCode"] = edbCode
  196. param["EdbInfoId"] = edbInfoId
  197. param["StartDate"] = startDate
  198. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  199. return
  200. }
  201. // AddEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
  202. func AddEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  203. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/add")
  204. err = json.Unmarshal(resultByte, &resp)
  205. if err != nil {
  206. return
  207. }
  208. return
  209. }
  210. // EditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
  211. func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  212. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/edit")
  213. err = json.Unmarshal(resultByte, &resp)
  214. if err != nil {
  215. return
  216. }
  217. return
  218. }
  219. // BatchSaveEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
  220. func BatchSaveEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  221. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/save")
  222. err = json.Unmarshal(resultByte, &resp)
  223. if err != nil {
  224. return
  225. }
  226. return
  227. }
  228. // BatchEditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
  229. func BatchEditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  230. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/edit")
  231. err = json.Unmarshal(resultByte, &resp)
  232. if err != nil {
  233. return
  234. }
  235. return
  236. }
  237. //// EditEdbCalculateData 修改计算指标数据请求
  238. //func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *models.BaseResponse, err error) {
  239. // resp, _, err = postAddEdbData(edbInfoCalculateBatchSaveReqStr, "batch/save")
  240. // return
  241. //}
  242. // ExecPythonCode 执行python代码
  243. func ExecPythonCode(pythonCode string) (resp *models.BaseResponse, err error) {
  244. param := make(map[string]interface{})
  245. param["PythonCode"] = pythonCode
  246. urlStr := `/python/exec`
  247. resp, err = postRefreshEdbData(param, urlStr)
  248. return
  249. }
  250. // AddPythonEdbData 新增python指标
  251. func AddPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  252. urlStr := ``
  253. urlStr = "python/add"
  254. resp, _, err = postAddEdbData(param, urlStr)
  255. return
  256. }
  257. // EditPythonEdbData 编辑python指标
  258. func EditPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  259. urlStr := ``
  260. urlStr = "python/edit"
  261. resp, _, err = postAddEdbData(param, urlStr)
  262. return
  263. }
  264. // SaveAdjustEdbInfo 保存数据调整指标
  265. func SaveAdjustEdbInfo(param string) (resp *models.BaseResponse, err error) {
  266. urlStr := ``
  267. urlStr = "calculate/adjust/save"
  268. resp, _, err = postAddEdbData(param, urlStr)
  269. return
  270. }
  271. // CalculateComputeCorrelationResp 拟合残差计算相关性的值返回
  272. type CalculateComputeCorrelationResp struct {
  273. Ret int
  274. Msg string
  275. ErrMsg string
  276. ErrCode string
  277. Data float64
  278. Success bool `description:"true 执行成功,false 执行失败"`
  279. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  280. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  281. }
  282. // CalculateComputeCorrelation 拟合残差计算相关性的值
  283. func CalculateComputeCorrelation(edbInfoCalculateBatchSaveReqStr string) (resp *CalculateComputeCorrelationResp, err error) {
  284. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/compute_correlation")
  285. err = json.Unmarshal(resultByte, &resp)
  286. if err != nil {
  287. return
  288. }
  289. return
  290. }
  291. // PredictCalculateComputeCorrelation 拟合残差计算相关性的值(预测指标)
  292. func PredictCalculateComputeCorrelation(edbInfoCalculateBatchSaveReqStr string) (resp *CalculateComputeCorrelationResp, err error) {
  293. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/compute_correlation")
  294. err = json.Unmarshal(resultByte, &resp)
  295. if err != nil {
  296. return
  297. }
  298. return
  299. }
  300. // postRefreshEdbData 刷新指标数据
  301. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  302. postUrl := utils.EDB_LIB_URL + urlStr
  303. postData, err := json.Marshal(param)
  304. if err != nil {
  305. return
  306. }
  307. result, err := HttpPost(postUrl, string(postData), "application/json")
  308. if err != nil {
  309. return
  310. }
  311. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  312. err = json.Unmarshal(result, &resp)
  313. if err != nil {
  314. return
  315. }
  316. return resp, nil
  317. }
  318. // postAddEdbData 新增指标数据
  319. func postAddEdbData(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  320. postUrl := utils.EDB_LIB_URL + urlStr
  321. result, err = HttpPost(postUrl, paramStr, "application/json")
  322. if err != nil {
  323. return
  324. }
  325. err = json.Unmarshal(result, &resp)
  326. if err != nil {
  327. return
  328. }
  329. return
  330. }
  331. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  332. body := ioutil.NopCloser(strings.NewReader(postData))
  333. client := &http.Client{}
  334. req, err := http.NewRequest("POST", url, body)
  335. if err != nil {
  336. return nil, err
  337. }
  338. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  339. if len(params) > 0 && params[0] != "" {
  340. contentType = params[0]
  341. }
  342. req.Header.Set("Content-Type", contentType)
  343. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  344. resp, err := client.Do(req)
  345. defer resp.Body.Close()
  346. b, err := ioutil.ReadAll(resp.Body)
  347. fmt.Println("HttpPost:" + string(b))
  348. return b, err
  349. }