index_config.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. package ai_predict_model
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. aiPredictModel "eta/eta_api/models/ai_predict_model"
  7. "eta/eta_api/models/ai_predict_model/request"
  8. "eta/eta_api/models/ai_predict_model/response"
  9. "eta/eta_api/services"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "time"
  14. )
  15. // AiPredictModelIndexConfigController AI预测模型标的配置项
  16. type AiPredictModelIndexConfigController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 列表
  21. // @Description 列表
  22. // @Param PageSize query int true "每页数据条数"
  23. // @Param CurrentIndex query int true "当前页页码,从1开始"
  24. // @Param IndexId query int true "标的id"
  25. // @Success 200 {object} []*response.AiPredictModelIndexConfigListResp
  26. // @router /index_config/list [get]
  27. func (c *AiPredictModelIndexConfigController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. c.Data["json"] = br
  31. c.ServeJSON()
  32. }()
  33. sysUser := c.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. return
  38. }
  39. pageSize, _ := c.GetInt("PageSize")
  40. currentIndex, _ := c.GetInt("CurrentIndex")
  41. indexId, _ := c.GetInt("IndexId")
  42. if indexId <= 0 {
  43. br.Msg = "标的id不能为空"
  44. br.ErrMsg = "标的id不能为空"
  45. return
  46. }
  47. var startSize int
  48. if pageSize <= 0 {
  49. pageSize = utils.PageSize20
  50. }
  51. if currentIndex <= 0 {
  52. currentIndex = 1
  53. }
  54. startSize = utils.StartIndex(currentIndex, pageSize)
  55. // 查询标的情况
  56. indexOb := new(aiPredictModel.AiPredictModelIndex)
  57. indexItem, e := indexOb.GetItemById(indexId)
  58. if e != nil {
  59. if utils.IsErrNoRow(e) {
  60. br.Msg = "标的已被删除,请刷新页面"
  61. return
  62. }
  63. br.Msg = "获取失败"
  64. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  65. return
  66. }
  67. var total int
  68. viewList := make([]aiPredictModel.AiPredictModelIndexConfigView, 0)
  69. var condition string
  70. var pars []interface{}
  71. condition += fmt.Sprintf(` AND %s = ? `, aiPredictModel.AiPredictModelIndexConfigColumns.AiPredictModelIndexId)
  72. pars = append(pars, indexId)
  73. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  74. tmpTotal, list, err := obj.GetPageListByCondition(condition, pars, startSize, pageSize)
  75. if err != nil {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取失败,Err:" + err.Error()
  78. return
  79. }
  80. total = tmpTotal
  81. if list != nil && len(list) > 0 {
  82. for _, v := range list {
  83. tmpView := v.ToView()
  84. if tmpView.AiPredictModelIndexConfigId == indexItem.AiPredictModelIndexConfigId {
  85. tmpView.IsCurr = 1
  86. }
  87. viewList = append(viewList, tmpView)
  88. }
  89. }
  90. page := paging.GetPaging(currentIndex, pageSize, total)
  91. resp := response.AiPredictModelIndexConfigListResp{
  92. List: viewList,
  93. Paging: page,
  94. }
  95. br.Ret = 200
  96. br.Success = true
  97. br.Msg = "获取成功"
  98. br.Data = resp
  99. }
  100. // CurrVersion
  101. // @Title 获取当前版本参数信息
  102. // @Description 获取当前版本参数信息
  103. // @Param IndexId query int true "标的ID"
  104. // @Success 200 {object} []*data_manage.AiPredictModelIndexConfigView
  105. // @router /index_config/version/curr [get]
  106. func (c *AiPredictModelIndexConfigController) CurrVersion() {
  107. br := new(models.BaseResponse).Init()
  108. defer func() {
  109. c.Data["json"] = br
  110. c.ServeJSON()
  111. }()
  112. sysUser := c.SysUser
  113. if sysUser == nil {
  114. br.Msg = "请登录"
  115. br.ErrMsg = "请登录,SysUser Is Empty"
  116. return
  117. }
  118. indexId, _ := c.GetInt("IndexId")
  119. if indexId <= 0 {
  120. br.Msg = "标的id不能为空"
  121. br.ErrMsg = "标的id不能为空"
  122. return
  123. }
  124. // 查询标的情况
  125. indexOb := new(aiPredictModel.AiPredictModelIndex)
  126. indexItem, e := indexOb.GetItemById(indexId)
  127. if e != nil {
  128. if utils.IsErrNoRow(e) {
  129. br.Msg = "标的已被删除,请刷新页面"
  130. return
  131. }
  132. br.Msg = "获取失败"
  133. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  134. return
  135. }
  136. if indexItem.AiPredictModelIndexConfigId <= 0 {
  137. br.Msg = "标的默认配置为空"
  138. br.IsSendEmail = false
  139. return
  140. }
  141. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  142. configItem, err := obj.GetById(indexItem.AiPredictModelIndexConfigId)
  143. if err != nil {
  144. br.Msg = "获取失败"
  145. br.ErrMsg = "获取失败,Err:" + err.Error()
  146. return
  147. }
  148. br.Data = configItem.ToView()
  149. br.Ret = 200
  150. br.Success = true
  151. br.Msg = "获取成功"
  152. }
  153. // SetCurr
  154. // @Title 设置为当前版本
  155. // @Description 设置为当前版本
  156. // @Param request body request.DelConfigReq true "type json string"
  157. // @Success 200 Ret=200 设置成功
  158. // @router /index_config/version/set_curr [post]
  159. func (c *AiPredictModelIndexConfigController) SetCurr() {
  160. br := new(models.BaseResponse).Init()
  161. defer func() {
  162. c.Data["json"] = br
  163. c.ServeJSON()
  164. }()
  165. sysUser := c.SysUser
  166. if sysUser == nil {
  167. br.Msg = "请登录"
  168. br.ErrMsg = "请登录,SysUser Is Empty"
  169. return
  170. }
  171. var req request.DelConfigReq
  172. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  173. // 查找配置
  174. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  175. configItem, err := obj.GetById(req.AiPredictModelIndexConfigId)
  176. if err != nil {
  177. br.Msg = "修改失败"
  178. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  179. if utils.IsErrNoRow(err) {
  180. br.Msg = "配置不存在"
  181. br.IsSendEmail = false
  182. }
  183. return
  184. }
  185. // 查询标的情况
  186. indexOb := new(aiPredictModel.AiPredictModelIndex)
  187. indexItem, e := indexOb.GetItemById(configItem.AiPredictModelIndexId)
  188. if e != nil {
  189. br.Msg = "操作失败"
  190. br.ErrMsg = fmt.Sprintf("获取失败,根据配置ID获取标的信息失败, %v", e)
  191. return
  192. }
  193. indexItem.AiPredictModelIndexConfigId = configItem.AiPredictModelIndexConfigId
  194. indexItem.ModifyTime = time.Now()
  195. err = indexItem.Update([]string{indexOb.Cols().ModifyTime, indexOb.Cols().AiPredictModelIndexConfigId})
  196. if err != nil {
  197. br.Msg = "操作失败"
  198. br.ErrMsg = fmt.Sprintf("配置失败,Err:%v", e)
  199. return
  200. }
  201. br.Ret = 200
  202. br.Success = true
  203. br.Msg = "操作成功"
  204. }
  205. // Del
  206. // @Title 删除模型配置
  207. // @Description 删除模型配置
  208. // @Param request body request.DelConfigReq true "type json string"
  209. // @Success 200 Ret=200 删除成功
  210. // @router /index_config/del [post]
  211. func (c *AiPredictModelIndexConfigController) Del() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. c.Data["json"] = br
  215. c.ServeJSON()
  216. }()
  217. var req request.DelConfigReq
  218. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  219. if err != nil {
  220. br.Msg = "参数解析异常!"
  221. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  222. return
  223. }
  224. if req.AiPredictModelIndexConfigId <= 0 {
  225. br.Msg = "配置id不能为空"
  226. br.IsSendEmail = false
  227. return
  228. }
  229. // 查找配置
  230. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  231. item, err := obj.GetById(req.AiPredictModelIndexConfigId)
  232. if err != nil {
  233. br.Msg = "修改失败"
  234. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  235. if utils.IsErrNoRow(err) {
  236. br.Msg = "配置不存在"
  237. br.IsSendEmail = false
  238. }
  239. return
  240. }
  241. // 查找是否被标的引用为默认模型
  242. {
  243. // 查询标的情况
  244. indexOb := new(aiPredictModel.AiPredictModelIndex)
  245. count, e := indexOb.GetCountByCondition(fmt.Sprintf(` AND %s = ? `, indexOb.Cols().AiPredictModelIndexConfigId), []interface{}{item.AiPredictModelIndexConfigId})
  246. if e != nil {
  247. br.Msg = "删除失败"
  248. br.ErrMsg = fmt.Sprintf("删除失败,根据配置ID获取标的信息失败, %v", e)
  249. return
  250. }
  251. if count > 0 {
  252. br.Msg = "删除失败,该版本配置正在被使用"
  253. br.IsSendEmail = false
  254. return
  255. }
  256. }
  257. if !utils.InArrayByStr([]string{aiPredictModel.TrainStatusSuccess, aiPredictModel.TrainStatusFailed}, item.TrainStatus) {
  258. br.Msg = "删除失败,该版本配置正在训练中"
  259. br.IsSendEmail = false
  260. return
  261. }
  262. item.IsDeleted = 1
  263. item.ModifyTime = time.Now()
  264. err = item.Update([]string{"IsDeleted", "ModifyTime"})
  265. if err != nil {
  266. br.Msg = "删除失败"
  267. br.ErrMsg = "删除失败,Err:" + err.Error()
  268. return
  269. }
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = `删除成功`
  273. }
  274. // ChartDetail
  275. // @Title 获取当前版本的图表信息
  276. // @Description 获取当前版本的图表信息
  277. // @Param AiPredictModelIndexConfigId query int true "标的配置ID"
  278. // @Success 200 {object} []*response.AiPredictModelDetailResp
  279. // @router /index_config/chart/detail [get]
  280. func (c *AiPredictModelIndexConfigController) ChartDetail() {
  281. br := new(models.BaseResponse).Init()
  282. defer func() {
  283. c.Data["json"] = br
  284. c.ServeJSON()
  285. }()
  286. sysUser := c.SysUser
  287. if sysUser == nil {
  288. br.Msg = "请登录"
  289. br.ErrMsg = "请登录,SysUser Is Empty"
  290. return
  291. }
  292. indexConfigId, _ := c.GetInt("AiPredictModelIndexConfigId")
  293. if indexConfigId <= 0 {
  294. br.Msg = "标的配置id不能为空"
  295. br.ErrMsg = "标的配置id不能为空"
  296. return
  297. }
  298. resp := response.AiPredictModelDetailResp{}
  299. // TODO 后面加上数据缓存
  300. // 查找配置
  301. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  302. configItem, err := obj.GetById(indexConfigId)
  303. if err != nil {
  304. br.Msg = "修改失败"
  305. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  306. if utils.IsErrNoRow(err) {
  307. br.Msg = "配置不存在"
  308. br.IsSendEmail = false
  309. }
  310. return
  311. }
  312. // 查询标的情况
  313. indexOb := new(aiPredictModel.AiPredictModelIndex)
  314. indexItem, e := indexOb.GetItemById(configItem.AiPredictModelIndexId)
  315. if e != nil {
  316. br.Msg = "获取失败"
  317. br.ErrMsg = fmt.Sprintf("获取失败,根据配置ID获取标的信息失败, %v", e)
  318. return
  319. }
  320. // 获取标的数据
  321. dailyData := make([]*aiPredictModel.AiPredictModelIndexConfigTrainData, 0)
  322. {
  323. dataOb := new(aiPredictModel.AiPredictModelIndexConfigTrainData)
  324. dataCond := fmt.Sprintf(` AND %s = ?`, aiPredictModel.AiPredictModelIndexConfigTrainDataColumns.AiPredictModelIndexConfigId)
  325. dataPars := make([]interface{}, 0)
  326. dataPars = append(dataPars, configItem.AiPredictModelIndexConfigId)
  327. list, e := dataOb.GetAllListByCondition(dataCond, dataPars, []string{}, fmt.Sprintf("%s DESC", aiPredictModel.AiPredictModelIndexConfigTrainDataColumns.DataTime))
  328. if e != nil {
  329. br.Msg = "获取失败"
  330. br.ErrMsg = fmt.Sprintf("获取标的数据失败, %v", e)
  331. return
  332. }
  333. for _, v := range list {
  334. // 日度数据
  335. if v.Source == aiPredictModel.ModelDataSourceDaily {
  336. dailyData = append(dailyData, v)
  337. continue
  338. }
  339. }
  340. }
  341. // 日度图表
  342. if len(dailyData) > 0 {
  343. dailyChartDetail, e := services.GetAiPredictConfigChartDetailByData(indexItem.IndexName, configItem, dailyData, aiPredictModel.ModelDataSourceDaily)
  344. if e != nil {
  345. br.Msg = "获取失败"
  346. br.ErrMsg = fmt.Sprintf("获取日度图表失败, %v", e)
  347. return
  348. }
  349. resp.DailyChartView = dailyChartDetail
  350. }
  351. br.Ret = 200
  352. br.Success = true
  353. br.Msg = "获取成功"
  354. br.Data = resp
  355. }
  356. // Train
  357. // @Title 训练模型
  358. // @Description 训练模型
  359. // @Param request body request.TrainReq true "type json string"
  360. // @Success 200 Ret=200 训练中
  361. // @router /index_config/train [post]
  362. func (c *AiPredictModelIndexConfigController) Train() {
  363. br := new(models.BaseResponse).Init()
  364. defer func() {
  365. c.Data["json"] = br
  366. c.ServeJSON()
  367. }()
  368. var req request.TrainReq
  369. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  370. if err != nil {
  371. br.Msg = "参数解析异常!"
  372. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  373. return
  374. }
  375. if req.IndexId <= 0 {
  376. br.Msg = "标的id不能为空"
  377. br.IsSendEmail = false
  378. return
  379. }
  380. paramsStrByte, err := json.Marshal(req.Params)
  381. if err != nil {
  382. br.Msg = "训练失败!"
  383. br.ErrMsg = "训练失败,参数转json失败,Err:" + err.Error()
  384. return
  385. }
  386. // 查询标的情况
  387. indexOb := new(aiPredictModel.AiPredictModelIndex)
  388. indexItem, err := indexOb.GetItemById(req.IndexId)
  389. if err != nil {
  390. br.Msg = "训练失败,查找标的失败"
  391. br.ErrMsg = fmt.Sprintf("训练失败,查找标的失败, %v", err)
  392. if utils.IsErrNoRow(err) {
  393. br.Msg = "标的不存在"
  394. br.IsSendEmail = false
  395. }
  396. return
  397. }
  398. if indexItem.ScriptPath == `` {
  399. br.Msg = "训练失败,脚本路径不能为空"
  400. br.IsSendEmail = false
  401. return
  402. }
  403. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  404. // 查找当前标的是否存在待训练/训练中的模型
  405. count, err := services.GetCurrentRunningAiPredictModelIndexCount()
  406. if err != nil {
  407. br.Msg = "训练失败"
  408. br.ErrMsg = "训练失败,查找待训练的模型失败,Err:" + err.Error()
  409. return
  410. }
  411. if count > 0 {
  412. br.Msg = "该标的存在待训练/训练中的模型,不允许重复训练"
  413. br.IsSendEmail = false
  414. return
  415. }
  416. var indexConfig *aiPredictModel.AiPredictModelIndexConfig
  417. if req.AiPredictModelIndexConfigId > 0 {
  418. // 查找配置
  419. item, err := obj.GetById(req.AiPredictModelIndexConfigId)
  420. if err != nil {
  421. br.Msg = "训练失败"
  422. br.ErrMsg = "训练失败,查找配置失败,Err:" + err.Error()
  423. if utils.IsErrNoRow(err) {
  424. br.Msg = "配置不存在"
  425. br.IsSendEmail = false
  426. }
  427. return
  428. }
  429. if item.AiPredictModelIndexId != indexItem.AiPredictModelIndexId {
  430. br.Msg = "训练失败"
  431. br.ErrMsg = "训练失败,配置与标的不匹配"
  432. return
  433. }
  434. if item.TrainStatus != aiPredictModel.TrainStatusFailed {
  435. br.Msg = "该模型训练状态异常,不允许重新训练"
  436. br.ErrMsg = "该模型训练状态异常,不允许重新训练,当前状态:" + item.TrainStatus
  437. br.IsSendEmail = false
  438. return
  439. }
  440. item.Params = string(paramsStrByte)
  441. item.ModifyTime = time.Now()
  442. err = item.Update([]string{"params", "modify_time"})
  443. if err != nil {
  444. br.Msg = "训练失败"
  445. br.ErrMsg = "训练失败,Err:" + err.Error()
  446. return
  447. }
  448. indexConfig = item
  449. } else {
  450. // 新增训练模型
  451. item := &aiPredictModel.AiPredictModelIndexConfig{
  452. AiPredictModelIndexConfigId: 0,
  453. AiPredictModelIndexId: indexItem.AiPredictModelIndexId,
  454. TrainStatus: aiPredictModel.TrainStatusWaiting,
  455. Params: string(paramsStrByte),
  456. TrainMse: "",
  457. TrainR2: "",
  458. TestMse: "",
  459. TestR2: "",
  460. Remark: "",
  461. IsDeleted: 0,
  462. LeftMin: "",
  463. LeftMax: "",
  464. ModifyTime: time.Now(),
  465. CreateTime: time.Now(),
  466. }
  467. err = item.Create()
  468. if err != nil {
  469. br.Msg = "训练失败"
  470. br.ErrMsg = "训练失败,Err:" + err.Error()
  471. return
  472. }
  473. indexConfig = item
  474. }
  475. indexItem.TrainStatus = aiPredictModel.TrainStatusWaiting
  476. indexItem.ModifyTime = time.Now()
  477. err = indexItem.Update([]string{"train_status", "modify_time"})
  478. if err != nil {
  479. br.Msg = "训练失败"
  480. br.ErrMsg = "训练失败,Err:" + err.Error()
  481. return
  482. }
  483. // 加入模型训练任务中
  484. go services.AddAiModelTrainTask(indexItem, indexConfig, c.SysUser)
  485. br.Ret = 200
  486. br.Success = true
  487. br.Msg = `训练中`
  488. }