@@ -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
- ``` typescript
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
- let result = Array <TimeSeriesPoint >();
99
- let input = fs .readFileSync (path ).toString ();
100
- let parsed = parse (input , { skip_empty_lines: true });
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
- export async function main() {
108
- // create client
109
- const credential = new AzureKeyCredential (apiKey );
110
- const client = AnomalyDetector (endpoint , credential );
111
-
112
- // construct request
113
- const options: DetectUnivariateEntireSeriesParameters = {
114
- body: {
115
- granularity: " daily" ,
116
- imputeMode: " auto" ,
117
- maxAnomalyRatio: 0.25 ,
118
- sensitivity: 95 ,
119
- series: read_series_from_file (timeSeriesDataPath ),
120
- },
121
- headers: { " Content-Type" : " application/json" },
122
- };
123
-
124
- // get last detect result
125
- const result = await client .path (" /timeseries/entire/detect" ).post (options );
126
- if (isUnexpected (result )) {
127
- throw result ;
128
- }
129
-
130
- if (result .body .isAnomaly ) {
131
- result .body .isAnomaly .forEach (function (anomaly , index ) {
132
- if (anomaly === true ) {
133
- console .log (index );
134
- }
135
- });
136
- } else {
137
- console .log (" There is no anomaly detected from the series." );
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
- ` ` ` typescript
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
- let result = Array <TimeSeriesPoint >();
150
- let input = fs .readFileSync (path ).toString ();
151
- let parsed = parse (input , { skip_empty_lines: true });
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
- export async function main() {
159
- // create client
160
- const credential = new AzureKeyCredential (apiKey );
161
- const client = AnomalyDetector (endpoint , credential );
162
-
163
- // construct request
164
- const options: DetectUnivariateLastPointParameters = {
165
- body: {
166
- granularity: " daily" ,
167
- imputeFixedValue: 800 ,
168
- imputeMode: " fixed" ,
169
- maxAnomalyRatio: 0.25 ,
170
- sensitivity: 95 ,
171
- series: read_series_from_file (timeSeriesDataPath ),
172
- },
173
- headers: { " Content-Type" : " application/json" },
174
- };
175
-
176
- // get last detect result
177
- const result = await client .path (" /timeseries/last/detect" ).post (options );
178
- if (isUnexpected (result )) {
179
- throw result ;
180
- }
181
-
182
- if (result .body .isAnomaly ) {
183
- console .log (" The latest point is detected as anomaly." );
184
- } else {
185
- console .log (" The latest point is not detected as anomaly." );
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
- ` ` ` typescript
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
- let result = Array <TimeSeriesPoint >();
198
- let input = fs .readFileSync (path ).toString ();
199
- let parsed = parse (input , { skip_empty_lines: true });
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
- export async function main() {
207
- const credential = new AzureKeyCredential (apiKey );
208
- const client = AnomalyDetector (endpoint , credential );
209
- const options: DetectUnivariateChangePointParameters = {
210
- body: {
211
- granularity: " daily" ,
212
- series: read_series_from_file (timeSeriesDataPath ),
213
- },
214
- headers: { " Content-Type" : " application/json" },
215
- };
216
- const result = await client .path (" /timeseries/changepoint/detect" ).post (options );
217
- if (isUnexpected (result )) {
218
- throw result ;
219
- }
220
-
221
- if (result .body .isChangePoint === undefined ) throw new Error (" Empty isChangePoint" );
222
- if (
223
- result .body .isChangePoint .some (function (changePoint ) {
224
- return changePoint === true ;
225
- })
226
- ) {
227
- console .log (" Change points were detected from the series at index:" );
228
- result .body .isChangePoint .forEach (function (changePoint , index ) {
229
- if (changePoint === true ) console .log (index );
230
- });
231
- } else {
232
- console .log (" There is no change point detected from the series." );
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
- ` ` ` javascript
249
- const { setLogLevel } = require ( " @azure/logger" ) ;
272
+ ``` ts snippet:SetLogLevel
273
+ import { setLogLevel } from " @azure/logger" ;
250
274
251
275
setLogLevel (" info" );
252
276
```
0 commit comments