index_config.go 15 KB

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