index_config.go 14 KB

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