github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/ces/v1/metrics/results.go (about)

     1  package metrics
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  type Metrics struct {
    12  	Metrics  []Metric `json:"metrics"`
    13  	MetaData MetaData `json:"meta_data"`
    14  }
    15  
    16  type MetaData struct {
    17  	Count  int    `json:"count"`
    18  	Marker string `json:"marker"`
    19  	Total  int    `json:"total"`
    20  }
    21  
    22  type Metric struct {
    23  	// Specifies the metric namespace.
    24  	Namespace string `json:"namespace"`
    25  
    26  	// Specifies the metric name, such as cpu_util.
    27  	MetricName string `json:"metric_name"`
    28  
    29  	// Specifies the metric unit.
    30  	Unit string `json:"unit"`
    31  
    32  	//Specifies the list of dimensions.
    33  	Dimensions []Dimension `json:"dimensions"`
    34  }
    35  
    36  type Dimension struct {
    37  	Name  string `json:"name"`
    38  	Value string `json:"value"`
    39  }
    40  
    41  type ListResult struct {
    42  	golangsdk.Result
    43  }
    44  
    45  //Extract is a function that accepts a result and extracts metrics.
    46  func ExtractMetrics(r pagination.Page) (Metrics, error) {
    47  	var s Metrics
    48  	err := r.(MetricsPage).ExtractInto(&s)
    49  	return s, err
    50  }
    51  
    52  //Extract is a function that all accepts a result and extracts metrics.
    53  func ExtractAllPagesMetrics(r pagination.Page) (Metrics, error) {
    54  	var s Metrics
    55  	s.Metrics = make([]Metric, 0)
    56  	err := r.(MetricsPage).ExtractInto(&s)
    57  	if len(s.Metrics) == 0 {
    58  		return s, nil
    59  	}
    60  	s.MetaData.Count = len(s.Metrics)
    61  	s.MetaData.Total = len(s.Metrics)
    62  	var buf bytes.Buffer
    63  	buf.WriteString(s.Metrics[len(s.Metrics)-1].Namespace)
    64  	buf.WriteString(".")
    65  	buf.WriteString(s.Metrics[len(s.Metrics)-1].MetricName)
    66  	for _, dimension := range s.Metrics[len(s.Metrics)-1].Dimensions {
    67  		buf.WriteString(".")
    68  		buf.WriteString(dimension.Name)
    69  		buf.WriteString(":")
    70  		buf.WriteString(dimension.Value)
    71  	}
    72  	s.MetaData.Marker = buf.String()
    73  	return s, err
    74  }
    75  
    76  // MetricsPage is the page returned by a pager when traversing over a
    77  // collection of metrics.
    78  type MetricsPage struct {
    79  	pagination.LinkedPageBase
    80  }
    81  
    82  // NextPageURL is invoked when a paginated collection of metrics has reached
    83  // the end of a page and the pager seeks to traverse over a new one. In order
    84  // to do this, it needs to construct the next page's URL.
    85  func (r MetricsPage) NextPageURL() (string, error) {
    86  	metrics, err := ExtractMetrics(r)
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  
    91  	if len(metrics.Metrics) < 1 {
    92  		return "", nil
    93  	}
    94  
    95  	limit := r.URL.Query().Get("limit")
    96  	num, _ := strconv.Atoi(limit)
    97  	if num > len(metrics.Metrics) {
    98  		return "", nil
    99  	}
   100  
   101  	metricslen := len(metrics.Metrics) - 1
   102  
   103  	var buf bytes.Buffer
   104  	buf.WriteString(metrics.Metrics[metricslen].Namespace)
   105  	buf.WriteString(".")
   106  	buf.WriteString(metrics.Metrics[metricslen].MetricName)
   107  	for _, dimension := range metrics.Metrics[metricslen].Dimensions {
   108  		buf.WriteString(".")
   109  		buf.WriteString(dimension.Name)
   110  		buf.WriteString(":")
   111  		buf.WriteString(dimension.Value)
   112  	}
   113  	return r.WrapNextPageURL(buf.String())
   114  }
   115  
   116  // IsEmpty checks whether a NetworkPage struct is empty.
   117  func (r MetricsPage) IsEmpty() (bool, error) {
   118  	s, err := ExtractMetrics(r)
   119  	return len(s.Metrics) == 0, err
   120  }
   121  
   122  /*
   123  ExtractNextURL is an internal function useful for packages of collection
   124  resources that are paginated in a certain way.
   125  
   126  It attempts to extract the "start" URL from slice of Link structs, or
   127  "" if no such URL is present.
   128  */
   129  func (r MetricsPage) WrapNextPageURL(start string) (string, error) {
   130  	limit := r.URL.Query().Get("limit")
   131  	if limit == "" {
   132  		return "", nil
   133  	}
   134  	uq := r.URL.Query()
   135  	uq.Set("start", start)
   136  	r.URL.RawQuery = uq.Encode()
   137  	return r.URL.String(), nil
   138  }