Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Microsoft
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
Expand Down
214 changes: 57 additions & 157 deletions sdk/cognitiveservices/cognitiveservices-anomalydetector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,197 +15,97 @@ npm install @azure/cognitiveservices-anomalydetector

### How to use

#### nodejs - Authentication, client creation and entireDetect as an example written in TypeScript.
#### nodejs - client creation and detectEntireSeries as an example written in TypeScript.

##### Install @azure/ms-rest-azure-js
##### Install @azure/ms-rest-nodeauth

- Please install minimum version of `"@azure/ms-rest-nodeauth": "^3.0.0"`.
```bash
npm install @azure/ms-rest-azure-js
npm install @azure/ms-rest-nodeauth@"^3.0.0"
```

##### 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/)

```javascript
While the below sample uses the interactive login, other authentication options can be found in the [README.md file of @azure/ms-rest-nodeauth](https://www.npmjs.com/package/@azure/ms-rest-nodeauth) package
```typescript
const msRestNodeAuth = require("@azure/ms-rest-nodeauth");
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 subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new AnomalyDetectorClient(creds, subscriptionId);
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",
series: [{
timestamp: new Date().toISOString(),
value: 1.01
}],
granularity: "yearly",
customInterval: 1,
period: 1,
maxAnomalyRatio: 0.3,
maxAnomalyRatio: 1.01,
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();
client.detectEntireSeries(body).then((result) => {
console.log("The result is:");
console.log(result);
});
}).catch((err) => {
console.error(err);
});
```

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

##### Install @azure/ms-rest-browserauth

```bash
npm install @azure/ms-rest-browserauth
```

##### Sample code

- index.html
See https://github.com/Azure/ms-rest-browserauth to learn how to authenticate to Azure in the browser.

- 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/ms-rest-browserauth/dist/msAuth.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 subscriptionId = "<Subscription_Id>";
const authManager = new msAuth.AuthManager({
clientId: "<client id for your Azure AD app>",
tenant: "<optional tenant for your organization>"
});
const client = new Azure.CognitiveservicesAnomalydetector.AnomalyDetectorClient(
cognitiveServiceCredentials,
anomalyDetectorEndPoint
);

const body = {
series: [
{
timestamp: new Date("December 15, 2018"),
authManager.finalizeLogin().then((res) => {
if (!res.isLoggedIn) {
// may cause redirects
authManager.login();
}
const client = new Azure.CognitiveservicesAnomalydetector.AnomalyDetectorClient(res.creds, subscriptionId);
const body = {
series: [{
timestamp: new Date().toISOString(),
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) => {
}],
granularity: "yearly",
customInterval: 1,
period: 1,
maxAnomalyRatio: 1.01,
sensitivity: 1
};
client.detectEntireSeries(body).then((result) => {
console.log("The result is:");
console.log(result);
})
.catch((err) => {
}).catch((err) => {
console.log("An error occurred:");
console.error(err);
});
});
</script>
</head>
<body></body>
Expand All @@ -216,4 +116,4 @@ main();

- [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)
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js/sdk/cognitiveservices/cognitiveservices-anomalydetector/README.png)
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const config = {
"@azure/ms-rest-azure-js": "msRestAzure"
},
banner: `/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Loading