ai_predict_model_index.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "time"
  10. )
  11. func ImportAiPredictModelIndexAndData(imports []*aiPredictModel.AiPredictModelImportData) (err error) {
  12. if len(imports) == 0 {
  13. return
  14. }
  15. // 查询已存在的标的
  16. indexOb := new(aiPredictModel.AiPredictModelIndex)
  17. indexNameItem := make(map[string]*aiPredictModel.AiPredictModelIndex)
  18. {
  19. list, e := indexOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  20. if e != nil {
  21. err = fmt.Errorf("获取标的失败, %v", e)
  22. return
  23. }
  24. for _, v := range list {
  25. indexNameItem[v.IndexName] = v
  26. }
  27. }
  28. 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}
  29. updateIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  30. createIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  31. for _, v := range imports {
  32. exist := indexNameItem[v.Index.IndexName]
  33. // 编辑
  34. if exist != nil {
  35. // 图例信息
  36. if exist.ExtraConfig != "" && v.Index.ExtraConfig != "" {
  37. var oldConfig, newConfig aiPredictModel.AiPredictModelIndexExtraConfig
  38. if e := json.Unmarshal([]byte(exist.ExtraConfig), &oldConfig); e != nil {
  39. err = fmt.Errorf("标的原配置解析失败, Config: %s, Err: %v", exist.ExtraConfig, e)
  40. return
  41. }
  42. if e := json.Unmarshal([]byte(v.Index.ExtraConfig), &newConfig); e != nil {
  43. err = fmt.Errorf("标的新配置解析失败, Config: %s, Err: %v", v.Index.ExtraConfig, e)
  44. return
  45. }
  46. oldConfig.DailyChart.PredictLegendName = newConfig.DailyChart.PredictLegendName
  47. b, _ := json.Marshal(oldConfig)
  48. v.Index.ExtraConfig = string(b)
  49. }
  50. v.Index.AiPredictModelIndexId = exist.AiPredictModelIndexId
  51. v.Index.IndexCode = exist.IndexCode
  52. updateIndexes = append(updateIndexes, v)
  53. continue
  54. }
  55. // 新增
  56. indexCode, e := utils.GenerateEdbCode(1, "IPM")
  57. if e != nil {
  58. err = fmt.Errorf("生成标的编码失败, %v", e)
  59. return
  60. }
  61. v.Index.IndexCode = indexCode
  62. createIndexes = append(createIndexes, v)
  63. }
  64. // 新增/更新指标
  65. if e := indexOb.ImportIndexAndData(createIndexes, updateIndexes, updateCols); e != nil {
  66. err = fmt.Errorf("导入指标失败, %v", e)
  67. return
  68. }
  69. return
  70. }
  71. func GetAiPredictChartDetailByData(indexItem *aiPredictModel.AiPredictModelIndex, indexData []*aiPredictModel.AiPredictModelData, source int) (resp *data_manage.ChartInfoDetailResp, err error) {
  72. resp = new(data_manage.ChartInfoDetailResp)
  73. // 标的配置
  74. var extraConfig aiPredictModel.AiPredictModelIndexExtraConfig
  75. if indexItem.ExtraConfig != "" {
  76. if e := json.Unmarshal([]byte(indexItem.ExtraConfig), &extraConfig); e != nil {
  77. err = fmt.Errorf("标的额外配置解析失败, Config: %s, Err: %v", indexItem.ExtraConfig, e)
  78. return
  79. }
  80. }
  81. // 图表信息
  82. var predictLegendName, confLeftMin, confLeftMax, unit string
  83. if source == aiPredictModel.ModelDataSourceDaily {
  84. predictLegendName = extraConfig.DailyChart.PredictLegendName
  85. if predictLegendName == "" {
  86. predictLegendName = "Predicted"
  87. }
  88. unit = extraConfig.DailyChart.Unit
  89. confLeftMin = extraConfig.DailyChart.LeftMin
  90. confLeftMax = extraConfig.DailyChart.LeftMax
  91. }
  92. if source == aiPredictModel.ModelDataSourceMonthly {
  93. predictLegendName = "预测值"
  94. unit = extraConfig.MonthlyChart.Unit
  95. confLeftMin = extraConfig.MonthlyChart.LeftMin
  96. confLeftMax = extraConfig.MonthlyChart.LeftMax
  97. }
  98. // 这里简单兼容下吧,暂时就不修数据了
  99. if confLeftMin == "" {
  100. confLeftMin = indexItem.LeftMin
  101. }
  102. if confLeftMax == "" {
  103. confLeftMax = indexItem.LeftMax
  104. }
  105. // 获取曲线图主题样式
  106. chartView := new(data_manage.ChartInfoView)
  107. chartView.ChartType = utils.CHART_SOURCE_DEFAULT
  108. chartTheme, e := data.GetChartThemeConfig(0, chartView.ChartType, utils.CHART_TYPE_CURVE)
  109. if e != nil {
  110. err = fmt.Errorf("获取图表主题样式失败, %v", e)
  111. return
  112. }
  113. chartView.ChartThemeStyle = chartTheme.Config
  114. chartView.ChartThemeId = chartTheme.ChartThemeId
  115. chartView.ChartName = indexItem.IndexName
  116. chartView.ChartNameEn = indexItem.IndexName
  117. chartView.DateType = 3
  118. chartView.Calendar = "公历"
  119. chartView.ChartSource = "AI预测模型"
  120. chartView.ChartSourceEn = "AI预测模型"
  121. chartView.Unit = unit
  122. chartView.UnitEn = unit
  123. // EdbList-固定一条为标的实际值、一条为预测值
  124. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  125. edbActual, edbPredict := new(data_manage.ChartEdbInfoMapping), new(data_manage.ChartEdbInfoMapping)
  126. edbActual.EdbName = indexItem.IndexName
  127. edbActual.EdbNameEn = indexItem.IndexName
  128. edbActual.IsAxis = 1
  129. edbActual.Unit = unit
  130. edbActual.UnitEn = unit
  131. edbPredict.EdbName = predictLegendName
  132. edbPredict.EdbNameEn = predictLegendName
  133. edbPredict.IsAxis = 1
  134. edbPredict.Unit = unit
  135. edbPredict.UnitEn = unit
  136. actualData, predictData := make([]*data_manage.EdbDataList, 0), make([]*data_manage.EdbDataList, 0)
  137. var startDate, endDate time.Time
  138. var actualValues, predictValues []float64
  139. var actualNewest, predictNewest bool
  140. var actualLatestTimestamp int64 // 实际值最后一天的时间戳,作为日度图表的分割线
  141. for k, v := range indexData {
  142. // 如果实际值和预测值都是null那么该日期无效直接忽略
  143. if !v.Value.Valid && !v.PredictValue.Valid {
  144. continue
  145. }
  146. // 将有效值加入[]float64,最后取极值
  147. if v.Value.Valid {
  148. actualValues = append(actualValues, v.Value.Float64)
  149. }
  150. if v.PredictValue.Valid {
  151. predictValues = append(predictValues, v.PredictValue.Float64)
  152. }
  153. // 开始结束时间
  154. if k == 0 {
  155. startDate = v.DataTime
  156. endDate = v.CreateTime
  157. }
  158. if v.DataTime.Before(startDate) {
  159. startDate = v.DataTime
  160. }
  161. if v.DataTime.After(endDate) {
  162. endDate = v.DataTime
  163. }
  164. // 指标数据
  165. if v.Value.Valid {
  166. if !actualNewest {
  167. edbActual.LatestDate = v.DataTime.Format(utils.FormatDate)
  168. edbActual.LatestValue = v.Value.Float64
  169. actualLatestTimestamp = v.DataTime.UnixNano() / 1e6
  170. actualNewest = true
  171. }
  172. actualData = append(actualData, &data_manage.EdbDataList{
  173. DataTime: v.DataTime.Format(utils.FormatDate),
  174. Value: v.Value.Float64,
  175. DataTimestamp: v.DataTimestamp,
  176. })
  177. }
  178. if v.PredictValue.Valid {
  179. if !predictNewest {
  180. edbPredict.LatestDate = v.DataTime.Format(utils.FormatDate)
  181. edbPredict.LatestValue = v.Value.Float64
  182. predictNewest = true
  183. }
  184. predictData = append(predictData, &data_manage.EdbDataList{
  185. DataTime: v.DataTime.Format(utils.FormatDate),
  186. Value: v.PredictValue.Float64,
  187. DataTimestamp: v.DataTimestamp,
  188. })
  189. }
  190. }
  191. // 极值
  192. actualMin, actualMax := utils.FindMinMax(actualValues)
  193. predictMin, predictMax := utils.FindMinMax(predictValues)
  194. edbActual.MinData = actualMin
  195. edbActual.MaxData = actualMax
  196. edbPredict.MinData = predictMin
  197. edbPredict.MaxData = predictMax
  198. edbActual.DataList = actualData
  199. edbPredict.DataList = predictData
  200. edbList = append(edbList, edbActual, edbPredict)
  201. // 上下限
  202. if confLeftMin != "" {
  203. chartView.LeftMin = confLeftMin
  204. } else {
  205. leftMin := actualMin
  206. if leftMin > predictMin {
  207. leftMin = predictMin
  208. }
  209. chartView.LeftMin = fmt.Sprint(leftMin)
  210. }
  211. if confLeftMax != "" {
  212. chartView.LeftMax = confLeftMax
  213. } else {
  214. leftMax := actualMax
  215. if leftMax < predictMax {
  216. leftMax = predictMax
  217. }
  218. chartView.LeftMax = fmt.Sprint(leftMax)
  219. }
  220. chartView.StartDate = startDate.Format(utils.FormatDate)
  221. chartView.EndDate = endDate.Format(utils.FormatDate)
  222. // 日度图表的分割线日期
  223. if source == aiPredictModel.ModelDataSourceDaily {
  224. var dataResp struct {
  225. ActualLatestTimestamp int64
  226. }
  227. dataResp.ActualLatestTimestamp = actualLatestTimestamp
  228. resp.DataResp = dataResp
  229. }
  230. resp.ChartInfo = chartView
  231. resp.EdbInfoList = edbList
  232. return
  233. }