-
Notifications
You must be signed in to change notification settings - Fork 31
/
Program.cs
417 lines (360 loc) · 19.1 KB
/
Program.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace TimeSeriesInsightsQuerySample
{
class Program
{
// For automated execution under application identity,
// use application created in Active Directory.
// To create the application in AAD, follow the steps provided here:
// https://docs.microsoft.com/en-us/azure/time-series-insights/time-series-insights-authentication-and-authorization
// SET the application ID of application registered in your Azure Active Directory
private static string ApplicationClientId = "#DUMMY#";
// SET the application key of the application registered in your Azure Active Directory
private static string ApplicationClientSecret = "#DUMMY#";
// SET the Azure Active Directory tenant.
private static string Tenant = "#DUMMY#.onmicrosoft.com";
public static async Task SampleAsync()
{
// Acquire an access token.
string accessToken = await AcquireAccessTokenAsync();
// Obtain list of environments and get environment FQDN for the environment of interest.
string environmentFqdn;
{
HttpWebRequest request = CreateHttpsWebRequest("api.timeseries.azure.com", "GET", "environments", accessToken);
JToken responseContent = await GetResponseAsync(request);
JArray environmentsList = (JArray)responseContent["environments"];
if (environmentsList.Count == 0)
{
// List of user environments is empty, fallback to sample environment.
environmentFqdn = "10000000-0000-0000-0000-100000000108.env.timeseries.azure.com";
}
else
{
// Assume the first environment is the environment of interest.
JObject firstEnvironment = (JObject)environmentsList[0];
environmentFqdn = firstEnvironment["environmentFqdn"].Value<string>();
}
}
Console.WriteLine("Using environment FQDN '{0}'", environmentFqdn);
Console.WriteLine();
// Obtain availability data for the environment and get availability range.
DateTime fromAvailabilityTimestamp;
DateTime toAvailabilityTimestamp;
{
HttpWebRequest request = CreateHttpsWebRequest(environmentFqdn, "GET", "availability", accessToken);
JToken responseContent = await GetResponseAsync(request);
JObject range = (JObject)responseContent["range"];
fromAvailabilityTimestamp = range["from"].Value<DateTime>();
toAvailabilityTimestamp = range["to"].Value<DateTime>();
}
Console.WriteLine(
"Obtained availability range [{0}, {1}]",
fromAvailabilityTimestamp,
toAvailabilityTimestamp);
Console.WriteLine();
// Assume data for the whole availablility range is requested.
DateTime from = fromAvailabilityTimestamp;
DateTime to = toAvailabilityTimestamp;
// Obtain metadata for the environment.
{
JObject inputPayload = new JObject(
new JProperty("searchSpan", new JObject(
new JProperty("from", from),
new JProperty("to", to))));
HttpWebRequest request = CreateHttpsWebRequest(environmentFqdn, "POST", "metadata", accessToken);
await WriteRequestStreamAsync(request, inputPayload);
JToken responseContent = await GetResponseAsync(request);
DumpMetadata(responseContent);
Console.WriteLine();
}
// Get events for the environment.
{
int requestedEventCount = 10;
JObject contentInputPayload = new JObject(
new JProperty("take", requestedEventCount),
new JProperty("searchSpan", new JObject(
new JProperty("from", from),
new JProperty("to", to))));
// Use HTTP request.
HttpWebRequest request = CreateHttpsWebRequest(environmentFqdn, "POST", "events", accessToken, new[] { "timeout=PT20S" });
await WriteRequestStreamAsync(request, contentInputPayload);
JToken responseContent = await GetResponseAsync(request);
Console.WriteLine("Requested {0} events via HTTP request.", requestedEventCount);
DumpEvents(responseContent);
Console.WriteLine();
// Use WebSocket request.
JObject inputPayload = new JObject(
// Send HTTP headers as a part of the message since .NET WebSocket does not support
// sending custom headers on HTTP GET upgrade request to WebSocket protocol request.
new JProperty("headers", new JObject(
new JProperty("x-ms-client-application-name", "TimeSeriesInsightsQuerySample"),
new JProperty("Authorization", "Bearer " + accessToken))),
new JProperty("content", contentInputPayload));
IReadOnlyList<JToken> responseMessagesContent = await ReadWebSocketResponseAsync(environmentFqdn, "events", inputPayload);
Console.WriteLine("Requested {0} events via WebSocket request.", requestedEventCount);
DumpEventsStreamed(responseMessagesContent);
Console.WriteLine();
}
// Get aggregates for the environment: group by Event Source Name and calculate number of events in each group.
{
JObject contentInputPayload = new JObject(
new JProperty("aggregates", new JArray(new JObject(
new JProperty("dimension", new JObject(
new JProperty("uniqueValues", new JObject(
new JProperty("input", new JObject(
new JProperty("builtInProperty", "$esn"))),
new JProperty("take", 1000))))),
new JProperty("measures", new JArray(new JObject(
new JProperty("count", new JObject()))))))),
new JProperty("searchSpan", new JObject(
new JProperty("from", from),
new JProperty("to", to))));
// Use HTTP request.
HttpWebRequest request = CreateHttpsWebRequest(environmentFqdn, "POST", "aggregates", accessToken, new[] { "timeout=PT20S" });
await WriteRequestStreamAsync(request, contentInputPayload);
JToken responseContent = await GetResponseAsync(request);
Console.WriteLine("Group by Event Source Name via HTTP request.");
DumpAggregates(responseContent);
Console.WriteLine();
// Use WebSocket request.
JObject inputPayload = new JObject(
// Send HTTP headers as a part of the message since .NET WebSocket does not support
// sending custom headers on HTTP GET upgrade request to WebSocket protocol request.
new JProperty("headers", new JObject(
new JProperty("x-ms-client-application-name", "TimeSeriesInsightsQuerySample"),
new JProperty("Authorization", "Bearer " + accessToken))),
new JProperty("content", contentInputPayload));
IReadOnlyList<JToken> responseMessagesContent = await ReadWebSocketResponseAsync(environmentFqdn, "aggregates", inputPayload);
Console.WriteLine("Group by Event Source Name via WebSocket request.");
DumpAggregatesStreamed(responseMessagesContent);
Console.WriteLine();
}
}
private static async Task<string> AcquireAccessTokenAsync()
{
if (AadClientApplicationId == "#PLACEHOLDER#" || AadScopes.Length == 0 || AadRedirectUri == "#PLACEHOLDER#" || AadTenantName.StartsWith("#PLACEHOLDER#"))
{
throw new Exception($"Use the link {"https://docs.microsoft.com/azure/time-series-insights/time-series-insights-get-started"} to update the values of 'AadClientApplicationId', 'AadScopes', 'AadRedirectUri', and 'AadAuthenticationAuthority'.");
}
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(AadClientApplicationId)
.WithRedirectUri(AadRedirectUri)
.WithAuthority(AadAuthenticationAuthority)
.Build();
AuthenticationResult result = await app
.AcquireTokenInteractive(AadScopes)
.ExecuteAsync();
// Show interactive logon dialog to acquire token on behalf of the user.
// Suitable for native apps, and not on server-side of a web application.
// AuthenticationResult result = await app
// .AcquireTokenInteractive(AadScopes)
// .ExecuteAsync();
//
// // Set redirect URI for Azure PowerShell
// redirectUri: new Uri("urn:ietf:wg:oauth:2.0:oob"),
// parameters: new PlatformParameters(PromptBehavior.Auto));
return result.AccessToken;
}
private static HttpWebRequest CreateHttpsWebRequest(string host, string method, string path, string accessToken, string[] queryArgs = null)
{
string query = "api-version=2016-12-12";
if (queryArgs != null && queryArgs.Any())
{
query += "&" + String.Join("&", queryArgs);
}
Uri uri = new UriBuilder("https", host)
{
Path = path,
Query = query
}.Uri;
HttpWebRequest request = WebRequest.CreateHttp(uri);
request.Method = method;
request.Headers.Add("x-ms-client-application-name", "TimeSeriesInsightsQuerySample");
request.Headers.Add("Authorization", "Bearer " + accessToken);
return request;
}
private static async Task WriteRequestStreamAsync(HttpWebRequest request, JObject inputPayload)
{
using (var stream = await request.GetRequestStreamAsync())
using (var streamWriter = new StreamWriter(stream))
{
await streamWriter.WriteAsync(inputPayload.ToString());
await streamWriter.FlushAsync();
streamWriter.Close();
}
}
private static async Task<JToken> GetResponseAsync(HttpWebRequest request)
{
using (WebResponse webResponse = await request.GetResponseAsync())
using (var sr = new StreamReader(webResponse.GetResponseStream()))
{
string result = await sr.ReadToEndAsync();
return JsonConvert.DeserializeObject<JToken>(result);
}
}
private static async Task<IReadOnlyList<JToken>> ReadWebSocketResponseAsync(string environmentFqdn, string path, JObject inputPayload)
{
var webSocket = new ClientWebSocket();
// Establish web socket connection.
Uri uri = new UriBuilder("wss", environmentFqdn)
{
Path = path,
Query = "api-version=2016-12-12"
}.Uri;
await webSocket.ConnectAsync(uri, CancellationToken.None);
// Send input payload.
byte[] inputPayloadBytes = Encoding.UTF8.GetBytes(inputPayload.ToString());
await webSocket.SendAsync(
new ArraySegment<byte>(inputPayloadBytes),
WebSocketMessageType.Text,
endOfMessage: true,
cancellationToken: CancellationToken.None);
// Read response messages from web socket.
List<JToken> responseMessagesContent = new List<JToken>();
using (webSocket)
{
while (true)
{
string message;
using (var ms = new MemoryStream())
{
// Write from socket to memory stream.
const int bufferSize = 16 * 1024;
var temporaryBuffer = new byte[bufferSize];
while (true)
{
WebSocketReceiveResult response = await webSocket.ReceiveAsync(
new ArraySegment<byte>(temporaryBuffer),
CancellationToken.None);
ms.Write(temporaryBuffer, 0, response.Count);
if (response.EndOfMessage)
{
break;
}
}
// Reset position to the beginning to allow reads.
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
message = sr.ReadToEnd();
}
}
JObject messageObj = JsonConvert.DeserializeObject<JObject>(message);
// Stop reading if error is emitted.
if (messageObj["error"] != null)
{
break;
}
// Actual response contents is wrapped into "content" object.
responseMessagesContent.Add(messageObj["content"]);
// Stop reading if 100% of completeness is reached.
if (messageObj["percentCompleted"] != null &&
Math.Abs((double)messageObj["percentCompleted"] - 100d) < 0.01)
{
break;
}
}
// Close web socket connection.
if (webSocket.State == WebSocketState.Open)
{
await webSocket.CloseAsync(
WebSocketCloseStatus.NormalClosure,
"CompletedByClient",
CancellationToken.None);
}
}
return responseMessagesContent;
}
private static void DumpMetadata(JToken responseContent)
{
// Response content has a list of properties under "properties" property.
JArray properties = (JArray)responseContent["properties"];
int propertiesCount = properties.Count;
Console.WriteLine("Acquired {0} properties:", propertiesCount);
for (int i = 0; i < propertiesCount; i++)
{
JObject currentProperty = (JObject)properties[i];
Console.WriteLine("'{0}': {1}", currentProperty["name"], currentProperty["type"]);
}
}
private static void DumpEvents(JToken responseContent)
{
// Response content has a list of events under "events" property for HTTP request.
JArray events = (JArray)responseContent["events"];
int eventCount = events.Count;
Console.WriteLine("Acquired {0} events:", eventCount);
for (int i = 0; i < eventCount; i++)
{
JObject currentEvent = (JObject)events[i];
JArray values = (JArray)currentEvent["values"];
Console.WriteLine("{{$ts: {0}, values: {1}}}", currentEvent["$ts"].ToString(), String.Join(",", values));
}
}
private static void DumpEventsStreamed(IReadOnlyList<JToken> responseMessagesContent)
{
// For events stream next message always contains additional events, i.e. new message is additive to the previous one.
// New message contains new events that were not in the previous message.
// The previous message should be kept and accumulated with the new message.
// Response content has a list of events under "events" property both for HTTP and WebSocket requests.
JArray events = new JArray(responseMessagesContent.SelectMany(m => (JArray)m["events"]));
int eventCount = events.Count;
Console.WriteLine("Acquired {0} events:", eventCount);
for (int i = 0; i < eventCount; i++)
{
JObject currentEvent = (JObject)events[i];
JArray values = (JArray)currentEvent["values"];
Console.WriteLine("{{$ts: {0}, values: {1}}}", currentEvent["$ts"].ToString(), String.Join(",", values));
}
}
private static void DumpAggregates(JToken responseContent)
{
// HTTP response content contains the list of aggregates under "aggregates" property.
JArray aggregates = (JArray)responseContent["aggregates"];
DumpAggregatesInternal(aggregates);
}
private static void DumpAggregatesStreamed(IReadOnlyList<JToken> responseMessagesContent)
{
// For aggregates stream next message always contains a replacement (snapshot) of all the values.
// Previous message can be discarded by the client.
// WebSocket response represents the list of aggregates.
JArray aggregates = (JArray)responseMessagesContent.Last();
DumpAggregatesInternal(aggregates);
}
private static void DumpAggregatesInternal(JArray aggregates)
{
// Number of items corresponds to number of aggregates in input payload.
// In this sample list of aggregates in input payload contains only 1 item since request contains 1 aggregate.
JObject aggregate = (JObject)aggregates[0];
JArray dimensionValues = (JArray)aggregate["dimension"];
JArray measures = (JArray)aggregate["measures"];
Debug.Assert(
dimensionValues.Count == measures.Count,
"Number of measures and dimensions should match.");
Console.WriteLine("Dimension Value\t\tCount");
for (int i = 0; i < dimensionValues.Count; i++)
{
string currentDimensionValue = (string)dimensionValues[i];
JArray currentMeasureValues = (JArray)measures[i];
double currentCount = (double)currentMeasureValues[0];
Console.WriteLine("{0}\t\t{1}", currentDimensionValue, currentCount);
}
}
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Task.Run(async () => await SampleAsync()).Wait();
}
}
}