ai_predict_model_index.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. package services
  2. import (
  3. "encoding/json"
  4. aiPredictModel "eta/eta_api/models/ai_predict_model"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/services/data"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "sort"
  10. "strconv"
  11. "time"
  12. )
  13. func ImportAiPredictModelIndexAndData(imports []*aiPredictModel.AiPredictModelImportData, adminId int, adminRealName string) (err error) {
  14. if len(imports) == 0 {
  15. return
  16. }
  17. // 查询已存在的标的
  18. indexOb := new(aiPredictModel.AiPredictModelIndex)
  19. indexNameItem := make(map[string]*aiPredictModel.AiPredictModelIndex)
  20. {
  21. list, e := indexOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  22. if e != nil {
  23. err = fmt.Errorf("获取标的失败, %v", e)
  24. return
  25. }
  26. for _, v := range list {
  27. indexNameItem[v.IndexName] = v
  28. }
  29. }
  30. updateCols := []string{indexOb.Cols().ClassifyId, indexOb.Cols().ModelFramework, indexOb.Cols().PredictDate, indexOb.Cols().PredictValue, indexOb.Cols().DirectionAccuracy, indexOb.Cols().AbsoluteDeviation, indexOb.Cols().ExtraConfig, indexOb.Cols().SysUserId, indexOb.Cols().SysUserRealName, indexOb.Cols().ModifyTime}
  31. updateIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  32. createIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  33. for _, v := range imports {
  34. exist := indexNameItem[v.Index.IndexName]
  35. // 编辑
  36. if exist != nil {
  37. // 图例信息
  38. if exist.ExtraConfig != "" && v.Index.ExtraConfig != "" {
  39. var oldConfig, newConfig aiPredictModel.AiPredictModelIndexExtraConfig
  40. if e := json.Unmarshal([]byte(exist.ExtraConfig), &oldConfig); e != nil {
  41. err = fmt.Errorf("标的原配置解析失败, Config: %s, Err: %v", exist.ExtraConfig, e)
  42. return
  43. }
  44. if e := json.Unmarshal([]byte(v.Index.ExtraConfig), &newConfig); e != nil {
  45. err = fmt.Errorf("标的新配置解析失败, Config: %s, Err: %v", v.Index.ExtraConfig, e)
  46. return
  47. }
  48. oldConfig.DailyChart.PredictLegendName = newConfig.DailyChart.PredictLegendName
  49. b, _ := json.Marshal(oldConfig)
  50. v.Index.ExtraConfig = string(b)
  51. }
  52. v.Index.AiPredictModelIndexId = exist.AiPredictModelIndexId
  53. v.Index.IndexCode = exist.IndexCode
  54. updateIndexes = append(updateIndexes, v)
  55. continue
  56. }
  57. // 新增标的/图表
  58. indexCode, e := utils.GenerateEdbCode(1, "IPM")
  59. if e != nil {
  60. err = fmt.Errorf("生成标的编码失败, %v", e)
  61. return
  62. }
  63. v.Index.IndexCode = indexCode
  64. v.Charts = GetAiPredictCharts(v.Index.IndexName, adminId, adminRealName)
  65. createIndexes = append(createIndexes, v)
  66. }
  67. // 新增/更新指标
  68. chartIds, e := indexOb.ImportIndexAndData(createIndexes, updateIndexes, updateCols)
  69. if e != nil {
  70. err = fmt.Errorf("导入指标失败, %v", e)
  71. return
  72. }
  73. // 更新图表ES
  74. if len(chartIds) == 0 {
  75. return
  76. }
  77. go func() {
  78. for _, v := range chartIds {
  79. data.EsAddOrEditChartInfo(v)
  80. }
  81. }()
  82. return
  83. }
  84. func GetAiPredictChartDetailByData(indexItem *aiPredictModel.AiPredictModelIndex, indexData []*aiPredictModel.AiPredictModelData, source int) (resp *data_manage.ChartInfoDetailResp, err error) {
  85. resp = new(data_manage.ChartInfoDetailResp)
  86. // 标的配置
  87. var extraConfig aiPredictModel.AiPredictModelIndexExtraConfig
  88. if indexItem.ExtraConfig != "" {
  89. if e := json.Unmarshal([]byte(indexItem.ExtraConfig), &extraConfig); e != nil {
  90. err = fmt.Errorf("标的额外配置解析失败, Config: %s, Err: %v", indexItem.ExtraConfig, e)
  91. return
  92. }
  93. }
  94. // 图表信息
  95. var predictLegendName, confLeftMin, confLeftMax, unit string
  96. if source == aiPredictModel.ModelDataSourceDaily {
  97. predictLegendName = extraConfig.DailyChart.PredictLegendName
  98. if predictLegendName == "" {
  99. predictLegendName = "Predicted"
  100. }
  101. unit = extraConfig.DailyChart.Unit
  102. confLeftMin = extraConfig.DailyChart.LeftMin
  103. confLeftMax = extraConfig.DailyChart.LeftMax
  104. }
  105. if source == aiPredictModel.ModelDataSourceMonthly {
  106. predictLegendName = "预测值"
  107. unit = extraConfig.MonthlyChart.Unit
  108. confLeftMin = extraConfig.MonthlyChart.LeftMin
  109. confLeftMax = extraConfig.MonthlyChart.LeftMax
  110. }
  111. // 这里简单兼容下吧,暂时就不修数据了
  112. if confLeftMin == "" {
  113. confLeftMin = indexItem.LeftMin
  114. }
  115. if confLeftMax == "" {
  116. confLeftMax = indexItem.LeftMax
  117. }
  118. // 获取指标对应的图表
  119. chartSourceMapping := map[int]int{
  120. aiPredictModel.ModelDataSourceMonthly: utils.CHART_SOURCE_AI_PREDICT_MODEL_MONTHLY,
  121. aiPredictModel.ModelDataSourceDaily: utils.CHART_SOURCE_AI_PREDICT_MODEL_DAILY,
  122. }
  123. chartInfo, e := data_manage.GetAiPredictChartInfoByIndexId(chartSourceMapping[source], indexItem.AiPredictModelIndexId)
  124. if e != nil && !utils.IsErrNoRow(e) {
  125. err = fmt.Errorf("获取标的图表失败, %v", e)
  126. return
  127. }
  128. // 获取曲线图主题样式
  129. chartView := new(data_manage.ChartInfoView)
  130. if chartInfo != nil && chartInfo.ChartInfoId > 0 {
  131. chartView.ChartInfoId = chartInfo.ChartInfoId
  132. chartView.ChartName = chartInfo.ChartName
  133. chartView.ChartNameEn = chartInfo.ChartNameEn
  134. chartView.Source = chartInfo.Source
  135. chartView.ChartImage = chartInfo.ChartImage
  136. } else {
  137. chartView.ChartName = indexItem.IndexName
  138. chartView.ChartNameEn = indexItem.IndexName
  139. }
  140. chartView.ChartType = utils.CHART_SOURCE_DEFAULT
  141. chartTheme, e := data.GetChartThemeConfig(0, chartView.ChartType, utils.CHART_TYPE_CURVE)
  142. if e != nil {
  143. err = fmt.Errorf("获取图表主题样式失败, %v", e)
  144. return
  145. }
  146. chartView.ChartThemeStyle = chartTheme.Config
  147. chartView.ChartThemeId = chartTheme.ChartThemeId
  148. chartView.ChartName = indexItem.IndexName
  149. chartView.ChartNameEn = indexItem.IndexName
  150. chartView.DateType = 3
  151. chartView.Calendar = "公历"
  152. chartView.ChartSource = "AI预测模型"
  153. chartView.ChartSourceEn = "AI预测模型"
  154. chartView.Unit = unit
  155. chartView.UnitEn = unit
  156. // EdbList-固定一条为标的实际值、一条为预测值
  157. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  158. edbActual, edbPredict := new(data_manage.ChartEdbInfoMapping), new(data_manage.ChartEdbInfoMapping)
  159. edbActual.EdbName = indexItem.IndexName
  160. edbActual.EdbNameEn = indexItem.IndexName
  161. edbActual.IsAxis = 1
  162. edbActual.Unit = unit
  163. edbActual.UnitEn = unit
  164. edbPredict.EdbName = predictLegendName
  165. edbPredict.EdbNameEn = predictLegendName
  166. edbPredict.IsAxis = 1
  167. edbPredict.Unit = unit
  168. edbPredict.UnitEn = unit
  169. actualData, predictData := make([]*data_manage.EdbDataList, 0), make([]*data_manage.EdbDataList, 0)
  170. var startDate, endDate time.Time
  171. var actualValues, predictValues []float64
  172. var actualNewest, predictNewest bool
  173. var actualLatestTimestamp int64 // 实际值最后一天的时间戳,作为日度图表的分割线
  174. for k, v := range indexData {
  175. // 如果实际值和预测值都是null那么该日期无效直接忽略
  176. if !v.Value.Valid && !v.PredictValue.Valid {
  177. continue
  178. }
  179. // 将有效值加入[]float64,最后取极值
  180. if v.Value.Valid {
  181. actualValues = append(actualValues, v.Value.Float64)
  182. }
  183. if v.PredictValue.Valid {
  184. predictValues = append(predictValues, v.PredictValue.Float64)
  185. }
  186. // 开始结束时间
  187. if k == 0 {
  188. startDate = v.DataTime
  189. endDate = v.CreateTime
  190. }
  191. if v.DataTime.Before(startDate) {
  192. startDate = v.DataTime
  193. }
  194. if v.DataTime.After(endDate) {
  195. endDate = v.DataTime
  196. }
  197. // 指标数据
  198. if v.Value.Valid {
  199. if !actualNewest {
  200. edbActual.LatestDate = v.DataTime.Format(utils.FormatDate)
  201. edbActual.LatestValue = v.Value.Float64
  202. actualLatestTimestamp = v.DataTime.UnixNano() / 1e6
  203. actualNewest = true
  204. }
  205. actualData = append(actualData, &data_manage.EdbDataList{
  206. DataTime: v.DataTime.Format(utils.FormatDate),
  207. Value: v.Value.Float64,
  208. DataTimestamp: v.DataTimestamp,
  209. })
  210. }
  211. if v.PredictValue.Valid {
  212. if !predictNewest {
  213. edbPredict.LatestDate = v.DataTime.Format(utils.FormatDate)
  214. edbPredict.LatestValue = v.Value.Float64
  215. predictNewest = true
  216. }
  217. predictData = append(predictData, &data_manage.EdbDataList{
  218. DataTime: v.DataTime.Format(utils.FormatDate),
  219. Value: v.PredictValue.Float64,
  220. DataTimestamp: v.DataTimestamp,
  221. })
  222. }
  223. }
  224. // 图表数据这里均做一个升序排序
  225. sort.Slice(actualData, func(i, j int) bool {
  226. return actualData[i].DataTimestamp < actualData[j].DataTimestamp
  227. })
  228. sort.Slice(predictData, func(i, j int) bool {
  229. return predictData[i].DataTimestamp < predictData[j].DataTimestamp
  230. })
  231. // 极值
  232. actualMin, actualMax := utils.FindMinMax(actualValues)
  233. predictMin, predictMax := utils.FindMinMax(predictValues)
  234. edbActual.MinData = actualMin
  235. edbActual.MaxData = actualMax
  236. edbPredict.MinData = predictMin
  237. edbPredict.MaxData = predictMax
  238. edbActual.DataList = actualData
  239. edbPredict.DataList = predictData
  240. edbList = append(edbList, edbActual, edbPredict)
  241. // 上下限
  242. if confLeftMin != "" {
  243. chartView.LeftMin = confLeftMin
  244. } else {
  245. leftMin := actualMin
  246. if leftMin > predictMin {
  247. leftMin = predictMin
  248. }
  249. chartView.LeftMin = fmt.Sprint(leftMin)
  250. }
  251. if confLeftMax != "" {
  252. chartView.LeftMax = confLeftMax
  253. } else {
  254. leftMax := actualMax
  255. if leftMax < predictMax {
  256. leftMax = predictMax
  257. }
  258. chartView.LeftMax = fmt.Sprint(leftMax)
  259. }
  260. chartView.StartDate = startDate.Format(utils.FormatDate)
  261. chartView.EndDate = endDate.Format(utils.FormatDate)
  262. // 日度图表的分割线日期
  263. if source == aiPredictModel.ModelDataSourceDaily {
  264. var dataResp struct {
  265. ActualLatestTimestamp int64
  266. }
  267. dataResp.ActualLatestTimestamp = actualLatestTimestamp
  268. resp.DataResp = dataResp
  269. }
  270. resp.ChartInfo = chartView
  271. resp.EdbInfoList = edbList
  272. return
  273. }
  274. // GetAiPredictCharts 获取AI预测模型图表
  275. func GetAiPredictCharts(indexName string, adminId int, adminRealName string) (charts []*aiPredictModel.AiPredictModelImportCharts) {
  276. charts = make([]*aiPredictModel.AiPredictModelImportCharts, 0)
  277. // 日度/月度图表
  278. frequencyArr := []int{aiPredictModel.ModelDataSourceMonthly, aiPredictModel.ModelDataSourceDaily}
  279. sourceMapping := map[int]int{
  280. aiPredictModel.ModelDataSourceMonthly: utils.CHART_SOURCE_AI_PREDICT_MODEL_MONTHLY,
  281. aiPredictModel.ModelDataSourceDaily: utils.CHART_SOURCE_AI_PREDICT_MODEL_DAILY,
  282. }
  283. suffixNameMapping := map[int]string{
  284. aiPredictModel.ModelDataSourceMonthly: "预测模型/回测",
  285. aiPredictModel.ModelDataSourceDaily: "预测模型",
  286. }
  287. for _, v := range frequencyArr {
  288. chartSource := sourceMapping[v]
  289. newChart := new(aiPredictModel.AiPredictModelImportCharts)
  290. // 新增图表
  291. chartName := fmt.Sprintf("%s%s", indexName, suffixNameMapping[v])
  292. chartInfo := new(data_manage.ChartInfo)
  293. chartInfo.ChartName = chartName
  294. chartInfo.ChartNameEn = chartName
  295. chartInfo.ChartType = utils.CHART_TYPE_CURVE
  296. chartInfo.Calendar = "公历"
  297. chartInfo.SysUserId = adminId
  298. chartInfo.SysUserRealName = adminRealName
  299. chartInfo.CreateTime = time.Now()
  300. chartInfo.ModifyTime = time.Now()
  301. chartInfo.Source = chartSource
  302. time.Sleep(time.Microsecond)
  303. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10))
  304. newChart.ChartInfo = chartInfo
  305. // chart_edb_mapping中edb_info_id为标的ID
  306. edbMapping := new(data_manage.ChartEdbMapping)
  307. //edbMapping.EdbInfoId = indexId
  308. //edbMapping.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", indexId, "_", strconv.FormatInt(time.Now().UnixNano(), 10)))
  309. edbMapping.Source = chartSource
  310. edbMapping.CreateTime = time.Now().Local()
  311. edbMapping.ModifyTime = time.Now().Local()
  312. newChart.EdbMappings = append(newChart.EdbMappings, edbMapping)
  313. charts = append(charts, newChart)
  314. }
  315. return
  316. }
  317. // FixAiPredictCharts 修复AI预测模型图表
  318. func FixAiPredictCharts() {
  319. var err error
  320. defer func() {
  321. if err != nil {
  322. fmt.Println(err)
  323. }
  324. fmt.Println("修复完成")
  325. }()
  326. fmt.Println("开始修复")
  327. indexOb := new(aiPredictModel.AiPredictModelIndex)
  328. indexes, e := indexOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  329. if e != nil {
  330. err = fmt.Errorf("获取所有标的失败, %v", e)
  331. return
  332. }
  333. // 日度/月度图表
  334. frequencyArr := []int{aiPredictModel.ModelDataSourceMonthly, aiPredictModel.ModelDataSourceDaily}
  335. sourceMapping := map[int]int{
  336. aiPredictModel.ModelDataSourceMonthly: utils.CHART_SOURCE_AI_PREDICT_MODEL_MONTHLY,
  337. aiPredictModel.ModelDataSourceDaily: utils.CHART_SOURCE_AI_PREDICT_MODEL_DAILY,
  338. }
  339. suffixNameMapping := map[int]string{
  340. aiPredictModel.ModelDataSourceMonthly: "预测模型/回测",
  341. aiPredictModel.ModelDataSourceDaily: "预测模型",
  342. }
  343. chartOb := new(data_manage.ChartInfo)
  344. for _, v := range indexes {
  345. for _, fre := range frequencyArr {
  346. chartSource := sourceMapping[fre]
  347. item, e := data_manage.GetAiPredictChartInfoByIndexId(chartSource, v.AiPredictModelIndexId)
  348. if e != nil && !utils.IsErrNoRow(e) {
  349. err = fmt.Errorf("获取AI预测模型图表失败, %v", e)
  350. return
  351. }
  352. // 由于标的名称是固定的所以chart_info没有什么可更新的, 已加入过就忽略
  353. if item != nil && item.ChartInfoId > 0 {
  354. fmt.Printf("标的%d-%d图表已存在, continue\n", v.AiPredictModelIndexId, chartSource)
  355. continue
  356. }
  357. // 新增图表
  358. chartName := fmt.Sprintf("%s%s", v.IndexName, suffixNameMapping[fre])
  359. chartInfo := new(data_manage.ChartInfo)
  360. chartInfo.ChartName = chartName
  361. chartInfo.ChartNameEn = chartName
  362. chartInfo.ChartType = utils.CHART_TYPE_CURVE
  363. chartInfo.Calendar = "公历"
  364. chartInfo.SysUserId = v.SysUserId
  365. chartInfo.SysUserRealName = v.SysUserRealName
  366. chartInfo.CreateTime = time.Now()
  367. chartInfo.ModifyTime = time.Now()
  368. chartInfo.Source = chartSource
  369. time.Sleep(time.Microsecond)
  370. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10))
  371. // chart_edb_mapping中edb_info_id为标的ID
  372. mappings := make([]*data_manage.ChartEdbMapping, 0)
  373. edbMapping := new(data_manage.ChartEdbMapping)
  374. edbMapping.EdbInfoId = v.AiPredictModelIndexId
  375. edbMapping.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.AiPredictModelIndexId, "_", strconv.FormatInt(time.Now().UnixNano(), 10)))
  376. edbMapping.Source = chartSource
  377. edbMapping.CreateTime = time.Now().Local()
  378. edbMapping.ModifyTime = time.Now().Local()
  379. mappings = append(mappings, edbMapping)
  380. // 新增图表
  381. if e = chartOb.AddChartInfoAndEdbMappings(chartInfo, mappings); e != nil {
  382. err = fmt.Errorf("新增图表及mapping失败, %v", e)
  383. return
  384. }
  385. // 写入ES
  386. if chartInfo.ChartInfoId <= 0 {
  387. err = fmt.Errorf("图表ID有误")
  388. return
  389. }
  390. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  391. }
  392. }
  393. return
  394. }