Skip to content

Commit

Permalink
fixing merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
aahill committed Oct 14, 2019
2 parents 5bd7cbe + 59bb8c3 commit 9da1fa4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
19 changes: 12 additions & 7 deletions quickstarts/csharp-detect-anomalies.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// <usingStatements>
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
// </usingStatements>

namespace Console
{
class Program
{

// <vars>
// Replace the subscriptionKey string with your valid subscription key.
static readonly string subscriptionKey = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
static readonly string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");

// Replace the dataPath string with a path to the JSON formatted time series data.
const string dataPath = "[PATH_TO_TIME_SERIES_DATA]";

// Urls for anomaly detection on:
// A batch of data points, or
// The latest data point in the time series
const string latestPointDetectionUrl = "/anomalydetector/v1.0/timeseries/last/detect";
const string batchDetectionUrl = "/anomalydetector/v1.0/timeseries/entire/detect";
// </vars>


// <main>
static void Main(string[] args)
{
//read in the JSON time series data for the API request
Expand All @@ -37,7 +37,8 @@ static void Main(string[] args)
System.Console.WriteLine("\nPress any key to exit ");
System.Console.ReadKey();
}

// </main>
// <detectAnomaliesBatch>
static void detectAnomaliesBatch(string requestData)
{
System.Console.WriteLine("Detecting anomalies as a batch");
Expand Down Expand Up @@ -71,7 +72,8 @@ static void detectAnomaliesBatch(string requestData)
}
}
}

// </detectAnomaliesBatch>
// <detectAnomaliesLatest>
static void detectAnomaliesLatest(string requestData)
{
System.Console.WriteLine("\n\nDetermining if latest data point is an anomaly");
Expand All @@ -86,6 +88,7 @@ static void detectAnomaliesLatest(string requestData)
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
}
// </detectAnomaliesLatest>

/// <summary>
/// Sends a request to the Anomaly Detection API to detect anomaly points
Expand All @@ -95,6 +98,7 @@ static void detectAnomaliesLatest(string requestData)
/// <param name="subscriptionKey">The subscription key applied </param>
/// <param name="requestData">The JSON string for requet data points</param>
/// <returns>The JSON string for anomaly points and expected values.</returns>
// <requestMethod>
static async Task<string> Request(string apiAddress, string endpoint, string subscriptionKey, string requestData)
{
using (HttpClient client = new HttpClient { BaseAddress = new Uri(apiAddress) })
Expand All @@ -108,5 +112,6 @@ static async Task<string> Request(string apiAddress, string endpoint, string sub
return await res.Content.ReadAsStringAsync();
}
}
// </requestMethod>
}
}
28 changes: 15 additions & 13 deletions quickstarts/java-detect-anomalies.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <imports>
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -14,15 +14,13 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
// </imports>

public class JavaDetect {

// Replace the subscriptionKey string value with your valid subscription key.
static final String subscriptionKey = "[YOUR_SUBSCRIPTION_KEY]";

//replace the endpoint URL with the correct one for your subscription. Your endpoint can be found in the Azure portal.
//for example: https://westus2.api.cognitive.microsoft.com
static final String endpoint = "[YOUR_ENDPOINT_URL]";
// <vars>
// This sample assumes you have created an environment variable for your key and endpoint
static final String subscriptionKey = System.getenv("ANOMALY_DETECTOR_KEY");
static final String endpoint = System.getenv("ANOMALY_DETECTOR_ENDPOINT");

// Replace the dataPath string with a path to the JSON formatted time series data.
static final String dataPath = "[PATH_TO_TIME_SERIES_DATA]";
Expand All @@ -32,16 +30,17 @@ public class JavaDetect {
// The latest data point in the time series
static final String latestPointDetectionUrl = "/anomalydetector/v1.0/timeseries/last/detect";
static final String batchDetectionUrl = "/anomalydetector/v1.0/timeseries/entire/detect";


// </vars>
// <main>
public static void main(String[] args) throws Exception {

String requestData = new String(Files.readAllBytes(Paths.get(dataPath)), "utf-8");

detectAnomaliesBatch(requestData);
detectAnomaliesLatest(requestData);
}

// </main>
// <detectBatch>
static void detectAnomaliesBatch(String requestData) {
System.out.println("Detecting anomalies as a batch");

Expand All @@ -63,13 +62,15 @@ static void detectAnomaliesBatch(String requestData) {
}
}
}

// </detectBatch>
// <detectLatest>
static void detectAnomaliesLatest(String requestData) {
System.out.println("Determining if latest data point is an anomaly");
String result = sendRequest(latestPointDetectionUrl, endpoint, subscriptionKey, requestData);
System.out.println(result);
}

// </detectLatest>
// <request>
static String sendRequest(String apiAddress, String endpoint, String subscriptionKey, String requestData) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost request = new HttpPost(endpoint + apiAddress);
Expand All @@ -91,4 +92,5 @@ static String sendRequest(String apiAddress, String endpoint, String subscriptio
}
return null;
}
// <request>
}
35 changes: 16 additions & 19 deletions quickstarts/python-detect-anomalies.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# <imports>
import requests
import json

# </imports>
# <vars>
# URLs for anomaly detection with the Anomaly Detector API
batch_detection_url = "/anomalydetector/v1.0/timeseries/entire/detect"
latest_point_detection_url = "/anomalydetector/v1.0/timeseries/last/detect"

###############################################
#### Update or verify the following values. ###
###############################################

# Replace the endpoint URL with the correct one for your subscription. Your endpoint can be found in the Azure portal.
# for example: https://westus2.api.cognitive.microsoft.com
endpoint = "[YOUR_ENDPOINT_URL]"

# Replace with your valid subscription key.
subscription_key = "[YOUR_SUBSCRIPTION_KEY]"
# This sample assumes you have created an environment variable for your key and endpoint
endpoint = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
subscription_key = os.environ["ANOMALY_DETECTOR_KEY"]

# Replace with a path to the JSON formatted time series data.
data_location = "[PATH_TO_TIME_SERIES_DATA]"

###############################################
# </vars>

"""
Sends an anomaly detection request to the Anomaly Detector API.
If the request is successful, the JSON response is returned.
"""
# <request>
def send_request(endpoint, url, subscription_key, request_data):
headers = {'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key}
response = requests.post(endpoint+url, data=json.dumps(request_data), headers=headers)
return json.loads(response.content.decode("utf-8"))

# </request>
"""
Detect anomalies throughout the time series data by submitting it as a batch to the API.
"""
# <detectBatch>
def detect_batch(request_data):
print("Detecting anomalies as a batch")
# Send the request, and print the JSON result
Expand All @@ -52,21 +47,23 @@ def detect_batch(request_data):
for x in range(len(anomalies)):
if anomalies[x] == True:
print (x)

# </detectBatch>
"""
Detect if the latest data point in the time series is an anomaly.
"""
# <detectLatest>
def detect_latest(request_data):
print("Determining if latest data point is an anomaly")
# send the request, and print the JSON result
result = send_request(endpoint, latest_point_detection_url, subscription_key, request_data)
print(json.dumps(result, indent=4))

# </detectLatest>

# read json time series data from file
# <fileLoad>
file_handler = open(data_location)
json_data = json.load(file_handler)

# send the request
detect_batch(json_data)
detect_latest(json_data)
# </fileLoad>

2 changes: 1 addition & 1 deletion quickstarts/sdk/csharp-sdk-sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Program{
// <mainMethod>
static void Main(string[] args){
//This sample assumes you have created an environment variable for your key and endpoint
string endpoint = Environment.GetEnvironmentVariable(ANOMALY_DETECTOR_ENDPOINT);
string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
string datapath = "request-data.csv";

Expand Down

0 comments on commit 9da1fa4

Please sign in to comment.