Skip to content

Commit 56cce6c

Browse files
committed
update code sample to latest version
1 parent 8b533bd commit 56cce6c

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

articles/cognitive-services/Anomaly-Detector/includes/quickstarts/anomaly-detector-client-library-javascript.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: mrbullwinkle
66
manager: nitinme
77
ms.service: cognitive-services
88
ms.topic: include
9-
ms.date: 10/13/2022
9+
ms.date: 03/14/2023
1010
ms.author: mbullwin
1111
ms.custom: devx-track-js
1212
---
@@ -45,8 +45,9 @@ Create a `package.json` file with the following contents:
4545
{
4646
"dependencies": {
4747
"@azure/ai-anomaly-detector": "next",
48+
"@azure-rest/ai-anomaly-detector": "next",
4849
"@azure/core-auth": "^1.3.0",
49-
"csv-parse": "^4.4.0"
50+
"csv-parse": "^5.3.0"
5051
}
5152
}
5253
```
@@ -122,59 +123,68 @@ curl "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/main/sdk/anom
122123
Create a file named `index.js` and replace with the following code:
123124

124125
```javascript
125-
const { AnomalyDetectorClient, KnownTimeGranularity } = require("@azure/ai-anomaly-detector");
126+
const AnomalyDetector = require("@azure-rest/ai-anomaly-detector").default,
127+
{ isUnexpected } = require("@azure-rest/ai-anomaly-detector");
126128
const { AzureKeyCredential } = require("@azure/core-auth");
127129

130+
const { parse } = require("csv-parse/sync");
128131
const fs = require("fs");
129-
const parse = require("csv-parse/lib/sync");
130132

131133
// You will need to set this environment variables or edit the following values
132134
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
133135
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
134-
const datapath = "./request-data.csv";
136+
const timeSeriesDataPath = "./request-data.csv";
135137

136138
function read_series_from_file(path) {
137139
let result = Array();
138140
let input = fs.readFileSync(path).toString();
139141
let parsed = parse(input, { skip_empty_lines: true });
140-
parsed.forEach(function(e) {
142+
parsed.forEach(function (e) {
141143
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
142144
});
143145
return result;
144146
}
145147

146148
async function main() {
147149
// create client
148-
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));
150+
const credential = new AzureKeyCredential(apiKey);
151+
const client = AnomalyDetector(endpoint, credential);
149152

150153
// construct request
151-
const request = {
152-
series: read_series_from_file(datapath),
153-
granularity: KnownTimeGranularity.daily
154+
const options = {
155+
body: {
156+
granularity: "daily",
157+
imputeMode: "auto",
158+
maxAnomalyRatio: 0.25,
159+
sensitivity: 95,
160+
series: read_series_from_file(timeSeriesDataPath),
161+
},
162+
headers: { "Content-Type": "application/json" },
154163
};
155164

156-
// get entire detect result
157-
const result = await client.detectEntireSeries(request);
165+
// get last detect result
166+
const result = await client.path("/timeseries/entire/detect").post(options);
167+
if (isUnexpected(result)) {
168+
throw result;
169+
}
158170

159-
if (
160-
result.isAnomaly.some(function(anomaly) {
161-
return anomaly === true;
162-
})
163-
) {
164-
console.log("Anomalies were detected from the series at index:");
165-
result.isAnomaly.forEach(function(anomaly, index) {
171+
if (result.body.isAnomaly) {
172+
result.body.isAnomaly.forEach(function (anomaly, index) {
166173
if (anomaly === true) {
167174
console.log(index);
168175
}
169176
});
170177
} else {
171178
console.log("There is no anomaly detected from the series.");
172179
}
180+
173181
}
174182

175183
main().catch((err) => {
176184
console.error("The sample encountered an error:", err);
177185
});
186+
187+
module.exports = { main };
178188
```
179189

180190
## Run the application

0 commit comments

Comments
 (0)