Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Anomaly Detector SDK track1 to track2 #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion quickstarts/sdk/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"author": "[email protected]",
"license": "ISC",
"dependencies": {
"@azure/cognitiveservices-anomalydetector": "^1.0.0",
"@azure/ai-anomaly-detector": "^3.0.0-beta.2",
"@azure/core-auth": "^1.1.4",
"csv-parse": "^4.4.0",
"tsc": "^1.20150623.0",
"typescript": "^3.4.3"
Expand Down
39 changes: 15 additions & 24 deletions quickstarts/sdk/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import * as msRest from "@azure/ms-rest-js";
import { AnomalyDetectorClient, AnomalyDetectorModels, AnomalyDetectorMappers } from "@azure/cognitiveservices-anomalydetector";
import { AzureKeyCredential } from "@azure/core-auth";
import { AnomalyDetectorClient, DetectRequest, DetectEntireResponse, TimeSeriesPoint, TimeGranularity } from "@azure/ai-anomaly-detector";
import * as fs from "fs";
import parse from "csv-parse/lib/sync";

function entire_detect_sample(endpoint: string, key: string, request: AnomalyDetectorModels.Request){
function entire_detect_sample(endpoint: string, key: string, request: DetectRequest){
console.log("Sample of detecting anomalies in the entire series.");
const options: msRest.ApiKeyCredentialOptions = {
inHeader: {
"Ocp-Apim-Subscription-Key": key
}
};

const client = new AnomalyDetectorClient(new msRest.ApiKeyCredentials(options), endpoint);
client.entireDetect(request).then((result) => {

const client = new AnomalyDetectorClient(endpoint ,new AzureKeyCredential(key));
client.detectEntireSeries(request).then((result) => {
if(result.isAnomaly.some(function(e){return e === true;})){
console.log("Anomaly was detected from the series at index:");
result.isAnomaly.forEach(function(e, i){
Expand All @@ -31,16 +27,11 @@ function entire_detect_sample(endpoint: string, key: string, request: AnomalyDet
});
}

function last_detect_sample(endpoint: string, key: string, request: AnomalyDetectorModels.Request){
function last_detect_sample(endpoint: string, key: string, request: DetectRequest){
console.log("Sample of detecting whether the latest point in series is anomaly.");
const options: msRest.ApiKeyCredentialOptions = {
inHeader: {
"Ocp-Apim-Subscription-Key": key
}
};

const client = new AnomalyDetectorClient(new msRest.ApiKeyCredentials(options), endpoint);
client.lastDetect(request).then((result) => {
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
client.detectLastPoint(request).then((result) => {
if(result.isAnomaly){
console.log("The latest point is detected as anomaly.");
}else{
Expand All @@ -56,8 +47,8 @@ function last_detect_sample(endpoint: string, key: string, request: AnomalyDetec
});
}

function read_series_from_file(path: string): Array<AnomalyDetectorModels.Point>{
let result = Array<AnomalyDetectorModels.Point>();
function read_series_from_file(path: string): Array<TimeSeriesPoint>{
let result = Array<TimeSeriesPoint>();
let input = fs.readFileSync(path).toString();
let parsed = parse(input, {skip_empty_lines:true});
parsed.forEach(function(e: Array<string>){
Expand All @@ -66,13 +57,13 @@ function read_series_from_file(path: string): Array<AnomalyDetectorModels.Point>
return result;
}

const endpoint = "[YOUR_ENDPOINT_URL]";
const key = "[YOUR_SUBSCRIPTION_KEY]";
const endpoint = "[YOUR_ANOMALY_DETECTOR_ENDPOINT_URL]";
const key = "[YOUR_ANOMALY_DETECTOR_KEY]";
const path = "[PATH_TO_TIME_SERIES_DATA]";

const request: AnomalyDetectorModels.Request = {
const request: DetectRequest = {
series: read_series_from_file(path),
granularity: "daily",
granularity: TimeGranularity.daily,
};
entire_detect_sample(endpoint, key, request);
last_detect_sample(endpoint, key, request);
Expand Down