github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.get_datastream_stats.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  package opensearchapi
    11  
    12  import (
    13  	"context"
    14  	"net/http"
    15  	"strings"
    16  	"time"
    17  )
    18  
    19  func newIndicesGetDataStreamStatsFunc(t Transport) IndicesGetDataStreamStats {
    20  	return func(o ...func(*IndicesGetDataStreamStatsRequest)) (*Response, error) {
    21  		var r = IndicesGetDataStreamStatsRequest{}
    22  		for _, f := range o {
    23  			f(&r)
    24  		}
    25  		return r.Do(r.ctx, t)
    26  	}
    27  }
    28  
    29  // ----- API Definition -------------------------------------------------------
    30  
    31  // IndicesGetDataStreamStats returns a more insights about the data stream.
    32  type IndicesGetDataStreamStats func(o ...func(*IndicesGetDataStreamStatsRequest)) (*Response, error)
    33  
    34  // IndicesGetDataStreamStatsRequest configures the Indices Get Data Stream Stats API request.
    35  type IndicesGetDataStreamStatsRequest struct {
    36  	Name string
    37  
    38  	ClusterManagerTimeout time.Duration
    39  
    40  	Pretty     bool
    41  	Human      bool
    42  	ErrorTrace bool
    43  	FilterPath []string
    44  
    45  	Header http.Header
    46  
    47  	ctx context.Context
    48  }
    49  
    50  // Do execute the request and returns response or error.
    51  func (r IndicesGetDataStreamStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    52  	var (
    53  		method string
    54  		path   strings.Builder
    55  		params map[string]string
    56  	)
    57  
    58  	method = "GET"
    59  
    60  	path.Grow(1 + len("_data_stream") + 1 + len(r.Name) + 1 + len("_stats"))
    61  	path.WriteString("/_data_stream/")
    62  	path.WriteString(r.Name)
    63  	path.WriteString("/_stats")
    64  
    65  	params = make(map[string]string)
    66  
    67  	if r.ClusterManagerTimeout != 0 {
    68  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    69  	}
    70  
    71  	if r.Pretty {
    72  		params["pretty"] = "true"
    73  	}
    74  
    75  	if r.Human {
    76  		params["human"] = "true"
    77  	}
    78  
    79  	if r.ErrorTrace {
    80  		params["error_trace"] = "true"
    81  	}
    82  
    83  	if len(r.FilterPath) > 0 {
    84  		params["filter_path"] = strings.Join(r.FilterPath, ",")
    85  	}
    86  
    87  	req, err := newRequest(method, path.String(), nil)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	if len(params) > 0 {
    93  		q := req.URL.Query()
    94  		for k, v := range params {
    95  			q.Set(k, v)
    96  		}
    97  		req.URL.RawQuery = q.Encode()
    98  	}
    99  
   100  	if len(r.Header) > 0 {
   101  		if len(req.Header) == 0 {
   102  			req.Header = r.Header
   103  		} else {
   104  			for k, vv := range r.Header {
   105  				for _, v := range vv {
   106  					req.Header.Add(k, v)
   107  				}
   108  			}
   109  		}
   110  	}
   111  
   112  	if ctx != nil {
   113  		req = req.WithContext(ctx)
   114  	}
   115  
   116  	res, err := transport.Perform(req)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	response := Response{
   122  		StatusCode: res.StatusCode,
   123  		Body:       res.Body,
   124  		Header:     res.Header,
   125  	}
   126  
   127  	return &response, nil
   128  }
   129  
   130  // WithContext sets the request context.
   131  func (f IndicesGetDataStreamStats) WithContext(v context.Context) func(*IndicesGetDataStreamStatsRequest) {
   132  	return func(r *IndicesGetDataStreamStatsRequest) {
   133  		r.ctx = v
   134  	}
   135  }
   136  
   137  // WithName - the comma separated names of the index templates.
   138  func (f IndicesGetDataStreamStats) WithName(v string) func(*IndicesGetDataStreamStatsRequest) {
   139  	return func(r *IndicesGetDataStreamStatsRequest) {
   140  		r.Name = v
   141  	}
   142  }
   143  
   144  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   145  func (f IndicesGetDataStreamStats) WithClusterManagerTimeout(v time.Duration) func(*IndicesGetDataStreamStatsRequest) {
   146  	return func(r *IndicesGetDataStreamStatsRequest) {
   147  		r.ClusterManagerTimeout = v
   148  	}
   149  }
   150  
   151  // WithPretty makes the response body pretty-printed.
   152  func (f IndicesGetDataStreamStats) WithPretty() func(*IndicesGetDataStreamStatsRequest) {
   153  	return func(r *IndicesGetDataStreamStatsRequest) {
   154  		r.Pretty = true
   155  	}
   156  }
   157  
   158  // WithHuman makes statistical values human-readable.
   159  func (f IndicesGetDataStreamStats) WithHuman() func(*IndicesGetDataStreamStatsRequest) {
   160  	return func(r *IndicesGetDataStreamStatsRequest) {
   161  		r.Human = true
   162  	}
   163  }
   164  
   165  // WithErrorTrace includes the stack trace for errors in the response body.
   166  func (f IndicesGetDataStreamStats) WithErrorTrace() func(*IndicesGetDataStreamStatsRequest) {
   167  	return func(r *IndicesGetDataStreamStatsRequest) {
   168  		r.ErrorTrace = true
   169  	}
   170  }
   171  
   172  // WithFilterPath filters the properties of the response body.
   173  func (f IndicesGetDataStreamStats) WithFilterPath(v ...string) func(*IndicesGetDataStreamStatsRequest) {
   174  	return func(r *IndicesGetDataStreamStatsRequest) {
   175  		r.FilterPath = v
   176  	}
   177  }
   178  
   179  // WithHeader adds the headers to the HTTP request.
   180  func (f IndicesGetDataStreamStats) WithHeader(h map[string]string) func(*IndicesGetDataStreamStatsRequest) {
   181  	return func(r *IndicesGetDataStreamStatsRequest) {
   182  		if r.Header == nil {
   183  			r.Header = make(http.Header)
   184  		}
   185  		for k, v := range h {
   186  			r.Header.Add(k, v)
   187  		}
   188  	}
   189  }
   190  
   191  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   192  func (f IndicesGetDataStreamStats) WithOpaqueID(s string) func(*IndicesGetDataStreamStatsRequest) {
   193  	return func(r *IndicesGetDataStreamStatsRequest) {
   194  		if r.Header == nil {
   195  			r.Header = make(http.Header)
   196  		}
   197  		r.Header.Set("X-Opaque-Id", s)
   198  	}
   199  }