api.xpack.ml.get_influencers.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 newMLGetInfluencersFunc(t Transport) MLGetInfluencers {
  29. return func(job_id string, o ...func(*MLGetInfluencersRequest)) (*Response, error) {
  30. var r = MLGetInfluencersRequest{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. // MLGetInfluencers - Retrieves anomaly detection job results for one or more influencers.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html.
  41. type MLGetInfluencers func(job_id string, o ...func(*MLGetInfluencersRequest)) (*Response, error)
  42. // MLGetInfluencersRequest configures the ML Get Influencers API request.
  43. type MLGetInfluencersRequest struct {
  44. Body io.Reader
  45. JobID string
  46. Desc *bool
  47. End string
  48. ExcludeInterim *bool
  49. From *int
  50. InfluencerScore interface{}
  51. Size *int
  52. Sort string
  53. Start string
  54. Pretty bool
  55. Human bool
  56. ErrorTrace bool
  57. FilterPath []string
  58. Header http.Header
  59. ctx context.Context
  60. }
  61. // Do executes the request and returns response or error.
  62. func (r MLGetInfluencersRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  63. var (
  64. method string
  65. path strings.Builder
  66. params map[string]string
  67. )
  68. method = "POST"
  69. path.Grow(1 + len("_ml") + 1 + len("anomaly_detectors") + 1 + len(r.JobID) + 1 + len("results") + 1 + len("influencers"))
  70. path.WriteString("/")
  71. path.WriteString("_ml")
  72. path.WriteString("/")
  73. path.WriteString("anomaly_detectors")
  74. path.WriteString("/")
  75. path.WriteString(r.JobID)
  76. path.WriteString("/")
  77. path.WriteString("results")
  78. path.WriteString("/")
  79. path.WriteString("influencers")
  80. params = make(map[string]string)
  81. if r.Desc != nil {
  82. params["desc"] = strconv.FormatBool(*r.Desc)
  83. }
  84. if r.End != "" {
  85. params["end"] = r.End
  86. }
  87. if r.ExcludeInterim != nil {
  88. params["exclude_interim"] = strconv.FormatBool(*r.ExcludeInterim)
  89. }
  90. if r.From != nil {
  91. params["from"] = strconv.FormatInt(int64(*r.From), 10)
  92. }
  93. if r.InfluencerScore != nil {
  94. params["influencer_score"] = fmt.Sprintf("%v", r.InfluencerScore)
  95. }
  96. if r.Size != nil {
  97. params["size"] = strconv.FormatInt(int64(*r.Size), 10)
  98. }
  99. if r.Sort != "" {
  100. params["sort"] = r.Sort
  101. }
  102. if r.Start != "" {
  103. params["start"] = r.Start
  104. }
  105. if r.Pretty {
  106. params["pretty"] = "true"
  107. }
  108. if r.Human {
  109. params["human"] = "true"
  110. }
  111. if r.ErrorTrace {
  112. params["error_trace"] = "true"
  113. }
  114. if len(r.FilterPath) > 0 {
  115. params["filter_path"] = strings.Join(r.FilterPath, ",")
  116. }
  117. req, err := newRequest(method, path.String(), r.Body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. if len(params) > 0 {
  122. q := req.URL.Query()
  123. for k, v := range params {
  124. q.Set(k, v)
  125. }
  126. req.URL.RawQuery = q.Encode()
  127. }
  128. if len(r.Header) > 0 {
  129. if len(req.Header) == 0 {
  130. req.Header = r.Header
  131. } else {
  132. for k, vv := range r.Header {
  133. for _, v := range vv {
  134. req.Header.Add(k, v)
  135. }
  136. }
  137. }
  138. }
  139. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  140. req.Header[headerContentType] = headerContentTypeJSON
  141. }
  142. if ctx != nil {
  143. req = req.WithContext(ctx)
  144. }
  145. res, err := transport.Perform(req)
  146. if err != nil {
  147. return nil, err
  148. }
  149. response := Response{
  150. StatusCode: res.StatusCode,
  151. Body: res.Body,
  152. Header: res.Header,
  153. }
  154. return &response, nil
  155. }
  156. // WithContext sets the request context.
  157. func (f MLGetInfluencers) WithContext(v context.Context) func(*MLGetInfluencersRequest) {
  158. return func(r *MLGetInfluencersRequest) {
  159. r.ctx = v
  160. }
  161. }
  162. // WithBody - Influencer selection criteria.
  163. func (f MLGetInfluencers) WithBody(v io.Reader) func(*MLGetInfluencersRequest) {
  164. return func(r *MLGetInfluencersRequest) {
  165. r.Body = v
  166. }
  167. }
  168. // WithDesc - whether the results should be sorted in decending order.
  169. func (f MLGetInfluencers) WithDesc(v bool) func(*MLGetInfluencersRequest) {
  170. return func(r *MLGetInfluencersRequest) {
  171. r.Desc = &v
  172. }
  173. }
  174. // WithEnd - end timestamp for the requested influencers.
  175. func (f MLGetInfluencers) WithEnd(v string) func(*MLGetInfluencersRequest) {
  176. return func(r *MLGetInfluencersRequest) {
  177. r.End = v
  178. }
  179. }
  180. // WithExcludeInterim - exclude interim results.
  181. func (f MLGetInfluencers) WithExcludeInterim(v bool) func(*MLGetInfluencersRequest) {
  182. return func(r *MLGetInfluencersRequest) {
  183. r.ExcludeInterim = &v
  184. }
  185. }
  186. // WithFrom - skips a number of influencers.
  187. func (f MLGetInfluencers) WithFrom(v int) func(*MLGetInfluencersRequest) {
  188. return func(r *MLGetInfluencersRequest) {
  189. r.From = &v
  190. }
  191. }
  192. // WithInfluencerScore - influencer score threshold for the requested influencers.
  193. func (f MLGetInfluencers) WithInfluencerScore(v interface{}) func(*MLGetInfluencersRequest) {
  194. return func(r *MLGetInfluencersRequest) {
  195. r.InfluencerScore = v
  196. }
  197. }
  198. // WithSize - specifies a max number of influencers to get.
  199. func (f MLGetInfluencers) WithSize(v int) func(*MLGetInfluencersRequest) {
  200. return func(r *MLGetInfluencersRequest) {
  201. r.Size = &v
  202. }
  203. }
  204. // WithSort - sort field for the requested influencers.
  205. func (f MLGetInfluencers) WithSort(v string) func(*MLGetInfluencersRequest) {
  206. return func(r *MLGetInfluencersRequest) {
  207. r.Sort = v
  208. }
  209. }
  210. // WithStart - start timestamp for the requested influencers.
  211. func (f MLGetInfluencers) WithStart(v string) func(*MLGetInfluencersRequest) {
  212. return func(r *MLGetInfluencersRequest) {
  213. r.Start = v
  214. }
  215. }
  216. // WithPretty makes the response body pretty-printed.
  217. func (f MLGetInfluencers) WithPretty() func(*MLGetInfluencersRequest) {
  218. return func(r *MLGetInfluencersRequest) {
  219. r.Pretty = true
  220. }
  221. }
  222. // WithHuman makes statistical values human-readable.
  223. func (f MLGetInfluencers) WithHuman() func(*MLGetInfluencersRequest) {
  224. return func(r *MLGetInfluencersRequest) {
  225. r.Human = true
  226. }
  227. }
  228. // WithErrorTrace includes the stack trace for errors in the response body.
  229. func (f MLGetInfluencers) WithErrorTrace() func(*MLGetInfluencersRequest) {
  230. return func(r *MLGetInfluencersRequest) {
  231. r.ErrorTrace = true
  232. }
  233. }
  234. // WithFilterPath filters the properties of the response body.
  235. func (f MLGetInfluencers) WithFilterPath(v ...string) func(*MLGetInfluencersRequest) {
  236. return func(r *MLGetInfluencersRequest) {
  237. r.FilterPath = v
  238. }
  239. }
  240. // WithHeader adds the headers to the HTTP request.
  241. func (f MLGetInfluencers) WithHeader(h map[string]string) func(*MLGetInfluencersRequest) {
  242. return func(r *MLGetInfluencersRequest) {
  243. if r.Header == nil {
  244. r.Header = make(http.Header)
  245. }
  246. for k, v := range h {
  247. r.Header.Add(k, v)
  248. }
  249. }
  250. }
  251. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  252. func (f MLGetInfluencers) WithOpaqueID(s string) func(*MLGetInfluencersRequest) {
  253. return func(r *MLGetInfluencersRequest) {
  254. if r.Header == nil {
  255. r.Header = make(http.Header)
  256. }
  257. r.Header.Set("X-Opaque-Id", s)
  258. }
  259. }