ai_predict_model_index.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // EdbList-固定一条为标的实际值、一条为预测值
  122. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  123. edbActual, edbPredict := new(data_manage.ChartEdbInfoMapping), new(data_manage.ChartEdbInfoMapping)
  124. edbActual.EdbName = indexItem.IndexName
  125. edbActual.EdbNameEn = indexItem.IndexName
  126. edbActual.IsAxis = 1
  127. edbActual.Unit = unit
  128. edbActual.UnitEn = unit
  129. edbPredict.EdbName = predictLegendName
  130. edbPredict.EdbNameEn = predictLegendName
  131. edbPredict.IsAxis = 1
  132. edbPredict.Unit = unit
  133. edbPredict.UnitEn = unit
  134. actualData, predictData := make([]*data_manage.EdbDataList, 0), make([]*data_manage.EdbDataList, 0)
  135. var startDate, endDate time.Time
  136. var actualValues, predictValues []float64
  137. var actualNewest, predictNewest bool
  138. var actualLatestTimestamp int64 // 实际值最后一天的时间戳,作为日度图表的分割线
  139. for k, v := range indexData {
  140. // 如果实际值和预测值都是null那么该日期无效直接忽略
  141. if !v.Value.Valid && !v.PredictValue.Valid {
  142. continue
  143. }
  144. // 将有效值加入[]float64,最后取极值
  145. if v.Value.Valid {
  146. actualValues = append(actualValues, v.Value.Float64)
  147. }
  148. if v.PredictValue.Valid {
  149. predictValues = append(predictValues, v.PredictValue.Float64)
  150. }
  151. // 开始结束时间
  152. if k == 0 {
  153. startDate = v.DataTime
  154. endDate = v.CreateTime
  155. }
  156. if v.DataTime.Before(startDate) {
  157. startDate = v.DataTime
  158. }
  159. if v.DataTime.After(endDate) {
  160. endDate = v.DataTime
  161. }
  162. // 指标数据
  163. if v.Value.Valid {
  164. if !actualNewest {
  165. edbActual.LatestDate = v.DataTime.Format(utils.FormatDate)
  166. edbActual.LatestValue = v.Value.Float64
  167. actualLatestTimestamp = v.DataTime.UnixNano() / 1e6
  168. actualNewest = true
  169. }
  170. actualData = append(actualData, &data_manage.EdbDataList{
  171. DataTime: v.DataTime.Format(utils.FormatDate),
  172. Value: v.Value.Float64,
  173. DataTimestamp: v.DataTimestamp,
  174. })
  175. }
  176. if v.PredictValue.Valid {
  177. if !predictNewest {
  178. edbPredict.LatestDate = v.DataTime.Format(utils.FormatDate)
  179. edbPredict.LatestValue = v.Value.Float64
  180. predictNewest = true
  181. }
  182. predictData = append(predictData, &data_manage.EdbDataList{
  183. DataTime: v.DataTime.Format(utils.FormatDate),
  184. Value: v.PredictValue.Float64,
  185. DataTimestamp: v.DataTimestamp,
  186. })
  187. }
  188. }
  189. // 极值
  190. actualMin, actualMax := utils.FindMinMax(actualValues)
  191. predictMin, predictMax := utils.FindMinMax(predictValues)
  192. edbActual.MinData = actualMin
  193. edbActual.MaxData = actualMax
  194. edbPredict.MinData = predictMin
  195. edbPredict.MaxData = predictMax
  196. edbActual.DataList = actualData
  197. edbPredict.DataList = predictData
  198. edbList = append(edbList, edbActual, edbPredict)
  199. // 上下限
  200. if confLeftMin != "" {
  201. chartView.LeftMin = confLeftMin
  202. } else {
  203. leftMin := actualMin
  204. if leftMin > predictMin {
  205. leftMin = predictMin
  206. }
  207. chartView.LeftMin = fmt.Sprint(leftMin)
  208. }
  209. if confLeftMax != "" {
  210. chartView.LeftMax = confLeftMax
  211. } else {
  212. leftMax := actualMax
  213. if leftMax < predictMax {
  214. leftMax = predictMax
  215. }
  216. chartView.LeftMax = fmt.Sprint(leftMax)
  217. }
  218. chartView.StartDate = startDate.Format(utils.FormatDate)
  219. chartView.EndDate = endDate.Format(utils.FormatDate)
  220. // 日度图表的分割线日期
  221. if source == aiPredictModel.ModelDataSourceDaily {
  222. var dataResp struct {
  223. ActualLatestTimestamp int64
  224. }
  225. dataResp.ActualLatestTimestamp = actualLatestTimestamp
  226. resp.DataResp = dataResp
  227. }
  228. resp.ChartInfo = chartView
  229. resp.EdbInfoList = edbList
  230. return
  231. }