yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/client/modules/mod_ces.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package modules
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/onecloud/pkg/httperrors"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/manager"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/requests"
    26  )
    27  
    28  type SCloudEyeManager struct {
    29  	SResourceManager
    30  }
    31  
    32  func NewCloudEyeManager(cfg manager.IManagerConfig) *SCloudEyeManager {
    33  	return &SCloudEyeManager{SResourceManager: SResourceManager{
    34  		SBaseManager:    NewBaseManager(cfg),
    35  		ServiceName:     ServiceNameCES,
    36  		Region:          cfg.GetRegionId(),
    37  		ProjectId:       cfg.GetProjectId(),
    38  		version:         "V1.0",
    39  		Keyword:         "",
    40  		KeywordPlural:   "metrics",
    41  		ResourceKeyword: "metrics",
    42  	}}
    43  }
    44  
    45  type SMetricDimension struct {
    46  	Name  string `json:"name"`
    47  	Value string `json:"value"`
    48  }
    49  
    50  type SDatapoint struct {
    51  	Timestamp int64   `json:"timestamp"`
    52  	Max       float64 `json:"max,omitzero"`
    53  	Min       float64 `json:"min,omitzero"`
    54  	Average   float64 `json:"average,omitzero"`
    55  	Sum       float64 `json:"sum,omitzero"`
    56  	Variance  float64 `json:"variance,omitzero"`
    57  }
    58  
    59  type SMetricData struct {
    60  	SMetricMeta
    61  
    62  	Datapoints []SDatapoint
    63  }
    64  
    65  type SMetricMeta struct {
    66  	SMetric
    67  
    68  	Unit string `json:"unit"`
    69  }
    70  
    71  type SMetric struct {
    72  	MetricName string `json:"metric_name"`
    73  	Namespace  string `json:"namespace"`
    74  
    75  	Dimensions []SMetricDimension `json:"dimensions"`
    76  }
    77  
    78  func (ces *SCloudEyeManager) ListMetrics() ([]SMetricMeta, error) {
    79  	metrics := make([]SMetricMeta, 0)
    80  	next := ""
    81  	for {
    82  		marker, data, err := ces.listMetricsInternal(next)
    83  		if err != nil {
    84  			return nil, errors.Wrap(err, "ces.listMetricsInternal")
    85  		}
    86  		if len(data) == 0 {
    87  			break
    88  		}
    89  		metrics = append(metrics, data...)
    90  		next = marker
    91  	}
    92  	return metrics, nil
    93  }
    94  
    95  func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []SMetricMeta, error) {
    96  	request := requests.NewResourceRequest(ces.GetEndpoint(), "GET", string(ces.ServiceName), ces.version, ces.Region, ces.ProjectId, ces.ResourceKeyword)
    97  	request.AddQueryParam("limit", "1000")
    98  	if len(start) > 0 {
    99  		request.AddQueryParam("start", start)
   100  	}
   101  	_, resp, err := ces.jsonRequest(request)
   102  	if err != nil {
   103  		return "", nil, errors.Wrap(err, "ces.jsonRequest")
   104  	}
   105  	marker, _ := resp.GetString("meta_data", "marker")
   106  	metrics := make([]SMetricMeta, 0)
   107  	err = resp.Unmarshal(&metrics, "metrics")
   108  	if err != nil {
   109  		return "", nil, errors.Wrap(err, "resp.Unmarshal metrics")
   110  	}
   111  	return marker, metrics, nil
   112  }
   113  
   114  type SBatchQueryMetricDataInput struct {
   115  	Metrics []SMetric `json:"metrics"`
   116  
   117  	From   int64  `json:"from"`
   118  	To     int64  `json:"to"`
   119  	Period string `json:"period"`
   120  	Filter string `json:"filter"`
   121  }
   122  
   123  func (ces *SCloudEyeManager) GetMetricsData(metrics []SMetricMeta, since time.Time, until time.Time) ([]SMetricData, error) {
   124  	if len(metrics) > 10 {
   125  		return nil, errors.Wrap(httperrors.ErrTooLarge, "request more than 10 metrics")
   126  	}
   127  	metricReq := make([]SMetric, len(metrics))
   128  	for i := range metrics {
   129  		metricReq[i] = metrics[i].SMetric
   130  	}
   131  	request := requests.NewResourceRequest(ces.GetEndpoint(), "POST", string(ces.ServiceName), ces.version, ces.Region, ces.ProjectId, "batch-query-metric-data")
   132  	input := SBatchQueryMetricDataInput{
   133  		Metrics: metricReq,
   134  		From:    since.Unix() * 1000,
   135  		To:      until.Unix() * 1000,
   136  		Period:  "1",
   137  		Filter:  "average",
   138  	}
   139  	body := jsonutils.Marshal(&input).String()
   140  	request.SetContent([]byte(body))
   141  	_, resp, err := ces.jsonRequest(request)
   142  	if err != nil {
   143  		return nil, errors.Wrap(err, "ces.jsonRequest")
   144  	}
   145  	//log.Debugf("%s", resp)
   146  	result := make([]SMetricData, 0)
   147  	err = resp.Unmarshal(&result, "metrics")
   148  	if err != nil {
   149  		return nil, errors.Wrap(err, "resp.Unmarshal")
   150  	}
   151  	return result, nil
   152  }