<commit_before>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package endpoints

import (
	"encoding/json"
	"sync"
	"time"

	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

const (
	EndpointCacheExpireTime = 3600 //Seconds
)

type Cache struct {
	sync.RWMutex
	cache map[string]interface{}
}

func (c *Cache) Get(k string) (v interface{}) {
	c.RLock()
	v = c.cache[k]
	c.RUnlock()
	return
}

func (c *Cache) Set(k string, v interface{}) {
	c.Lock()
	c.cache[k] = v
	c.Unlock()
}

var lastClearTimePerProduct = &Cache{cache: make(map[string]interface{})}
var endpointCache = &Cache{cache: make(map[string]interface{})}

type LocationResolver struct {
}

func (resolver *LocationResolver) GetName() (name string) {
	name = "location resolver"
	return
}

func (resolver *LocationResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
	if len(param.LocationProduct) <= 0 {
		support = false
		return
	}

	//get from cache
	cacheKey := param.Product + "#" + param.RegionId
	var ok bool
	endpoint, ok = endpointCache.Get(cacheKey).(string)

	if ok && len(endpoint) > 0 && !CheckCacheIsExpire(cacheKey) {
		support = true
		return
	}

	//get from remote
	getEndpointRequest := requests.NewCommonRequest()

	getEndpointRequest.Product = "Location"
	getEndpointRequest.Version = "2015-06-12"
	getEndpointRequest.ApiName = "DescribeEndpoints"
	getEndpointRequest.Domain = "location-readonly.aliyuncs.com"
	getEndpointRequest.Method = "GET"
	getEndpointRequest.Scheme = requests.HTTPS

	getEndpointRequest.QueryParams["Id"] = param.RegionId
	getEndpointRequest.QueryParams["ServiceCode"] = param.LocationProduct
	if len(param.LocationEndpointType) > 0 {
		getEndpointRequest.QueryParams["Type"] = param.LocationEndpointType
	} else {
		getEndpointRequest.QueryParams["Type"] = "openAPI"
	}

	response, err := param.CommonApi(getEndpointRequest)
	if err != nil {
		support = false
		return
	}

	if !response.IsSuccess() {
		support = false
		return
	}

	var getEndpointResponse GetEndpointResponse
	err = json.Unmarshal([]byte(response.GetHttpContentString()), &getEndpointResponse)
	if err != nil {
		support = false
		return
	}

	if !getEndpointResponse.Success || getEndpointResponse.Endpoints == nil {
		support = false
		return
	}
	if len(getEndpointResponse.Endpoints.Endpoint) <= 0 {
		support = false
		return
	}
	if len(getEndpointResponse.Endpoints.Endpoint[0].Endpoint) > 0 {
		endpoint = getEndpointResponse.Endpoints.Endpoint[0].Endpoint
		endpointCache.Set(cacheKey, endpoint)
		lastClearTimePerProduct.Set(cacheKey, time.Now().Unix())
		support = true
		return
	}

	support = false
	return
}

func CheckCacheIsExpire(cacheKey string) bool {
	lastClearTime, ok := lastClearTimePerProduct.Get(cacheKey).(int64)
	if !ok {
		return true
	}

	if lastClearTime <= 0 {
		lastClearTime = time.Now().Unix()
		lastClearTimePerProduct.Set(cacheKey, lastClearTime)
	}

	now := time.Now().Unix()
	elapsedTime := now - lastClearTime
	if elapsedTime > EndpointCacheExpireTime {
		return true
	}

	return false
}

type GetEndpointResponse struct {
	Endpoints *EndpointsObj
	RequestId string
	Success   bool
}

type EndpointsObj struct {
	Endpoint []EndpointObj
}

type EndpointObj struct {
	Protocols   map[string]string
	Type        string
	Namespace   string
	Id          string
	SerivceCode string
	Endpoint    string
}<commit_msg>fix location response unmarshal failed issue<commit_after>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package endpoints

import (
	"encoding/json"
	"sync"
	"time"

	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

const (
	EndpointCacheExpireTime = 3600 //Seconds
)

type Cache struct {
	sync.RWMutex
	cache map[string]interface{}
}

func (c *Cache) Get(k string) (v interface{}) {
	c.RLock()
	v = c.cache[k]
	c.RUnlock()
	return
}

func (c *Cache) Set(k string, v interface{}) {
	c.Lock()
	c.cache[k] = v
	c.Unlock()
}

var lastClearTimePerProduct = &Cache{cache: make(map[string]interface{})}
var endpointCache = &Cache{cache: make(map[string]interface{})}

type LocationResolver struct {
}

func (resolver *LocationResolver) GetName() (name string) {
	name = "location resolver"
	return
}

func (resolver *LocationResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
	if len(param.LocationProduct) <= 0 {
		support = false
		return
	}

	//get from cache
	cacheKey := param.Product + "#" + param.RegionId
	var ok bool
	endpoint, ok = endpointCache.Get(cacheKey).(string)

	if ok && len(endpoint) > 0 && !CheckCacheIsExpire(cacheKey) {
		support = true
		return
	}

	//get from remote
	getEndpointRequest := requests.NewCommonRequest()

	getEndpointRequest.Product = "Location"
	getEndpointRequest.Version = "2015-06-12"
	getEndpointRequest.ApiName = "DescribeEndpoints"
	getEndpointRequest.Domain = "location-readonly.aliyuncs.com"
	getEndpointRequest.Method = "GET"
	getEndpointRequest.Scheme = requests.HTTPS

	getEndpointRequest.QueryParams["Id"] = param.RegionId
	getEndpointRequest.QueryParams["ServiceCode"] = param.LocationProduct
	if len(param.LocationEndpointType) > 0 {
		getEndpointRequest.QueryParams["Type"] = param.LocationEndpointType
	} else {
		getEndpointRequest.QueryParams["Type"] = "openAPI"
	}

	response, err := param.CommonApi(getEndpointRequest)
	if err != nil {
		support = false
		return
	}

	if !response.IsSuccess() {
		support = false
		return
	}

	var getEndpointResponse GetEndpointResponse
	err = json.Unmarshal([]byte(response.GetHttpContentString()), &getEndpointResponse)
	if err != nil {
		support = false
		return
	}

	if !getEndpointResponse.Success || getEndpointResponse.Endpoints == nil {
		support = false
		return
	}
	if len(getEndpointResponse.Endpoints.Endpoint) <= 0 {
		support = false
		return
	}
	if len(getEndpointResponse.Endpoints.Endpoint[0].Endpoint) > 0 {
		endpoint = getEndpointResponse.Endpoints.Endpoint[0].Endpoint
		endpointCache.Set(cacheKey, endpoint)
		lastClearTimePerProduct.Set(cacheKey, time.Now().Unix())
		support = true
		return
	}

	support = false
	return
}

func CheckCacheIsExpire(cacheKey string) bool {
	lastClearTime, ok := lastClearTimePerProduct.Get(cacheKey).(int64)
	if !ok {
		return true
	}

	if lastClearTime <= 0 {
		lastClearTime = time.Now().Unix()
		lastClearTimePerProduct.Set(cacheKey, lastClearTime)
	}

	now := time.Now().Unix()
	elapsedTime := now - lastClearTime
	if elapsedTime > EndpointCacheExpireTime {
		return true
	}

	return false
}

type GetEndpointResponse struct {
	Endpoints *EndpointsObj
	RequestId string
	Success   bool
}

type EndpointsObj struct {
	Endpoint []EndpointObj
}

type EndpointObj struct {
	// Protocols   map[string]string
	Type        string
	Namespace   string
	Id          string
	SerivceCode string
	Endpoint    string
}
{'fraction_non_alphanumeric': 0.10745501285347044, 'fraction_numerical': 0.006683804627249357, 'mean_word_length': 4.460350877192982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3085094', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2424, 'n_words': 643}