Пример #1
0
        public MetricsEntryResult RunMetricsCheck(string text, string metricName)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text), "Text cannot be null");
            }

            if (metricName == null)
            {
                throw new ArgumentNullException(nameof(metricName), "Metric name cannot be null");
            }

            MetricCaller mc = _options.Value.Callers.Single(metric => metric.Name == metricName);

            if (mc == null)
            {
                throw new ArgumentNullException(nameof(metricName), $"There is no metric with such name: {metricName}");
            }

            MetricsEntryResult metricsEntryResult = null;

            using (IServiceScope scope = _scopeFactory.CreateScope())
            {
                metricsEntryResult = RunCheck(scope, mc, text);
            }

            if (metricsEntryResult == null)
            {
                throw new ArgumentNullException(nameof(metricsEntryResult), "Metric must return MetricEntryResult");
            }

            return(metricsEntryResult);
        }
Пример #2
0
        public MetricsEntryResult CheckMetric(string text)
        {
            MatchCollection matches = _regex.Matches(text);

            MetricsEntryResult metricsEntryResult = new MetricsEntryResult(MetricName, Description)
            {
                Value = matches.Count > 0 ? $"Found {matches.Count} exclamation sentences" : "No exclamation sentences found"
            };

            return(metricsEntryResult);
        }
Пример #3
0
        private MetricsEntryResult RunCheck(IServiceScope scope, MetricCaller caller, string text)
        {
            IMetric metric = caller.Factory(scope.ServiceProvider);

            if (text == null)
            {
                throw new ArgumentNullException($"{nameof(text)}", "Stream argument should be provided");
            }

            MetricsEntryResult result = metric.CheckMetric(text);

            return(result);
        }
Пример #4
0
        public MetricsEntryResult CheckMetric(string text)
        {
            var character = text
                            .GroupBy(x => x)
                            .Where(x => !string.IsNullOrWhiteSpace(x.Key.ToString()))
                            .Select(x => new { key = x.Key, value = x.Count() })
                            .OrderByDescending(x => x.value)
                            .First();

            MetricsEntryResult metricsEntryResult = new MetricsEntryResult(MetricName, Description)
            {
                Value = character == null ? "String migth be empty" :
                        $"The most frequent character is '{character.key}' and number of its occurences is {character.value}"
            };

            return(metricsEntryResult);
        }