edb_info_calculate.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "github.com/shopspring/decimal"
  9. "math"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. func CheckFormula(formula string) map[string]string {
  15. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN", "EXP"}
  16. str := strings.ToUpper(formula)
  17. for _, v := range mathFormula {
  18. str = strings.Replace(str, v, "", -1)
  19. }
  20. str = strings.Replace(str, "(", "", -1)
  21. str = strings.Replace(str, ")", "", -1)
  22. byteMap := make(map[string]string)
  23. for i := 0; i < len(str); i++ {
  24. byteInt := str[i]
  25. if byteInt >= 65 && byteInt <= 90 {
  26. byteStr := string(byteInt)
  27. if _, ok := byteMap[byteStr]; !ok {
  28. byteMap[byteStr] = byteStr
  29. }
  30. }
  31. }
  32. return byteMap
  33. }
  34. type FormulaListItem struct {
  35. Formula string `json:"f"`
  36. Date string `json:"d"`
  37. }
  38. // CheckFormulaJson 检测计算公式json串是否异常
  39. func CheckFormulaJson(formula string) (formulaSlice []string, err error) {
  40. list := make([]FormulaListItem, 0)
  41. err = json.Unmarshal([]byte(formula), &list)
  42. if err != nil {
  43. err = fmt.Errorf("公式串解析失败: json.Unmarshal Err: %v", err)
  44. return
  45. }
  46. formulaSlice = make([]string, 0)
  47. // 日期排序
  48. for _, v := range list {
  49. formulaSlice = append(formulaSlice, v.Formula)
  50. }
  51. return
  52. }
  53. type CalculateItems struct {
  54. EdbInfoId int
  55. DataMap map[string]float64
  56. }
  57. // handleDataByLinearRegression 插值法补充数据(线性方程式)
  58. func handleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  59. if len(edbInfoDataList) < 2 {
  60. return
  61. }
  62. var startEdbInfoData *data_manage.EdbDataList
  63. for _, v := range edbInfoDataList {
  64. handleDataMap[v.DataTime] = v.Value
  65. // 第一个数据就给过滤了,给后面的试用
  66. if startEdbInfoData == nil {
  67. startEdbInfoData = v
  68. continue
  69. }
  70. // 获取两条数据之间相差的天数
  71. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  72. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  73. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  74. betweenDay := betweenHour / 24
  75. // 如果相差一天,那么过滤
  76. if betweenDay <= 1 {
  77. startEdbInfoData = v
  78. continue
  79. }
  80. // 生成线性方程式
  81. var a, b float64
  82. {
  83. coordinateData := make([]utils.Coordinate, 0)
  84. tmpCoordinate1 := utils.Coordinate{
  85. X: 1,
  86. Y: startEdbInfoData.Value,
  87. }
  88. coordinateData = append(coordinateData, tmpCoordinate1)
  89. tmpCoordinate2 := utils.Coordinate{
  90. X: float64(betweenDay) + 1,
  91. Y: v.Value,
  92. }
  93. coordinateData = append(coordinateData, tmpCoordinate2)
  94. a, b = utils.GetLinearResult(coordinateData)
  95. if math.IsNaN(a) || math.IsNaN(b) {
  96. err = errors.New("线性方程公式生成失败")
  97. return
  98. }
  99. }
  100. // 生成对应的值
  101. {
  102. for i := 1; i < betweenDay; i++ {
  103. tmpDataTime := startDataTime.AddDate(0, 0, i)
  104. aDecimal := decimal.NewFromFloat(a)
  105. xDecimal := decimal.NewFromInt(int64(i) + 1)
  106. bDecimal := decimal.NewFromFloat(b)
  107. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  108. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  109. }
  110. }
  111. startEdbInfoData = v
  112. }
  113. return
  114. }
  115. // HandleDataByLinearRegression 插值法补充数据(线性方程式)
  116. func HandleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  117. return handleDataByLinearRegression(edbInfoDataList, handleDataMap)
  118. }
  119. // CallCalculateComputeCorrelation 调用计算拟合残差的相关系数
  120. func CallCalculateComputeCorrelation(data *data_manage.EdbInfoCalculateBatchSaveReqByEdbLib, lang string) (val string, err error, errMsg string) {
  121. errMsg = "计算失败"
  122. // 调用指标库去更新
  123. reqJson, err := json.Marshal(data)
  124. if err != nil {
  125. errMsg = "计算相关系数参数解析异常!"
  126. err = errors.New("参数解析失败,Err:" + err.Error())
  127. return
  128. }
  129. respItem, err := CalculateComputeCorrelation(string(reqJson), lang)
  130. if err != nil {
  131. return
  132. }
  133. if respItem.Ret == 200 {
  134. val = respItem.Data
  135. }
  136. return
  137. }
  138. // HandleDataByLinearRegressionToList 插值法补充数据(线性方程式)
  139. func HandleDataByLinearRegressionToList(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
  140. if len(edbInfoDataList) < 2 {
  141. return
  142. }
  143. var startEdbInfoData *data_manage.EdbDataList
  144. for _, v := range edbInfoDataList {
  145. handleDataMap[v.DataTime] = v.Value
  146. dataTimeList = append(dataTimeList, v.DataTime)
  147. // 第一个数据就给过滤了,给后面的试用
  148. if startEdbInfoData == nil {
  149. startEdbInfoData = v
  150. //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
  151. continue
  152. }
  153. // 获取两条数据之间相差的天数
  154. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  155. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  156. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  157. betweenDay := betweenHour / 24
  158. // 如果相差一天,那么过滤
  159. if betweenDay <= 1 {
  160. startEdbInfoData = v
  161. continue
  162. }
  163. // 生成线性方程式
  164. var a, b float64
  165. {
  166. coordinateData := make([]utils.Coordinate, 0)
  167. tmpCoordinate1 := utils.Coordinate{
  168. X: 1,
  169. Y: startEdbInfoData.Value,
  170. }
  171. coordinateData = append(coordinateData, tmpCoordinate1)
  172. tmpCoordinate2 := utils.Coordinate{
  173. X: float64(betweenDay) + 1,
  174. Y: v.Value,
  175. }
  176. coordinateData = append(coordinateData, tmpCoordinate2)
  177. a, b = utils.GetLinearResult(coordinateData)
  178. if math.IsNaN(a) || math.IsNaN(b) {
  179. err = errors.New("线性方程公式生成失败")
  180. return
  181. }
  182. }
  183. // 生成对应的值
  184. {
  185. for i := 1; i < betweenDay; i++ {
  186. tmpDataTime := startDataTime.AddDate(0, 0, i)
  187. aDecimal := decimal.NewFromFloat(a)
  188. xDecimal := decimal.NewFromInt(int64(i) + 1)
  189. bDecimal := decimal.NewFromFloat(b)
  190. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  191. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  192. dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
  193. valueList = append(valueList, val)
  194. }
  195. }
  196. startEdbInfoData = v
  197. }
  198. return
  199. }
  200. // HandleDataByLinearRegressionToList 保证生成365个数据点的线性插值法
  201. func HandleDataByLinearRegressionToListV2(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
  202. if len(edbInfoDataList) < 2 {
  203. return
  204. }
  205. // 确保至少有两天数据来生成线性方程
  206. if len(edbInfoDataList) < 2 {
  207. err = errors.New("至少需要两天的数据来执行线性插值")
  208. return
  209. }
  210. // 对数据按日期排序,确保顺序正确
  211. sort.Slice(edbInfoDataList, func(i, j int) bool {
  212. t1, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[i].DataTime, time.Local)
  213. t2, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[j].DataTime, time.Local)
  214. return t1.Before(t2)
  215. })
  216. startEdbInfoData := edbInfoDataList[0]
  217. endEdbInfoData := edbInfoDataList[len(edbInfoDataList)-1]
  218. // 计算起始和结束日期间实际的天数
  219. startDate, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  220. endDate, _ := time.ParseInLocation(utils.FormatDate, endEdbInfoData.DataTime, time.Local)
  221. actualDays := endDate.Sub(startDate).Hours() / 24
  222. // 生成365个数据点,首先处理已有数据
  223. for _, v := range edbInfoDataList {
  224. handleDataMap[v.DataTime] = v.Value
  225. dataTimeList = append(dataTimeList, v.DataTime)
  226. valueList = append(valueList, v.Value)
  227. }
  228. // 如果已有数据跨越天数不足365天,则对缺失的日期进行线性插值
  229. if actualDays < 365 {
  230. // 使用已有数据点生成线性方程(这里简化处理,实际可能需更细致处理边界情况)
  231. var a, b float64
  232. coordinateData := []utils.Coordinate{
  233. {X: 1, Y: startEdbInfoData.Value},
  234. {X: float64(len(edbInfoDataList)), Y: endEdbInfoData.Value},
  235. }
  236. a, b = utils.GetLinearResult(coordinateData)
  237. if math.IsNaN(a) || math.IsNaN(b) {
  238. err = errors.New("线性方程公式生成失败")
  239. return
  240. }
  241. // 对剩余日期进行插值
  242. for i := 1; i < 365; i++ {
  243. day := startDate.AddDate(0, 0, i)
  244. if _, exists := handleDataMap[day.Format(utils.FormatDate)]; !exists {
  245. aDecimal := decimal.NewFromFloat(a)
  246. xDecimal := decimal.NewFromInt(int64(i) + 1)
  247. bDecimal := decimal.NewFromFloat(b)
  248. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  249. handleDataMap[day.Format(utils.FormatDate)] = val
  250. dataTimeList = append(dataTimeList, day.Format(utils.FormatDate))
  251. valueList = append(valueList, val)
  252. }
  253. }
  254. }
  255. return
  256. }
  257. // HandleDataByLinearRegressionToListV3 插值法补充数据(线性方程式)-直接补充指标起始日期间的所有数据
  258. func HandleDataByLinearRegressionToListV3(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (newEdbInfoDataList []*data_manage.EdbDataList, dataTimeList []string, valueList []float64, err error) {
  259. if len(edbInfoDataList) < 2 {
  260. return
  261. }
  262. var startEdbInfoData *data_manage.EdbDataList
  263. for _, v := range edbInfoDataList {
  264. handleDataMap[v.DataTime] = v.Value
  265. newEdbInfoDataList = append(newEdbInfoDataList, v)
  266. dataTimeList = append(dataTimeList, v.DataTime)
  267. // 第一个数据就给过滤了,给后面的试用
  268. if startEdbInfoData == nil {
  269. startEdbInfoData = v
  270. //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
  271. continue
  272. }
  273. // 获取两条数据之间相差的天数
  274. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  275. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  276. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  277. betweenDay := betweenHour / 24
  278. // 如果相差一天,那么过滤
  279. if betweenDay <= 1 {
  280. startEdbInfoData = v
  281. continue
  282. }
  283. // 生成线性方程式
  284. var a, b float64
  285. {
  286. coordinateData := make([]utils.Coordinate, 0)
  287. tmpCoordinate1 := utils.Coordinate{
  288. X: 1,
  289. Y: startEdbInfoData.Value,
  290. }
  291. coordinateData = append(coordinateData, tmpCoordinate1)
  292. tmpCoordinate2 := utils.Coordinate{
  293. X: float64(betweenDay) + 1,
  294. Y: v.Value,
  295. }
  296. coordinateData = append(coordinateData, tmpCoordinate2)
  297. a, b = utils.GetLinearResult(coordinateData)
  298. if math.IsNaN(a) || math.IsNaN(b) {
  299. err = errors.New("线性方程公式生成失败")
  300. return
  301. }
  302. }
  303. // 生成对应的值
  304. {
  305. for i := 1; i < betweenDay; i++ {
  306. tmpDataTime := startDataTime.AddDate(0, 0, i)
  307. aDecimal := decimal.NewFromFloat(a)
  308. xDecimal := decimal.NewFromInt(int64(i) + 1)
  309. bDecimal := decimal.NewFromFloat(b)
  310. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  311. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  312. dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
  313. valueList = append(valueList, val)
  314. newEdbInfoDataList = append(newEdbInfoDataList, &data_manage.EdbDataList{
  315. EdbDataId: v.EdbDataId,
  316. EdbInfoId: v.EdbInfoId,
  317. DataTime: tmpDataTime.Format(utils.FormatDate),
  318. DataTimestamp: tmpDataTime.UnixNano() / 1e6,
  319. Value: val,
  320. })
  321. }
  322. }
  323. startEdbInfoData = v
  324. }
  325. return
  326. }