diff --git a/sdk/anomalydetector/ai-anomaly-detector/samples-dev/sample_multivariate_detection.ts b/sdk/anomalydetector/ai-anomaly-detector/samples-dev/sample_multivariate_detection.ts new file mode 100644 index 000000000000..1f641bea3001 --- /dev/null +++ b/sdk/anomalydetector/ai-anomaly-detector/samples-dev/sample_multivariate_detection.ts @@ -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 = ""; + + +function sleep(time: number): Promise { + 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); +}) diff --git a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/README.md b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/README.md index 0ff409e74ec0..5526e56b26de 100644 --- a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/README.md +++ b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/README.md @@ -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: @@ -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 diff --git a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_multivariate_detection.js b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_multivariate_detection.js index 10978fed14d6..19589cc3e784 100644 --- a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_multivariate_detection.js +++ b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/javascript/sample_multivariate_detection.js @@ -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 = ""; - +const dataSource = ""; +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); - }); - \ No newline at end of file +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/README.md b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/README.md index 5a3e716f4cd1..01bce55775e7 100644 --- a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/README.md +++ b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/README.md @@ -9,19 +9,20 @@ products: urlFragment: ai-anomaly-detector-typescript --- -# Azure Data Tables client library samples for TypeScript +# Azure Anomaly Detector client library samples for TypeScript -These sample programs show how to use the TypeScript client libraries for Azure Data Tables in some common scenarios. +These sample programs show how to use the TypeScript client libraries for Azure Anomaly Detector in some common scenarios. | **File Name** | **Description** | | ----------------------------------------------------------------------------- | ------------------------------------------------- | | [sample_detect_change_point.ts][sample_detect_change_point] | detects change points. | | [sample_detect_entire_series_anomaly.ts][sample_detect_entire_series_anomaly] | detects anomaly points on entire series. | | [sample_detect_last_point_anomaly.ts][sample_detect_last_point_anomaly] | detects anomaly for the last point on the series. | +| [sample_multivariate_detection.ts][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/). Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. Install the TypeScript compiler using: @@ -58,7 +59,7 @@ npm run build 4. Run whichever samples you like (note that some samples may require additional setup, see the table above): ```bash -node dist/sample_detect_change_point.ts +node dist/sample_detect_change_point.js ``` Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): @@ -74,6 +75,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/typescript/src/sample_detect_change_point.ts [sample_detect_entire_series_anomaly]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_detect_entire_series_anomaly.ts [sample_detect_last_point_anomaly]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_detect_last_point_anomaly.ts +[sample_multivariate_detection]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_multivariate_detection.ts [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 diff --git a/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_multivariate_detection.ts b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_multivariate_detection.ts new file mode 100644 index 000000000000..1f641bea3001 --- /dev/null +++ b/sdk/anomalydetector/ai-anomaly-detector/samples/v3/typescript/src/sample_multivariate_detection.ts @@ -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 = ""; + + +function sleep(time: number): Promise { + 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); +})