index_config.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. itemView := configItem.ToView()
  149. itemView.IsCurr = 1
  150. br.Data = itemView
  151. br.Ret = 200
  152. br.Success = true
  153. br.Msg = "获取成功"
  154. }
  155. // SetCurr
  156. // @Title 设置为当前版本
  157. // @Description 设置为当前版本
  158. // @Param request body request.DelConfigReq true "type json string"
  159. // @Success 200 Ret=200 设置成功
  160. // @router /index_config/version/set_curr [post]
  161. func (c *AiPredictModelIndexConfigController) SetCurr() {
  162. br := new(models.BaseResponse).Init()
  163. defer func() {
  164. c.Data["json"] = br
  165. c.ServeJSON()
  166. }()
  167. sysUser := c.SysUser
  168. if sysUser == nil {
  169. br.Msg = "请登录"
  170. br.ErrMsg = "请登录,SysUser Is Empty"
  171. return
  172. }
  173. var req request.DelConfigReq
  174. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  175. // 查找配置
  176. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  177. configItem, err := obj.GetById(req.AiPredictModelIndexConfigId)
  178. if err != nil {
  179. br.Msg = "修改失败"
  180. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  181. if utils.IsErrNoRow(err) {
  182. br.Msg = "配置不存在"
  183. br.IsSendEmail = false
  184. }
  185. return
  186. }
  187. // 查询标的情况
  188. indexOb := new(aiPredictModel.AiPredictModelIndex)
  189. indexItem, e := indexOb.GetItemById(configItem.AiPredictModelIndexId)
  190. if e != nil {
  191. br.Msg = "操作失败"
  192. br.ErrMsg = fmt.Sprintf("获取失败,根据配置ID获取标的信息失败, %v", e)
  193. return
  194. }
  195. indexItem.AiPredictModelIndexConfigId = configItem.AiPredictModelIndexConfigId
  196. indexItem.ModifyTime = time.Now()
  197. err = indexItem.Update([]string{indexOb.Cols().ModifyTime, indexOb.Cols().AiPredictModelIndexConfigId})
  198. if err != nil {
  199. br.Msg = "操作失败"
  200. br.ErrMsg = fmt.Sprintf("配置失败,Err:%v", e)
  201. return
  202. }
  203. br.Ret = 200
  204. br.Success = true
  205. br.Msg = "操作成功"
  206. }
  207. // Del
  208. // @Title 删除模型配置
  209. // @Description 删除模型配置
  210. // @Param request body request.DelConfigReq true "type json string"
  211. // @Success 200 Ret=200 删除成功
  212. // @router /index_config/del [post]
  213. func (c *AiPredictModelIndexConfigController) Del() {
  214. br := new(models.BaseResponse).Init()
  215. defer func() {
  216. c.Data["json"] = br
  217. c.ServeJSON()
  218. }()
  219. var req request.DelConfigReq
  220. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  221. if err != nil {
  222. br.Msg = "参数解析异常!"
  223. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  224. return
  225. }
  226. if req.AiPredictModelIndexConfigId <= 0 {
  227. br.Msg = "配置id不能为空"
  228. br.IsSendEmail = false
  229. return
  230. }
  231. // 查找配置
  232. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  233. item, err := obj.GetById(req.AiPredictModelIndexConfigId)
  234. if err != nil {
  235. br.Msg = "修改失败"
  236. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  237. if utils.IsErrNoRow(err) {
  238. br.Msg = "配置不存在"
  239. br.IsSendEmail = false
  240. }
  241. return
  242. }
  243. // 查找是否被标的引用为默认模型
  244. {
  245. // 查询标的情况
  246. indexOb := new(aiPredictModel.AiPredictModelIndex)
  247. count, e := indexOb.GetCountByCondition(fmt.Sprintf(` AND %s = ? `, indexOb.Cols().AiPredictModelIndexConfigId), []interface{}{item.AiPredictModelIndexConfigId})
  248. if e != nil {
  249. br.Msg = "删除失败"
  250. br.ErrMsg = fmt.Sprintf("删除失败,根据配置ID获取标的信息失败, %v", e)
  251. return
  252. }
  253. if count > 0 {
  254. br.Msg = "删除失败,该版本配置正在被使用"
  255. br.IsSendEmail = false
  256. return
  257. }
  258. }
  259. if !utils.InArrayByStr([]string{aiPredictModel.TrainStatusSuccess, aiPredictModel.TrainStatusFailed}, item.TrainStatus) {
  260. br.Msg = "删除失败,该版本配置正在训练中"
  261. br.IsSendEmail = false
  262. return
  263. }
  264. item.IsDeleted = 1
  265. item.ModifyTime = time.Now()
  266. err = item.Update([]string{"IsDeleted", "ModifyTime"})
  267. if err != nil {
  268. br.Msg = "删除失败"
  269. br.ErrMsg = "删除失败,Err:" + err.Error()
  270. return
  271. }
  272. br.Ret = 200
  273. br.Success = true
  274. br.Msg = `删除成功`
  275. }
  276. // ChartDetail
  277. // @Title 获取当前版本的图表信息
  278. // @Description 获取当前版本的图表信息
  279. // @Param AiPredictModelIndexConfigId query int true "标的配置ID"
  280. // @Success 200 {object} []*response.AiPredictModelDetailResp
  281. // @router /index_config/chart/detail [get]
  282. func (c *AiPredictModelIndexConfigController) ChartDetail() {
  283. br := new(models.BaseResponse).Init()
  284. defer func() {
  285. c.Data["json"] = br
  286. c.ServeJSON()
  287. }()
  288. sysUser := c.SysUser
  289. if sysUser == nil {
  290. br.Msg = "请登录"
  291. br.ErrMsg = "请登录,SysUser Is Empty"
  292. return
  293. }
  294. indexConfigId, _ := c.GetInt("AiPredictModelIndexConfigId")
  295. if indexConfigId <= 0 {
  296. br.Msg = "标的配置id不能为空"
  297. br.ErrMsg = "标的配置id不能为空"
  298. return
  299. }
  300. resp := response.AiPredictModelDetailResp{}
  301. // TODO 后面加上数据缓存
  302. // 查找配置
  303. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  304. configItem, err := obj.GetById(indexConfigId)
  305. if err != nil {
  306. br.Msg = "修改失败"
  307. br.ErrMsg = "修改失败,查找配置失败,Err:" + err.Error()
  308. if utils.IsErrNoRow(err) {
  309. br.Msg = "配置不存在"
  310. br.IsSendEmail = false
  311. }
  312. return
  313. }
  314. // 查询标的情况
  315. indexOb := new(aiPredictModel.AiPredictModelIndex)
  316. indexItem, e := indexOb.GetItemById(configItem.AiPredictModelIndexId)
  317. if e != nil {
  318. br.Msg = "获取失败"
  319. br.ErrMsg = fmt.Sprintf("获取失败,根据配置ID获取标的信息失败, %v", e)
  320. return
  321. }
  322. // 获取标的数据
  323. dailyData := make([]*aiPredictModel.AiPredictModelIndexConfigTrainData, 0)
  324. {
  325. dataOb := new(aiPredictModel.AiPredictModelIndexConfigTrainData)
  326. dataCond := fmt.Sprintf(` AND %s = ?`, aiPredictModel.AiPredictModelIndexConfigTrainDataColumns.AiPredictModelIndexConfigId)
  327. dataPars := make([]interface{}, 0)
  328. dataPars = append(dataPars, configItem.AiPredictModelIndexConfigId)
  329. list, e := dataOb.GetAllListByCondition(dataCond, dataPars, []string{}, fmt.Sprintf("%s DESC", aiPredictModel.AiPredictModelIndexConfigTrainDataColumns.DataTime))
  330. if e != nil {
  331. br.Msg = "获取失败"
  332. br.ErrMsg = fmt.Sprintf("获取标的数据失败, %v", e)
  333. return
  334. }
  335. for _, v := range list {
  336. // 日度数据
  337. if v.Source == aiPredictModel.ModelDataSourceDaily {
  338. dailyData = append(dailyData, v)
  339. continue
  340. }
  341. }
  342. }
  343. // 日度图表
  344. if len(dailyData) > 0 {
  345. dailyChartDetail, e := services.GetAiPredictConfigChartDetailByData(indexItem.IndexName, configItem, dailyData, aiPredictModel.ModelDataSourceDaily)
  346. if e != nil {
  347. br.Msg = "获取失败"
  348. br.ErrMsg = fmt.Sprintf("获取日度图表失败, %v", e)
  349. return
  350. }
  351. resp.DailyChartView = dailyChartDetail
  352. }
  353. br.Ret = 200
  354. br.Success = true
  355. br.Msg = "获取成功"
  356. br.Data = resp
  357. }
  358. // Train
  359. // @Title 训练模型
  360. // @Description 训练模型
  361. // @Param request body request.TrainReq true "type json string"
  362. // @Success 200 Ret=200 训练中
  363. // @router /index_config/train [post]
  364. func (c *AiPredictModelIndexConfigController) Train() {
  365. br := new(models.BaseResponse).Init()
  366. defer func() {
  367. c.Data["json"] = br
  368. c.ServeJSON()
  369. }()
  370. var req request.TrainReq
  371. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  372. if err != nil {
  373. br.Msg = "参数解析异常!"
  374. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  375. return
  376. }
  377. if req.IndexId <= 0 {
  378. br.Msg = "标的id不能为空"
  379. br.IsSendEmail = false
  380. return
  381. }
  382. paramsStrByte, err := json.Marshal(req.Params)
  383. if err != nil {
  384. br.Msg = "训练失败!"
  385. br.ErrMsg = "训练失败,参数转json失败,Err:" + err.Error()
  386. return
  387. }
  388. // 查询标的情况
  389. indexOb := new(aiPredictModel.AiPredictModelIndex)
  390. indexItem, err := indexOb.GetItemById(req.IndexId)
  391. if err != nil {
  392. br.Msg = "训练失败,查找标的失败"
  393. br.ErrMsg = fmt.Sprintf("训练失败,查找标的失败, %v", err)
  394. if utils.IsErrNoRow(err) {
  395. br.Msg = "标的不存在"
  396. br.IsSendEmail = false
  397. }
  398. return
  399. }
  400. if indexItem.ScriptPath == `` {
  401. br.Msg = "训练失败,脚本路径不能为空"
  402. br.IsSendEmail = false
  403. return
  404. }
  405. obj := new(aiPredictModel.AiPredictModelIndexConfig)
  406. // 查找当前标的是否存在待训练/训练中的模型
  407. count, err := services.GetCurrentRunningAiPredictModelIndexCount()
  408. if err != nil {
  409. br.Msg = "训练失败"
  410. br.ErrMsg = "训练失败,查找待训练的模型失败,Err:" + err.Error()
  411. return
  412. }
  413. if count > 0 {
  414. br.Msg = "该标的存在待训练/训练中的模型,不允许重复训练"
  415. br.IsSendEmail = false
  416. return
  417. }
  418. var indexConfig *aiPredictModel.AiPredictModelIndexConfig
  419. if req.AiPredictModelIndexConfigId > 0 {
  420. // 查找配置
  421. item, err := obj.GetById(req.AiPredictModelIndexConfigId)
  422. if err != nil {
  423. br.Msg = "训练失败"
  424. br.ErrMsg = "训练失败,查找配置失败,Err:" + err.Error()
  425. if utils.IsErrNoRow(err) {
  426. br.Msg = "配置不存在"
  427. br.IsSendEmail = false
  428. }
  429. return
  430. }
  431. if item.AiPredictModelIndexId != indexItem.AiPredictModelIndexId {
  432. br.Msg = "训练失败"
  433. br.ErrMsg = "训练失败,配置与标的不匹配"
  434. return
  435. }
  436. if item.TrainStatus != aiPredictModel.TrainStatusFailed {
  437. br.Msg = "该模型训练状态异常,不允许重新训练"
  438. br.ErrMsg = "该模型训练状态异常,不允许重新训练,当前状态:" + item.TrainStatus
  439. br.IsSendEmail = false
  440. return
  441. }
  442. item.Params = string(paramsStrByte)
  443. item.ModifyTime = time.Now()
  444. err = item.Update([]string{"params", "modify_time"})
  445. if err != nil {
  446. br.Msg = "训练失败"
  447. br.ErrMsg = "训练失败,Err:" + err.Error()
  448. return
  449. }
  450. indexConfig = item
  451. } else {
  452. // 新增训练模型
  453. item := &aiPredictModel.AiPredictModelIndexConfig{
  454. AiPredictModelIndexConfigId: 0,
  455. AiPredictModelIndexId: indexItem.AiPredictModelIndexId,
  456. TrainStatus: aiPredictModel.TrainStatusWaiting,
  457. Params: string(paramsStrByte),
  458. TrainMse: "",
  459. TrainR2: "",
  460. TestMse: "",
  461. TestR2: "",
  462. Remark: "",
  463. IsDeleted: 0,
  464. LeftMin: "",
  465. LeftMax: "",
  466. ModifyTime: time.Now(),
  467. CreateTime: time.Now(),
  468. }
  469. err = item.Create()
  470. if err != nil {
  471. br.Msg = "训练失败"
  472. br.ErrMsg = "训练失败,Err:" + err.Error()
  473. return
  474. }
  475. indexConfig = item
  476. }
  477. indexItem.TrainStatus = aiPredictModel.TrainStatusWaiting
  478. indexItem.ModifyTime = time.Now()
  479. err = indexItem.Update([]string{"train_status", "modify_time"})
  480. if err != nil {
  481. br.Msg = "训练失败"
  482. br.ErrMsg = "训练失败,Err:" + err.Error()
  483. return
  484. }
  485. // 加入模型训练任务中
  486. go services.AddAiModelTrainTask(indexItem, indexConfig, c.SysUser)
  487. br.Ret = 200
  488. br.Success = true
  489. br.Msg = `训练中`
  490. }