| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.Serialization; |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.ApplicationInsights.Query.Models |
| | 6 | | { |
| | 7 | | public abstract class BaseMetricInfo |
| | 8 | | { |
| | 9 | | internal abstract IDictionary<string, object> GetAdditionalProperties(); |
| | 10 | |
|
| 0 | 11 | | public string MetricId { get; private set; } = null; |
| 0 | 12 | | public Dictionary<string, float> MetricValues { get; set; } = new Dictionary<string, float>(); |
| | 13 | |
|
| | 14 | | [OnDeserialized] |
| | 15 | | internal void InitMetricFields(StreamingContext context) |
| | 16 | | { |
| 0 | 17 | | var additionalProperties = GetAdditionalProperties(); |
| | 18 | |
|
| 0 | 19 | | if (additionalProperties != null) |
| | 20 | | { |
| 0 | 21 | | foreach (var additionalProp in additionalProperties) |
| | 22 | | { |
| 0 | 23 | | if (additionalProp.Value is object) |
| | 24 | | { |
| 0 | 25 | | var dict = additionalProp.Value as JObject; |
| 0 | 26 | | if (dict == null) continue; |
| | 27 | |
|
| 0 | 28 | | MetricId = additionalProp.Key; |
| | 29 | |
|
| 0 | 30 | | foreach (var prop in dict.Properties()) |
| | 31 | | { |
| 0 | 32 | | MetricValues.Add(prop.Name, prop.Value.Value<float>()); |
| | 33 | | } |
| | 34 | | } |
| | 35 | | } |
| | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private float? GetAggregatedValue(string aggregation) |
| | 40 | | { |
| 0 | 41 | | if (MetricValues.TryGetValue(aggregation, out var value)) |
| | 42 | | { |
| 0 | 43 | | return value; |
| | 44 | | } |
| | 45 | | else |
| | 46 | | { |
| 0 | 47 | | return null; |
| | 48 | | } |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public float? GetSum() |
| | 52 | | { |
| 0 | 53 | | return GetAggregatedValue("sum"); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public float? GetAverage() |
| | 57 | | { |
| 0 | 58 | | return GetAggregatedValue("avg"); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public float? GetMin() |
| | 62 | | { |
| 0 | 63 | | return GetAggregatedValue("min"); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public float? GetMax() |
| | 67 | | { |
| 0 | 68 | | return GetAggregatedValue("max"); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public int? GetCount() |
| | 72 | | { |
| 0 | 73 | | var count = GetAggregatedValue("count"); |
| 0 | 74 | | if (count != null) |
| | 75 | | { |
| 0 | 76 | | return (int) count; |
| | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 0 | 80 | | return null; |
| | 81 | | } |
| | 82 | | } |
| | 83 | | } |
| | 84 | | } |