Skip to content

Commit

Permalink
Refine MVAD sample (#16334)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidZeLiang authored Aug 6, 2021
1 parent 1a3420f commit b0c3a63
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Demonstrates how to train a model on multivariate data and use this model to detect anomalies.
*
* @summary detect multivaariate anomalies.
*/

import {
AnomalyDetectorClient,
AnomalyDetectorClientModelInfo,
DetectionRequest
} from "@azure/ai-anomaly-detector";
import { AzureKeyCredential } from "@azure/core-auth";

import * as fs from "fs";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

// You will need to set this environment variables or edit the following values
const apiKey = process.env["API_KEY"] || "";
const endpoint = process.env["ENDPOINT"] || "";
const dataSource = "<Your own data source>";


function sleep(time: number): Promise<NodeJS.Timer> {
return new Promise((resolve) => setTimeout(resolve, time));
}

export async function main() {

// create client
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));

// Already available models
const modelList = await client.listMultivariateModel();
console.log("The latest 5 available models (if exist):");
for (var i = 0; i < 5; i++) {
var modelDetail = (await modelList.next());
if (modelDetail.done) break;
console.log(modelDetail.value);
};

// construct model request (notice that the start and end time are local time and may not align with your data source)
const modelRequest: AnomalyDetectorClientModelInfo = {
source: dataSource,
startTime: new Date(2021, 0, 1, 0, 0, 0),
endTime: new Date(2021, 0, 2, 12, 0, 0),
slidingWindow: 200
};

// get train result
console.log("Training a new model(it may take a few minutes)...");
const trainResponse = await client.trainMultivariateModel(modelRequest);
const modelId = trainResponse.location?.split("/").pop() ?? "";
console.log("New model ID: " + modelId);

// get model status
var modelResponse = await client.getMultivariateModel(modelId);
var modelStatus = modelResponse.modelInfo?.status;

while (modelStatus != "READY" && modelStatus != "FAILED") {
await sleep(2000).then(() => { });
modelResponse = await client.getMultivariateModel(modelId);
modelStatus = modelResponse.modelInfo?.status;
};

if (modelStatus == "FAILED") {
console.log("Training failed.\nErrors:")
for (let error of modelResponse.modelInfo?.errors ?? []) {
console.log("Error code: " + error.code + ". Message: " + error.message);
};
return;
};

// if model status is "READY"
console.log("TRAINING FINISHED.");

// get result
console.log("Start detecting(it may take a few seconds)...");
const detectRequest: DetectionRequest = {
source: dataSource,
startTime: new Date(2021, 0, 2, 12, 0, 0),
endTime: new Date(2021, 0, 3, 0, 0, 0)
};
var resultHeader = await client.detectAnomaly(modelId, detectRequest);
var resultId = resultHeader.location?.split("/").pop() ?? "";
var result = await client.getDetectionResult(resultId);
var resultStatus = result.summary.status;

while (resultStatus != 'READY' && resultStatus != "FAILED") {
await sleep(1000).then(() => { });
result = await client.getDetectionResult(resultId);
resultStatus = result.summary.status;
};

if (resultStatus == "FAILED") {
console.log("Detection failed.")
console.log("Errors:")
for (let error of result.summary.errors ?? []) {
console.log("Error code: " + error.code + ". Message: " + error.message)
}
return;
};

// if result status is "READY"
console.log("Result status: " + resultStatus);
console.log("Result Id: " + result.resultId);

// export the model
var exportResult = await client.exportModel(modelId);
var modelPath = "model.zip"
var destination = fs.createWriteStream(modelPath);
exportResult.readableStreamBody?.pipe(destination);
console.log("New model has been exported to " + modelPath + ".");

// delete model
var deleteResult = await client.deleteMultivariateModel(modelId);

if (deleteResult._response.status == 204) {
console.log("New model has been deleted.")
}
else {
console.log("Failed to delete the new model.");
};
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ products:
urlFragment: ai-anomaly-detector-javascript
---

# Azure Data Tables client library samples for JavaScript
# Azure Anomaly Detector client library samples for JavaScript

These sample programs show how to use the JavaScript client libraries for Azure Data Tables in some common scenarios.
These sample programs show how to use the JavaScript client libraries for Azure Anomaly Detector in some common scenarios.

| **File Name** | **Description** |
| ----------------------------------------------------------------------------- | ------------------------------------------------- |
| [sample_detect_change_point.js][sample_detect_change_point] | detects change points. |
| [sample_detect_entire_series_anomaly.js][sample_detect_entire_series_anomaly] | detects anomaly points on entire series. |
| [sample_detect_last_point_anomaly.js][sample_detect_last_point_anomaly] | detects anomaly for the last point on the series. |
| [sample_multivariate_detection.js][sample_multivariate_detection] | detect multivaariate anomalies. |

## Prerequisites

The sample programs are compatible with Node.js >=12.0.0.
The sample programs are compatible with [LTS versions of Node.js](https://nodejs.org/about/releases/).

You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:

Expand Down Expand Up @@ -62,6 +63,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
[sample_detect_change_point]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_detect_change_point.js
[sample_detect_entire_series_anomaly]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_detect_entire_series_anomaly.js
[sample_detect_last_point_anomaly]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_detect_last_point_anomaly.js
[sample_multivariate_detection]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_multivariate_detection.js
[apiref]: https://docs.microsoft.com/javascript/api/@azure/ai-anomaly-detector
[freesub]: https://azure.microsoft.com/free/
[createinstance_azureanomalydetectorinstance]: https://docs.microsoft.com/azure/cognitive-services/anomaly-detector/quickstarts/client-libraries?tabs=windows&pivots=programming-language-javascript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,119 @@

/**
* Demonstrates how to train a model on multivariate data and use this model to detect anomalies.
*
* @summary detect multivaariate anomalies.
*/

const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector");
const { AzureKeyCredential } = require("@azure/core-auth");

const fs = require("fs");


// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();
// You will need to set this environment variables in .env file or edit the following values

// You will need to set this environment variables or edit the following values
const apiKey = process.env["API_KEY"] || "";
const endpoint = process.env["ENDPOINT"] || "";
const data_source = "<Your own data source>";

const dataSource = "<Your own data source>";

function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}

function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
async function main() {
// create client
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));

// Already available models
const modelList = await client.listMultivariateModel();
console.log("The latest 5 available models (if exist):");
for (var i = 0; i < 5; i++) {
var modelDetail = await modelList.next();
if (modelDetail.done) break;
console.log(modelDetail.value);
}

async function main() {

// create client
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));

// Already available models
const model_list = await client.listMultivariateModel();
console.log("The latest 5 available models (if exist):");
for(var i = 0 ; i < 5 ; i++) {
let model_detail = (await model_list.next());
if (model_detail.done == true) break
console.log(model_detail.value);
}
// construct model request (notice that the start and end time are local time and may not align with your data source)
const modelRequest = {
source: dataSource,
startTime: new Date(2021, 0, 1, 0, 0, 0),
endTime: new Date(2021, 0, 2, 12, 0, 0),
slidingWindow: 200
};

// construct model request (notice that the start and end time are local time and may not align with your data source)
const Modelrequest = {
source: data_source,
startTime: new Date(2021,0,1,0,0,0),
endTime: new Date(2021,0,2,12,0,0),
slidingWindow:200
};

// get train result
console.log("Training a new model...");
const train_response = await client.trainMultivariateModel(Modelrequest);
const model_id = train_response.location.split("/").pop();
console.log("New model ID: " + model_id);

// get model status
let model_response = await client.getMultivariateModel(model_id);
let model_status = model_response.modelInfo.status;

while (model_status != 'READY'){
await sleep(10000).then(() => {});
model_response = await client.getMultivariateModel(model_id);
model_status = model_response.modelInfo.status;
// get train result
console.log("Training a new model(it may take a few minutes)...");
const trainResponse = await client.trainMultivariateModel(modelRequest);
const modelId = trainResponse.location?.split("/").pop() ?? "";
console.log("New model ID: " + modelId);

// get model status
var modelResponse = await client.getMultivariateModel(modelId);
var modelStatus = modelResponse.modelInfo?.status;

while (modelStatus != "READY" && modelStatus != "FAILED") {
await sleep(2000).then(() => {});
modelResponse = await client.getMultivariateModel(modelId);
modelStatus = modelResponse.modelInfo?.status;
}
if (modelStatus == "FAILED") {
console.log("Training failed.\nErrors:");
for (let error of modelResponse.modelInfo?.errors ?? []) {
console.log("Error code: " + error.code + ". Message: " + error.message);
}
return;
}
// if model status is "READY"
console.log("TRAINING FINISHED.");

// get result
console.log("Start detecting(it may take a few seconds)...");
const detectRequest = {
source: dataSource,
startTime: new Date(2021, 0, 2, 12, 0, 0),
endTime: new Date(2021, 0, 3, 0, 0, 0)
};
var resultHeader = await client.detectAnomaly(modelId, detectRequest);
var resultId = resultHeader.location?.split("/").pop() ?? "";
var result = await client.getDetectionResult(resultId);
var resultStatus = result.summary.status;

console.log("TRAINING FINISHED.");

// get result
console.log("Start detecting...");
const detect_request = {
source: data_source,
startTime: new Date(2021,0,2,12,0,0),
endTime: new Date(2021,0,3,0,0,0)
};
const result_header = await client.detectAnomaly(model_id, detect_request);
const result_id = result_header.location?.split("/").pop() ?? "";
let result = await client.getDetectionResult(result_id);
let result_status = result.summary.status;

while (result_status != 'READY'){
await sleep(2000).then(() => {});
result = await client.getDetectionResult(result_id);
result_status = result.summary.status;
while (resultStatus != "READY" && resultStatus != "FAILED") {
await sleep(1000).then(() => {});
result = await client.getDetectionResult(resultId);
resultStatus = result.summary.status;
}
if (resultStatus == "FAILED") {
console.log("Detection failed.");
console.log("Errors:");
for (let error of result.summary.errors ?? []) {
console.log("Error code: " + error.code + ". Message: " + error.message);
}
return;
}
// if result status is "READY"
console.log("Result status: " + resultStatus);
console.log("Result Id: " + result.resultId);

console.log("Result status: " + result_status);
console.log("Result Id: " + result.resultId);

// export the model
const export_result = await client.exportModel(model_id);
const model_path = "model.zip"
const destination = fs.createWriteStream(model_path);
export_result.readableStreamBody?.pipe(destination);
console.log("New model has been exported to " + model_path + ".");

// delete model
let delete_result = client.deleteMultivariateModel(model_id);
if ((await delete_result)._response.status == "204")
console.log("New model has been deleted.");
else
console.log("Failed to delete the new model.");
// export the model
var exportResult = await client.exportModel(modelId);
var modelPath = "model.zip";
var destination = fs.createWriteStream(modelPath);
exportResult.readableStreamBody?.pipe(destination);
console.log("New model has been exported to " + modelPath + ".");

// delete model
var deleteResult = await client.deleteMultivariateModel(modelId);

if (deleteResult._response.status == 204) {
console.log("New model has been deleted.");
} else {
console.log("Failed to delete the new model.");
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Loading

0 comments on commit b0c3a63

Please sign in to comment.