Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoPR @azure/cognitiveservices-anomalydetector] Update Anomaly Detection API #6850

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions sdk/cognitiveservices/cognitiveservices-anomalydetector/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
260 changes: 69 additions & 191 deletions sdk/cognitiveservices/cognitiveservices-anomalydetector/README.md
Original file line number Diff line number Diff line change
@@ -1,219 +1,97 @@
## An isomorphic javascript sdk for - AnomalyDetectorClient
# Azure Anomaly Detector client library for JavaScript

This package contains an isomorphic SDK for AnomalyDetectorClient.
This package contains an isomorphic SDK (runs both in Node.js and in browsers) for Azure Anomaly Detector client.

The Anomaly Detector API detects anomalies automatically in time series data. It supports two kinds of mode, one is for stateless using, another is for stateful using. In stateless mode, there are three functionalities. Entire Detect is for detecting the whole series with model trained by the time series, Last Detect is detecting last point with model trained by points before. ChangePoint Detect is for detecting trend changes in time series. In stateful mode, user can store time series, the stored time series will be used for detection anomalies. Under this mode, user can still use the above three functionalities by only giving a time range without preparing time series in client side. Besides the above three functionalities, stateful model also provide group based detection and labeling service. By leveraging labeling service user can provide labels for each detection result, these labels will be used for retuning or regenerating detection models. Inconsistency detection is a kind of group based detection, this detection will find inconsistency ones in a set of time series. By using anomaly detector service, business customers can discover incidents and establish a logic flow for root cause analysis.

[Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cognitiveservices/cognitiveservices-anomalydetector) |
[Package (NPM)](https://www.npmjs.com/package/@azure/cognitiveservices-anomalydetector) |
[API reference documentation](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-anomalydetector?view=azure-node-preview) |
[Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cognitiveservices/cognitiveservices-anomalydetector/samples)

## Getting started

### Currently supported environments

- [LTS versions of Node.js](https://nodejs.org/about/releases/)
- Latest versions of Safari, Chrome, Edge, and Firefox.
- Latest versions of Safari, Chrome, Edge and Firefox.

### Prerequisites

- An [Azure subscription][azure_sub].

### Install the `@azure/cognitiveservices-anomalydetector` package

### How to Install
Install the Azure Anomaly Detector client library for JavaScript with `npm`:

```bash
npm install @azure/cognitiveservices-anomalydetector
```

### How to use
### Create and authenticate a `AnomalyDetectorClient`

#### nodejs - Authentication, client creation and entireDetect as an example written in TypeScript.
To create a client object to access the Azure Anomaly Detector API, you will need the `endpoint` of your Azure Anomaly Detector resource and a `credential`. The Azure Anomaly Detector client can use Azure Active Directory credentials to authenticate.
You can find the endpoint for your Azure Anomaly Detector resource in the [Azure Portal][azure_portal].

##### Install @azure/ms-rest-azure-js
You can authenticate with Azure Active Directory using a credential from the [@azure/identity][azure_identity] library or [an existing AAD Token](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token).

To use the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with the Azure SDK, please install the `@azure/identity` package:

```bash
npm install @azure/ms-rest-azure-js
npm install @azure/identity
```

##### Sample code
The following sample determines anamolies with the given time series. To know more, refer to the [Azure Documentation on Anomaly Detectors](https://docs.microsoft.com/azure/cognitive-services/anomaly-detector/)
You will also need to **register a new AAD application and grant access to Azure Anomaly Detector** by assigning the suitable role to your service principal (note: roles such as `"Owner"` will not grant the necessary permissions).
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`.

For more information about how to create an Azure AD Application check out [this guide](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).

```javascript
const { AnomalyDetectorClient } = require("@azure/cognitiveservices-anomalydetector");
const { CognitiveServicesCredentials } = require("@azure/ms-rest-azure-js");

async function main() {
const anomalyDetectorKey = process.env["anomalyDetectorKey"] || "<anomalyDetectorKey>";
const anomalyDetectorEndPoint =
process.env["anomalyDetectorEndPoint"] || "<anomalyDetectorEndPoint>";

const cognitiveServiceCredentials = new CognitiveServicesCredentials(anomalyDetectorKey);

const client = new AnomalyDetectorClient(cognitiveServiceCredentials, anomalyDetectorEndPoint);

const body = {
series: [
{
timestamp: new Date("December 15, 2018"),
value: 1.01
},
{
timestamp: new Date("December 16, 2018"),
value: 1.02
},
{
timestamp: new Date("December 17, 2018"),
value: 1.03
},
{
timestamp: new Date("December 18, 2018"),
value: 1.04
},
{
timestamp: new Date("December 19, 2018"),
value: 1.05
},
{
timestamp: new Date("December 20, 2018"),
value: 1.06
},
{
timestamp: new Date("December 21, 2018"),
value: 1.07
},
{
timestamp: new Date("December 22, 2018"),
value: 1.08
},
{
timestamp: new Date("December 23, 2018"),
value: 1.09
},
{
timestamp: new Date("December 24, 2018"),
value: 1.1
},
{
timestamp: new Date("December 25, 2018"),
value: 1.11
},
{
timestamp: new Date("December 26, 2018"),
value: 1.12
}
],
granularity: "daily",
customInterval: 1,
period: 1,
maxAnomalyRatio: 0.3,
sensitivity: 1
};

client
.entireDetect(body)
.then((result) => {
console.log("The result is:");
console.log(result);
})
.catch((err) => {
console.log("An error occurred:");
console.error(err);
});
}

main();
const { DefaultAzureCredential } = require("@azure/identity");
const client = new AnomalyDetectorClient("<endpoint>", new DefaultAzureCredential());
```

#### browser - Authentication, client creation and entireDetect as an example written in JavaScript.

##### Sample code

- index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-anomalydetector sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/cognitiveservices-anomalydetector/dist/cognitiveservices-anomalydetector.js"></script>
<script type="text/javascript">
const anomalyDetectorKey = "<YOUR_ANOMALY_DETECTOR_KEY>";
const anomalyDetectorEndPoint = "<YOUR_ANOMALY_DETECTOR_ENDPOINT>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": anomalyDetectorKey
}
});
const client = new Azure.CognitiveservicesAnomalydetector.AnomalyDetectorClient(
cognitiveServiceCredentials,
anomalyDetectorEndPoint
);

const body = {
series: [
{
timestamp: new Date("December 15, 2018"),
value: 1.01
},
{
timestamp: new Date("December 16, 2018"),
value: 1.02
},
{
timestamp: new Date("December 17, 2018"),
value: 1.03
},
{
timestamp: new Date("December 18, 2018"),
value: 1.04
},
{
timestamp: new Date("December 19, 2018"),
value: 1.05
},
{
timestamp: new Date("December 20, 2018"),
value: 1.06
},
{
timestamp: new Date("December 21, 2018"),
value: 1.07
},
{
timestamp: new Date("December 22, 2018"),
value: 1.08
},
{
timestamp: new Date("December 23, 2018"),
value: 1.09
},
{
timestamp: new Date("December 24, 2018"),
value: 1.1
},
{
timestamp: new Date("December 25, 2018"),
value: 1.11
},
{
timestamp: new Date("December 26, 2018"),
value: 1.12
}
],
granularity: "daily",
customInterval: 1,
period: 1,
maxAnomalyRatio: 0.3,
sensitivity: 1
};

client
.entireDetect(body)
.then((result) => {
console.log("The result is:");
console.log(result);
})
.catch((err) => {
console.log("An error occurred:");
console.error(err);
});
</script>
</head>
<body></body>
</html>

### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

## Key concepts

### AnomalyDetectorClient

`AnomalyDetectorClient` is the primary interface for developers using the Azure Anomaly Detector client library. Explore the methods on this client object to understand the different features of the Azure Anomaly Detector service that you can access.

## Troubleshooting

### Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:

```javascript
const { setLogLevel } = require("@azure/logger");
setLogLevel("info");
```

For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).

## Next steps

Please take a look at the [samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cognitiveservices/cognitiveservices-anomalydetector/samples) directory for detailed examples on how to use this library.

## Contributing

If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.

## Related projects

- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)
- [Microsoft Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js)

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcognitiveservices%2Fcognitiveservices-anomalydetector%2FREADME.png)

[azure_cli]: https://docs.microsoft.com/cli/azure
[azure_sub]: https://azure.microsoft.com/free/
[azure_sub]: https://azure.microsoft.com/free/
[azure_portal]: https://portal.azure.com
[azure_identity]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "./dist-esm/src/index.d.ts",
"docModel": { "enabled": true },
"apiReport": { "enabled": true, "reportFolder": "./review" },
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/cognitiveservices-anomalydetector.d.ts"
},
"messages": {
"tsdocMessageReporting": { "default": { "logLevel": "none" } },
"extractorMessageReporting": {
"ae-missing-release-tag": { "logLevel": "none" },
"ae-unresolved-link": { "logLevel": "none" }
}
}
}
Loading