-
Notifications
You must be signed in to change notification settings - Fork 5
/
AdlsInsightClient.cs
65 lines (55 loc) · 2.84 KB
/
AdlsInsightClient.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace AiDataLakeDevOpsDashboard.Clients
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class AdlsInsightClient
{
private string dataAtRestQueryUrlFormat =
@"https://{0}.azuredatalakestore.net/StorageInsight/CustomerDataAtRest?starttime={1}&endtime={2}&api-version=2016-11-01";
public async Task<long> GetDataAtRestInBytes(string accountName, string authToken)
{
var startDateTime = DateTime.Now.AddHours(-48);
var endDateTime = DateTime.Now;
var result = 0L;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
var requestUrl = string.Format(dataAtRestQueryUrlFormat, accountName,
Convert.ToString(startDateTime, CultureInfo.InvariantCulture), Convert.ToString(endDateTime, CultureInfo.InvariantCulture));
var response = await client.GetAsync(requestUrl);
if (!response.IsSuccessStatusCode)
{
Trace.TraceError("HTTP call failed. Response: {0}; response body: {1}.", response.ToString(),
response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty);
throw new Exception(string.Format("Failed to get response from {0}; HTTP status code: {1}",
dataAtRestQueryUrlFormat, response.StatusCode));
}
var responseList = JsonConvert.DeserializeObject<List<ExpandoObject>>(await response.Content.ReadAsStringAsync());
IDictionary<string, object> latestEntry = null;
foreach (var entry in responseList)
{
if (latestEntry == null)
{
latestEntry = entry as IDictionary<string, object>;
}
else if (DateTime.Parse(latestEntry["utcDate"].ToString(), CultureInfo.InvariantCulture)
< DateTime.Parse((entry as IDictionary<string, object>)["utcDate"].ToString(), CultureInfo.InvariantCulture))
{
latestEntry = entry;
}
}
result = latestEntry != null ? Convert.ToInt64(latestEntry["totalBytes"]) : 0L;
}
return result;
}
}
}