base_edb_lib.go 12 KB

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