/// <summary>
        /// Initializes an new instance of the PSMetricDefinitionNoDetails class
        /// </summary>
        /// <param name="metricDefinition">The MetricDefinition</param>
        public PSMetricDefinitionNoDetails(MetricDefinition metricDefinition)
        {
            // Keep the original value (localized string, Dictionary, List) in the base
            base.Name = metricDefinition.Name;

            this.Dimensions = metricDefinition.Dimensions;
            this.MetricAvailabilities = metricDefinition.MetricAvailabilities;
            this.Name = metricDefinition.Name != null ? metricDefinition.Name.Value : null;
            this.PrimaryAggregationType = metricDefinition.PrimaryAggregationType;
            this.Properties = metricDefinition.Properties;
            this.ResourceUri = metricDefinition.ResourceUri;
            this.Unit = metricDefinition.Unit;
        }
        /// <summary>
        /// Initializes an new instance of the PSMetricDefinitionNoDetails class
        /// </summary>
        /// <param name="metricDefinition">The MetricDefinition</param>
        public PSMetricDefinitionNoDetails(MetricDefinition metricDefinition)
        {
            // Keep the original value (localized string, Dictionary, List) in the base
            base.Name = metricDefinition.Name;

            // Because of the above protected internals these two properties won't show in the output, but they will be there for the base class
            this.Dimensions = metricDefinition.Dimensions;
            this.MetricAvailabilities = metricDefinition.MetricAvailabilities;

            this.Name = metricDefinition.Name != null ? metricDefinition.Name.Value : null;
            this.PrimaryAggregationType = metricDefinition.PrimaryAggregationType;
            this.Properties = metricDefinition.Properties;
            this.ResourceId = metricDefinition.ResourceId;
            this.Unit = metricDefinition.Unit;
        }
        /// <summary>
        /// Initializes an new instance of the PSMetricDefinition class
        /// </summary>
        /// <param name="metricDefinition">The MetricDefinition</param>
        public PSMetricDefinition(MetricDefinition metricDefinition)
        {
            // Keep the original value (localized string, Dictionary, List) in the base
            base.Dimensions = metricDefinition.Dimensions;
            base.MetricAvailabilities = metricDefinition.MetricAvailabilities;
            base.Name = metricDefinition.Name;
            base.Properties = metricDefinition.Properties;

            this.MetricAvailabilities = new PSAvailabilityCollection(metricDefinition.MetricAvailabilities);
            this.Name = metricDefinition.Name.ToString(localizedValue: false);
            this.PrimaryAggregationType = metricDefinition.PrimaryAggregationType;
            this.Properties = new PSDictionaryElement(metricDefinition.Properties);
            this.ResourceId = metricDefinition.ResourceId;
            this.Unit = metricDefinition.Unit;
        }
 private static bool IsMetricDefinitionIncluded(MetricFilter filter, MetricDefinition metricDefinition)
 {
     return filter == null || filter.DimensionFilters.Any(x => string.Equals(metricDefinition.Name.Value, x.Name, StringComparison.Ordinal));
 }
        private MetricDefinitionCollection GetMetricDefinitionCollection(string resourceUri)
        {
            var metriAvailabilities = new List<MetricAvailability>()
            {
                new MetricAvailability
                {
                    Location = new MetricLocation
                    {
                        PartitionKey = "p1",
                        TableEndpoint = "http://sa.table.core.windows.net/",
                        TableInfo = new List<MetricTableInfo>
                        {
                            new MetricTableInfo
                            {
                                EndTime = DateTime.Parse("2014-08-20T12:00:10Z"),
                                StartTime = DateTime.Parse("2014-08-19T12:00:00Z"),
                                SasToken = Guid.NewGuid().ToString("N"),
                                SasTokenExpirationTime = DateTime.Parse("2014-08-20T12:00:10Z"),
                                TableName = "datatable"
                            }
                        }
                    },
                    Retention = TimeSpan.FromDays(30),
                    TimeGrain = TimeSpan.FromMinutes(10)
                }
            };

            MetricDefinition[] metricDefinitions = new MetricDefinition[1];
            metricDefinitions[0] = new MetricDefinition
            {
                MetricAvailabilities = metriAvailabilities,
                Name = new LocalizableString() { LocalizedValue = "CPU Percentage", Value = "CpuPercentage" },
                PrimaryAggregationType = AggregationType.Average,
                Properties = new Dictionary<string, string>()
                {
                    {"prop1", "val1" }
                },
                ResourceUri = resourceUri,
                Unit = Unit.Bytes
            };
            return new MetricDefinitionCollection
            {
                Value = metricDefinitions
            };
        }
        private static void AreEqual(MetricDefinition exp, MetricDefinition act)
        {
            if (exp != null)
            {
                AreEqual(exp.Name, act.Name);
                Assert.Equal(exp.ResourceUri, act.ResourceUri);
                Assert.Equal(exp.Unit, act.Unit);
                AreEqual(exp.Properties, act.Properties);

                if (exp.MetricAvailabilities != null)
                {
                    for (int i = 0; i < exp.MetricAvailabilities.Count; i++)
                    {
                        AreEqual(exp.MetricAvailabilities[i], act.MetricAvailabilities[i]);
                    }
                }
            }
        }
        private static bool IsBlobSasMetric(MetricDefinition definition, TimeSpan timeGrain)
        {
            MetricAvailability availability = definition.MetricAvailabilities.FirstOrDefault(a => a.TimeGrain.Equals(timeGrain));

            // Definition has requested availability, Location is null (non-SAS) or contains SAS key
            if (availability != null)
            {
                return availability.BlobLocation != null;
            }

            // Definition has availabilities, but none with the requested timegrain (Bad request)
            if (definition.MetricAvailabilities.Any())
            {
                throw new InvalidOperationException(string.Format(
                    CultureInfo.InvariantCulture,
                    "MetricDefinition for {0} does not contain an availability with timegrain {1}",
                    definition.Name.Value, timeGrain));
            }

            // Definition has no availablilities (metrics are not configured for this resource), return empty metrics (non-SAS)
            return false;
        }
        private bool SupportsRequestedDimensions(MetricDefinition definition, MetricFilter filter)
        {
            MetricDimension metric = filter.DimensionFilters.FirstOrDefault(df => string.Equals(df.Name, definition.Name.Value, StringComparison.OrdinalIgnoreCase));
            var supportedDimensionNames = definition.Dimensions.Select(dim => dim.Name);
            var supportedDimensionValues = definition.Dimensions.ToDictionary(dim => dim.Name.Value, dim => dim.Values.Select(v => v.Value));

            // No dimensions specified for this metric
            if (metric == null || metric.Dimensions == null)
            {
                return true;
            }

            foreach (MetricFilterDimension dimension in metric.Dimensions)
            {
                // find dimension in definition
                Dimension d = definition.Dimensions.FirstOrDefault(dim => string.Equals(dim.Name.Value, dimension.Name));

                // Dimension name does't show up in definition
                if (d == null)
                {
                    return false;
                }

                // Requested dimension has any value that don't show up in the values list for the definiton
                if (dimension.Values.Any(value => !d.Values.Select(v => v.Value).Contains(value, StringComparer.OrdinalIgnoreCase)))
                {
                    return false;
                }
            }

            return true;
        }