123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- package services
- import (
- "encoding/json"
- "errors"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/services/alarm_msg"
- "eta/eta_index_lib/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "time"
- )
- const (
- WindNoAuthCode = -40522015
- )
- func GetEdbDataFromWind(edbCode, startDate, endDate string) (item *models.EdbDataFromWind, errorCode int, err error) {
- windUrl, err := GetWindUrl(edbCode)
- if err != nil {
- errorCode = 421
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("获取wind服务器地址失败,err:%s", err.Error()), 3)
- return
- }
- thsUrl := windUrl + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
- thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
- utils.FileLog.Info(fmt.Sprintf("windUrl:%s", thsUrl))
- body, err := http.Get(thsUrl)
- fmt.Println("GetEdbDataByWind body:")
- fmt.Println(string(body))
- utils.FileLog.Info(fmt.Sprint("指标编码:", edbCode, ";wind result:", string(body)))
- if err != nil {
- return
- }
- item = new(models.EdbDataFromWind)
- err = json.Unmarshal(body, &item)
-
- if len(item.ErrorCode) > 0 {
- if item.ErrorCode["0"] != 0 {
- if item.ErrorCode["0"] == -40522017 {
-
-
-
-
-
-
-
-
-
-
-
-
- SetIsLimitEdbCodeInWindUrl(windUrl)
- err = DeleteEdbCodeInWindUrl(edbCode)
- if err != nil {
- return
- }
- return GetEdbDataFromWind(edbCode, startDate, endDate)
- } else if item.ErrorCode["0"] == -40520005 {
-
-
- SetIsLimitEdbCodeInWindUrl(windUrl)
- err = DeleteEdbCodeInWindUrl(edbCode)
- if err != nil {
- return
- }
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
- return GetEdbDataFromWind(edbCode, startDate, endDate)
- } else if item.ErrorCode["0"] == WindNoAuthCode {
-
- return nil, WindNoAuthCode, nil
- } else {
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
- }
- }
- }
- return
- }
- func GetEdbDataFromWindUrl(windUrl, edbCode, startDate, endDate string) (item *models.EdbDataFromWind, errorCode int, err error) {
-
- if windUrl == `` {
- return GetEdbDataFromWind(edbCode, startDate, endDate)
- }
- requestWindUrl := windUrl + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
- requestWindUrl = fmt.Sprintf(requestWindUrl, edbCode, startDate, endDate)
- utils.FileLog.Info(fmt.Sprintf("windUrl:%s", requestWindUrl))
- body, err := http.Get(requestWindUrl)
- fmt.Println("GetEdbDataByWind body:")
- fmt.Println(string(body))
- utils.FileLog.Info(fmt.Sprint("wind result:", string(body)))
- if err != nil {
- return
- }
- item = new(models.EdbDataFromWind)
- err = json.Unmarshal(body, &item)
-
- if len(item.ErrorCode) > 0 {
- if item.ErrorCode["0"] != 0 {
- if item.ErrorCode["0"] == -40522017 {
-
-
-
-
-
-
-
-
-
-
-
-
- errorCode = 421
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据接口超限,地址:%s", requestWindUrl), 2)
-
- return
- } else if item.ErrorCode["0"] == -40520005 {
-
-
- err = errors.New("No Python API Authority")
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
- return
- } else if item.ErrorCode["0"] == WindNoAuthCode {
-
- return nil, WindNoAuthCode, nil
- } else {
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
- }
- }
- }
- return
- }
- func GetWindUrl(edbCode string) (windUrl string, err error) {
- defer func() {
- if err == nil && windUrl == "" {
- err = errors.New("获取wind服务器地址失败,指标超限了")
- }
- }()
-
- cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
- windUrl, _ = utils.Rc.RedisString(cacheKey)
- if windUrl != "" {
- return
- }
- terminalList, err := models.GetEdbTerminalListBySource(utils.DATA_SOURCE_WIND)
- if err != nil {
- return
- }
-
- for _, t := range terminalList {
-
-
-
-
-
-
-
-
-
-
-
-
- isLimit, tmpErr := GetIsLimitEdbCodeInWindUrl(t.ServerUrl)
- if isLimit {
- err = tmpErr
- continue
- }
- windUrl = t.ServerUrl
- AddEdbCodeInWindUrl(windUrl, t.TerminalCode, edbCode)
- return
- }
- return
- }
- func GetCountEdbCodeInWindUrl(windUrl string) (num int, err error) {
- cacheKey := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
- num, err = utils.Rc.RedisInt(cacheKey)
- if err != nil && err.Error() == "redigo: nil returned" {
- err = nil
- }
- return
- }
- func GetIsLimitEdbCodeInWindUrl(windUrl string) (isLimit bool, err error) {
- cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
- num, err := utils.Rc.RedisInt(cacheKey)
- if err != nil && err.Error() == "redigo: nil returned" {
- err = nil
- }
- if num > 0 {
- isLimit = true
- }
- return
- }
- func SetIsLimitEdbCodeInWindUrl(windUrl string) {
- cacheKey := utils.CACHE_WIND_URL + ":limit:" + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
- _ = utils.Rc.SetNX(cacheKey, 1, utils.GetTodayLastSecond())
- return
- }
- func AddEdbCodeInWindUrl(windUrl, terminalCode, edbCode string) (isInsert bool) {
- cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
- isInsert = utils.Rc.SetNX(cacheKey, windUrl, utils.GetTodayLastSecond())
- cacheKey2 := utils.CACHE_WIND_URL + time.Now().Format(utils.FormatDateUnSpace) + ":" + utils.MD5(windUrl)
- utils.Rc.Incrby(cacheKey2, 1)
-
- terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
- _ = utils.Rc.Put(terminalCodeCacheKey, terminalCode, utils.GetTodayLastSecond())
- return
- }
- func DeleteEdbCodeInWindUrl(edbCode string) (err error) {
- cacheKey := utils.CACHE_WIND_URL + ":" + edbCode
- err = utils.Rc.Delete(cacheKey)
- terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
- err = utils.Rc.Delete(terminalCodeCacheKey)
- return
- }
|