...

Source file src/github.com/Azure/azure-sdk-for-go/services/preview/cognitiveservices/v1.0/anomalydetector/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/preview/cognitiveservices/v1.0/anomalydetector

     1  // Package anomalydetector implements the Azure ARM Anomalydetector service API version 1.0.
     2  //
     3  // The Anomaly Detector API detects anomalies automatically in time series data. It supports two kinds of mode, one is
     4  // for stateless using, another is for stateful using. In stateless mode, there are three functionalities. Entire
     5  // Detect is for detecting the whole series with model trained by the time series, Last Detect is detecting last point
     6  // with model trained by points before. ChangePoint Detect is for detecting trend changes in time series. In stateful
     7  // mode, user can store time series, the stored time series will be used for detection anomalies. Under this mode, user
     8  // can still use the above three functionalities by only giving a time range without preparing time series in client
     9  // side. Besides the above three functionalities, stateful model also provide group based detection and labeling
    10  // service. By leveraging labeling service user can provide labels for each detection result, these labels will be used
    11  // for retuning or regenerating detection models. Inconsistency detection is a kind of group based detection, this
    12  // detection will find inconsistency ones in a set of time series. By using anomaly detector service, business
    13  // customers can discover incidents and establish a logic flow for root cause analysis.
    14  package anomalydetector
    15  
    16  // Copyright (c) Microsoft Corporation. All rights reserved.
    17  // Licensed under the MIT License. See License.txt in the project root for license information.
    18  //
    19  // Code generated by Microsoft (R) AutoRest Code Generator.
    20  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
    21  
    22  import (
    23  	"context"
    24  	"crypto/tls"
    25  	"github.com/Azure/go-autorest/autorest"
    26  	"github.com/Azure/go-autorest/autorest/azure"
    27  	"github.com/Azure/go-autorest/autorest/validation"
    28  	"github.com/Azure/go-autorest/tracing"
    29  	"net/http"
    30  )
    31  
    32  // BaseClient is the base client for Anomalydetector.
    33  type BaseClient struct {
    34  	autorest.Client
    35  	Endpoint string
    36  }
    37  
    38  // New creates an instance of the BaseClient client.
    39  func New(endpoint string) BaseClient {
    40  	return NewWithoutDefaults(endpoint)
    41  }
    42  
    43  // NewWithoutDefaults creates an instance of the BaseClient client.
    44  func NewWithoutDefaults(endpoint string) BaseClient {
    45  	return BaseClient{
    46  		Client:   autorest.NewClientWithOptions(autorest.ClientOptions{UserAgent: UserAgent(), Renegotiation: tls.RenegotiateFreelyAsClient}),
    47  		Endpoint: endpoint,
    48  	}
    49  }
    50  
    51  // DetectChangePoint evaluate change point score of every series point
    52  // Parameters:
    53  // body - time series points and granularity is needed. Advanced model parameters can also be set in the
    54  // request if needed.
    55  func (client BaseClient) DetectChangePoint(ctx context.Context, body ChangePointDetectRequest) (result ChangePointDetectResponse, err error) {
    56  	if tracing.IsEnabled() {
    57  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.DetectChangePoint")
    58  		defer func() {
    59  			sc := -1
    60  			if result.Response.Response != nil {
    61  				sc = result.Response.Response.StatusCode
    62  			}
    63  			tracing.EndSpan(ctx, sc, err)
    64  		}()
    65  	}
    66  	if err := validation.Validate([]validation.Validation{
    67  		{TargetValue: body,
    68  			Constraints: []validation.Constraint{{Target: "body.Series", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
    69  		return result, validation.NewError("anomalydetector.BaseClient", "DetectChangePoint", err.Error())
    70  	}
    71  
    72  	req, err := client.DetectChangePointPreparer(ctx, body)
    73  	if err != nil {
    74  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectChangePoint", nil, "Failure preparing request")
    75  		return
    76  	}
    77  
    78  	resp, err := client.DetectChangePointSender(req)
    79  	if err != nil {
    80  		result.Response = autorest.Response{Response: resp}
    81  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectChangePoint", resp, "Failure sending request")
    82  		return
    83  	}
    84  
    85  	result, err = client.DetectChangePointResponder(resp)
    86  	if err != nil {
    87  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectChangePoint", resp, "Failure responding to request")
    88  		return
    89  	}
    90  
    91  	return
    92  }
    93  
    94  // DetectChangePointPreparer prepares the DetectChangePoint request.
    95  func (client BaseClient) DetectChangePointPreparer(ctx context.Context, body ChangePointDetectRequest) (*http.Request, error) {
    96  	urlParameters := map[string]interface{}{
    97  		"Endpoint": client.Endpoint,
    98  	}
    99  
   100  	preparer := autorest.CreatePreparer(
   101  		autorest.AsContentType("application/json; charset=utf-8"),
   102  		autorest.AsPost(),
   103  		autorest.WithCustomBaseURL("{Endpoint}/anomalydetector/v1.0", urlParameters),
   104  		autorest.WithPath("/timeseries/changepoint/detect"),
   105  		autorest.WithJSON(body))
   106  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   107  }
   108  
   109  // DetectChangePointSender sends the DetectChangePoint request. The method will close the
   110  // http.Response Body if it receives an error.
   111  func (client BaseClient) DetectChangePointSender(req *http.Request) (*http.Response, error) {
   112  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   113  }
   114  
   115  // DetectChangePointResponder handles the response to the DetectChangePoint request. The method always
   116  // closes the http.Response Body.
   117  func (client BaseClient) DetectChangePointResponder(resp *http.Response) (result ChangePointDetectResponse, err error) {
   118  	err = autorest.Respond(
   119  		resp,
   120  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   121  		autorest.ByUnmarshallingJSON(&result),
   122  		autorest.ByClosing())
   123  	result.Response = autorest.Response{Response: resp}
   124  	return
   125  }
   126  
   127  // DetectEntireSeries this operation generates a model using an entire series, each point is detected with the same
   128  // model. With this method, points before and after a certain point are used to determine whether it is an anomaly. The
   129  // entire detection can give user an overall status of the time series.
   130  // Parameters:
   131  // body - time series points and period if needed. Advanced model parameters can also be set in the request.
   132  func (client BaseClient) DetectEntireSeries(ctx context.Context, body DetectRequest) (result EntireDetectResponse, err error) {
   133  	if tracing.IsEnabled() {
   134  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.DetectEntireSeries")
   135  		defer func() {
   136  			sc := -1
   137  			if result.Response.Response != nil {
   138  				sc = result.Response.Response.StatusCode
   139  			}
   140  			tracing.EndSpan(ctx, sc, err)
   141  		}()
   142  	}
   143  	if err := validation.Validate([]validation.Validation{
   144  		{TargetValue: body,
   145  			Constraints: []validation.Constraint{{Target: "body.Series", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
   146  		return result, validation.NewError("anomalydetector.BaseClient", "DetectEntireSeries", err.Error())
   147  	}
   148  
   149  	req, err := client.DetectEntireSeriesPreparer(ctx, body)
   150  	if err != nil {
   151  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectEntireSeries", nil, "Failure preparing request")
   152  		return
   153  	}
   154  
   155  	resp, err := client.DetectEntireSeriesSender(req)
   156  	if err != nil {
   157  		result.Response = autorest.Response{Response: resp}
   158  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectEntireSeries", resp, "Failure sending request")
   159  		return
   160  	}
   161  
   162  	result, err = client.DetectEntireSeriesResponder(resp)
   163  	if err != nil {
   164  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectEntireSeries", resp, "Failure responding to request")
   165  		return
   166  	}
   167  
   168  	return
   169  }
   170  
   171  // DetectEntireSeriesPreparer prepares the DetectEntireSeries request.
   172  func (client BaseClient) DetectEntireSeriesPreparer(ctx context.Context, body DetectRequest) (*http.Request, error) {
   173  	urlParameters := map[string]interface{}{
   174  		"Endpoint": client.Endpoint,
   175  	}
   176  
   177  	preparer := autorest.CreatePreparer(
   178  		autorest.AsContentType("application/json; charset=utf-8"),
   179  		autorest.AsPost(),
   180  		autorest.WithCustomBaseURL("{Endpoint}/anomalydetector/v1.0", urlParameters),
   181  		autorest.WithPath("/timeseries/entire/detect"),
   182  		autorest.WithJSON(body))
   183  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   184  }
   185  
   186  // DetectEntireSeriesSender sends the DetectEntireSeries request. The method will close the
   187  // http.Response Body if it receives an error.
   188  func (client BaseClient) DetectEntireSeriesSender(req *http.Request) (*http.Response, error) {
   189  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   190  }
   191  
   192  // DetectEntireSeriesResponder handles the response to the DetectEntireSeries request. The method always
   193  // closes the http.Response Body.
   194  func (client BaseClient) DetectEntireSeriesResponder(resp *http.Response) (result EntireDetectResponse, err error) {
   195  	err = autorest.Respond(
   196  		resp,
   197  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   198  		autorest.ByUnmarshallingJSON(&result),
   199  		autorest.ByClosing())
   200  	result.Response = autorest.Response{Response: resp}
   201  	return
   202  }
   203  
   204  // DetectLastPoint this operation generates a model using points before the latest one. With this method, only
   205  // historical points are used to determine whether the target point is an anomaly. The latest point detecting operation
   206  // matches the scenario of real-time monitoring of business metrics.
   207  // Parameters:
   208  // body - time series points and period if needed. Advanced model parameters can also be set in the request.
   209  func (client BaseClient) DetectLastPoint(ctx context.Context, body DetectRequest) (result LastDetectResponse, err error) {
   210  	if tracing.IsEnabled() {
   211  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.DetectLastPoint")
   212  		defer func() {
   213  			sc := -1
   214  			if result.Response.Response != nil {
   215  				sc = result.Response.Response.StatusCode
   216  			}
   217  			tracing.EndSpan(ctx, sc, err)
   218  		}()
   219  	}
   220  	if err := validation.Validate([]validation.Validation{
   221  		{TargetValue: body,
   222  			Constraints: []validation.Constraint{{Target: "body.Series", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
   223  		return result, validation.NewError("anomalydetector.BaseClient", "DetectLastPoint", err.Error())
   224  	}
   225  
   226  	req, err := client.DetectLastPointPreparer(ctx, body)
   227  	if err != nil {
   228  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectLastPoint", nil, "Failure preparing request")
   229  		return
   230  	}
   231  
   232  	resp, err := client.DetectLastPointSender(req)
   233  	if err != nil {
   234  		result.Response = autorest.Response{Response: resp}
   235  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectLastPoint", resp, "Failure sending request")
   236  		return
   237  	}
   238  
   239  	result, err = client.DetectLastPointResponder(resp)
   240  	if err != nil {
   241  		err = autorest.NewErrorWithError(err, "anomalydetector.BaseClient", "DetectLastPoint", resp, "Failure responding to request")
   242  		return
   243  	}
   244  
   245  	return
   246  }
   247  
   248  // DetectLastPointPreparer prepares the DetectLastPoint request.
   249  func (client BaseClient) DetectLastPointPreparer(ctx context.Context, body DetectRequest) (*http.Request, error) {
   250  	urlParameters := map[string]interface{}{
   251  		"Endpoint": client.Endpoint,
   252  	}
   253  
   254  	preparer := autorest.CreatePreparer(
   255  		autorest.AsContentType("application/json; charset=utf-8"),
   256  		autorest.AsPost(),
   257  		autorest.WithCustomBaseURL("{Endpoint}/anomalydetector/v1.0", urlParameters),
   258  		autorest.WithPath("/timeseries/last/detect"),
   259  		autorest.WithJSON(body))
   260  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   261  }
   262  
   263  // DetectLastPointSender sends the DetectLastPoint request. The method will close the
   264  // http.Response Body if it receives an error.
   265  func (client BaseClient) DetectLastPointSender(req *http.Request) (*http.Response, error) {
   266  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   267  }
   268  
   269  // DetectLastPointResponder handles the response to the DetectLastPoint request. The method always
   270  // closes the http.Response Body.
   271  func (client BaseClient) DetectLastPointResponder(resp *http.Response) (result LastDetectResponse, err error) {
   272  	err = autorest.Respond(
   273  		resp,
   274  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   275  		autorest.ByUnmarshallingJSON(&result),
   276  		autorest.ByClosing())
   277  	result.Response = autorest.Response{Response: resp}
   278  	return
   279  }
   280  

View as plain text