base_from_python.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // PythonController 计算指标
  15. type PythonController struct {
  16. BaseAuthController
  17. }
  18. // ExcePython
  19. // @Title 执行python代码接口
  20. // @Description 执行python代码接口
  21. // @Success 200 {object} models.ExecPythonEdbReq
  22. // @router /exec [post]
  23. func (this *PythonController) ExcePython() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. //source := utils.DATA_SOURCE_WIND
  30. var req models.ExecPythonEdbReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.PythonCode == "" {
  38. br.Msg = "请填写python代码"
  39. return
  40. }
  41. req.PythonCode, err = url.QueryUnescape(req.PythonCode)
  42. if err != nil {
  43. br.Msg = "python代码解析失败"
  44. return
  45. }
  46. edbData, err, errMsg := services.ExecPythonCode("test", req.PythonCode)
  47. if err != nil {
  48. br.Msg = "获取数据失败"
  49. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  50. if errMsg != "" {
  51. br.ErrMsg = errMsg
  52. }
  53. return
  54. }
  55. br.Ret = 200
  56. br.Success = true
  57. br.Msg = "保存成功"
  58. br.Data = edbData
  59. br.IsAddLog = true
  60. }
  61. // Add
  62. // @Title 编辑指标接口
  63. // @Description 编辑指标接口
  64. // @Success 200 {object} models.EditEdbInfoReq
  65. // @router /add [post]
  66. func (this *PythonController) Add() {
  67. br := new(models.BaseResponse).Init()
  68. defer func() {
  69. this.Data["json"] = br
  70. this.ServeJSON()
  71. }()
  72. //source := utils.DATA_SOURCE_WIND
  73. var req models.AddPythonEdbReq
  74. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  75. if err != nil {
  76. br.Msg = "参数解析异常!"
  77. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  78. return
  79. }
  80. req.EdbName = strings.Trim(req.EdbName, " ")
  81. if req.EdbName == "" {
  82. br.Msg = "指标名称不能为空"
  83. return
  84. }
  85. if req.Frequency == "" {
  86. br.Msg = "频率不能为空"
  87. return
  88. }
  89. if req.Unit == "" {
  90. br.Msg = "单位不能为空"
  91. return
  92. }
  93. if req.ClassifyId <= 0 {
  94. br.Msg = "请选择分类"
  95. return
  96. }
  97. if req.PythonCode == "" {
  98. br.Msg = "请填写python代码"
  99. return
  100. }
  101. //加入缓存机制,避免创建同一个名称的指标 start
  102. redisKey := fmt.Sprint("edb_info:python:add:", utils.DATA_SOURCE_PYTHON, ":", req.EdbName)
  103. isExist := utils.Rc.IsExist(redisKey)
  104. if isExist {
  105. br.Msg = "指标正在处理,请勿重复提交"
  106. return
  107. } else {
  108. //设置3分钟缓存
  109. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  110. defer func() {
  111. utils.Rc.Delete(redisKey)
  112. }()
  113. }
  114. //获取指标数据
  115. req.PythonCode, err = url.QueryUnescape(req.PythonCode)
  116. if err != nil {
  117. br.Msg = "python代码解析失败"
  118. return
  119. }
  120. edbData, err, errMsg := services.ExecPythonCode(req.EdbName, req.PythonCode)
  121. if err != nil {
  122. br.Msg = "获取数据失败"
  123. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  124. if errMsg != "" {
  125. br.ErrMsg = errMsg
  126. }
  127. return
  128. }
  129. var condition string
  130. var pars []interface{}
  131. condition += " AND edb_name=? "
  132. pars = append(pars, req.EdbName)
  133. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  134. if err != nil {
  135. br.Msg = "判断指标名称是否存在失败"
  136. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  137. return
  138. }
  139. if count > 0 {
  140. br.Msg = "指标名称已存在,请重新填写"
  141. br.ErrMsg = "指标名称已存在,请重新填写"
  142. return
  143. }
  144. //指标code生成
  145. randStr := utils.GetRandDigit(4)
  146. edbCode := `C` + time.Now().Format("060102") + randStr
  147. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  148. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  149. edbInfo := &models.EdbInfo{
  150. Source: utils.DATA_SOURCE_PYTHON,
  151. SourceName: "代码运算",
  152. EdbCode: edbCode,
  153. EdbName: req.EdbName,
  154. EdbNameSource: req.EdbName,
  155. Frequency: req.Frequency,
  156. Unit: req.Unit,
  157. ClassifyId: req.ClassifyId,
  158. SysUserId: req.AdminId,
  159. SysUserRealName: req.AdminName,
  160. CreateTime: time.Now(),
  161. ModifyTime: time.Now(),
  162. UniqueCode: uniqueCode,
  163. EdbType: 2,
  164. }
  165. edbInfoId, err := models.AddEdbInfo(edbInfo)
  166. if err != nil {
  167. br.Msg = "生成python运算指标失败"
  168. br.Msg = "生成python运算指标失败,AddEdbInfo Err:" + err.Error()
  169. return
  170. }
  171. edbInfo.EdbInfoId = int(edbInfoId)
  172. //处理同名指标
  173. {
  174. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  175. if err != nil {
  176. br.Msg = "保存失败"
  177. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  178. return
  179. }
  180. if len(edbNameList) >= 2 {
  181. for _, v := range edbNameList {
  182. edbName := v.EdbName + "(" + v.SourceName + ")"
  183. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  184. if err != nil {
  185. br.Msg = "保存失败"
  186. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  187. return
  188. }
  189. }
  190. }
  191. }
  192. // 存储python代码
  193. edbPythonCode := &models.EdbPythonCode{
  194. EdbInfoId: int(edbInfoId),
  195. EdbCode: edbInfo.EdbCode,
  196. PythonCode: req.PythonCode,
  197. ModifyTime: time.Now(),
  198. CreateTime: time.Now(),
  199. }
  200. _, err = models.AddEdbPythonCode(edbPythonCode)
  201. if err != nil {
  202. br.Msg = "生成python运算指标失败"
  203. br.Msg = "生成python运算指标失败,存储python代码失败,AddEdbPythonCode Err:" + err.Error()
  204. return
  205. }
  206. //匹配所有的关联指标
  207. edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)
  208. //pythonCode
  209. err = models.AddPythonEdb(edbInfo, edbData, edbInfoList)
  210. if err != nil {
  211. br.Msg = "生成python指标失败"
  212. br.Msg = "生成python指标失败,AddPythonEdb Err:" + err.Error()
  213. return
  214. }
  215. // 更新指标最大最小值
  216. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  217. if err != nil {
  218. br.Msg = errMsg
  219. br.ErrMsg = err.Error()
  220. return
  221. }
  222. resp := models.AddEdbInfoResp{
  223. EdbInfoId: int(edbInfoId),
  224. UniqueCode: uniqueCode,
  225. }
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "保存成功"
  229. br.Data = resp
  230. br.IsAddLog = true
  231. }
  232. // Edit
  233. // @Title 编辑指标接口
  234. // @Description 编辑指标接口
  235. // @Success 200 {object} models.EditEdbInfoReq
  236. // @router /edit [post]
  237. func (this *PythonController) Edit() {
  238. br := new(models.BaseResponse).Init()
  239. defer func() {
  240. this.Data["json"] = br
  241. this.ServeJSON()
  242. }()
  243. //source := utils.DATA_SOURCE_WIND
  244. var req models.AddPythonEdbReq
  245. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  246. if err != nil {
  247. br.Msg = "参数解析异常!"
  248. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  249. return
  250. }
  251. req.EdbName = strings.Trim(req.EdbName, " ")
  252. if req.EdbInfoId <= 0 {
  253. br.Msg = "指标id不能为空"
  254. return
  255. }
  256. if req.EdbName == "" {
  257. br.Msg = "指标名称不能为空"
  258. return
  259. }
  260. if req.Frequency == "" {
  261. br.Msg = "频率不能为空"
  262. return
  263. }
  264. if req.Unit == "" {
  265. br.Msg = "单位不能为空"
  266. return
  267. }
  268. if req.ClassifyId <= 0 {
  269. br.Msg = "请选择分类"
  270. return
  271. }
  272. if req.PythonCode == "" {
  273. br.Msg = "请填写python代码"
  274. return
  275. }
  276. //加入缓存机制,避免创建同一个名称的指标 start
  277. redisKey := fmt.Sprint("edb_info:python:edit:", utils.DATA_SOURCE_PYTHON, ":", req.EdbInfoId)
  278. isExist := utils.Rc.IsExist(redisKey)
  279. if isExist {
  280. br.Msg = "指标正在处理,请勿重复提交"
  281. return
  282. } else {
  283. //设置3分钟缓存
  284. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  285. defer func() {
  286. utils.Rc.Delete(redisKey)
  287. }()
  288. }
  289. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  290. if err != nil {
  291. br.Msg = "获取指标信息失败"
  292. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  293. return
  294. }
  295. //获取指标数据
  296. req.PythonCode, err = url.QueryUnescape(req.PythonCode)
  297. if err != nil {
  298. br.Msg = "python代码解析失败"
  299. return
  300. }
  301. edbData, err, errMsg := services.ExecPythonCode(req.EdbName, req.PythonCode)
  302. if err != nil {
  303. br.Msg = "执行python代码失败"
  304. br.ErrMsg = "执行python代码失败,err:" + err.Error()
  305. if errMsg != "" {
  306. br.ErrMsg = errMsg
  307. }
  308. return
  309. }
  310. // 判断指标名称是否重复
  311. {
  312. var condition string
  313. var pars []interface{}
  314. condition += " AND edb_name=? "
  315. pars = append(pars, req.EdbName)
  316. condition += " AND edb_info_id<>? "
  317. pars = append(pars, req.EdbInfoId)
  318. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  319. if err != nil {
  320. br.Msg = "判断指标名称是否存在失败"
  321. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  322. return
  323. }
  324. if count > 0 {
  325. br.Msg = "指标名称已存在,请重新填写"
  326. br.ErrMsg = "指标名称已存在,请重新填写"
  327. return
  328. }
  329. }
  330. edbInfo.EdbName = utils.TrimStr(req.EdbName)
  331. edbInfo.EdbNameSource = utils.TrimStr(req.EdbName)
  332. edbInfo.Unit = req.Unit
  333. edbInfo.ClassifyId = req.ClassifyId
  334. edbInfo.Frequency = req.Frequency
  335. err = edbInfo.Update([]string{"EdbName", "EdbNameSource", "Unit", "ClassifyId", "Frequency"})
  336. if err != nil {
  337. br.Msg = "修改python运算指标失败"
  338. br.Msg = "修改python运算指标失败,ModifyEdbInfo Err:" + err.Error()
  339. return
  340. }
  341. // 修改存储python代码
  342. edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
  343. edbPythonCode.PythonCode = req.PythonCode
  344. edbPythonCode.ModifyTime = time.Now()
  345. err = edbPythonCode.Update([]string{"PythonCode", "ModifyTime"})
  346. if err != nil {
  347. br.Msg = "修改python运算指标失败"
  348. br.Msg = "修改python运算指标失败,存储python代码失败,EditEdbPythonCode Err:" + err.Error()
  349. return
  350. }
  351. //匹配所有的关联指标
  352. edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)
  353. err = models.EditEdbInfoCalculateMapping(edbInfo, edbInfoList)
  354. if err != nil {
  355. br.Msg = "修改python运算指标失败"
  356. br.Msg = "修改python运算指标失败,存储python代码失败,EditEdbPythonCode Err:" + err.Error()
  357. return
  358. }
  359. //刷新数据
  360. err = models.RefreshAllPythonEdb(edbInfo, edbData)
  361. if err != nil {
  362. br.Msg = "刷新python指标失败"
  363. br.Msg = "刷新python指标失败,EditPythonEdb Err:" + err.Error()
  364. return
  365. }
  366. // 更新指标最大最小值
  367. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  368. if err != nil {
  369. br.Msg = errMsg
  370. br.ErrMsg = err.Error()
  371. return
  372. }
  373. // 更新ES
  374. go logic.UpdateEs(edbInfo.EdbInfoId)
  375. resp := models.AddEdbInfoResp{
  376. EdbInfoId: edbInfo.EdbInfoId,
  377. UniqueCode: edbInfo.UniqueCode,
  378. }
  379. br.Ret = 200
  380. br.Success = true
  381. br.Msg = "修改成功"
  382. br.Data = resp
  383. br.IsAddLog = true
  384. }