Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Adding end point support for Anomaly Detector service (#113)
Browse files Browse the repository at this point in the history
* Added location for Anomaly Detector service and a little refactor in SettingsHelper

* Update AnomalyDetectorHelper.cs

removed extra blank line

* Update AnomalyDetectorHelper.cs

Added spaces between words with +

* Update SettingsHelper.cs

removed extra blank line
  • Loading branch information
dizapico authored Oct 28, 2020
1 parent dc8983c commit 022c3c6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 69 deletions.
3 changes: 2 additions & 1 deletion Kiosk/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey;
CoreUtil.MinDetectableFaceCoveragePercentage = SettingsHelper.Instance.MinDetectableFaceCoveragePercentage;
AnomalyDetectorHelper.ApiKey = SettingsHelper.Instance.AnomalyDetectorApiKey;
AnomalyDetectorHelper.Endpoint = SettingsHelper.Instance.AnomalyDetectorKeyEndpoint;
ReceiptOCRHelper.ApiKey = SettingsHelper.Instance.FormRecognizerApiKey;
ReceiptOCRHelper.ApiEndpoint = SettingsHelper.Instance.FormRecognizerApiKeyEndpoint;
};
Expand Down Expand Up @@ -180,7 +181,7 @@ private static async void TestApiKeysAsync()
: Task.CompletedTask,

!string.IsNullOrEmpty(SettingsHelper.Instance.AnomalyDetectorApiKey)
? CognitiveServiceApiKeyTester.TestAnomalyDetectorApiKeyAsync(SettingsHelper.Instance.AnomalyDetectorApiKey)
? CognitiveServiceApiKeyTester.TestAnomalyDetectorApiKeyAsync(SettingsHelper.Instance.AnomalyDetectorApiKey, SettingsHelper.Instance.AnomalyDetectorKeyEndpoint)
: Task.CompletedTask,

!string.IsNullOrEmpty(SettingsHelper.Instance.FormRecognizerApiKey)
Expand Down
38 changes: 23 additions & 15 deletions Kiosk/CognitiveServiceApiKeyTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,38 @@ public static async Task TestTranslatorTextApiKeyAsync(string key)
var result = await service.DetectLanguageAsync(testQuery);
}

public static async Task TestAnomalyDetectorApiKeyAsync(string key)
public static async Task TestAnomalyDetectorApiKeyAsync(string key, string apiEndpoint)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Invalid API Key");
}
bool isUri = !string.IsNullOrEmpty(apiEndpoint) ? Uri.IsWellFormedUriString(apiEndpoint, UriKind.Absolute) : false;
if (!isUri)
{
throw new ArgumentException("Invalid URI");
}
else
{

string endpoint = "https://westus2.api.cognitive.microsoft.com/anomalydetector/v1.0/timeseries/last/detect";
string requestBody = "{\"series\":[{\"timestamp\":\"1972-01-01T00:00:00Z\",\"value\":826},{\"timestamp\":\"1972-02-01T00:00:00Z\",\"value\":799},{\"timestamp\":\"1972-03-01T00:00:00Z\",\"value\":890},{\"timestamp\":\"1972-04-01T00:00:00Z\",\"value\":900},{\"timestamp\":\"1972-05-01T00:00:00Z\",\"value\":961},{\"timestamp\":\"1972-06-01T00:00:00Z\",\"value\":935},{\"timestamp\":\"1972-07-01T00:00:00Z\",\"value\":894},{\"timestamp\":\"1972-08-01T00:00:00Z\",\"value\":855},{\"timestamp\":\"1972-09-01T00:00:00Z\",\"value\":809},{\"timestamp\":\"1972-10-01T00:00:00Z\",\"value\":810},{\"timestamp\":\"1972-11-01T00:00:00Z\",\"value\":766},{\"timestamp\":\"1972-12-01T00:00:00Z\",\"value\":805}],\"maxAnomalyRatio\":0.25,\"sensitivity\":95,\"granularity\":\"monthly\"}";
apiEndpoint += "/anomalydetector/v1.0/timeseries/last/detect";
string requestBody = "{\"series\":[{\"timestamp\":\"1972-01-01T00:00:00Z\",\"value\":826},{\"timestamp\":\"1972-02-01T00:00:00Z\",\"value\":799},{\"timestamp\":\"1972-03-01T00:00:00Z\",\"value\":890},{\"timestamp\":\"1972-04-01T00:00:00Z\",\"value\":900},{\"timestamp\":\"1972-05-01T00:00:00Z\",\"value\":961},{\"timestamp\":\"1972-06-01T00:00:00Z\",\"value\":935},{\"timestamp\":\"1972-07-01T00:00:00Z\",\"value\":894},{\"timestamp\":\"1972-08-01T00:00:00Z\",\"value\":855},{\"timestamp\":\"1972-09-01T00:00:00Z\",\"value\":809},{\"timestamp\":\"1972-10-01T00:00:00Z\",\"value\":810},{\"timestamp\":\"1972-11-01T00:00:00Z\",\"value\":766},{\"timestamp\":\"1972-12-01T00:00:00Z\",\"value\":805}],\"maxAnomalyRatio\":0.25,\"sensitivity\":95,\"granularity\":\"monthly\"}";

string response = string.Empty;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);
HttpResponseMessage result = await RequestAndAutoRetryWhenThrottled(() => client.PostAsync(endpoint, new StringContent(requestBody, Encoding.UTF8, "application/json")));
string response = string.Empty;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);
HttpResponseMessage result = await RequestAndAutoRetryWhenThrottled(() => client.PostAsync(apiEndpoint, new StringContent(requestBody, Encoding.UTF8, "application/json")));

result.EnsureSuccessStatusCode();
response = await result.Content.ReadAsStringAsync();
}
result.EnsureSuccessStatusCode();
response = await result.Content.ReadAsStringAsync();
}

dynamic data = JObject.Parse(response);
if (data.isAnomaly == null)
{
throw new Exception("Response data is missing");
dynamic data = JObject.Parse(response);
if (data.isAnomaly == null)
{
throw new Exception("Response data is missing");
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions Kiosk/ServiceHelpers/AnomalyDetectorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ namespace ServiceHelpers
public class AnomalyDetectorHelper : ServiceBase
{
private static readonly string HEADER_SUB_KEY = "Ocp-Apim-Subscription-Key";
private static readonly Uri BATCH_SERVICE_URL = new Uri("https://westus2.api.cognitive.microsoft.com/anomalydetector/v1.0/timeseries/entire/detect");
private static readonly Uri STREAMING_SERVICE_URL = new Uri("https://westus2.api.cognitive.microsoft.com/anomalydetector/v1.0/timeseries/last/detect");
private static readonly string SERVICE_URL_FORMAT = "{0}/anomalydetector/v1.0";
private static readonly string BATCH_SERVICE_URL = "/timeseries/entire/detect";
private static readonly string STREAMING_SERVICE_URL = "/timeseries/last/detect";

private static IDictionary<string, string> defaultRequestHeaders;

Expand All @@ -62,6 +63,17 @@ public static string ApiKey
}
}

private static string endpoint = string.Empty;

public static string Endpoint
{
get { return endpoint; }
set
{
endpoint = string.Format(SERVICE_URL_FORMAT, value) ;
}
}

static AnomalyDetectorHelper()
{
InitializeService();
Expand All @@ -77,12 +89,12 @@ private static void InitializeService()

public static async Task<AnomalyLastDetectResult> GetStreamingDetectionResult(AnomalyDetectionRequest dataRequest)
{
return await HttpClientUtility.PostAsJsonAsync<AnomalyLastDetectResult>(STREAMING_SERVICE_URL, defaultRequestHeaders, dataRequest);
return await HttpClientUtility.PostAsJsonAsync<AnomalyLastDetectResult>(new Uri(endpoint + STREAMING_SERVICE_URL), defaultRequestHeaders, dataRequest);
}

public static async Task<AnomalyEntireDetectResult> GetBatchDetectionResult(AnomalyDetectionRequest dataRequest)
{
return await HttpClientUtility.PostAsJsonAsync<AnomalyEntireDetectResult>(BATCH_SERVICE_URL, defaultRequestHeaders, dataRequest);
return await HttpClientUtility.PostAsJsonAsync<AnomalyEntireDetectResult>(new Uri(endpoint + BATCH_SERVICE_URL), defaultRequestHeaders, dataRequest);
}
}
}
Loading

0 comments on commit 022c3c6

Please sign in to comment.