Exemplo n.º 1
0
        public IDimensionKey GetOrCreate(string name)
        {
            IDimensionKey dimensionKey;

            if (_dimensionKeys.TryGetValue(name, out IDimensionKey found))
            {
                dimensionKey = found;
            }
            else
            {
                lock (SyncLock)
                {
                    if (_dimensionKeys.TryGetValue(name, out IDimensionKey found2))
                    {
                        dimensionKey = found2;
                    }
                    else
                    {
                        dimensionKey         = new DimensionKey(name);
                        _dimensionKeys[name] = dimensionKey;
                    }
                }
            }

            return(dimensionKey);
        }
Exemplo n.º 2
0
        public void Compare_Inverted_ShouldBePi()
        {
            // Arrange
            var           unitUnderTest = CreateVectorComparer();
            IDimensionKey xDimension    = new DimensionKey("xDimension");
            IDimensionKey yDimension    = new DimensionKey("yDimension");
            IVector       v1            = new Vector(new []
            {
                new DimensionValue(xDimension, 0),
                new DimensionValue(yDimension, 1),
            });
            IVector v2 = new Vector(new []
            {
                new DimensionValue(xDimension, 0),
                new DimensionValue(yDimension, -1),
            });

            // Act
            var actual = unitUnderTest.Compare(
                v1,
                v2);

            // Assert
            const double expected = Math.PI;

            Assert.AreEqual(expected, actual);
        }
        public async Task AddAndGetCommentFeedbackWithOptionalTimeFilters()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensions = new Dictionary <string, string>()
            {
                { "region", ExpectedRegion }, { "category", ExpectedCategory }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var comment = "Feedback created in a .NET test.";

            var feedbackToAdd = new MetricCommentFeedback(MetricId, dimensionKey, comment)
            {
                StartsOn = CreatedFeedbackStartTime,
                EndsOn   = CreatedFeedbackEndTime
            };

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.Comment));

            var commentFeedback = addedFeedback as MetricCommentFeedback;

            Assert.That(commentFeedback, Is.Not.Null);
            Assert.That(commentFeedback.Comment, Is.EqualTo(comment));
            Assert.That(commentFeedback.StartsOn, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(commentFeedback.EndsOn, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(commentFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
        }
        public async Task AddAndGetChangePointFeedback()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensions = new Dictionary <string, string>()
            {
                { "region", ExpectedRegion }, { "category", ExpectedCategory }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var feedbackToAdd = new MetricChangePointFeedback(MetricId, dimensionKey, CreatedFeedbackStartTime, CreatedFeedbackEndTime, ChangePointValue.AutoDetect);

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.ChangePoint));

            var changePointFeedback = addedFeedback as MetricChangePointFeedback;

            Assert.That(changePointFeedback, Is.Not.Null);
            Assert.That(changePointFeedback.ChangePointValue, Is.EqualTo(ChangePointValue.AutoDetect));
            Assert.That(changePointFeedback.StartsOn, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(changePointFeedback.EndsOn, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(changePointFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
        }
Exemplo n.º 5
0
        public async Task AddAndGetCommentFeedbackWithMinimumSetup()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensions = new Dictionary <string, string>()
            {
                { "Dim1", ExpectedDim1 }, { "Dim2", ExpectedDim2 }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var comment = "Feedback created in a .NET test.";

            var feedbackToAdd = new MetricCommentFeedback(MetricId, dimensionKey, comment);

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.Comment));

            var commentFeedback = addedFeedback as MetricCommentFeedback;

            Assert.That(commentFeedback, Is.Not.Null);
            Assert.That(commentFeedback.Comment, Is.EqualTo(comment));
            Assert.That(commentFeedback.StartsOn, Is.Null);
            Assert.That(commentFeedback.EndsOn, Is.Null);
            Assert.That(commentFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
        }
Exemplo n.º 6
0
        public async Task AddFeedbackAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string metricId = MetricId;

            var dimensions = new Dictionary <string, string>()
            {
                { "region", "Karachi" }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var startsOn = DateTimeOffset.Parse("2020-02-01T00:00:00Z");
            var endsOn   = DateTimeOffset.Parse("2020-02-03T00:00:00Z");

            // Other types of feedback, such as MetricCommentFeedback, MetricChangePointFeedback,
            // and MetricPeriodFeedback are supported as well.

            var anomalyFeedback = new MetricAnomalyFeedback(metricId, dimensionKey, startsOn, endsOn, AnomalyValue.NotAnomaly);

            Response <MetricFeedback> response = await client.AddFeedbackAsync(anomalyFeedback);

            MetricFeedback addedFeedback = response.Value;

            Console.WriteLine($"Feedback ID: {addedFeedback.Id}");
        }
        public async Task AddAndGetAnomalyFeedbackWithOptionalDetectionConfigurationFilter()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensions = new Dictionary <string, string>()
            {
                { "region", ExpectedRegion }, { "category", ExpectedCategory }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var feedbackToAdd = new MetricAnomalyFeedback(MetricId, dimensionKey, CreatedFeedbackStartTime, CreatedFeedbackEndTime, AnomalyValue.AutoDetect)
            {
                DetectionConfigurationId = DetectionConfigurationId
            };

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.Anomaly));

            var anomalyFeedback = addedFeedback as MetricAnomalyFeedback;

            Assert.That(anomalyFeedback, Is.Not.Null);
            Assert.That(anomalyFeedback.AnomalyValue, Is.EqualTo(AnomalyValue.AutoDetect));
            Assert.That(anomalyFeedback.StartsOn, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(anomalyFeedback.EndsOn, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(anomalyFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
            Assert.That(anomalyFeedback.DetectionConfigurationId, Is.EqualTo(DetectionConfigurationId));
            // TODO: Add snapshot validation (https://github.com/azure/azure-sdk-for-net/issues/15915)
        }
Exemplo n.º 8
0
        public async Task AddAndGetAnomalyFeedbackWithMinimumSetup(bool useTokenCredential)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient(useTokenCredential);

            var dimensionKey = new DimensionKey();

            dimensionKey.AddDimensionColumn("city", "Delhi");
            dimensionKey.AddDimensionColumn("category", "Handmade");

            var filter = new FeedbackDimensionFilter(dimensionKey);

            var feedbackToAdd = new MetricAnomalyFeedback(MetricId, filter, CreatedFeedbackStartTime, CreatedFeedbackEndTime, AnomalyValue.AutoDetect);

            string feedbackId = await client.AddFeedbackAsync(feedbackToAdd);

            Assert.That(feedbackId, Is.Not.Null);

            MetricFeedback addedFeedback = await client.GetFeedbackAsync(feedbackId);

            ValidateMetricFeedback(addedFeedback, feedbackId, dimensionKey);

            Assert.That(addedFeedback.Type, Is.EqualTo(FeedbackType.Anomaly));

            var anomalyFeedback = addedFeedback as MetricAnomalyFeedback;

            Assert.That(anomalyFeedback, Is.Not.Null);
            Assert.That(anomalyFeedback.AnomalyValue, Is.EqualTo(AnomalyValue.AutoDetect));
            Assert.That(anomalyFeedback.StartTime, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(anomalyFeedback.EndTime, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(anomalyFeedback.AnomalyDetectionConfigurationId, Is.Null);
            Assert.That(anomalyFeedback.AnomalyDetectionConfigurationSnapshot, Is.Null);
        }
        public async Task AddAndGetPeriodFeedback()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensions = new Dictionary <string, string>()
            {
                { "region", ExpectedRegion }, { "category", ExpectedCategory }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var periodValue = 10;

            var feedbackToAdd = new MetricPeriodFeedback(MetricId, dimensionKey, MetricPeriodType.AutoDetect, periodValue);

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.Period));

            var periodFeedback = addedFeedback as MetricPeriodFeedback;

            Assert.That(periodFeedback, Is.Not.Null);
            Assert.That(periodFeedback.PeriodType, Is.EqualTo(MetricPeriodType.AutoDetect));
            Assert.That(periodFeedback.PeriodValue, Is.EqualTo(periodValue));
            Assert.That(periodFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
        }
Exemplo n.º 10
0
        public async Task AddAndGetPeriodFeedback()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensionKey = new DimensionKey();

            dimensionKey.AddDimensionColumn("city", "Delhi");
            dimensionKey.AddDimensionColumn("category", "Handmade");

            var filter      = new FeedbackDimensionFilter(dimensionKey);
            var periodValue = 10;

            var feedbackToAdd = new MetricPeriodFeedback(MetricId, filter, PeriodType.AutoDetect, periodValue);

            string feedbackId = await client.AddFeedbackAsync(feedbackToAdd);

            Assert.That(feedbackId, Is.Not.Null);

            MetricFeedback addedFeedback = await client.GetFeedbackAsync(feedbackId);

            ValidateMetricFeedback(addedFeedback, feedbackId, dimensionKey);

            Assert.That(addedFeedback.Type, Is.EqualTo(FeedbackType.Period));

            var periodFeedback = addedFeedback as MetricPeriodFeedback;

            Assert.That(periodFeedback, Is.Not.Null);
            Assert.That(periodFeedback.PeriodType, Is.EqualTo(PeriodType.AutoDetect));
            Assert.That(periodFeedback.PeriodValue, Is.EqualTo(periodValue));
        }
Exemplo n.º 11
0
        public async Task AddAndGetAnomalyFeedbackWithOptionalDetectionConfigurationFilter()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensionKey = new DimensionKey();

            dimensionKey.AddDimensionColumn("city", "Delhi");
            dimensionKey.AddDimensionColumn("category", "Handmade");

            var filter = new FeedbackDimensionFilter(dimensionKey);

            var feedbackToAdd = new MetricAnomalyFeedback(MetricId, filter, CreatedFeedbackStartTime, CreatedFeedbackEndTime, AnomalyValue.AutoDetect)
            {
                AnomalyDetectionConfigurationId = DetectionConfigurationId
            };

            string feedbackId = await client.AddFeedbackAsync(feedbackToAdd);

            Assert.That(feedbackId, Is.Not.Null);

            MetricFeedback addedFeedback = await client.GetFeedbackAsync(feedbackId);

            ValidateMetricFeedback(addedFeedback, feedbackId, dimensionKey);

            Assert.That(addedFeedback.Type, Is.EqualTo(FeedbackType.Anomaly));

            var anomalyFeedback = addedFeedback as MetricAnomalyFeedback;

            Assert.That(anomalyFeedback, Is.Not.Null);
            Assert.That(anomalyFeedback.AnomalyValue, Is.EqualTo(AnomalyValue.AutoDetect));
            Assert.That(anomalyFeedback.StartTime, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(anomalyFeedback.EndTime, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(anomalyFeedback.AnomalyDetectionConfigurationId, Is.EqualTo(DetectionConfigurationId));
            // TODO: Add snapshot validation (https://github.com/azure/azure-sdk-for-net/issues/15915)
        }
        public async Task AddMetricFeedbackAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string metricId = MetricId;

            DimensionKey groupKey = new DimensionKey();

            groupKey.AddDimensionColumn("city", "Belo Horizonte");

            FeedbackDimensionFilter filter = new FeedbackDimensionFilter(groupKey);
            var startTime = DateTimeOffset.Parse("2020-02-01T00:00:00Z");
            var endTime   = DateTimeOffset.Parse("2020-02-03T00:00:00Z");

            // Other types of feedback, such as MetricCommentFeedback, MetricChangePointFeedback,
            // and MetricPeriodFeedback are supported as well.

            var anomalyFeedback = new MetricAnomalyFeedback(metricId, filter, startTime, endTime, AnomalyValue.NotAnomaly);

            Response <string> response = await client.AddMetricFeedbackAsync(anomalyFeedback);

            string feedbackId = response.Value;

            Console.WriteLine($"Feedback ID: {feedbackId}");
        }
Exemplo n.º 13
0
        public async Task AddAndGetChangePointFeedback()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensionKey = new DimensionKey();

            dimensionKey.AddDimensionColumn("city", "Delhi");
            dimensionKey.AddDimensionColumn("category", "Handmade");

            var filter = new FeedbackDimensionFilter(dimensionKey);

            var feedbackToAdd = new MetricChangePointFeedback(MetricId, filter, CreatedFeedbackStartTime, CreatedFeedbackEndTime, ChangePointValue.AutoDetect);

            string feedbackId = await client.AddFeedbackAsync(feedbackToAdd);

            Assert.That(feedbackId, Is.Not.Null);

            MetricFeedback addedFeedback = await client.GetFeedbackAsync(feedbackId);

            ValidateMetricFeedback(addedFeedback, feedbackId, dimensionKey);

            Assert.That(addedFeedback.Type, Is.EqualTo(FeedbackType.ChangePoint));

            var changePointFeedback = addedFeedback as MetricChangePointFeedback;

            Assert.That(changePointFeedback, Is.Not.Null);
            Assert.That(changePointFeedback.ChangePointValue, Is.EqualTo(ChangePointValue.AutoDetect));
            Assert.That(changePointFeedback.StartTime, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(changePointFeedback.EndTime, Is.EqualTo(CreatedFeedbackEndTime));
        }
Exemplo n.º 14
0
        public async Task AddAndGetCommentFeedbackWithOptionalTimeFilters()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var dimensionKey = new DimensionKey();

            dimensionKey.AddDimensionColumn("city", "Delhi");
            dimensionKey.AddDimensionColumn("category", "Handmade");

            var filter  = new FeedbackDimensionFilter(dimensionKey);
            var comment = "Feedback created in a .NET test.";

            var feedbackToAdd = new MetricCommentFeedback(MetricId, filter, comment)
            {
                StartTime = CreatedFeedbackStartTime,
                EndTime   = CreatedFeedbackEndTime
            };

            string feedbackId = await client.AddFeedbackAsync(feedbackToAdd);

            Assert.That(feedbackId, Is.Not.Null);

            MetricFeedback addedFeedback = await client.GetFeedbackAsync(feedbackId);

            ValidateMetricFeedback(addedFeedback, feedbackId, dimensionKey);

            Assert.That(addedFeedback.Type, Is.EqualTo(FeedbackType.Comment));

            var commentFeedback = addedFeedback as MetricCommentFeedback;

            Assert.That(commentFeedback, Is.Not.Null);
            Assert.That(commentFeedback.Comment, Is.EqualTo(comment));
            Assert.That(commentFeedback.StartTime, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(commentFeedback.EndTime, Is.EqualTo(CreatedFeedbackEndTime));
        }
        public async Task AddAndGetAnomalyFeedbackWithMinimumSetup(bool useTokenCredential)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient(useTokenCredential);

            var dimensions = new Dictionary <string, string>()
            {
                { "region", ExpectedRegion }, { "category", ExpectedCategory }
            };
            var dimensionKey = new DimensionKey(dimensions);

            var feedbackToAdd = new MetricAnomalyFeedback(MetricId, dimensionKey, CreatedFeedbackStartTime, CreatedFeedbackEndTime, AnomalyValue.AutoDetect);

            MetricFeedback addedFeedback = await client.AddFeedbackAsync(feedbackToAdd);

            ValidateMetricFeedback(addedFeedback);

            Assert.That(addedFeedback.FeedbackKind, Is.EqualTo(MetricFeedbackKind.Anomaly));

            var anomalyFeedback = addedFeedback as MetricAnomalyFeedback;

            Assert.That(anomalyFeedback, Is.Not.Null);
            Assert.That(anomalyFeedback.AnomalyValue, Is.EqualTo(AnomalyValue.AutoDetect));
            Assert.That(anomalyFeedback.StartsOn, Is.EqualTo(CreatedFeedbackStartTime));
            Assert.That(anomalyFeedback.EndsOn, Is.EqualTo(CreatedFeedbackEndTime));
            Assert.That(anomalyFeedback.UserPrincipal, Is.Not.Null.And.Not.Empty);
            Assert.That(anomalyFeedback.DetectionConfigurationId, Is.Null);
            Assert.That(anomalyFeedback.DetectionConfigurationSnapshot, Is.Null);
        }
Exemplo n.º 16
0
        public async Task GetMetricSeriesDataAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string metricId = MetricId;

            var startsOn = DateTimeOffset.Parse("2020-01-01T00:00:00Z");
            var endsOn   = DateTimeOffset.UtcNow;
            var options  = new GetMetricSeriesDataOptions(startsOn, endsOn);

            // Only the two time series with the keys specified below will be returned.

            var dimensions = new Dictionary <string, string>()
            {
                { "Dim1", "JPN" },
                { "Dim2", "__SUM__" }
            };
            var seriesKey1 = new DimensionKey(dimensions);

            dimensions = new Dictionary <string, string>()
            {
                { "Dim1", "USD" },
                { "Dim2", "US" }
            };
            var seriesKey2 = new DimensionKey(dimensions);

            options.SeriesKeys.Add(seriesKey1);
            options.SeriesKeys.Add(seriesKey2);

            await foreach (MetricSeriesData seriesData in client.GetMetricSeriesDataAsync(metricId, options))
            {
                Console.WriteLine($"Time series metric ID: {seriesData.MetricId}");
                Console.WriteLine("Time series key:");

                foreach (KeyValuePair <string, string> keyValuePair in seriesData.SeriesKey)
                {
                    Console.WriteLine($"  Dimension '{keyValuePair.Key}': {keyValuePair.Value}");
                }

                Console.WriteLine("Data points:");

                // Print at most 3 points per time series.
                int totalPoints = seriesData.Timestamps.Count < 3 ? seriesData.Timestamps.Count : 3;

                for (int pointIndex = 0; pointIndex < totalPoints; pointIndex++)
                {
                    Console.WriteLine($"  Point {pointIndex}:");
                    Console.WriteLine($"   - Timestamp: {seriesData.Timestamps[pointIndex]}");
                    Console.WriteLine($"   - Metric value: {seriesData.MetricValues[pointIndex]}");
                }

                Console.WriteLine();
            }
        }
Exemplo n.º 17
0
        public async Task GetIncidents(bool populateOptionalMembers)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var options = new GetIncidentsForDetectionConfigurationOptions(SamplingStartTime, SamplingEndTime);

            if (populateOptionalMembers)
            {
                var groupKey1 = new DimensionKey();
                groupKey1.AddDimensionColumn("city", "Delhi");
                groupKey1.AddDimensionColumn("category", "Handmade");

                var groupKey2 = new DimensionKey();
                groupKey2.AddDimensionColumn("city", "Kolkata");

                options.DimensionsToFilter.Add(groupKey1);
                options.DimensionsToFilter.Add(groupKey2);
            }

            var incidentCount = 0;

            await foreach (AnomalyIncident incident in client.GetIncidentsAsync(DetectionConfigurationId, options))
            {
                Assert.That(incident, Is.Not.Null);
                Assert.That(incident.MetricId, Is.Null);

                Assert.That(incident.Id, Is.Not.Null.And.Not.Empty);
                Assert.That(incident.DetectionConfigurationId, Is.EqualTo(DetectionConfigurationId));
                Assert.That(incident.StartTime, Is.GreaterThanOrEqualTo(SamplingStartTime));
                Assert.That(incident.LastTime, Is.LessThanOrEqualTo(SamplingEndTime));
                Assert.That(incident.Status, Is.Not.EqualTo(default(AnomalyIncidentStatus)));
                Assert.That(incident.Severity, Is.Not.EqualTo(default(AnomalySeverity)));
                Assert.That(incident.DimensionKey, Is.Not.Null);

                Dictionary <string, string> dimensionColumns = incident.DimensionKey.AsDictionary();

                Assert.That(dimensionColumns.Count, Is.EqualTo(2));
                Assert.That(dimensionColumns.ContainsKey("city"));
                Assert.That(dimensionColumns.ContainsKey("category"));

                string city     = dimensionColumns["city"];
                string category = dimensionColumns["category"];

                Assert.That(city, Is.Not.Null.And.Not.Empty);
                Assert.That(category, Is.Not.Null.And.Not.Empty);

                if (populateOptionalMembers)
                {
                    Assert.That((city == "Delhi" && category == "Handmade") || city == "Kolkata");
                }

                if (++incidentCount >= MaximumSamplesCount)
                {
                    break;
                }
            }

            Assert.That(incidentCount, Is.GreaterThan(0));
        }
Exemplo n.º 18
0
        public async Task GetIncidentsForDetectionConfigurationAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string detectionConfigurationId = DetectionConfigurationId;

            // Only incidents from time series that are part of one of the groups specified
            // will be returned.

            var groupKey1 = new DimensionKey();

            groupKey1.AddDimensionColumn("city", "Bengaluru");

            var groupKey2 = new DimensionKey();

            groupKey2.AddDimensionColumn("city", "Hong Kong");
            groupKey2.AddDimensionColumn("category", "Industrial & Scientific");

            var startTime = DateTimeOffset.Parse("2020-01-01T00:00:00Z");
            var endTime   = DateTimeOffset.UtcNow;
            var options   = new GetIncidentsForDetectionConfigurationOptions(startTime, endTime)
            {
                DimensionsToFilter = new List <DimensionKey>()
                {
                    groupKey1, groupKey2
                },
                TopCount = 3
            };

            int incidentCount = 0;

            await foreach (AnomalyIncident incident in client.GetIncidentsForDetectionConfigurationAsync(detectionConfigurationId, options))
            {
                Console.WriteLine($"Incident ID: {incident.Id}");
                Console.WriteLine($"First associated anomaly occurred at: {incident.StartTime}");
                Console.WriteLine($"Last associated anomaly occurred at: {incident.LastTime}");
                Console.WriteLine($"Status: {incident.Status}");
                Console.WriteLine($"Severity: {incident.Severity}");
                Console.WriteLine("Series key:");

                foreach (KeyValuePair <string, string> keyValuePair in incident.DimensionKey.AsDictionary())
                {
                    Console.WriteLine($"  Dimension '{keyValuePair.Key}': {keyValuePair.Value}");
                }

                Console.WriteLine();

                // Print at most 3 incidents.
                if (++incidentCount >= 3)
                {
                    break;
                }
            }
        }
Exemplo n.º 19
0
        protected void ValidateTempDataFeedDimensionKey(DimensionKey dimensionKey, string expectedDimensionA)
        {
            Assert.That(dimensionKey, Is.Not.Null);

            Assert.That(Count(dimensionKey), Is.EqualTo(1));
            Assert.That(dimensionKey.TryGetValue(TempDataFeedDimensionNameA, out string dimensionA));
            Assert.That(dimensionA, Is.EqualTo(expectedDimensionA));
        }
Exemplo n.º 20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricCommentFeedback"/> class.
        /// </summary>
        /// <param name="metricId">The identifier of the metric to which the <see cref="MetricCommentFeedback"/> applies.</param>
        /// <param name="dimensionKey">
        /// A key that identifies a set of time series to which the <see cref="MetricCommentFeedback"/> applies.
        /// If all possible dimensions are set, this key uniquely identifies a single time series
        /// for the specified <paramref name="metricId"/>. If only a subset of dimensions are set, this
        /// key uniquely identifies a group of time series.
        /// </param>
        /// <param name="comment">The comment content for the feedback.</param>
        /// <exception cref="ArgumentNullException"><paramref name="metricId"/>, <paramref name="dimensionKey"/>, or <paramref name="comment"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="metricId"/> or <paramref name="comment"/> is empty.</exception>
        public MetricCommentFeedback(string metricId, DimensionKey dimensionKey, string comment)
            : base(metricId, dimensionKey)
        {
            Argument.AssertNotNullOrEmpty(comment, nameof(comment));

            ValueInternal = new CommentFeedbackValue(comment);
            FeedbackKind  = MetricFeedbackKind.Comment;
        }
Exemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MetricAnomalyFeedback"/> class.
 /// </summary>
 /// <param name="metricId">The identifier of the metric to which the <see cref="MetricAnomalyFeedback"/> applies.</param>
 /// <param name="dimensionKey">
 /// A key that identifies a set of time series to which the <see cref="MetricAnomalyFeedback"/> applies.
 /// If all possible dimensions are set, this key uniquely identifies a single time series
 /// for the specified <paramref name="metricId"/>. If only a subset of dimensions are set, this
 /// key uniquely identifies a group of time series.
 /// </param>
 /// <param name="startsOn">The start timestamp of feedback time range.</param>
 /// <param name="endsOn">The end timestamp of feedback time range. When this is equal to <paramref name="startsOn"/> it indicates a single timestamp.</param>
 /// <param name="value">Indicates whether or not the data points should have been labeled as anomalies by the service.</param>
 /// <exception cref="ArgumentNullException"><paramref name="metricId"/> or <paramref name="dimensionKey"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="metricId"/> is empty.</exception>
 public MetricAnomalyFeedback(string metricId, DimensionKey dimensionKey, DateTimeOffset startsOn, DateTimeOffset endsOn, AnomalyValue value)
     : base(metricId, dimensionKey)
 {
     StartsOn      = startsOn;
     EndsOn        = endsOn;
     ValueInternal = new AnomalyFeedbackValue(value);
     FeedbackKind  = MetricFeedbackKind.Anomaly;
 }
Exemplo n.º 22
0
        public void TryGetValueValidatesArguments()
        {
            var dimensions   = new Dictionary <string, string>();
            var dimensionKey = new DimensionKey(dimensions);

            Assert.That(() => dimensionKey.TryGetValue(null, out var _), Throws.ArgumentNullException);
            Assert.That(() => dimensionKey.TryGetValue("", out var _), Throws.ArgumentException);
        }
Exemplo n.º 23
0
        public void ContainsValidatesArguments()
        {
            var dimensions   = new Dictionary <string, string>();
            var dimensionKey = new DimensionKey(dimensions);

            Assert.That(() => dimensionKey.Contains(null), Throws.ArgumentNullException);
            Assert.That(() => dimensionKey.Contains(""), Throws.ArgumentException);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MetricChangePointFeedback"/> class.
 /// </summary>
 /// <param name="metricId">The identifier of the metric to which the <see cref="MetricChangePointFeedback"/> applies.</param>
 /// <param name="dimensionKey">
 /// A key that identifies a set of time series to which the <see cref="MetricChangePointFeedback"/> applies.
 /// If all possible dimensions are set, this key uniquely identifies a single time series
 /// for the specified <paramref name="metricId"/>. If only a subset of dimensions are set, this
 /// key uniquely identifies a group of time series.
 /// </param>
 /// <param name="startsOn">The start timestamp of feedback time range.</param>
 /// <param name="endsOn">The end timestamp of feedback time range. When this is equal to <paramref name="startsOn"/> it indicates a single timestamp.</param>
 /// <param name="value">Indicate whether or not the data points should be considered change points by the service.</param>
 /// <exception cref="ArgumentNullException"><paramref name="metricId"/> or <paramref name="dimensionKey"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="metricId"/> is empty.</exception>
 public MetricChangePointFeedback(string metricId, DimensionKey dimensionKey, DateTimeOffset startsOn, DateTimeOffset endsOn, ChangePointValue value)
     : base(metricId, dimensionKey)
 {
     StartsOn      = startsOn;
     EndsOn        = endsOn;
     ValueInternal = new ChangePointFeedbackValue(value);
     FeedbackKind  = MetricFeedbackKind.ChangePoint;
 }
Exemplo n.º 25
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricFeedback"/> class.
        /// </summary>
        /// <param name="metricId">The identifier of the metric to which the <see cref="MetricFeedback"/> applies.</param>
        /// <param name="dimensionKey">
        /// A key that identifies a set of time series to which the <see cref="MetricFeedback"/> applies.
        /// If all possible dimensions are set, this key uniquely identifies a single time series
        /// for the specified <paramref name="metricId"/>. If only a subset of dimensions are set, this
        /// key uniquely identifies a group of time series.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="metricId"/> or <paramref name="dimensionKey"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="metricId"/> is empty.</exception>
        internal MetricFeedback(string metricId, DimensionKey dimensionKey)
        {
            Argument.AssertNotNullOrEmpty(metricId, nameof(metricId));
            Argument.AssertNotNull(dimensionKey, nameof(dimensionKey));

            MetricId     = metricId;
            DimensionKey = dimensionKey;
        }
        public async Task GetMetricEnrichedSeriesDataAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string detectionConfigurationId = DetectionConfigurationId;

            // Only the two time series with the keys specified below will be returned.

            var seriesKey1 = new DimensionKey();

            seriesKey1.AddDimensionColumn("city", "Belo Horizonte");
            seriesKey1.AddDimensionColumn("category", "__SUM__");

            var seriesKey2 = new DimensionKey();

            seriesKey2.AddDimensionColumn("city", "Hong Kong");
            seriesKey2.AddDimensionColumn("category", "Industrial & Scientific");

            var seriesKeys = new List <DimensionKey>()
            {
                seriesKey1, seriesKey2
            };

            var startTime = DateTimeOffset.Parse("2020-01-01T00:00:00Z");
            var endTime   = DateTimeOffset.UtcNow;

            await foreach (MetricEnrichedSeriesData seriesData in client.GetMetricEnrichedSeriesDataAsync(seriesKeys, detectionConfigurationId, startTime, endTime))
            {
                Console.WriteLine("Time series key:");

                foreach (KeyValuePair <string, string> keyValuePair in seriesData.SeriesKey.AsDictionary())
                {
                    Console.WriteLine($"  Dimension '{keyValuePair.Key}': {keyValuePair.Value}");
                }

                Console.WriteLine("Data points:");

                // Print at most 2 points per time series.
                for (int pointIndex = 0; pointIndex < 2; pointIndex++)
                {
                    Console.WriteLine($"  Point {pointIndex}:");
                    Console.WriteLine($"   - Timestamp: {seriesData.Timestamps[pointIndex]}");
                    Console.WriteLine($"   - Metric value: {seriesData.MetricValues[pointIndex]}");
                    Console.WriteLine($"   - Expected metric value: {seriesData.ExpectedMetricValues[pointIndex]}");
                    Console.WriteLine($"   - Lower boundary: {seriesData.LowerBoundaryValues[pointIndex]}");
                    Console.WriteLine($"   - Upper boundary: {seriesData.UpperBoundaryValues[pointIndex]}");
                    Console.WriteLine($"   - Is this point an anomaly: {seriesData.IsAnomaly[pointIndex]}");
                    Console.WriteLine($"   - Period: {seriesData.Periods[pointIndex]}");
                }

                Console.WriteLine();
            }
        }
Exemplo n.º 27
0
        public async Task CreateAndGetAlertConfigurationWithSeriesGroupScope()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            await using DisposableDataFeed disposableDataFeed = await CreateTempDataFeedAsync(adminClient);

            string metricId = disposableDataFeed.DataFeed.MetricIds[TempDataFeedMetricName];

            await using DisposableDetectionConfiguration disposableDetectionConfig = await CreateTempDetectionConfigurationAsync(adminClient, metricId);

            var dimensions = new Dictionary <string, string>()
            {
                { TempDataFeedDimensionNameA, "Delhi" }
            };
            DimensionKey groupKey = new DimensionKey(dimensions);

            var detectionConfigId = disposableDetectionConfig.Configuration.Id;
            var scope             = MetricAnomalyAlertScope.CreateScopeForSeriesGroup(groupKey);
            var metricAlertConfig = new MetricAlertConfiguration(detectionConfigId, scope);

            string configName = Recording.GenerateAlphaNumericId("config");

            var configToCreate = new AnomalyAlertConfiguration()
            {
                Name = configName,
                MetricAlertConfigurations = { metricAlertConfig }
            };

            await using var disposableConfig = await DisposableAlertConfiguration.CreateAlertConfigurationAsync(adminClient, configToCreate);

            AnomalyAlertConfiguration createdConfig = disposableConfig.Configuration;

            Assert.That(createdConfig.Id, Is.Not.Null.And.Not.Empty);
            Assert.That(createdConfig.Name, Is.EqualTo(configName));
            Assert.That(createdConfig.Description, Is.Empty);
            Assert.That(createdConfig.CrossMetricsOperator, Is.Null);
            Assert.That(createdConfig.IdsOfHooksToAlert, Is.Not.Null.And.Empty);
            Assert.That(createdConfig.DimensionsToSplitAlert, Is.Not.Null.And.Empty);
            Assert.That(createdConfig.MetricAlertConfigurations, Is.Not.Null);

            MetricAlertConfiguration createdMetricAlertConfig = createdConfig.MetricAlertConfigurations.Single();

            Assert.That(createdMetricAlertConfig.DetectionConfigurationId, Is.EqualTo(detectionConfigId));

            Assert.That(createdMetricAlertConfig.AlertScope, Is.Not.Null);
            Assert.That(createdMetricAlertConfig.AlertScope.ScopeType, Is.EqualTo(MetricAnomalyAlertScopeType.SeriesGroup));
            Assert.That(createdMetricAlertConfig.AlertScope.TopNGroupInScope, Is.Null);

            ValidateTempDataFeedDimensionKey(createdMetricAlertConfig.AlertScope.SeriesGroupInScope, "Delhi");

            Assert.That(createdMetricAlertConfig.AlertConditions, Is.Not.Null);
            Assert.That(createdMetricAlertConfig.AlertConditions.MetricBoundaryCondition, Is.Null);
            Assert.That(createdMetricAlertConfig.AlertConditions.SeverityCondition, Is.Null);

            Assert.That(createdMetricAlertConfig.AlertSnoozeCondition, Is.Null);
            Assert.That(createdMetricAlertConfig.UseDetectionResultToFilterAnomalies, Is.False);
        }
        public async Task GetAnomalies(bool populateOptionalMembers)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            var options = new GetAnomaliesForDetectionConfigurationOptions(SamplingStartTime, SamplingEndTime);

            if (populateOptionalMembers)
            {
                options.Filter = new GetAnomaliesForDetectionConfigurationFilter(AnomalySeverity.Medium, AnomalySeverity.Medium);

                var groupKey1 = new DimensionKey();
                groupKey1.AddDimensionColumn("city", "Delhi");
                groupKey1.AddDimensionColumn("category", "Handmade");

                var groupKey2 = new DimensionKey();
                groupKey2.AddDimensionColumn("city", "Kolkata");

                options.Filter.SeriesGroupKeys.Add(groupKey1);
                options.Filter.SeriesGroupKeys.Add(groupKey2);
            }

            var anomalyCount = 0;

            await foreach (DataPointAnomaly anomaly in client.GetAnomaliesAsync(DetectionConfigurationId, options))
            {
                Assert.That(anomaly, Is.Not.Null);
                Assert.That(anomaly.MetricId, Is.Null);
                Assert.That(anomaly.AnomalyDetectionConfigurationId, Is.Null);
                Assert.That(anomaly.CreatedTime, Is.Null);
                Assert.That(anomaly.ModifiedTime, Is.Null);
                Assert.That(anomaly.Status, Is.Null);

                Assert.That(anomaly.Timestamp, Is.InRange(SamplingStartTime, SamplingEndTime));
                Assert.That(anomaly.Severity, Is.Not.EqualTo(default(AnomalySeverity)));

                ValidateDimensionKey(anomaly.SeriesKey);

                if (populateOptionalMembers)
                {
                    Assert.That(anomaly.Severity, Is.EqualTo(AnomalySeverity.Medium));

                    Dictionary <string, string> dimensionColumns = anomaly.SeriesKey.AsDictionary();

                    string city     = dimensionColumns["city"];
                    string category = dimensionColumns["category"];

                    Assert.That((city == "Delhi" && category == "Handmade") || city == "Kolkata");
                }

                if (++anomalyCount >= MaximumSamplesCount)
                {
                    break;
                }
            }

            Assert.That(anomalyCount, Is.GreaterThan(0));
        }
Exemplo n.º 29
0
        public async Task GetMetricEnrichedSeriesData(bool useTokenCredential)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient(useTokenCredential);

            var dimensions = new Dictionary <string, string>()
            {
                { "city", "Delhi" }, { "category", "Handmade" }
            };
            var seriesKey1 = new DimensionKey(dimensions);

            dimensions = new Dictionary <string, string>()
            {
                { "city", "Kolkata" }, { "category", "__SUM__" }
            };
            var seriesKey2 = new DimensionKey(dimensions);

            var seriesKeys = new List <DimensionKey>()
            {
                seriesKey1, seriesKey2
            };
            var returnedKeys = new List <DimensionKey>();

            await foreach (MetricEnrichedSeriesData seriesData in client.GetMetricEnrichedSeriesDataAsync(DetectionConfigurationId, seriesKeys, SamplingStartTime, SamplingEndTime))
            {
                Assert.That(seriesData, Is.Not.Null);
                Assert.That(seriesData.SeriesKey, Is.Not.Null);
                Assert.That(seriesData.Timestamps, Is.Not.Null);
                Assert.That(seriesData.MetricValues, Is.Not.Null);
                Assert.That(seriesData.ExpectedMetricValues, Is.Not.Null);
                Assert.That(seriesData.IsAnomaly, Is.Not.Null);
                Assert.That(seriesData.Periods, Is.Not.Null);
                Assert.That(seriesData.LowerBoundaryValues, Is.Not.Null);
                Assert.That(seriesData.UpperBoundaryValues, Is.Not.Null);

                int pointsCount = seriesData.Timestamps.Count;

                Assert.That(seriesData.MetricValues.Count, Is.EqualTo(pointsCount));
                Assert.That(seriesData.ExpectedMetricValues.Count, Is.EqualTo(pointsCount));
                Assert.That(seriesData.IsAnomaly.Count, Is.EqualTo(pointsCount));
                Assert.That(seriesData.Periods.Count, Is.EqualTo(pointsCount));
                Assert.That(seriesData.LowerBoundaryValues.Count, Is.EqualTo(pointsCount));
                Assert.That(seriesData.UpperBoundaryValues.Count, Is.EqualTo(pointsCount));

                foreach (DateTimeOffset timestamp in seriesData.Timestamps)
                {
                    Assert.That(timestamp, Is.InRange(SamplingStartTime, SamplingEndTime));
                }

                returnedKeys.Add(seriesData.SeriesKey);
            }

            IEnumerable <List <KeyValuePair <string, string> > > expectedKvps = seriesKeys.Select(key => key.ToList());
            IEnumerable <List <KeyValuePair <string, string> > > returnedKvps = returnedKeys.Select(key => key.ToList());

            Assert.That(returnedKvps, Is.EquivalentTo(expectedKvps));
        }
Exemplo n.º 30
0
        protected void ValidateTempDataFeedDimensionKey(DimensionKey dimensionKey, string expectedDimensionA)
        {
            Assert.That(dimensionKey, Is.Not.Null);

            Dictionary <string, string> dimensionColumns = dimensionKey.AsDictionary();

            Assert.That(dimensionColumns.Count, Is.EqualTo(1));
            Assert.That(dimensionColumns.ContainsKey(TempDataFeedDimensionNameA));
            Assert.That(dimensionColumns[TempDataFeedDimensionNameA], Is.EqualTo(expectedDimensionA));
        }