@azure-rest/ai-anomaly-detector 1.0.0-alpha.20250108.1 → 1.0.0-alpha.20250110.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -103
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -89,148 +89,172 @@ The following section provides several code snippets covering some of the most c
|
|
|
89
89
|
|
|
90
90
|
### Batch detection
|
|
91
91
|
|
|
92
|
-
```
|
|
92
|
+
```ts snippet:batch_detection
|
|
93
|
+
import {
|
|
94
|
+
TimeSeriesPoint,
|
|
95
|
+
AnomalyDetector,
|
|
96
|
+
DetectUnivariateEntireSeriesParameters,
|
|
97
|
+
isUnexpected,
|
|
98
|
+
} from "@azure-rest/ai-anomaly-detector";
|
|
99
|
+
import { parse } from "csv-parse/sync";
|
|
100
|
+
import { AzureKeyCredential } from "@azure/core-auth";
|
|
101
|
+
|
|
93
102
|
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
|
|
94
103
|
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
|
|
95
104
|
const timeSeriesDataPath = "./samples-dev/example-data/request-data.csv";
|
|
96
105
|
|
|
97
106
|
function read_series_from_file(path: string): Array<TimeSeriesPoint> {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
107
|
+
const result = Array<TimeSeriesPoint>();
|
|
108
|
+
const input = fs.readFileSync(path).toString();
|
|
109
|
+
const parsed = parse(input, { skip_empty_lines: true });
|
|
101
110
|
parsed.forEach(function (e: Array<string>) {
|
|
102
111
|
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
|
|
103
112
|
});
|
|
104
113
|
return result;
|
|
105
114
|
}
|
|
106
115
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
116
|
+
// create client
|
|
117
|
+
const credential = new AzureKeyCredential(apiKey);
|
|
118
|
+
const client = AnomalyDetector(endpoint, credential);
|
|
119
|
+
|
|
120
|
+
// construct request
|
|
121
|
+
const options: DetectUnivariateEntireSeriesParameters = {
|
|
122
|
+
body: {
|
|
123
|
+
granularity: "daily",
|
|
124
|
+
imputeMode: "auto",
|
|
125
|
+
maxAnomalyRatio: 0.25,
|
|
126
|
+
sensitivity: 95,
|
|
127
|
+
series: read_series_from_file(timeSeriesDataPath),
|
|
128
|
+
},
|
|
129
|
+
headers: { "Content-Type": "application/json" },
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// get last detect result
|
|
133
|
+
const result = await client.path("/timeseries/entire/detect").post(options);
|
|
134
|
+
if (isUnexpected(result)) {
|
|
135
|
+
throw result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (result.body.isAnomaly) {
|
|
139
|
+
result.body.isAnomaly.forEach(function (anomaly, index) {
|
|
140
|
+
if (anomaly === true) {
|
|
141
|
+
console.log(index);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
} else {
|
|
145
|
+
console.log("There is no anomaly detected from the series.");
|
|
146
|
+
}
|
|
139
147
|
```
|
|
140
148
|
|
|
141
149
|
### Streaming Detection
|
|
142
150
|
|
|
143
|
-
```
|
|
151
|
+
```ts snippet:streaming_detection
|
|
152
|
+
import {
|
|
153
|
+
TimeSeriesPoint,
|
|
154
|
+
AnomalyDetector,
|
|
155
|
+
DetectUnivariateLastPointParameters,
|
|
156
|
+
isUnexpected,
|
|
157
|
+
} from "@azure-rest/ai-anomaly-detector";
|
|
158
|
+
import { parse } from "csv-parse/sync";
|
|
159
|
+
import { AzureKeyCredential } from "@azure/core-auth";
|
|
160
|
+
|
|
144
161
|
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
|
|
145
162
|
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
|
|
146
163
|
const timeSeriesDataPath = "./samples-dev/example-data/request-data.csv";
|
|
147
164
|
|
|
148
165
|
function read_series_from_file(path: string): Array<TimeSeriesPoint> {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
166
|
+
const result = Array<TimeSeriesPoint>();
|
|
167
|
+
const input = fs.readFileSync(path).toString();
|
|
168
|
+
const parsed = parse(input, { skip_empty_lines: true });
|
|
152
169
|
parsed.forEach(function (e: Array<string>) {
|
|
153
170
|
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
|
|
154
171
|
});
|
|
155
172
|
return result;
|
|
156
173
|
}
|
|
157
174
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
175
|
+
// create client
|
|
176
|
+
const credential = new AzureKeyCredential(apiKey);
|
|
177
|
+
const client = AnomalyDetector(endpoint, credential);
|
|
178
|
+
|
|
179
|
+
// construct request
|
|
180
|
+
const options: DetectUnivariateLastPointParameters = {
|
|
181
|
+
body: {
|
|
182
|
+
granularity: "daily",
|
|
183
|
+
imputeFixedValue: 800,
|
|
184
|
+
imputeMode: "fixed",
|
|
185
|
+
maxAnomalyRatio: 0.25,
|
|
186
|
+
sensitivity: 95,
|
|
187
|
+
series: read_series_from_file(timeSeriesDataPath),
|
|
188
|
+
},
|
|
189
|
+
headers: { "Content-Type": "application/json" },
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// get last detect result
|
|
193
|
+
const result = await client.path("/timeseries/last/detect").post(options);
|
|
194
|
+
if (isUnexpected(result)) {
|
|
195
|
+
throw result;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (result.body.isAnomaly) {
|
|
199
|
+
console.log("The latest point is detected as anomaly.");
|
|
200
|
+
} else {
|
|
201
|
+
console.log("The latest point is not detected as anomaly.");
|
|
202
|
+
}
|
|
187
203
|
```
|
|
188
204
|
|
|
189
205
|
### Detect change points
|
|
190
206
|
|
|
191
|
-
```
|
|
207
|
+
```ts snippet:detect_change_points
|
|
208
|
+
import {
|
|
209
|
+
TimeSeriesPoint,
|
|
210
|
+
AnomalyDetector,
|
|
211
|
+
DetectUnivariateChangePointParameters,
|
|
212
|
+
isUnexpected,
|
|
213
|
+
} from "@azure-rest/ai-anomaly-detector";
|
|
214
|
+
import { parse } from "csv-parse/sync";
|
|
215
|
+
import { AzureKeyCredential } from "@azure/core-auth";
|
|
216
|
+
|
|
192
217
|
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
|
|
193
218
|
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
|
|
194
219
|
const timeSeriesDataPath = "./samples-dev/example-data/request-data.csv";
|
|
195
220
|
|
|
196
221
|
function read_series_from_file(path: string): Array<TimeSeriesPoint> {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
222
|
+
const result = Array<TimeSeriesPoint>();
|
|
223
|
+
const input = fs.readFileSync(path).toString();
|
|
224
|
+
const parsed = parse(input, { skip_empty_lines: true });
|
|
200
225
|
parsed.forEach(function (e: Array<string>) {
|
|
201
226
|
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
|
|
202
227
|
});
|
|
203
228
|
return result;
|
|
204
229
|
}
|
|
205
230
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
231
|
+
const credential = new AzureKeyCredential(apiKey);
|
|
232
|
+
const client = AnomalyDetector(endpoint, credential);
|
|
233
|
+
const options: DetectUnivariateChangePointParameters = {
|
|
234
|
+
body: {
|
|
235
|
+
granularity: "daily",
|
|
236
|
+
series: read_series_from_file(timeSeriesDataPath),
|
|
237
|
+
},
|
|
238
|
+
headers: { "Content-Type": "application/json" },
|
|
239
|
+
};
|
|
240
|
+
const result = await client.path("/timeseries/changepoint/detect").post(options);
|
|
241
|
+
if (isUnexpected(result)) {
|
|
242
|
+
throw result;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (result.body.isChangePoint === undefined) throw new Error("Empty isChangePoint");
|
|
246
|
+
if (
|
|
247
|
+
result.body.isChangePoint.some(function (changePoint) {
|
|
248
|
+
return changePoint === true;
|
|
249
|
+
})
|
|
250
|
+
) {
|
|
251
|
+
console.log("Change points were detected from the series at index:");
|
|
252
|
+
result.body.isChangePoint.forEach(function (changePoint, index) {
|
|
253
|
+
if (changePoint === true) console.log(index);
|
|
254
|
+
});
|
|
255
|
+
} else {
|
|
256
|
+
console.log("There is no change point detected from the series.");
|
|
257
|
+
}
|
|
234
258
|
```
|
|
235
259
|
|
|
236
260
|
### Multivariate Anomaly Detection Sample
|
|
@@ -245,8 +269,8 @@ To see how to use Anomaly Detector library to conduct Multivariate Anomaly Detec
|
|
|
245
269
|
|
|
246
270
|
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`:
|
|
247
271
|
|
|
248
|
-
```
|
|
249
|
-
|
|
272
|
+
```ts snippet:SetLogLevel
|
|
273
|
+
import { setLogLevel } from "@azure/logger";
|
|
250
274
|
|
|
251
275
|
setLogLevel("info");
|
|
252
276
|
```
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@azure-rest/ai-anomaly-detector",
|
|
3
3
|
"sdk-type": "client",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
|
-
"version": "1.0.0-alpha.
|
|
5
|
+
"version": "1.0.0-alpha.20250110.1",
|
|
6
6
|
"description": "A generated SDK for AnomalyDetectorRest.",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"node",
|
|
@@ -50,18 +50,18 @@
|
|
|
50
50
|
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
|
|
51
51
|
"unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser",
|
|
52
52
|
"unit-test:node": "dev-tool run test:vitest",
|
|
53
|
-
"update-snippets": "
|
|
53
|
+
"update-snippets": "dev-tool run update-snippets"
|
|
54
54
|
},
|
|
55
55
|
"sideEffects": false,
|
|
56
56
|
"autoPublish": false,
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@azure-rest/core-client": "^
|
|
59
|
-
"@azure/core-auth": "^1.
|
|
60
|
-
"@azure/core-lro": "^2.2
|
|
61
|
-
"@azure/core-paging": "^1.2
|
|
62
|
-
"@azure/core-rest-pipeline": "^1.
|
|
63
|
-
"@azure/logger": "^1.
|
|
64
|
-
"tslib": "^2.
|
|
58
|
+
"@azure-rest/core-client": "^2.3.1",
|
|
59
|
+
"@azure/core-auth": "^1.9.0",
|
|
60
|
+
"@azure/core-lro": "^2.7.2",
|
|
61
|
+
"@azure/core-paging": "^1.6.2",
|
|
62
|
+
"@azure/core-rest-pipeline": "^1.18.1",
|
|
63
|
+
"@azure/logger": "^1.1.4",
|
|
64
|
+
"tslib": "^2.8.1"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@azure-tools/test-credential": "^2.0.0",
|
|
@@ -96,8 +96,7 @@
|
|
|
96
96
|
"productSlugs": [
|
|
97
97
|
"azure"
|
|
98
98
|
],
|
|
99
|
-
"disableDocsMs": true
|
|
100
|
-
"apiRefLink": "https://docs.microsoft.com/javascript/api/@azure-rest/ai-anomaly-detector?view=azure-node-preview"
|
|
99
|
+
"disableDocsMs": true
|
|
101
100
|
},
|
|
102
101
|
"type": "module",
|
|
103
102
|
"tshy": {
|