api.xpack.ml.get_buckets.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "fmt"
  23. "io"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. )
  28. func newMLGetBucketsFunc(t Transport) MLGetBuckets {
  29. return func(job_id string, o ...func(*MLGetBucketsRequest)) (*Response, error) {
  30. var r = MLGetBucketsRequest{JobID: job_id}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // MLGetBuckets - Retrieves anomaly detection job results for one or more buckets.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html.
  41. type MLGetBuckets func(job_id string, o ...func(*MLGetBucketsRequest)) (*Response, error)
  42. // MLGetBucketsRequest configures the ML Get Buckets API request.
  43. type MLGetBucketsRequest struct {
  44. Body io.Reader
  45. JobID string
  46. Timestamp string
  47. AnomalyScore interface{}
  48. Desc *bool
  49. End string
  50. ExcludeInterim *bool
  51. Expand *bool
  52. From *int
  53. Size *int
  54. Sort string
  55. Start string
  56. Pretty bool
  57. Human bool
  58. ErrorTrace bool
  59. FilterPath []string
  60. Header http.Header
  61. ctx context.Context
  62. }
  63. // Do executes the request and returns response or error.
  64. func (r MLGetBucketsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  65. var (
  66. method string
  67. path strings.Builder
  68. params map[string]string
  69. )
  70. method = "POST"
  71. path.Grow(1 + len("_ml") + 1 + len("anomaly_detectors") + 1 + len(r.JobID) + 1 + len("results") + 1 + len("buckets") + 1 + len(r.Timestamp))
  72. path.WriteString("/")
  73. path.WriteString("_ml")
  74. path.WriteString("/")
  75. path.WriteString("anomaly_detectors")
  76. path.WriteString("/")
  77. path.WriteString(r.JobID)
  78. path.WriteString("/")
  79. path.WriteString("results")
  80. path.WriteString("/")
  81. path.WriteString("buckets")
  82. if r.Timestamp != "" {
  83. path.WriteString("/")
  84. path.WriteString(r.Timestamp)
  85. }
  86. params = make(map[string]string)
  87. if r.AnomalyScore != nil {
  88. params["anomaly_score"] = fmt.Sprintf("%v", r.AnomalyScore)
  89. }
  90. if r.Desc != nil {
  91. params["desc"] = strconv.FormatBool(*r.Desc)
  92. }
  93. if r.End != "" {
  94. params["end"] = r.End
  95. }
  96. if r.ExcludeInterim != nil {
  97. params["exclude_interim"] = strconv.FormatBool(*r.ExcludeInterim)
  98. }
  99. if r.Expand != nil {
  100. params["expand"] = strconv.FormatBool(*r.Expand)
  101. }
  102. if r.From != nil {
  103. params["from"] = strconv.FormatInt(int64(*r.From), 10)
  104. }
  105. if r.Size != nil {
  106. params["size"] = strconv.FormatInt(int64(*r.Size), 10)
  107. }
  108. if r.Sort != "" {
  109. params["sort"] = r.Sort
  110. }
  111. if r.Start != "" {
  112. params["start"] = r.Start
  113. }
  114. if r.Pretty {
  115. params["pretty"] = "true"
  116. }
  117. if r.Human {
  118. params["human"] = "true"
  119. }
  120. if r.ErrorTrace {
  121. params["error_trace"] = "true"
  122. }
  123. if len(r.FilterPath) > 0 {
  124. params["filter_path"] = strings.Join(r.FilterPath, ",")
  125. }
  126. req, err := newRequest(method, path.String(), r.Body)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if len(params) > 0 {
  131. q := req.URL.Query()
  132. for k, v := range params {
  133. q.Set(k, v)
  134. }
  135. req.URL.RawQuery = q.Encode()
  136. }
  137. if len(r.Header) > 0 {
  138. if len(req.Header) == 0 {
  139. req.Header = r.Header
  140. } else {
  141. for k, vv := range r.Header {
  142. for _, v := range vv {
  143. req.Header.Add(k, v)
  144. }
  145. }
  146. }
  147. }
  148. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  149. req.Header[headerContentType] = headerContentTypeJSON
  150. }
  151. if ctx != nil {
  152. req = req.WithContext(ctx)
  153. }
  154. res, err := transport.Perform(req)
  155. if err != nil {
  156. return nil, err
  157. }
  158. response := Response{
  159. StatusCode: res.StatusCode,
  160. Body: res.Body,
  161. Header: res.Header,
  162. }
  163. return &response, nil
  164. }
  165. // WithContext sets the request context.
  166. func (f MLGetBuckets) WithContext(v context.Context) func(*MLGetBucketsRequest) {
  167. return func(r *MLGetBucketsRequest) {
  168. r.ctx = v
  169. }
  170. }
  171. // WithBody - Bucket selection details if not provided in URI.
  172. func (f MLGetBuckets) WithBody(v io.Reader) func(*MLGetBucketsRequest) {
  173. return func(r *MLGetBucketsRequest) {
  174. r.Body = v
  175. }
  176. }
  177. // WithTimestamp - the timestamp of the desired single bucket result.
  178. func (f MLGetBuckets) WithTimestamp(v string) func(*MLGetBucketsRequest) {
  179. return func(r *MLGetBucketsRequest) {
  180. r.Timestamp = v
  181. }
  182. }
  183. // WithAnomalyScore - filter for the most anomalous buckets.
  184. func (f MLGetBuckets) WithAnomalyScore(v interface{}) func(*MLGetBucketsRequest) {
  185. return func(r *MLGetBucketsRequest) {
  186. r.AnomalyScore = v
  187. }
  188. }
  189. // WithDesc - set the sort direction.
  190. func (f MLGetBuckets) WithDesc(v bool) func(*MLGetBucketsRequest) {
  191. return func(r *MLGetBucketsRequest) {
  192. r.Desc = &v
  193. }
  194. }
  195. // WithEnd - end time filter for buckets.
  196. func (f MLGetBuckets) WithEnd(v string) func(*MLGetBucketsRequest) {
  197. return func(r *MLGetBucketsRequest) {
  198. r.End = v
  199. }
  200. }
  201. // WithExcludeInterim - exclude interim results.
  202. func (f MLGetBuckets) WithExcludeInterim(v bool) func(*MLGetBucketsRequest) {
  203. return func(r *MLGetBucketsRequest) {
  204. r.ExcludeInterim = &v
  205. }
  206. }
  207. // WithExpand - include anomaly records.
  208. func (f MLGetBuckets) WithExpand(v bool) func(*MLGetBucketsRequest) {
  209. return func(r *MLGetBucketsRequest) {
  210. r.Expand = &v
  211. }
  212. }
  213. // WithFrom - skips a number of buckets.
  214. func (f MLGetBuckets) WithFrom(v int) func(*MLGetBucketsRequest) {
  215. return func(r *MLGetBucketsRequest) {
  216. r.From = &v
  217. }
  218. }
  219. // WithSize - specifies a max number of buckets to get.
  220. func (f MLGetBuckets) WithSize(v int) func(*MLGetBucketsRequest) {
  221. return func(r *MLGetBucketsRequest) {
  222. r.Size = &v
  223. }
  224. }
  225. // WithSort - sort buckets by a particular field.
  226. func (f MLGetBuckets) WithSort(v string) func(*MLGetBucketsRequest) {
  227. return func(r *MLGetBucketsRequest) {
  228. r.Sort = v
  229. }
  230. }
  231. // WithStart - start time filter for buckets.
  232. func (f MLGetBuckets) WithStart(v string) func(*MLGetBucketsRequest) {
  233. return func(r *MLGetBucketsRequest) {
  234. r.Start = v
  235. }
  236. }
  237. // WithPretty makes the response body pretty-printed.
  238. func (f MLGetBuckets) WithPretty() func(*MLGetBucketsRequest) {
  239. return func(r *MLGetBucketsRequest) {
  240. r.Pretty = true
  241. }
  242. }
  243. // WithHuman makes statistical values human-readable.
  244. func (f MLGetBuckets) WithHuman() func(*MLGetBucketsRequest) {
  245. return func(r *MLGetBucketsRequest) {
  246. r.Human = true
  247. }
  248. }
  249. // WithErrorTrace includes the stack trace for errors in the response body.
  250. func (f MLGetBuckets) WithErrorTrace() func(*MLGetBucketsRequest) {
  251. return func(r *MLGetBucketsRequest) {
  252. r.ErrorTrace = true
  253. }
  254. }
  255. // WithFilterPath filters the properties of the response body.
  256. func (f MLGetBuckets) WithFilterPath(v ...string) func(*MLGetBucketsRequest) {
  257. return func(r *MLGetBucketsRequest) {
  258. r.FilterPath = v
  259. }
  260. }
  261. // WithHeader adds the headers to the HTTP request.
  262. func (f MLGetBuckets) WithHeader(h map[string]string) func(*MLGetBucketsRequest) {
  263. return func(r *MLGetBucketsRequest) {
  264. if r.Header == nil {
  265. r.Header = make(http.Header)
  266. }
  267. for k, v := range h {
  268. r.Header.Add(k, v)
  269. }
  270. }
  271. }
  272. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  273. func (f MLGetBuckets) WithOpaqueID(s string) func(*MLGetBucketsRequest) {
  274. return func(r *MLGetBucketsRequest) {
  275. if r.Header == nil {
  276. r.Header = make(http.Header)
  277. }
  278. r.Header.Set("X-Opaque-Id", s)
  279. }
  280. }