index_config.go 15 KB

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