package services import ( "encoding/json" "errors" "fmt" "github.com/rdlucklib/rdluck_tools/http" "hongze/hongze_edb_lib/services/alarm_msg" "hongze/hongze_edb_lib/utils" "time" ) type EdbDataFromWind struct { Close map[string]float64 `json:"CLOSE"` Dt map[string]int64 `json:"DT"` ErrorCode map[string]int64 `json:"ErrorCode"` ErrMsg string } // GetEdbDataFromWind 获取wind数据 func GetEdbDataFromWind(edbCode, startDate, endDate string) (item *EdbDataFromWind, err error) { windUrl, err := GetWindUrl(edbCode) if err != nil { 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("wind result:", string(body))) if err != nil { return } item = new(EdbDataFromWind) err = json.Unmarshal(body, &item) //异常的话,需要邮件通知 if len(item.ErrorCode) > 0 { if item.ErrorCode["0"] != 0 { go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind数据服务异常,edbCode:%s,ErrorCode:%d", edbCode, item.ErrorCode["0"]), 3) } } return } // GetWindUrl 获取wind的url 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 } //如果缓存中没有的话,那么从配置中获取 for _, windUrlMap := range utils.Hz_Wind_Data_Url_LIST { //判断该url是否被占满了 count, tmpErr := GetCountEdbCodeInWindUrl(windUrlMap.Url) if tmpErr != nil && tmpErr.Error() != "nil returned" { err = tmpErr return } if count < windUrlMap.Num { windUrl = windUrlMap.Url AddEdbCodeInWindUrl(windUrlMap.Url, edbCode) return } } return } // GetCountEdbCodeInWindUrl 从缓存key中获取已经插入入的指标数 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 } // AddEdbCodeInWindUrl 将指标插入到缓存key中 // @return isInsert bool 是否插入数据,true时为插入数据,false表示数据已存在 func AddEdbCodeInWindUrl(windUrl, 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) return }