api.xpack.ml.flush_job.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. "io"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. )
  27. func newMLFlushJobFunc(t Transport) MLFlushJob {
  28. return func(job_id string, o ...func(*MLFlushJobRequest)) (*Response, error) {
  29. var r = MLFlushJobRequest{JobID: job_id}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // MLFlushJob - Forces any buffered data to be processed by the job.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-flush-job.html.
  40. type MLFlushJob func(job_id string, o ...func(*MLFlushJobRequest)) (*Response, error)
  41. // MLFlushJobRequest configures the ML Flush Job API request.
  42. type MLFlushJobRequest struct {
  43. Body io.Reader
  44. JobID string
  45. AdvanceTime string
  46. CalcInterim *bool
  47. End string
  48. SkipTime string
  49. Start string
  50. Pretty bool
  51. Human bool
  52. ErrorTrace bool
  53. FilterPath []string
  54. Header http.Header
  55. ctx context.Context
  56. }
  57. // Do executes the request and returns response or error.
  58. func (r MLFlushJobRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  59. var (
  60. method string
  61. path strings.Builder
  62. params map[string]string
  63. )
  64. method = "POST"
  65. path.Grow(1 + len("_ml") + 1 + len("anomaly_detectors") + 1 + len(r.JobID) + 1 + len("_flush"))
  66. path.WriteString("/")
  67. path.WriteString("_ml")
  68. path.WriteString("/")
  69. path.WriteString("anomaly_detectors")
  70. path.WriteString("/")
  71. path.WriteString(r.JobID)
  72. path.WriteString("/")
  73. path.WriteString("_flush")
  74. params = make(map[string]string)
  75. if r.AdvanceTime != "" {
  76. params["advance_time"] = r.AdvanceTime
  77. }
  78. if r.CalcInterim != nil {
  79. params["calc_interim"] = strconv.FormatBool(*r.CalcInterim)
  80. }
  81. if r.End != "" {
  82. params["end"] = r.End
  83. }
  84. if r.SkipTime != "" {
  85. params["skip_time"] = r.SkipTime
  86. }
  87. if r.Start != "" {
  88. params["start"] = r.Start
  89. }
  90. if r.Pretty {
  91. params["pretty"] = "true"
  92. }
  93. if r.Human {
  94. params["human"] = "true"
  95. }
  96. if r.ErrorTrace {
  97. params["error_trace"] = "true"
  98. }
  99. if len(r.FilterPath) > 0 {
  100. params["filter_path"] = strings.Join(r.FilterPath, ",")
  101. }
  102. req, err := newRequest(method, path.String(), r.Body)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if len(params) > 0 {
  107. q := req.URL.Query()
  108. for k, v := range params {
  109. q.Set(k, v)
  110. }
  111. req.URL.RawQuery = q.Encode()
  112. }
  113. if len(r.Header) > 0 {
  114. if len(req.Header) == 0 {
  115. req.Header = r.Header
  116. } else {
  117. for k, vv := range r.Header {
  118. for _, v := range vv {
  119. req.Header.Add(k, v)
  120. }
  121. }
  122. }
  123. }
  124. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  125. req.Header[headerContentType] = headerContentTypeJSON
  126. }
  127. if ctx != nil {
  128. req = req.WithContext(ctx)
  129. }
  130. res, err := transport.Perform(req)
  131. if err != nil {
  132. return nil, err
  133. }
  134. response := Response{
  135. StatusCode: res.StatusCode,
  136. Body: res.Body,
  137. Header: res.Header,
  138. }
  139. return &response, nil
  140. }
  141. // WithContext sets the request context.
  142. func (f MLFlushJob) WithContext(v context.Context) func(*MLFlushJobRequest) {
  143. return func(r *MLFlushJobRequest) {
  144. r.ctx = v
  145. }
  146. }
  147. // WithBody - Flush parameters.
  148. func (f MLFlushJob) WithBody(v io.Reader) func(*MLFlushJobRequest) {
  149. return func(r *MLFlushJobRequest) {
  150. r.Body = v
  151. }
  152. }
  153. // WithAdvanceTime - advances time to the given value generating results and updating the model for the advanced interval.
  154. func (f MLFlushJob) WithAdvanceTime(v string) func(*MLFlushJobRequest) {
  155. return func(r *MLFlushJobRequest) {
  156. r.AdvanceTime = v
  157. }
  158. }
  159. // WithCalcInterim - calculates interim results for the most recent bucket or all buckets within the latency period.
  160. func (f MLFlushJob) WithCalcInterim(v bool) func(*MLFlushJobRequest) {
  161. return func(r *MLFlushJobRequest) {
  162. r.CalcInterim = &v
  163. }
  164. }
  165. // WithEnd - when used in conjunction with calc_interim, specifies the range of buckets on which to calculate interim results.
  166. func (f MLFlushJob) WithEnd(v string) func(*MLFlushJobRequest) {
  167. return func(r *MLFlushJobRequest) {
  168. r.End = v
  169. }
  170. }
  171. // WithSkipTime - skips time to the given value without generating results or updating the model for the skipped interval.
  172. func (f MLFlushJob) WithSkipTime(v string) func(*MLFlushJobRequest) {
  173. return func(r *MLFlushJobRequest) {
  174. r.SkipTime = v
  175. }
  176. }
  177. // WithStart - when used in conjunction with calc_interim, specifies the range of buckets on which to calculate interim results.
  178. func (f MLFlushJob) WithStart(v string) func(*MLFlushJobRequest) {
  179. return func(r *MLFlushJobRequest) {
  180. r.Start = v
  181. }
  182. }
  183. // WithPretty makes the response body pretty-printed.
  184. func (f MLFlushJob) WithPretty() func(*MLFlushJobRequest) {
  185. return func(r *MLFlushJobRequest) {
  186. r.Pretty = true
  187. }
  188. }
  189. // WithHuman makes statistical values human-readable.
  190. func (f MLFlushJob) WithHuman() func(*MLFlushJobRequest) {
  191. return func(r *MLFlushJobRequest) {
  192. r.Human = true
  193. }
  194. }
  195. // WithErrorTrace includes the stack trace for errors in the response body.
  196. func (f MLFlushJob) WithErrorTrace() func(*MLFlushJobRequest) {
  197. return func(r *MLFlushJobRequest) {
  198. r.ErrorTrace = true
  199. }
  200. }
  201. // WithFilterPath filters the properties of the response body.
  202. func (f MLFlushJob) WithFilterPath(v ...string) func(*MLFlushJobRequest) {
  203. return func(r *MLFlushJobRequest) {
  204. r.FilterPath = v
  205. }
  206. }
  207. // WithHeader adds the headers to the HTTP request.
  208. func (f MLFlushJob) WithHeader(h map[string]string) func(*MLFlushJobRequest) {
  209. return func(r *MLFlushJobRequest) {
  210. if r.Header == nil {
  211. r.Header = make(http.Header)
  212. }
  213. for k, v := range h {
  214. r.Header.Add(k, v)
  215. }
  216. }
  217. }
  218. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  219. func (f MLFlushJob) WithOpaqueID(s string) func(*MLFlushJobRequest) {
  220. return func(r *MLFlushJobRequest) {
  221. if r.Header == nil {
  222. r.Header = make(http.Header)
  223. }
  224. r.Header.Set("X-Opaque-Id", s)
  225. }
  226. }