|
@@ -2,17 +2,22 @@ package services
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "eta/eta_index_lib/logic"
|
|
|
"eta/eta_index_lib/models"
|
|
|
"eta/eta_index_lib/services/alarm_msg"
|
|
|
"eta/eta_index_lib/utils"
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
+ "sort"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
BridgeApiJiaYueIndexDataUrl = "/api/index_data/jiayue/index_data" // 获取指标数据API
|
|
|
+ BridgeApiJiaYueNewIndexUrl = "/api/index_data/jiayue/new_index" // 获取增量指标API
|
|
|
+ BridgeApiJiaYueMenuListUrl = "/api/index_data/jiayue/menu_list" // 获取指标目录API
|
|
|
)
|
|
|
|
|
|
// GetJiaYueIndexDataFromBridge 从桥接服务获取指标数据
|
|
@@ -20,7 +25,8 @@ func GetJiaYueIndexDataFromBridge(param models.BridgeJiaYueIndexDataParams) (ind
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
b, _ := json.Marshal(param)
|
|
|
- tips := fmt.Sprintf("桥接服务-获取嘉悦指标数据, err: %s, params: %s", err.Error(), string(b))
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取嘉悦指标数据失败, err: %s, params: %s", err.Error(), string(b))
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
}
|
|
|
}()
|
|
@@ -79,3 +85,263 @@ func GetJiaYueIndexDataFromBridge(param models.BridgeJiaYueIndexDataParams) (ind
|
|
|
indexData = result.Data
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// GetJiaYueNewIndexFromBridge 从桥接服务获取增量指标
|
|
|
+func GetJiaYueNewIndexFromBridge() (indexData []models.BridgeJiaYueIndexAndData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取嘉悦增量指标失败, err: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueNewIndexUrl)
|
|
|
+ body := ioutil.NopCloser(strings.NewReader(""))
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", url, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("Authorization", checkToken)
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ b, e := ioutil.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(b) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 生产环境解密
|
|
|
+ if utils.RunMode == "release" {
|
|
|
+ str := string(b)
|
|
|
+ str = strings.Trim(str, `"`)
|
|
|
+ b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(models.BridgeJiaYueResultNewIndexData)
|
|
|
+ if e = json.Unmarshal(b, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 200 {
|
|
|
+ err = fmt.Errorf("result: %s", string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexData = result.Data
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetJiaYueMenuListFromBridge 从桥接服务获取指标目录列表
|
|
|
+func GetJiaYueMenuListFromBridge() (indexData []models.BridgeJiaYueIndexMenuData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取嘉悦增量指标失败, err: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueMenuListUrl)
|
|
|
+ body := ioutil.NopCloser(strings.NewReader(""))
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", url, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("Authorization", checkToken)
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ b, e := ioutil.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(b) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 生产环境解密
|
|
|
+ if utils.RunMode == "release" {
|
|
|
+ str := string(b)
|
|
|
+ str = strings.Trim(str, `"`)
|
|
|
+ b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(models.BridgeJiaYueResultMenuListData)
|
|
|
+ if e = json.Unmarshal(b, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 200 {
|
|
|
+ err = fmt.Errorf("result: %s", string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexData = result.Data
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// SyncJiaYueNewIndex 同步嘉悦增量指标
|
|
|
+func SyncJiaYueNewIndex(item models.BridgeJiaYueIndexAndData, menus []models.BridgeJiaYueIndexMenuData) (err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ b, _ := json.Marshal(item)
|
|
|
+ tips := fmt.Sprintf("SyncJiaYueNewIndex同步增量指标失败, Err: %s\nIndex: %s", err.Error(), string(b))
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 校验指标来源
|
|
|
+ sourceId := models.EdbSourceExtendIdMap[item.SourceType]
|
|
|
+ if sourceId <= 0 {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("指标来源ID有误, 忽略同步, sourceId: %d; sourceType: %s", sourceId, item.SourceType))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ sourceTableName := models.EdbDataTableNameMap[sourceId]
|
|
|
+ if sourceTableName == "" {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("指标数据表有误, 忽略同步, sourceId: %d", sourceId))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 指标编码默认使用自增ID
|
|
|
+ indexCode := strconv.Itoa(item.Id)
|
|
|
+
|
|
|
+ // 从桥接服务获取指标和数据
|
|
|
+ var params models.BridgeJiaYueIndexDataParams
|
|
|
+ params.IndexCode = indexCode
|
|
|
+ params.SourceExtend = item.SourceType
|
|
|
+ params.StartDate = utils.BASE_START_DATE
|
|
|
+ params.EndDate = utils.BASE_END_DATE
|
|
|
+ indexData, e := GetJiaYueIndexDataFromBridge(params)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("GetJiaYueIndexDataFromBridge err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没找到数据, 频度有误的忽略掉
|
|
|
+ if indexData.Id <= 0 {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("SyncJiaYueNewIndex indexCode empty, IndexCode: %s", indexCode))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ frequency := TransJiaYueFrequency(item.Frequency)
|
|
|
+ if frequency == "" {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("SyncJiaYueNewIndex frequency empty, Frequency: %s", item.Frequency))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取指标目录对应的所有父级目录
|
|
|
+ parentId := 0
|
|
|
+ parentMenus := GetJiaYueParentMenusByMenu(indexData.MenuData, menus, 0)
|
|
|
+ level := len(parentMenus)
|
|
|
+ if level > 0 {
|
|
|
+ // 将父级目录按照Level倒序排序(因为是从子目录往上找的, 所以Level最大的对应为顶级目录), 再进行遍历
|
|
|
+ sort.Slice(parentMenus, func(i, j int) bool {
|
|
|
+ return parentMenus[i].Level > parentMenus[j].Level
|
|
|
+ })
|
|
|
+ for _, p := range parentMenus {
|
|
|
+ l := level - p.Level - 1
|
|
|
+ c, _, msg := models.SaveEdbClassify(p.Menu.Name, parentId, l, 0, utils.InitAdminId, utils.InitAdminName)
|
|
|
+ if msg != "" {
|
|
|
+ err = fmt.Errorf("SaveEdbClassify err: %s", msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ parentId = c.ClassifyId
|
|
|
+ }
|
|
|
+ }
|
|
|
+ classify, _, msg := models.SaveEdbClassify(indexData.MenuData.Name, parentId, level, 0, utils.InitAdminId, utils.InitAdminName)
|
|
|
+ if msg != "" {
|
|
|
+ err = fmt.Errorf("SaveEdbClassify err: %s", msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增指标
|
|
|
+ addParams := new(models.AddEdbInfoParams)
|
|
|
+ addParams.EdbCode = indexCode
|
|
|
+ addParams.EdbName = item.IndexName
|
|
|
+ addParams.Frequency = frequency
|
|
|
+ addParams.Unit = item.Unit
|
|
|
+ addParams.ClassifyId = classify.ClassifyId
|
|
|
+ addParams.Source = sourceId
|
|
|
+ edbInfo, e := models.EdbInfoAdd(addParams, "", 0, "")
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("EdbInfoAdd err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 刷新指标数据
|
|
|
+ e = models.RefreshEdbDataFromJiaYue(sourceId, edbInfo.EdbInfoId, sourceTableName, edbInfo.EdbCode, params.StartDate, indexData.IndexData)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("RefreshEdbDataFromJiaYue err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新指标最大最小值
|
|
|
+ e, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("UnifiedModifyEdbInfoMaxAndMinInfo err: %s, errMsg: %s", e.Error(), errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新ES
|
|
|
+ go logic.UpdateEs(edbInfo.EdbInfoId)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// TransJiaYueFrequency 频度转换
|
|
|
+func TransJiaYueFrequency(origin string) string {
|
|
|
+ mapping := map[string]string{
|
|
|
+ "日": "日度",
|
|
|
+ "周": "周度",
|
|
|
+ "旬": "旬度",
|
|
|
+ "半月": "旬度",
|
|
|
+ "月": "月度",
|
|
|
+ "季": "季度",
|
|
|
+ "半年": "半年度",
|
|
|
+ "年": "年度",
|
|
|
+ }
|
|
|
+ return mapping[origin]
|
|
|
+}
|
|
|
+
|
|
|
+// GetJiaYueParentMenusByMenu 获取指定目录的父级目录
|
|
|
+func GetJiaYueParentMenusByMenu(menu models.BridgeJiaYueIndexMenuData, menus []models.BridgeJiaYueIndexMenuData, level int) (results []models.BridgeJiaYueIndexMenuWithLevel) {
|
|
|
+ results = make([]models.BridgeJiaYueIndexMenuWithLevel, 0)
|
|
|
+ for _, m := range menus {
|
|
|
+ if menu.ParentId == m.Id {
|
|
|
+ results = append(results, models.BridgeJiaYueIndexMenuWithLevel{
|
|
|
+ Level: level,
|
|
|
+ Menu: m,
|
|
|
+ })
|
|
|
+ ps := GetJiaYueParentMenusByMenu(m, menus, level+1)
|
|
|
+ if len(ps) > 0 {
|
|
|
+ results = append(results, ps...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|