12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- package range_analysis
- import (
- "encoding/json"
- "errors"
- "eta/eta_chart_lib/models"
- "eta/eta_chart_lib/models/data_manage"
- "eta/eta_chart_lib/services/alarm_msg"
- "eta/eta_chart_lib/services/data"
- "eta/eta_chart_lib/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "time"
- )
- // GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
- func GetAutoCalculateDateDataList(currentDate string, dataList []*models.EdbDataList, req *models.ChartRangeAnalysisExtraConf) (newDataList []*models.EdbDataList, err error) {
- currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
- switch req.DateRangeType {
- case 0:
- // 智能划分得到一个开始日期,和结束日期
- var startDateTime time.Time
- if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
- startDate := req.AutoDateConf.StartDate
- if startDate == "" {
- startDate = "2020-01-01"
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- } else {
- startConf := req.AutoDateConf.StartDateConf
- startDate := ""
- if startConf.BaseDateType == 0 { //
- startDate = currentDate
- } else if startConf.BaseDateType == 1 {
- startDate = time.Now().Format(utils.FormatDate)
- }
- if startConf.MoveForward > 0 {
- startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
- }
- if len(startConf.DateChange) > 0 {
- startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
- if err != nil {
- err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
- return
- }
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- }
- var calStartTime, calEndTime time.Time
- if currentDateTime.Before(startDateTime) {
- calStartTime = currentDateTime
- calEndTime = startDateTime
- } else {
- calStartTime = startDateTime
- calEndTime = currentDateTime
- }
- // 根据日期,获取数据
- for _, vv := range dataList {
- dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
- if dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime) ||
- dataTimeT.Equal(calStartTime) ||
- dataTimeT.Equal(calEndTime) {
- newDataList = append(newDataList, vv)
- }
- }
- }
- return
- }
- // HandleDataByCalculateType 根据计算公式处理数据
- func HandleDataByCalculateType(originList []*models.ChartRangeAnalysisDateDataItem, originDataList []*models.EdbDataList, req *models.ChartRangeAnalysisExtraConf) (newList []*models.EdbDataList, err error) {
- if len(originList) == 0 {
- return
- }
- calculateType := req.CalculateType
- switch calculateType {
- case 0: //均值
- var sum float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- sum = 0
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for _, vv := range calDataList {
- sum += vv.Value
- }
- val := sum / float64(len(calDataList))
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- sum = 0
- for k, v := range item.DataList {
- sum += v.Value
- val := sum / float64(k+1)
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 1: //累计值
- var sum float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- sum = 0
- for _, v := range item.DataList {
- sum = 0
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for _, vv := range calDataList {
- sum += vv.Value
- }
- val := sum
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- sum = 0
- for _, v := range item.DataList {
- sum += v.Value
- val := sum
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 2: //涨幅
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- var baseVal float64
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- if len(calDataList) == 0 {
- continue
- }
- baseVal = calDataList[0].Value
- baseDate := calDataList[0].DataTime
- if baseVal == 0 {
- continue
- }
- if v.DataTime == baseDate {
- continue
- }
- val := (v.Value - baseVal) / baseVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- if len(item.DataList) == 0 {
- break
- }
- baseVal := item.DataList[0].Value
- baseDate := item.DataList[0].DataTime
- if baseVal == 0 {
- break
- }
- for _, v := range item.DataList {
- if v.DataTime == baseDate {
- continue
- }
- val := (v.Value - baseVal) / baseVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 3: //复合增长率
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- var baseVal float64
- var baseDate string
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- if len(calDataList) == 0 {
- continue
- }
- baseVal = calDataList[0].Value
- baseDate = calDataList[0].DataTime
- if v.DataTime == baseDate {
- continue
- }
- if baseVal == 0 {
- continue
- }
- baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- // 计算两个日期相差的天数
- diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
- val := v.Value / baseVal
- val = math.Pow(val, 1/diff) - 1
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- if len(item.DataList) == 0 {
- break
- }
- baseVal := item.DataList[0].Value
- baseDate := item.DataList[0].DataTime
- if baseVal == 0 {
- break
- }
- for _, v := range item.DataList {
- if v.DataTime == baseDate {
- continue
- }
- baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- // 计算两个日期相差的天数
- diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
- val := v.Value / baseVal
- val = math.Pow(val, 1/diff) - 1
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- case 4: //最大值
- var maxVal float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for kk, vv := range calDataList {
- if kk == 0 {
- maxVal = vv.Value
- }
- if vv.Value > maxVal {
- maxVal = vv.Value
- }
- }
- val := maxVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- for k, v := range item.DataList {
- if k == 0 {
- maxVal = v.Value
- }
- if v.Value > maxVal {
- maxVal = v.Value
- }
- val := maxVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- case 5: //最小值
- var minVal float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for kk, vv := range calDataList {
- if kk == 0 {
- minVal = vv.Value
- }
- if vv.Value < minVal {
- minVal = vv.Value
- }
- }
- val := minVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- for k, v := range item.DataList {
- if k == 0 {
- minVal = v.Value
- }
- if v.Value < minVal {
- minVal = v.Value
- }
- val := minVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- }
- return
- }
- // GetChartEdbInfoFormat 区间计算图表-获取指标信息
- func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingList []*models.ChartEdbInfoMapping) (edbList []*models.ChartEdbInfoMapping, err error) {
- edbList = make([]*models.ChartEdbInfoMapping, 0)
- for _, edbInfoMapping := range edbInfoMappingList {
- if edbInfoMapping == nil {
- err = fmt.Errorf("指标信息有误")
- return
- }
- edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
- if edbInfoMapping.Unit == `无` {
- edbInfoMapping.Unit = ``
- }
- if chartInfoId <= 0 {
- edbInfoMapping.IsAxis = 1
- edbInfoMapping.LeadValue = 0
- edbInfoMapping.LeadUnit = ""
- edbInfoMapping.ChartEdbMappingId = 0
- edbInfoMapping.ChartInfoId = 0
- edbInfoMapping.IsOrder = false
- edbInfoMapping.EdbInfoType = 1
- edbInfoMapping.ChartStyle = ""
- edbInfoMapping.ChartColor = ""
- edbInfoMapping.ChartWidth = 0
- } else {
- edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
- }
- edbList = append(edbList, edbInfoMapping)
- }
- return
- }
- // GetChartDataByEdbInfoList 区间计算图表-根据指标信息获取x轴和y轴
- func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, dataResp models.ChartRangeAnalysisDataResp, err error) {
- if chartInfoId > 0 && req.EdbInfoMode == 1 {
- edbList, xEdbIdValue, dataResp, err = GetChartDataByEdbInfoListBySeries(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, req)
- return
- }
- for _, edbInfoMapping := range edbInfoMappingList {
- edbInfoMapping, err = getChartDataByEdbInfo(edbInfoMapping, req)
- if err != nil {
- return
- }
- edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
- edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
- if req.CalculateType == 2 || req.CalculateType == 3 {
- edbInfoMapping.ConvertUnit = "无"
- edbInfoMapping.ConvertEnUnit = "无"
- }
- if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
- edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
- edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
- }
- dataList := edbInfoMapping.DataList.([]*models.EdbDataList)
- // 处理上下限
- var maxData, minData float64
- if len(dataList) > 0 {
- maxData = dataList[0].Value
- minData = dataList[0].Value
- for _, v := range dataList {
- if v.Value > maxData {
- maxData = v.Value
- }
- if v.Value < minData {
- minData = v.Value
- }
- }
- }
- edbInfoMapping.MaxData = maxData
- edbInfoMapping.MinData = minData
- xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
- }
- //根据时间类型来筛选最终的数据
- yearMax := 0
- if dateType == utils.DateTypeNYears {
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*models.EdbDataList)
- if len(dataList) > 0 {
- latestDate := dataList[len(dataList)-1].DataTime
- if latestDate != "" {
- lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
- if tErr != nil {
- err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
- return
- }
- if lastDateT.Year() > yearMax {
- yearMax = lastDateT.Year()
- }
- }
- }
- }
- }
- startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
- if startDate != "" {
- for k, v := range edbInfoMappingList {
- var maxData, minData float64
- dataList := v.DataList.([]*models.EdbDataList)
- newDataList := make([]*models.EdbDataList, 0)
- if len(dataList) == 0 {
- newDataList = dataList
- } else {
- firstFlag := true
- for _, d := range dataList {
- if endDate != "" && d.DataTime > endDate {
- continue
- }
- if d.DataTime < startDate {
- continue
- }
- newDataList = append(newDataList, d)
- if firstFlag {
- maxData = d.Value
- minData = d.Value
- firstFlag = false
- } else {
- if d.Value > maxData {
- maxData = d.Value
- }
- if d.Value < minData {
- minData = d.Value
- }
- }
- }
- }
- edbInfoMappingList[k].DataList = newDataList
- edbInfoMappingList[k].MinData = minData
- edbInfoMappingList[k].MaxData = maxData
- }
- }
- dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
- if req.MultipleGraphConfigId > 0 {
- multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- // 查询是否已经生成指标
- dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
- }
- edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
- if err != nil {
- err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
- return
- }
- return
- }
- func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, dataResp models.ChartRangeAnalysisDataResp, err error) {
- //查询seriesId
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = fmt.Errorf("图表关联关系不存在")
- return
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- }
- //根据seriesId查询数据
- //并把数据放到dataList中
- for _, edbInfoMapping := range edbInfoMappingList {
- dataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
- dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
- if e != nil {
- err = e
- return
- }
- edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
- edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
- if req.CalculateType == 2 || req.CalculateType == 3 {
- edbInfoMapping.ConvertUnit = "无"
- edbInfoMapping.ConvertEnUnit = "无"
- }
- if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
- edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
- edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
- }
- edbInfoMapping.DataList = dataList
- // 处理上下限
- var maxData, minData float64
- if len(dataList) > 0 {
- maxData = dataList[0].Value
- minData = dataList[0].Value
- for _, v := range dataList {
- if v.Value > maxData {
- maxData = v.Value
- }
- if v.Value < minData {
- minData = v.Value
- }
- }
- }
- edbInfoMapping.MaxData = maxData
- edbInfoMapping.MinData = minData
- xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
- }
- yearMax := 0
- if dateType == utils.DateTypeNYears {
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*models.EdbDataList)
- latestDate := dataList[len(dataList)-1].DataTime
- if latestDate != "" {
- lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
- if tErr != nil {
- err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
- return
- }
- if lastDateT.Year() > yearMax {
- yearMax = lastDateT.Year()
- }
- }
- }
- }
- startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
- if startDate != "" {
- for k, v := range edbInfoMappingList {
- var maxData, minData float64
- dataList := v.DataList.([]*models.EdbDataList)
- newDataList := make([]*models.EdbDataList, 0)
- if len(dataList) == 0 {
- newDataList = dataList
- } else {
- firstFlag := true
- for _, d := range dataList {
- if endDate != "" && d.DataTime > endDate {
- continue
- }
- if d.DataTime < startDate {
- continue
- }
- newDataList = append(newDataList, d)
- if firstFlag {
- maxData = d.Value
- minData = d.Value
- firstFlag = false
- } else {
- if d.Value > maxData {
- maxData = d.Value
- }
- if d.Value < minData {
- minData = d.Value
- }
- }
- }
- }
- edbInfoMappingList[k].DataList = newDataList
- edbInfoMappingList[k].MinData = minData
- edbInfoMappingList[k].MaxData = maxData
- }
- }
- dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
- // 查询配置关联关系
- if req.MultipleGraphConfigId > 0 {
- multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- // 查询是否已经生成指标
- dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
- }
- edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
- if err != nil {
- err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
- return
- }
- return
- }
- // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
- func getChartDataByEdbInfo(edbInfoMapping *models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *models.ChartEdbInfoMapping, err error) {
- newEdbInfoMapping = edbInfoMapping
- // 指标的开始日期和结束日期
- edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
- //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
- edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
- edbEndDate := edbEndDateTime.Format(utils.FormatDate)
- // 获取时间基准指标在时间区间内的值
- dataList := make([]*models.EdbDataList, 0)
- switch edbInfoMapping.EdbInfoCategoryType {
- case 0:
- dataList, err = models.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
- default:
- err = errors.New("指标base类型异常")
- return
- }
- dateList := make([]*models.ChartRangeAnalysisDateDataItem, 0)
- switch req.DateRangeType {
- case 0:
- // 智能划分得到一个开始日期,和结束日期
- var startDateTime, endDateTime time.Time
- startDateTime = edbStartDateTime
- if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
- startDate := req.AutoDateConf.StartDate
- if startDate == "" {
- startDate = "2020-01-01"
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- }
- if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
- endDate := req.AutoDateConf.EndDate
- if endDate == "" {
- err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
- return
- }
- // todo 如果截止日期比指标日期还要大,则用指标的最新日期
- endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- } else {
- endConf := req.AutoDateConf.EndDateConf
- endDate := edbEndDate
- if endConf.MoveForward > 0 {
- endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
- }
- if len(endConf.DateChange) > 0 {
- endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
- if err != nil {
- err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
- return
- }
- }
- endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- }
- dateList = append(dateList, &models.ChartRangeAnalysisDateDataItem{
- StartDate: startDateTime,
- EndDate: endDateTime})
- case 1:
- // 手工划分得到多个开始日期和结束日期(已排序)
- for _, v := range req.ManualDateConf {
- startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
- endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- tmp := &models.ChartRangeAnalysisDateDataItem{
- StartDate: startDateT,
- EndDate: endDateT,
- }
- dateList = append(dateList, tmp)
- }
- case 2:
- // 跨年划分得到多个开始日期和结束日期
- startYear := edbStartDateTime.Year()
- endYear := edbEndDateTime.Year()
- startDay := req.YearDateConf.StartDay
- endDay := req.YearDateConf.EndDay
- for year := startYear; year <= endYear; year++ {
- startDate := fmt.Sprintf("%d-%s", year, startDay)
- endDate := fmt.Sprintf("%d-%s", year+1, endDay)
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- if startDateTime.Before(edbStartDateTime) {
- break
- }
- tmp := &models.ChartRangeAnalysisDateDataItem{
- StartDate: startDateTime,
- EndDate: endDateTime,
- }
- dateList = append(dateList, tmp)
- }
- }
- // 根据日期,获取数据
- for _, v := range dateList {
- for _, vv := range dataList {
- dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
- if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
- dataTimeT.Equal(v.StartDate) ||
- dataTimeT.Equal(v.EndDate) {
- v.DataList = append(v.DataList, vv)
- }
- }
- }
- // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
- newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
- if err != nil {
- return
- }
- if req.UnNormalDataDealType > 0 {
- switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
- case 1:
- dealDataList := make([]*models.EdbDataList, 0)
- for _, v := range newDataList {
- if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
- dealDataList = append(dealDataList, v)
- }
- }
- case 2:
- for i, v := range newDataList {
- if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
- newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
- }
- }
- }
- }
- if req.DataConvertType > 0 {
- // 数据转换类型 0不转, 1乘 2除 3对数
- switch req.DataConvertType {
- case 1:
- for i, v := range newDataList {
- val := v.Value * req.DataConvertConf.Value
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = item.MaxData * v.ConvertValue
- //item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, v := range newDataList {
- val := v.Value / req.DataConvertConf.Value
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = item.MaxData / v.ConvertValue
- //item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, v := range newDataList {
- if v.Value <= 0 {
- err = errors.New("数据中含有负数或0,无法对数运算")
- return
- }
- val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- newEdbInfoMapping.DataList = newDataList
- //时间处理
- return
- }
- // RollingCorrelationChartDataResp 滚动区间计算图表数据
- type RollingCorrelationChartDataResp struct {
- MaxData float64
- MinData float64
- LatestDate string `description:"真实数据的最后日期"`
- EdbInfoCategoryType int
- ChartColor string
- ChartStyle string
- PredictChartColor string
- ChartType int
- ChartWidth int
- EdbName string
- EdbNameEn string
- Unit string
- UnitEn string
- IsAxis int
- DataList []models.EdbDataList
- }
- // ChartInfoRefresh 图表刷新
- func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- var chartInfo *models.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = models.GetChartInfoById(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- } else {
- chartInfo, err = models.GetChartInfoByUniqueCode(uniqueCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- }
- // 1.刷新图表关联的指标
- mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
- if e != nil {
- utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
- return
- }
- if len(mappings) == 0 {
- utils.FileLog.Info("图表无关联指标")
- return
- }
- var edbIds []int
- for _, v := range mappings {
- edbIds = append(edbIds, v.EdbInfoId)
- }
- if e, _ = data.EdbInfoRefreshAllFromBase(edbIds, false); e != nil {
- utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
- return
- }
- // todo 重新计算
- // 区间计算图表配置校验
- var extraConfig models.ChartRangeAnalysisExtraConf
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- } else {
- _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
- if e != nil {
- err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
- return
- }
- }
- // 4.清除图表缓存
- key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
- _ = utils.Rc.Delete(key)
- return
- }
- func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*models.EdbDataList) (date string) {
- // 根据日期进行排序
- index := 0
- length := len(edbDataList)
- for i := length - 1; i >= 0; i-- {
- item := edbDataList[i]
- if item.DataTime == startDate {
- index += 1
- continue
- }
- if index >= moveForward {
- date = item.DataTime
- break
- }
- if index > 0 {
- index += 1
- date = item.DataTime
- }
- }
- return
- }
- // HandleEdbDateChange 处理日期变换
- func HandleEdbDateChange(date string, dateChange []*models.EdbDataDateChangeConf) (newDate string, err error) {
- newDate = date
- if newDate != "" {
- if len(dateChange) > 0 {
- var dateTime time.Time
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- for _, v := range dateChange {
- if v.ChangeType == 1 {
- dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
- newDate = dateTime.Format(utils.FormatDate)
- } else if v.ChangeType == 2 {
- newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
- if err != nil {
- return
- }
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- }
- }
- }
- }
- return
- }
- // FactorEdbStepCalculateRange 因子指标-区间计算
- func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf models.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
- // todo 如果指标已保存,则用指标数据还是图表指标数据?
- // 获取图表x轴y轴
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("FactorEdbStepCalculateRange 区间计算, ErrMsg: %v", err)
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- /*if len(calculateResp.Fail) > 0 {
- tips := "StepCalculate计算失败, ErrMsg: "
- for _, f := range calculateResp.Fail {
- tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
- }
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 2)
- }*/
- }()
- edbInfoMappingList, e := models.GetChartEdbMappingListByEdbInfoIdList(edbArr)
- if e != nil {
- err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
- return
- }
- _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
- if err != nil {
- err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
- return
- }
- // 重新计算-先清除原数据
- calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
- cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
- pars := make([]interface{}, 0)
- pars = append(pars, seriesId)
- if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
- err = fmt.Errorf("清除原数据失败, err: %v", e)
- return
- }
- // 计算成功的保存结果
- dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0)
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*models.EdbDataList)
- for _, dataItem := range dataList {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
- dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{
- FactorEdbSeriesId: seriesId,
- EdbInfoId: v.EdbInfoId,
- EdbCode: v.EdbCode,
- DataTime: dataTime,
- Value: dataItem.Value,
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- DataTimestamp: dataItem.DataTimestamp,
- })
- }
- }
- if len(dataArr) == 0 {
- err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
- return
- }
- if e = calculateDataOb.CreateMulti(dataArr); e != nil {
- err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
- return
- }
- return
- }
|