123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338 |
- package correlation
- import (
- "encoding/json"
- "errors"
- "eta/eta_mobile/models/data_manage"
- "eta/eta_mobile/models/system"
- "eta/eta_mobile/services/alarm_msg"
- "eta/eta_mobile/services/data"
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // HandleDataByLinearRegression 线性方程插值法补全数据
- func HandleDataByLinearRegression(originList []*data_manage.EdbDataList, handleDataMap map[string]float64) (newList []*data_manage.EdbDataList, err error) {
- if len(originList) < 2 {
- return
- }
- var startEdbInfoData *data_manage.EdbDataList
- for _, v := range originList {
- handleDataMap[v.DataTime] = v.Value
- // 第一个数据就给过滤了,给后面的试用
- if startEdbInfoData == nil {
- startEdbInfoData = v
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: v.Value,
- })
- continue
- }
- // 获取两条数据之间相差的天数
- startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- betweenHour := int(currDataTime.Sub(startDataTime).Hours())
- betweenDay := betweenHour / 24
- // 如果相差一天,那么过滤
- if betweenDay <= 1 {
- startEdbInfoData = v
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: v.Value,
- })
- continue
- }
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- tmpCoordinate1 := utils.Coordinate{
- X: 1,
- Y: startEdbInfoData.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- tmpCoordinate2 := utils.Coordinate{
- X: float64(betweenDay) + 1,
- Y: v.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate2)
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = fmt.Errorf("线性方程公式生成失败")
- return
- }
- }
- // 生成对应的值
- {
- for i := 1; i < betweenDay; i++ {
- tmpDataTime := startDataTime.AddDate(0, 0, i)
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: tmpDataTime.Format(utils.FormatDate),
- Value: val,
- })
- }
- }
- // 最后将自己赋值
- newList = append(newList, &data_manage.EdbDataList{
- EdbDataId: v.EdbDataId,
- DataTime: v.DataTime,
- Value: v.Value,
- })
- startEdbInfoData = v
- }
- return
- }
- // MoveDataDaysToNewDataList 平移指标数据生成新的数据序列
- func MoveDataDaysToNewDataList(dataList []*data_manage.EdbDataList, moveDay int) (newDataList []data_manage.EdbDataList, dateDataMap map[string]float64) {
- dateMap := make(map[time.Time]float64)
- var minDate, maxDate time.Time
- dateDataMap = make(map[string]float64)
- for _, v := range dataList {
- currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if minDate.IsZero() || currDate.Before(minDate) {
- minDate = currDate
- }
- if maxDate.IsZero() || currDate.After(maxDate) {
- maxDate = currDate
- }
- dateMap[currDate] = v.Value
- }
- // 处理领先、滞后数据
- newDateMap := make(map[time.Time]float64)
- for currDate, value := range dateMap {
- newDate := currDate.AddDate(0, 0, moveDay)
- newDateMap[newDate] = value
- }
- minDate = minDate.AddDate(0, 0, moveDay)
- maxDate = maxDate.AddDate(0, 0, moveDay)
- // 获取日期相差日
- dayNum := utils.GetTimeSubDay(minDate, maxDate)
- for i := 0; i <= dayNum; i++ {
- currDate := minDate.AddDate(0, 0, i)
- tmpValue, ok := newDateMap[currDate]
- if !ok {
- //找不到数据,那么就用前面的数据吧
- if len(newDataList)-1 < 0 {
- tmpValue = 0
- } else {
- tmpValue = newDataList[len(newDataList)-1].Value
- }
- }
- tmpData := data_manage.EdbDataList{
- DataTime: currDate.Format(utils.FormatDate),
- Value: tmpValue,
- }
- dateDataMap[tmpData.DataTime] = tmpData.Value
- newDataList = append(newDataList, tmpData)
- }
- return
- }
- // GetChartEdbInfoFormat 相关性图表-获取指标信息
- func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- if edbInfoMappingA == nil || edbInfoMappingB == nil {
- err = fmt.Errorf("指标信息有误")
- return
- }
- edbInfoMappingA.FrequencyEn = data.GetFrequencyEn(edbInfoMappingA.Frequency)
- if edbInfoMappingA.Unit == `无` {
- edbInfoMappingA.Unit = ``
- }
- if edbInfoMappingB.Unit == `无` {
- edbInfoMappingB.Unit = ``
- }
- if chartInfoId <= 0 {
- edbInfoMappingA.IsAxis = 1
- edbInfoMappingA.LeadValue = 0
- edbInfoMappingA.LeadUnit = ""
- edbInfoMappingA.ChartEdbMappingId = 0
- edbInfoMappingA.ChartInfoId = 0
- edbInfoMappingA.IsOrder = false
- edbInfoMappingA.EdbInfoType = 1
- edbInfoMappingA.ChartStyle = ""
- edbInfoMappingA.ChartColor = ""
- edbInfoMappingA.ChartWidth = 0
- edbInfoMappingB.IsAxis = 1
- edbInfoMappingB.LeadValue = 0
- edbInfoMappingB.LeadUnit = ""
- edbInfoMappingB.ChartEdbMappingId = 0
- edbInfoMappingB.ChartInfoId = 0
- edbInfoMappingB.IsOrder = false
- edbInfoMappingB.EdbInfoType = 1
- edbInfoMappingB.ChartStyle = ""
- edbInfoMappingB.ChartColor = ""
- edbInfoMappingB.ChartWidth = 0
- } else {
- edbInfoMappingA.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingA.LeadUnit)
- edbInfoMappingB.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingB.LeadUnit)
- }
- edbList = append(edbList, edbInfoMappingA, edbInfoMappingB)
- return
- }
- // GetChartDataByEdbInfo 相关性图表-根据指标信息获取x轴和y轴
- func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping, leadValue int, leadUnit, startDate, endDate string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
- xData := make([]int, 0)
- yData := make([]float64, 0)
- if leadValue == 0 {
- xData = append(xData, 0)
- }
- if leadValue > 0 {
- leadMin := 0 - leadValue
- xLen := 2*leadValue + 1
- for i := 0; i < xLen; i++ {
- n := leadMin + i
- xData = append(xData, n)
- }
- }
- // 计算窗口,不包含第一天
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
- //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
- //baseEdbInfo := edbInfoMappingB
- //changeEdbInfo := edbInfoMappingA
- // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
- baseEdbInfo := edbInfoMappingA
- changeEdbInfo := edbInfoMappingB
- // 获取时间基准指标在时间区间内的值
- aDataList := make([]*data_manage.EdbDataList, 0)
- switch baseEdbInfo.EdbInfoCategoryType {
- case 0:
- aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
- case 1:
- _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
- default:
- err = errors.New("指标base类型异常")
- return
- }
- // 获取变频指标所有日期的值, 插值法完善数据
- bDataList := make([]*data_manage.EdbDataList, 0)
- switch changeEdbInfo.EdbInfoCategoryType {
- case 0:
- bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
- case 1:
- _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
- default:
- err = errors.New("指标change类型异常")
- return
- }
- //changeDataMap := make(map[string]float64)
- //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
- //if e != nil {
- // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- // return
- //}
- // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
- baseDataList := make([]*data_manage.EdbDataList, 0)
- baseDataMap := make(map[string]float64)
- changeDataList := make([]*data_manage.EdbDataList, 0)
- changeDataMap := make(map[string]float64)
- // 先把低频指标升频为高频
- {
- frequencyIntMap := map[string]int{
- "日度": 1,
- "周度": 2,
- "旬度": 3,
- "月度": 4,
- "季度": 5,
- "年度": 6,
- }
- // 如果A指标是高频,那么就需要对B指标进行升频
- if frequencyIntMap[edbInfoMappingA.Frequency] < frequencyIntMap[edbInfoMappingB.Frequency] {
- tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- changeDataList = tmpNewChangeDataList
- baseDataList = aDataList
- for _, v := range baseDataList {
- baseDataMap[v.DataTime] = v.Value
- }
- } else if frequencyIntMap[edbInfoMappingA.Frequency] > frequencyIntMap[edbInfoMappingB.Frequency] {
- // 如果B指标是高频,那么就需要对A指标进行升频
- tmpNewChangeDataList, e := HandleDataByLinearRegression(aDataList, baseDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- baseDataList = tmpNewChangeDataList
- changeDataList = bDataList
- for _, v := range changeDataList {
- changeDataMap[v.DataTime] = v.Value
- }
- } else {
- baseDataList = aDataList
- for _, v := range baseDataList {
- baseDataMap[v.DataTime] = v.Value
- }
- changeDataList = bDataList
- for _, v := range changeDataList {
- changeDataMap[v.DataTime] = v.Value
- }
- }
- }
- // 计算不领先也不滞后时的相关系数
- baseCalculateData := make([]float64, 0)
- baseDataTimeArr := make([]string, 0)
- for i := range baseDataList {
- baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
- baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
- }
- //zeroBaseData := make([]float64, 0)
- //zeroCalculateData := make([]float64, 0)
- //for i := range baseDataTimeArr {
- // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
- // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
- // if ok1 && ok2 {
- // zeroBaseData = append(zeroBaseData, tmpBaseVal)
- // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
- // }
- //}
- //if len(zeroBaseData) != len(zeroCalculateData) {
- // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
- // return
- //}
- //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
- //if leadValue == 0 {
- // yData = append(yData, zeroRatio)
- //}
- // 计算领先/滞后N期
- if leadValue > 0 {
- // 平移变频指标领先/滞后的日期(单位天)
- moveUnitDays := utils.FrequencyDaysMap[leadUnit]
- for i := range xData {
- //if xData[i] == 0 {
- // yData = append(yData, zeroRatio)
- // continue
- //}
- xCalculateData := make([]float64, 0)
- yCalculateData := make([]float64, 0)
- // 平移指定天数
- mDays := int(moveUnitDays) * xData[i]
- _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
- // 取出对应的基准日期的值
- for i2 := range baseDataTimeArr {
- tmpDate := baseDataTimeArr[i2]
- if yVal, ok := dMap[tmpDate]; ok {
- xCalculateData = append(xCalculateData, baseCalculateData[i2])
- yCalculateData = append(yCalculateData, yVal)
- }
- }
- if len(yCalculateData) <= 0 {
- //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
- //return
- // 领先滞后后,没有可以计算的数据了
- continue
- }
- // 公式计算出领先/滞后频度对应点的相关性系数
- ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
- yData = append(yData, ratio)
- }
- }
- xEdbIdValue = xData
- yDataList = make([]data_manage.YData, 0)
- yDate := "0000-00-00"
- yDataList = append(yDataList, data_manage.YData{
- Date: yDate,
- Value: yData,
- })
- 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 []data_manage.EdbDataList
- }
- // GetRollingCorrelationChartDataByEdbInfo 滚动相关性计算
- func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping, leadValue int, leadUnit string, calculateValue int, calculateUnit string, startDate, endDate, chartName, chartNameEn string) (dataResp RollingCorrelationChartDataResp, err error) {
- dataResp = RollingCorrelationChartDataResp{
- DataList: make([]data_manage.EdbDataList, 0),
- MaxData: 0,
- MinData: 0,
- ChartColor: "#00f",
- ChartStyle: `spline`,
- PredictChartColor: `#00f`,
- ChartType: 0,
- ChartWidth: 3,
- EdbName: chartName,
- EdbNameEn: chartNameEn,
- IsAxis: 1,
- }
- dataList := make([]data_manage.EdbDataList, 0)
- // 计算窗口,不包含第一天
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
- baseEdbInfo := edbInfoMappingA
- changeEdbInfo := edbInfoMappingB
- // 获取时间基准指标在时间区间内的值
- aDataList := make([]*data_manage.EdbDataList, 0)
- switch baseEdbInfo.EdbInfoCategoryType {
- case 0:
- aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
- case 1:
- _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, true)
- default:
- err = errors.New("指标base类型异常")
- return
- }
- // 获取变频指标所有日期的值, 插值法完善数据
- bDataList := make([]*data_manage.EdbDataList, 0)
- switch changeEdbInfo.EdbInfoCategoryType {
- case 0:
- bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
- case 1:
- _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
- default:
- err = errors.New("指标change类型异常")
- return
- }
- // 数据平移变频指标领先/滞后的日期(单位天)
- // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
- //baseDataList := make([]*data_manage.EdbDataList, 0)
- baseDataMap := make(map[string]float64)
- changeDataList := make([]*data_manage.EdbDataList, 0)
- changeDataMap := make(map[string]float64)
- // A指标不管三七二十一,先变个频再说
- {
- _, e := HandleDataByLinearRegression(aDataList, baseDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- //baseDataList = tmpNewChangeDataList
- }
- // B指标不管三七二十一,先变个频再说
- {
- tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- changeDataList = tmpNewChangeDataList
- // 平移下日期
- moveUnitDays := utils.FrequencyDaysMap[leadUnit]
- _, changeDataMap = MoveDataDaysToNewDataList(changeDataList, leadValue*moveUnitDays)
- }
- // 计算计算时,需要多少个日期内数据
- calculateDay := utils.FrequencyDaysMap[calculateUnit] * calculateValue
- // 计算 每个日期的相关性值
- {
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- if endDate == `` {
- endDate = baseEdbInfo.EndDate
- }
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- endDateTime = endDateTime.AddDate(0, 0, -(calculateDay - 1))
- // 是否开始第一条数据
- var isStart, isNotFirst bool
- for currDay := startDateTime; !currDay.After(endDateTime); currDay = currDay.AddDate(0, 0, 1) {
- yCalculateData := make([]float64, 0)
- baseCalculateData := make([]float64, 0)
- // 取出对应的基准日期的值
- for i := 0; i < calculateDay; i++ {
- iDay := currDay.AddDate(0, 0, i).Format(utils.FormatDate)
- tmpBaseValue, ok1 := baseDataMap[iDay]
- tmpChangeValue, ok2 := changeDataMap[iDay]
- if ok1 && ok2 {
- baseCalculateData = append(baseCalculateData, tmpBaseValue)
- yCalculateData = append(yCalculateData, tmpChangeValue)
- } else {
- continue
- }
- }
- // 公式计算出领先/滞后频度对应点的相关性系数
- var ratio float64
- if len(baseCalculateData) > 0 {
- ratio = utils.CalculateCorrelationByIntArr(baseCalculateData, yCalculateData)
- } else {
- // 没有数据的话,那就不返回
- continue
- }
- // 过滤前面都是0的数据
- {
- if ratio != 0 {
- isStart = true
- }
- if !isStart {
- continue
- }
- }
- dataTime := currDay.AddDate(0, 0, calculateDay-1)
- dataList = append(dataList, data_manage.EdbDataList{
- //EdbDataId: 0,
- EdbInfoId: 0,
- DataTime: dataTime.Format(utils.FormatDate),
- DataTimestamp: dataTime.UnixNano() / 1e6,
- Value: ratio,
- })
- if !isNotFirst {
- dataResp.MinData = ratio
- dataResp.MaxData = ratio
- isNotFirst = true
- }
- if dataResp.MinData > ratio {
- dataResp.MinData = ratio
- }
- if dataResp.MaxData < ratio {
- dataResp.MaxData = ratio
- }
- }
- dataResp.DataList = dataList
- }
- return
- }
- // 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)
- }
- }()
- correlationChart := new(data_manage.ChartInfoCorrelation)
- if err = correlationChart.GetItemById(chartInfoId); err != nil {
- errMsg = "获取相关性图表失败, Err: " + err.Error()
- return
- }
- // 多因子刷新-异步
- if correlationChart.AnalysisMode == 1 {
- isAsync = true
- go func() {
- // 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.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
- utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
- return
- }
- // 2.刷新指标系列计算数据
- for _, v := range mappings {
- _, e = data.PostRefreshFactorEdbRecalculate(v.EdbInfoId, v.EdbCode)
- if e != nil {
- utils.FileLog.Info(fmt.Sprintf("PostRefreshFactorEdbRecalculate err: %v", e))
- continue
- }
- }
- // 3.刷新图表矩阵
- _, e = data.PostRefreshFactorEdbChartRecalculate(chartInfoId)
- if e != nil {
- utils.FileLog.Info(fmt.Sprintf("PostRefreshFactorEdbRecalculate err: %v", e))
- return
- }
- // 4.清除图表缓存
- key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
- _ = utils.Rc.Delete(key)
- }()
- return
- }
- // 批量刷新ETA指标
- err, _ = data.EdbInfoRefreshAllFromBaseV3([]int{correlationChart.EdbInfoIdFirst, correlationChart.EdbInfoIdSecond}, false, true, false)
- if err != nil {
- return
- }
- // 重新生成数据并更新
- edbInfoMappingA, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
- if err != nil {
- errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + err.Error()
- return
- }
- edbInfoMappingB, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
- if err != nil {
- errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + err.Error()
- return
- }
- periodData, correlationData, err := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
- if err != nil {
- errMsg = "获取相关性图表, 图表计算值失败, Err:" + err.Error()
- return
- }
- periodDataByte, err := json.Marshal(periodData)
- if err != nil {
- errMsg = "相关性图表, X轴信息有误, Err:" + err.Error()
- return
- }
- correlationDataByte, err := json.Marshal(correlationData[0].Value)
- if err != nil {
- errMsg = "相关性图表, Y轴信息有误, Err:" + err.Error()
- return
- }
- correlationChart.PeriodData = string(periodDataByte)
- correlationChart.CorrelationData = string(correlationDataByte)
- correlationChart.ModifyTime = time.Now().Local()
- correlationUpdateCols := []string{"PeriodData", "CorrelationData", "ModifyTime"}
- if err = correlationChart.Update(correlationUpdateCols); err != nil {
- errMsg = "更新相关性图表失败, Err:" + err.Error()
- return
- }
- return
- }
- // GetChartAndCorrelationInfo 获取图表信息和相关信息信息
- func GetChartAndCorrelationInfo(chartInfoId int) (chartInfo *data_manage.ChartInfo, correlationInfo *data_manage.ChartInfoCorrelation, tips string, err error) {
- item, e := data_manage.GetChartInfoById(chartInfoId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- tips = "图表已被删除, 请刷新页面"
- err = fmt.Errorf("图表已被删除, 请刷新页面")
- return
- }
- err = fmt.Errorf("获取图表信息失败, Err: %s", e.Error())
- return
- }
- if item.Source != utils.CHART_SOURCE_CORRELATION {
- tips = "该图不是相关性图表"
- err = fmt.Errorf("该图不是相关性图表")
- return
- }
- chartInfo = item
- correlationInfo = new(data_manage.ChartInfoCorrelation)
- if e = correlationInfo.GetItemById(chartInfo.ChartInfoId); e != nil {
- err = fmt.Errorf("获取图表相关性信息失败, Err: %s", e.Error())
- return
- }
- return
- }
- // AddChartInfo 添加图表
- func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- errMsg = "请填写图表名称!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 相关性图表配置
- if req.CorrelationChartInfo.LeadValue == 0 && source == utils.CHART_SOURCE_CORRELATION {
- errMsg = "请输入领先期数"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.CorrelationChartInfo.LeadUnit == "" {
- errMsg = "请填写领先单位"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
- // errMsg = "请填写开始结束日期"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
- //if e != nil {
- // errMsg = "开始日期格式有误"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //endDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
- //if e != nil {
- // errMsg = "结束日期格式有误"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
- errMsg = "请选择AB指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取分类信息失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var edbInfoIdArr []int
- for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取指标信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- if edbInfo.EdbInfoId <= 0 {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- edbInfo.EdbNameSource = edbInfo.EdbName
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- var chartInfoId int
- // 判断图表是否存在
- {
- var condition string
- var pars []interface{}
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name=? AND source = ? "
- }
- pars = append(pars, req.ChartName, source)
- count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- disableVal := data.CheckIsDisableChart(edbInfoIdArr)
- chartInfo = new(data_manage.ChartInfo)
- chartInfo.ChartName = req.ChartName
- chartInfo.ChartNameEn = req.ChartName
- chartInfo.EdbInfoIds = edbInfoIdStr
- chartInfo.ChartClassifyId = req.ChartClassifyId
- chartInfo.SysUserId = sysUser.AdminId
- chartInfo.SysUserRealName = sysUser.RealName
- chartInfo.CreateTime = time.Now()
- chartInfo.ModifyTime = time.Now()
- chartInfo.IsSetName = 0
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
- chartInfo.ChartType = 9 // 相关性图
- chartInfo.Calendar = "公历"
- chartInfo.DateType = 6
- chartInfo.StartDate = req.StartDate
- chartInfo.EndDate = req.EndDate
- chartInfo.SeasonStartDate = req.StartDate
- chartInfo.SeasonEndDate = req.EndDate
- chartInfo.LeftMin = req.LeftMin
- chartInfo.LeftMax = req.LeftMax
- chartInfo.RightMin = req.RightMin
- chartInfo.RightMax = req.RightMax
- chartInfo.Disabled = disableVal
- chartInfo.Source = source
- chartInfo.ChartThemeId = req.ChartThemeId
- chartInfo.SourcesFrom = req.SourcesFrom
- chartInfo.Instructions = req.Instructions
- chartInfo.MarkersLines = req.MarkersLines
- chartInfo.MarkersAreas = req.MarkersAreas
- // 指标信息
- mapList := make([]*data_manage.ChartEdbMapping, 0)
- for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
- mapItem := new(data_manage.ChartEdbMapping)
- mapItem.EdbInfoId = v.EdbInfoId
- mapItem.CreateTime = time.Now()
- mapItem.ModifyTime = time.Now()
- edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + edbTimestamp + "_" + strconv.Itoa(v.EdbInfoId))
- mapItem.IsOrder = true
- mapItem.IsAxis = 1
- mapItem.EdbInfoType = 1
- mapItem.Source = utils.CHART_SOURCE_CORRELATION
- mapList = append(mapList, mapItem)
- }
- // 相关性图表扩展信息
- correlationChart := new(data_manage.ChartInfoCorrelation)
- correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
- correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
- correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
- correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
- correlationChart.BaseCalculateValue = req.CorrelationChartInfo.BaseCalculateValue
- correlationChart.BaseCalculateUnit = req.CorrelationChartInfo.BaseCalculateUnit
- // 滚动相关性会有日期等信息
- if source == utils.CHART_SOURCE_ROLLING_CORRELATION {
- correlationChart.DateType = req.CorrelationChartInfo.DateType
- if req.CorrelationChartInfo.StartDate != `` {
- startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.StartDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- correlationChart.StartDate = startDateTime
- }
- if req.CorrelationChartInfo.EndDate != `` {
- endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.EndDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- correlationChart.EndDate = endDateTime
- }
- }
- correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
- correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
- correlationChart.CreateTime = time.Now().Local()
- correlationChart.ModifyTime = time.Now().Local()
- //// 生成图表x轴y轴数据
- //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
- // return
- //}
- //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
- // return
- //}
- //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
- // return
- //}
- //periodDataByte, e := json.Marshal(periodData)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
- // return
- //}
- //correlationDataByte, e := json.Marshal(correlationData[0].Value)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
- // return
- //}
- //correlationChart.PeriodData = string(periodDataByte)
- //correlationChart.CorrelationData = string(correlationDataByte)
- // 新增图表和指标mapping
- chartInfoId, e := data_manage.CreateCorrelationChartAndEdb(chartInfo, mapList, correlationChart)
- if e != nil {
- errMsg = "操作失败"
- err = errors.New("新增相关性图表失败, Err: " + e.Error())
- return
- }
- //添加es数据
- go data.EsAddOrEditChartInfo(chartInfoId)
- return
- }
- // EditChartInfo 编辑图表
- func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- if chartItem.Source != utils.CHART_SOURCE_CORRELATION && chartItem.Source != utils.CHART_SOURCE_ROLLING_CORRELATION {
- errMsg = "该图不是相关性图表!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 相关性图表配置
- if req.CorrelationChartInfo.LeadValue == 0 && chartItem.Source == utils.CHART_SOURCE_CORRELATION {
- errMsg = "请输入领先期数"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.CorrelationChartInfo.LeadUnit == "" {
- errMsg = "请填写领先单位"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
- // errMsg = "请填写开始结束日期"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
- if e != nil {
- errMsg = "开始日期格式有误"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var endDate time.Time
- if req.CorrelationChartInfo.EndDate != `` {
- endDate, e = time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
- if e != nil {
- errMsg = "结束日期格式有误"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
- errMsg = "请选择AB指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取分类信息失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 图表操作权限
- ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
- if !ok {
- errMsg = "没有该图表的操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var edbInfoIdArr []int
- for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "图表不存在!"
- err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取图表信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //判断图表是否存在
- {
- var condition string
- var pars []interface{}
- condition += " AND chart_info_id <> ? "
- pars = append(pars, req.ChartInfoId)
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name=? AND source = ? "
- }
- pars = append(pars, req.ChartName, chartItem.Source)
- count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- correlationChart := new(data_manage.ChartInfoCorrelation)
- if e := correlationChart.GetItemById(chartItem.ChartInfoId); e != nil {
- errMsg = "操作失败"
- err = errors.New("图表相关性信息不存在, Err: " + e.Error())
- return
- }
- // 图表启用与否
- disableVal := data.CheckIsDisableChart(edbInfoIdArr)
- // 重新生成图表值, 并修改相关性图表扩展信息
- //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
- // return
- //}
- //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
- // return
- //}
- //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
- // return
- //}
- //periodDataByte, e := json.Marshal(periodData)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
- // return
- //}
- //correlationDataByte, e := json.Marshal(correlationData[0].Value)
- //if e != nil {
- // errMsg = "获取失败"
- // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
- // return
- //}
- correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
- correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
- correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
- correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
- correlationChart.StartDate = startDate
- correlationChart.EndDate = endDate
- correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
- correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
- // 滚动相关性会有日期等信息
- if chartItem.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
- correlationChart.DateType = req.CorrelationChartInfo.DateType
- }
- //correlationChart.PeriodData = string(periodDataByte)
- //correlationChart.CorrelationData = string(correlationDataByte)
- correlationChart.ModifyTime = time.Now().Local()
- //correlationUpdateCols := []string{"LeadValue", "LeadUnit", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "PeriodData","CorrelationData", "ModifyTime"}
- correlationUpdateCols := []string{"LeadValue", "LeadUnit", "CalculateValue", "CalculateUnit", "DateType", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "ModifyTime"}
- // 修改图表与指标mapping
- req.ChartType = 9
- err = data_manage.EditCorrelationChartInfoAndMapping(&req, edbInfoIdStr, "公历", 6, disableVal, ``,
- correlationChart, correlationUpdateCols)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- resp := new(data_manage.AddChartInfoResp)
- resp.ChartInfoId = chartItem.ChartInfoId
- resp.UniqueCode = chartItem.UniqueCode
- resp.ChartType = req.ChartType
- //添加es数据
- go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
- //修改my eta es数据
- go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
- return
- }
- // CopyChartInfo 复制图表
- func CopyChartInfo(configId, classifyId int, chartName string, correlationChartInfoReq data_manage.CorrelationChartInfoReq, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- configSource := 2
- isSendEmail = true
- // 获取相关性图的配置
- multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(configId, configSource)
- if err != nil {
- return
- }
- multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(configId)
- if err != nil {
- return
- }
- multipleGraphConfig.MultipleGraphConfigId = 0
- err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
- if err != nil {
- return
- }
- // 添加图
- addChartReq := data_manage.AddChartInfoReq{
- ChartClassifyId: classifyId,
- ChartName: chartName,
- ChartType: utils.CHART_TYPE_CURVE,
- Calendar: "公历",
- CorrelationChartInfo: correlationChartInfoReq,
- ChartThemeId: oldChartInfo.ChartThemeId,
- SourcesFrom: oldChartInfo.SourcesFrom,
- Instructions: oldChartInfo.Instructions,
- MarkersLines: oldChartInfo.MarkersLines,
- MarkersAreas: oldChartInfo.MarkersAreas,
- }
- chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
- chartInfo, err, errMsg, isSendEmail = AddChartInfo(addChartReq, chartSource, sysUser, lang)
- if err != nil {
- return
- }
- // 添加关系
- multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
- //Id: 0,
- MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
- ChartInfoId: chartInfo.ChartInfoId,
- Source: configSource,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
- if err != nil {
- return
- }
- //添加es数据
- go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
- return
- }
- // GetFactorChartDataByChartId 获取多因子相关性图表数据
- func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
- if chartInfoId <= 0 {
- return
- }
- // 指标对应的图例
- extra := new(data_manage.CorrelationChartInfoExtraConfig)
- if extraConfig != "" {
- if e := json.Unmarshal([]byte(extraConfig), extra); e != nil {
- err = fmt.Errorf("解析图表额外配置失败, err: %v", e)
- return
- }
- }
- legends := make(map[string]*data_manage.CorrelationChartLegend)
- if extra != nil {
- for _, v := range extra.LegendConfig {
- s := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
- legends[s] = v
- }
- }
- // 获取图表引用到的系列指标
- chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
- cond := fmt.Sprintf(" AND %s = ? AND %s = 1", chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().EdbUsed)
- pars := make([]interface{}, 0)
- pars = append(pars, chartInfoId)
- chartMappings, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
- if e != nil {
- err = fmt.Errorf("获取图表引用系列指标失败")
- return
- }
- // 取出计算结果
- yDataList = make([]data_manage.YData, 0)
- yDate := "0000-00-00"
- for k, m := range chartMappings {
- var values []data_manage.FactorEdbSeriesCorrelationMatrixValues
- if m.CalculateData != "" {
- e = json.Unmarshal([]byte(m.CalculateData), &values)
- if e != nil {
- err = fmt.Errorf("系列指标计算数据有误, err: %v", e)
- return
- }
- }
- var y []float64
- for _, v := range values {
- if k == 0 {
- xEdbIdValue = append(xEdbIdValue, v.XData)
- }
- y = append(y, v.YData)
- }
- var yData data_manage.YData
- yData.Date = yDate
- yData.Value = y
- yData.SeriesEdb.SeriesId = m.FactorEdbSeriesId
- yData.SeriesEdb.EdbInfoId = m.EdbInfoId
- // 图例
- s := fmt.Sprintf("%d-%d", m.FactorEdbSeriesId, m.EdbInfoId)
- legend := legends[s]
- if legend != nil {
- yData.Name = legend.LegendName
- yData.Color = legend.Color
- }
- yDataList = append(yDataList, yData)
- }
- return
- }
|