base_from_python.go 11 KB

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