Example #1
0
        /// <summary>Initializes a new instance of the <see cref="MetricInfo"/> class.</summary>
        /// <param name="metricAttributeType">
        /// Type of the attribute used for metric annotation.
        /// Should implement <see cref="IMetricAttribute{TMetricProvider}"/> or
        /// <see cref="IMetricAttribute{TMetricProvider, TUnitOfMeasurement}"/>;
        /// you can use <see cref="MetricAttributeBase"/> as a base implementation.
        /// </param>
        /// <returns>Composite range that describes measurement units</returns>
        internal MetricInfo([NotNull] Type metricAttributeType)
        {
            // Performs arg validation
            var metricMeta = MetricInfoHelpers.GetMetricInfoAttribute(metricAttributeType);

            if (typeof(CompetitionBenchmarkAttribute).IsAssignableFrom(metricAttributeType) &&
                metricAttributeType != typeof(CompetitionBenchmarkAttribute))
            {
                throw CodeExceptions.Argument(
                          nameof(metricAttributeType),
                          $"Attributes derived from {nameof(CompetitionBenchmarkAttribute)} are not supported");
            }

            var typeArgs = GetMetricAttributeTypeArgs(metricAttributeType);

            if (typeArgs == null)
            {
                throw CodeExceptions.Argument(
                          nameof(metricAttributeType),
                          $"The {metricAttributeType} should implement {typeof(IMetricAttribute<>)} interface.");
            }

            var displayName = metricMeta?.DisplayName;

            if (displayName.IsNullOrEmpty())
            {
                displayName = metricAttributeType.GetShortAttributeName();
            }
            DisplayName = displayName;

            AttributeType   = metricAttributeType;
            IsPrimaryMetric = AttributeType == typeof(CompetitionBenchmarkAttribute);

            ValuesProvider = (IMetricValuesProvider)Activator.CreateInstance(typeArgs[0]);
            var enumType = typeArgs.Length < 2 ? null : typeArgs[1];

            MetricUnits = MetricUnitScale.FromEnumValues(enumType);

            if (metricMeta != null)
            {
                Category        = metricMeta.Category;
                AnnotateInPlace = metricMeta.AnnotateInPlace;
                SingleValueMode = metricMeta.SingleValueMode;
                MetricColumns   = metricMeta.MetricColumns;
            }
        }
 /// <summary>Returns a <see cref="string"/> representation of a metric value.</summary>
 /// <param name="metricValues">Range of metric values.</param>
 /// <param name="metricUnitScale">The metric measurement scale.</param>
 /// <returns>A <see cref="string"/> that represents the metric value.</returns>
 public static string ToString(this MetricRange metricValues, [NotNull] MetricUnitScale metricUnitScale) =>
 ToString(metricValues, metricUnitScale[metricValues]);
 /// <summary>Returns a <see cref="string"/> representation of a metric value.</summary>
 /// <param name="metricValue">The metric value.</param>
 /// <param name="metricUnitScale">The metric measurement scale.</param>
 /// <returns>A <see cref="string"/> that represents the metric value.</returns>
 public static string ToString(this double metricValue, [NotNull] MetricUnitScale metricUnitScale) =>
 ToString(metricValue, metricUnitScale[metricValue]);