api.xpack.ml.forecast.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. "strings"
  25. "time"
  26. )
  27. func newMLForecastFunc(t Transport) MLForecast {
  28. return func(job_id string, o ...func(*MLForecastRequest)) (*Response, error) {
  29. var r = MLForecastRequest{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. // MLForecast - Predicts the future behavior of a time series by using its historical behavior.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-forecast.html.
  40. type MLForecast func(job_id string, o ...func(*MLForecastRequest)) (*Response, error)
  41. // MLForecastRequest configures the ML Forecast API request.
  42. type MLForecastRequest struct {
  43. Body io.Reader
  44. JobID string
  45. Duration time.Duration
  46. ExpiresIn time.Duration
  47. MaxModelMemory string
  48. Pretty bool
  49. Human bool
  50. ErrorTrace bool
  51. FilterPath []string
  52. Header http.Header
  53. ctx context.Context
  54. }
  55. // Do executes the request and returns response or error.
  56. func (r MLForecastRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  57. var (
  58. method string
  59. path strings.Builder
  60. params map[string]string
  61. )
  62. method = "POST"
  63. path.Grow(1 + len("_ml") + 1 + len("anomaly_detectors") + 1 + len(r.JobID) + 1 + len("_forecast"))
  64. path.WriteString("/")
  65. path.WriteString("_ml")
  66. path.WriteString("/")
  67. path.WriteString("anomaly_detectors")
  68. path.WriteString("/")
  69. path.WriteString(r.JobID)
  70. path.WriteString("/")
  71. path.WriteString("_forecast")
  72. params = make(map[string]string)
  73. if r.Duration != 0 {
  74. params["duration"] = formatDuration(r.Duration)
  75. }
  76. if r.ExpiresIn != 0 {
  77. params["expires_in"] = formatDuration(r.ExpiresIn)
  78. }
  79. if r.MaxModelMemory != "" {
  80. params["max_model_memory"] = r.MaxModelMemory
  81. }
  82. if r.Pretty {
  83. params["pretty"] = "true"
  84. }
  85. if r.Human {
  86. params["human"] = "true"
  87. }
  88. if r.ErrorTrace {
  89. params["error_trace"] = "true"
  90. }
  91. if len(r.FilterPath) > 0 {
  92. params["filter_path"] = strings.Join(r.FilterPath, ",")
  93. }
  94. req, err := newRequest(method, path.String(), r.Body)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(params) > 0 {
  99. q := req.URL.Query()
  100. for k, v := range params {
  101. q.Set(k, v)
  102. }
  103. req.URL.RawQuery = q.Encode()
  104. }
  105. if len(r.Header) > 0 {
  106. if len(req.Header) == 0 {
  107. req.Header = r.Header
  108. } else {
  109. for k, vv := range r.Header {
  110. for _, v := range vv {
  111. req.Header.Add(k, v)
  112. }
  113. }
  114. }
  115. }
  116. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  117. req.Header[headerContentType] = headerContentTypeJSON
  118. }
  119. if ctx != nil {
  120. req = req.WithContext(ctx)
  121. }
  122. res, err := transport.Perform(req)
  123. if err != nil {
  124. return nil, err
  125. }
  126. response := Response{
  127. StatusCode: res.StatusCode,
  128. Body: res.Body,
  129. Header: res.Header,
  130. }
  131. return &response, nil
  132. }
  133. // WithContext sets the request context.
  134. func (f MLForecast) WithContext(v context.Context) func(*MLForecastRequest) {
  135. return func(r *MLForecastRequest) {
  136. r.ctx = v
  137. }
  138. }
  139. // WithBody - Query parameters can be specified in the body.
  140. func (f MLForecast) WithBody(v io.Reader) func(*MLForecastRequest) {
  141. return func(r *MLForecastRequest) {
  142. r.Body = v
  143. }
  144. }
  145. // WithDuration - the duration of the forecast.
  146. func (f MLForecast) WithDuration(v time.Duration) func(*MLForecastRequest) {
  147. return func(r *MLForecastRequest) {
  148. r.Duration = v
  149. }
  150. }
  151. // WithExpiresIn - the time interval after which the forecast expires. expired forecasts will be deleted at the first opportunity..
  152. func (f MLForecast) WithExpiresIn(v time.Duration) func(*MLForecastRequest) {
  153. return func(r *MLForecastRequest) {
  154. r.ExpiresIn = v
  155. }
  156. }
  157. // WithMaxModelMemory - the max memory able to be used by the forecast. default is 20mb..
  158. func (f MLForecast) WithMaxModelMemory(v string) func(*MLForecastRequest) {
  159. return func(r *MLForecastRequest) {
  160. r.MaxModelMemory = v
  161. }
  162. }
  163. // WithPretty makes the response body pretty-printed.
  164. func (f MLForecast) WithPretty() func(*MLForecastRequest) {
  165. return func(r *MLForecastRequest) {
  166. r.Pretty = true
  167. }
  168. }
  169. // WithHuman makes statistical values human-readable.
  170. func (f MLForecast) WithHuman() func(*MLForecastRequest) {
  171. return func(r *MLForecastRequest) {
  172. r.Human = true
  173. }
  174. }
  175. // WithErrorTrace includes the stack trace for errors in the response body.
  176. func (f MLForecast) WithErrorTrace() func(*MLForecastRequest) {
  177. return func(r *MLForecastRequest) {
  178. r.ErrorTrace = true
  179. }
  180. }
  181. // WithFilterPath filters the properties of the response body.
  182. func (f MLForecast) WithFilterPath(v ...string) func(*MLForecastRequest) {
  183. return func(r *MLForecastRequest) {
  184. r.FilterPath = v
  185. }
  186. }
  187. // WithHeader adds the headers to the HTTP request.
  188. func (f MLForecast) WithHeader(h map[string]string) func(*MLForecastRequest) {
  189. return func(r *MLForecastRequest) {
  190. if r.Header == nil {
  191. r.Header = make(http.Header)
  192. }
  193. for k, v := range h {
  194. r.Header.Add(k, v)
  195. }
  196. }
  197. }
  198. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  199. func (f MLForecast) WithOpaqueID(s string) func(*MLForecastRequest) {
  200. return func(r *MLForecastRequest) {
  201. if r.Header == nil {
  202. r.Header = make(http.Header)
  203. }
  204. r.Header.Set("X-Opaque-Id", s)
  205. }
  206. }