base_from_python.go 11 KB

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