@@ -6,7 +6,7 @@ author: mrbullwinkle
6
6
manager : nitinme
7
7
ms.service : cognitive-services
8
8
ms.topic : include
9
- ms.date : 10/13/2022
9
+ ms.date : 03/14/2023
10
10
ms.author : mbullwin
11
11
ms.custom : devx-track-js
12
12
---
@@ -45,8 +45,9 @@ Create a `package.json` file with the following contents:
45
45
{
46
46
"dependencies" : {
47
47
"@azure/ai-anomaly-detector" : " next" ,
48
+ "@azure-rest/ai-anomaly-detector" : " next" ,
48
49
"@azure/core-auth" : " ^1.3.0" ,
49
- "csv-parse" : " ^4.4 .0"
50
+ "csv-parse" : " ^5.3 .0"
50
51
}
51
52
}
52
53
```
@@ -122,59 +123,68 @@ curl "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/main/sdk/anom
122
123
Create a file named ` index.js ` and replace with the following code:
123
124
124
125
``` 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" );
126
128
const { AzureKeyCredential } = require (" @azure/core-auth" );
127
129
130
+ const { parse } = require (" csv-parse/sync" );
128
131
const fs = require (" fs" );
129
- const parse = require (" csv-parse/lib/sync" );
130
132
131
133
// You will need to set this environment variables or edit the following values
132
134
const apiKey = process .env [" ANOMALY_DETECTOR_API_KEY" ] || " " ;
133
135
const endpoint = process .env [" ANOMALY_DETECTOR_ENDPOINT" ] || " " ;
134
- const datapath = " ./request-data.csv" ;
136
+ const timeSeriesDataPath = " ./request-data.csv" ;
135
137
136
138
function read_series_from_file (path ) {
137
139
let result = Array ();
138
140
let input = fs .readFileSync (path).toString ();
139
141
let parsed = parse (input, { skip_empty_lines: true });
140
- parsed .forEach (function (e ) {
142
+ parsed .forEach (function (e ) {
141
143
result .push ({ timestamp: new Date (e[0 ]), value: Number (e[1 ]) });
142
144
});
143
145
return result;
144
146
}
145
147
146
148
async function main () {
147
149
// create client
148
- const client = new AnomalyDetectorClient (endpoint, new AzureKeyCredential (apiKey));
150
+ const credential = new AzureKeyCredential (apiKey);
151
+ const client = AnomalyDetector (endpoint, credential);
149
152
150
153
// 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" },
154
163
};
155
164
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
+ }
158
170
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 ) {
166
173
if (anomaly === true ) {
167
174
console .log (index);
168
175
}
169
176
});
170
177
} else {
171
178
console .log (" There is no anomaly detected from the series." );
172
179
}
180
+
173
181
}
174
182
175
183
main ().catch ((err ) => {
176
184
console .error (" The sample encountered an error:" , err);
177
185
});
186
+
187
+ module .exports = { main };
178
188
```
179
189
180
190
## Run the application
0 commit comments