-
Notifications
You must be signed in to change notification settings - Fork 167
/
csharp-detect-anomalies.cs
153 lines (139 loc) · 6.13 KB
/
csharp-detect-anomalies.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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>
//This sample assumes you have created an environment variable for your key and endpoint
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]";
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
var requestData = File.ReadAllText(dataPath);
detectAnomaliesBatch(requestData);
detectAnomaliesLatest(requestData);
detectChangePoints(requestData);
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");
//construct the request
var result = Request(
endpoint,
batchDetectionUrl,
subscriptionKey,
requestData).Result;
//deserialize the JSON object, and display it
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
if (jsonObj["code"] != null)
{
System.Console.WriteLine($"Detection failed. ErrorCode:{jsonObj["code"]}, ErrorMessage:{jsonObj["message"]}");
}
else
{
//Find and display the positions of anomalies in the data set
bool[] anomalies = jsonObj["isAnomaly"].ToObject<bool[]>();
System.Console.WriteLine("\nAnomalies detected in the following data positions:");
for (var i = 0; i < anomalies.Length; i++)
{
if (anomalies[i])
{
System.Console.Write(i + ", ");
}
}
}
}
// </detectAnomaliesBatch>
// <detectAnomaliesLatest>
static void detectAnomaliesLatest(string requestData)
{
System.Console.WriteLine("\n\nDetermining if latest data point is an anomaly");
//construct the request
var result = Request(
endpoint,
latestPointDetectionUrl,
subscriptionKey,
requestData).Result;
//deserialize the JSON object, and display it
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
}
// </detectAnomaliesLatest>
// <detectChangePoints>
static void detectChangePoints(string requestData)
{
System.Console.WriteLine("\n\nDetecting change points in the series.");
//construct the request
var result = Request(
endpoint,
changePointDetectionUrl,
subscriptionKey,
requestData).Result;
//deserialize the JSON object, and display it
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
if (jsonObj["code"] != null)
{
System.Console.WriteLine($"Detection failed. ErrorCode:{jsonObj["code"]}, ErrorMessage:{jsonObj["message"]}");
}
else
{
//Find and display the positions of anomalies in the data set
bool[] anomalies = jsonObj["isChangePoint"].ToObject<bool[]>();
System.Console.WriteLine("\Change points detected in the following data positions:");
for (var i = 0; i < anomalies.Length; i++)
{
if (anomalies[i])
{
System.Console.Write(i + ", ");
}
}
}
}
// </detectChangePoints>
/// <summary>
/// Sends a request to the Anomaly Detection API to detect anomaly points
/// </summary>
/// <param name="apiAddress">Address of the API.</param>
/// <param name="endpoint">The endpoint of the API</param>
/// <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) })
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
var res = await client.PostAsync(endpoint, content);
return await res.Content.ReadAsStringAsync();
}
}
// </requestMethod>
}
}