-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUsage.cs
201 lines (179 loc) · 7.09 KB
/
Usage.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
/********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web.Helpers;
namespace Microsoft.Partner.CSP.Api.V1.Samples
{
class Usage
{
/// <summary>
/// This method gets a collection resource containing all the entitlements that belong to a given customer
/// </summary>
/// <param name="customerCid">cid of the customer</param>
/// <param name="customerAuthorizationToken">customer authorization token</param>
/// <returns>object that contains all the entitlements</returns>
public static dynamic GetEntitlements(string customerCid, string customerAuthorizationToken)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/Entitlements ", customerCid));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + customerAuthorizationToken);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
var entitlements = Json.Decode(responseContent);
return entitlements;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method gets a customer's Azure usage information between the given dates.
/// </summary>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="entitlementId">Specifies the entitlement for which the caller is requesting the usage</param>
/// <param name="sa_Token">sales agent token</param>
/// <param name="startTime">Specified the start time range of when the usage data was metered within the billing system</param>
/// <param name="endTime">Specifies the end time range of when the usage data was metered within the billing system.</param>
/// <returns>object containing usage records of the customer</returns>
public static DataModels.UsageResponse GetUsageRecords(string resellerCid, string entitlementId, string sa_Token, string startTime, string endTime)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/usage-records?entitlement_id={1}&reported_start_time={2}&reported_end_time={3}", resellerCid, entitlementId, startTime, endTime));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
DataModels.UsageResponse usageRecordsPayload = new DataModels.UsageResponse();
try
{
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
var usageRecords = Json.Decode(responseContent);
usageRecordsPayload = JsonConvert.DeserializeObject<DataModels.UsageResponse>(responseContent);
//// Prints the data to console only if the usageRecord is NOT empty
//if (usageRecords.items.Length > 0)
//{
// Utilities.PrintWebRequest(request, string.Empty);
// Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
//}
return usageRecordsPayload;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return usageRecordsPayload;
}
/// <summary>
/// Compute rated usage per meter
/// </summary>
/// <param name="rates">rate</param>
/// <param name="usage">usage</param>
/// <returns>total cost for meter</returns>
public static double computeRatedUsagePerMeter(Dictionary<double, double> rates, double usage)
{
double total = 0.0;
if (rates.Count == 0)
return 0.0;
else if (rates.Count == 1)
return (usage * rates.Values.FirstOrDefault());
var remainingUsage = usage;
while (rates.Count > 0)
{
//double currentValue=rates.GetEnumerator().Current.Key;
double LastKey = rates.Keys.Last();
if (remainingUsage > LastKey)
{
double LastKeyValue = 0.0;
if (rates.TryGetValue(LastKey, out LastKeyValue))
{
total = total + ((remainingUsage - LastKey + 1) * LastKeyValue); // remainingUsage - LastKey +1 because tiered pricing is exclusive
remainingUsage = LastKey - 1;
}
rates.Remove(LastKey);
}
else if (remainingUsage <= LastKey)
{
rates.Remove(LastKey);
}
}
return total;
}
/// <summary>
///
/// </summary>
/// <param name="rates"></param>
/// <param name="meterId"></param>
/// <returns>Rates for meter</returns>
public static Dictionary<double, double> GetRatesForMeterID(DataModels.RateCardResponse rates, string meterId)
{
List<DataModels.Resource> resources = rates.Meters;
DataModels.Resource r = resources.Find(x => x.MeterId == meterId);
return r != null ? r.MeterRates : new Dictionary<double, double>();
}
/// <summary>
/// GetRatableUsage
/// </summary>
/// <param name="usage"></param>
/// <param name="includedQuantity"></param>
/// <returns></returns>
public static double GetRatableUsage(double usage, double includedQuantity)
{
if ((usage - includedQuantity) > 0)
{
return (usage - includedQuantity);
}
else
{
return 0.0;
}
}
/// <summary>
/// GetIncludedQuantityByMeterID
/// </summary>
/// <param name="rates"></param>
/// <param name="meterId"></param>
/// <returns></returns>
public static double GetIncludedQuantityByMeterID(DataModels.RateCardResponse rates, string meterId)
{
List<DataModels.Resource> resources = rates.Meters;
IEnumerable<double> includedQty = from r in resources
where r.MeterId == meterId
select r.IncludedQuantity;
return includedQty.FirstOrDefault();
}
}
}