예제 #1
0
        public async Task CreateAndGetDetectionConfigurationWithChangeAndSmartConditions()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

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

            var wholeConditions = new MetricWholeSeriesDetectionCondition()
            {
                CrossConditionsOperator  = DetectionConditionsOperator.And,
                ChangeThresholdCondition = new (90.0, 5, true, AnomalyDetectorDirection.Both, new (1, 2.0)),
                SmartDetectionCondition  = new (23.0, AnomalyDetectorDirection.Down, new (3, 4.0))
            };

            var configToCreate = new AnomalyDetectionConfiguration(MetricId, configName, wholeConditions);

            await using var disposableConfig = await DisposableDetectionConfiguration.CreateDetectionConfigurationAsync(adminClient, configToCreate);

            AnomalyDetectionConfiguration createdConfig = await adminClient.GetDetectionConfigurationAsync(disposableConfig.Id);

            Assert.That(createdConfig.Id, Is.EqualTo(disposableConfig.Id));
            Assert.That(createdConfig.MetricId, Is.EqualTo(MetricId));
            Assert.That(createdConfig.Name, Is.EqualTo(configName));
            Assert.That(createdConfig.Description, Is.Empty);
            Assert.That(createdConfig.SeriesGroupDetectionConditions, Is.Not.Null.And.Empty);
            Assert.That(createdConfig.SeriesDetectionConditions, Is.Not.Null.And.Empty);

            MetricWholeSeriesDetectionCondition createdWholeConditions = createdConfig.WholeSeriesDetectionConditions;

            Assert.That(createdWholeConditions, Is.Not.Null);
            Assert.That(createdWholeConditions.CrossConditionsOperator, Is.EqualTo(DetectionConditionsOperator.And));
            Assert.That(createdWholeConditions.HardThresholdCondition, Is.Null);

            ValidateChangeThresholdCondition(createdWholeConditions.ChangeThresholdCondition, 90.0, 5, true, AnomalyDetectorDirection.Both, 1, 2.0);
            ValidateSmartDetectionCondition(createdWholeConditions.SmartDetectionCondition, 23.0, AnomalyDetectorDirection.Down, 3, 4.0);
        }
        public void AnomalyDetectionConfiguration()
        {
            var id          = "id";
            var name        = "name";
            var description = "description";
            var metricId    = "metricId";
            var wholeSeriesDetectionConditions = new MetricWholeSeriesDetectionCondition();
            var seriesGroupDetectionConditions = new List <MetricSeriesGroupDetectionCondition>()
            {
                new MetricSeriesGroupDetectionCondition(SampleDimensionKey),
                new MetricSeriesGroupDetectionCondition(SampleDimensionKey)
            };
            var seriesDetectionConditions = new List <MetricSingleSeriesDetectionCondition>()
            {
                new MetricSingleSeriesDetectionCondition(SampleDimensionKey),
                new MetricSingleSeriesDetectionCondition(SampleDimensionKey)
            };

            var anomalyDetectionConfiguration = MetricsAdvisorModelFactory.AnomalyDetectionConfiguration(id, name, description, metricId,
                                                                                                         wholeSeriesDetectionConditions, seriesGroupDetectionConditions, seriesDetectionConditions);

            Assert.That(anomalyDetectionConfiguration.Id, Is.EqualTo(id));
            Assert.That(anomalyDetectionConfiguration.Name, Is.EqualTo(name));
            Assert.That(anomalyDetectionConfiguration.Description, Is.EqualTo(description));
            Assert.That(anomalyDetectionConfiguration.MetricId, Is.EqualTo(metricId));
            Assert.That(anomalyDetectionConfiguration.WholeSeriesDetectionConditions, Is.EqualTo(wholeSeriesDetectionConditions));
            Assert.That(anomalyDetectionConfiguration.SeriesGroupDetectionConditions, Is.EqualTo(seriesGroupDetectionConditions));
            Assert.That(anomalyDetectionConfiguration.SeriesDetectionConditions, Is.EqualTo(seriesDetectionConditions));
        }
        public async Task CreateAndGetDetectionConfigurationWithHardCondition(bool useTokenCredential)
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(useTokenCredential);

            await using DisposableDataFeed disposableDataFeed = await CreateTempDataFeedAsync(adminClient);

            string configName  = Recording.GenerateAlphaNumericId("config");
            string metricId    = disposableDataFeed.DataFeed.MetricIds[TempDataFeedMetricName];
            var    description = "This configuration was created to test the .NET client.";

            var wholeConditions = new MetricWholeSeriesDetectionCondition()
            {
                HardThresholdCondition = new(AnomalyDetectorDirection.Up, new(1, 2.0))
                {
                    UpperBound = 10.0
                }
            };

            var configToCreate = new AnomalyDetectionConfiguration()
            {
                MetricId = metricId,
                Name     = configName,
                WholeSeriesDetectionConditions = wholeConditions,
                // This is the only test that validates description during creation. Please don't remove it!
                Description = description
            };

            await using var disposableConfig = await DisposableDetectionConfiguration.CreateDetectionConfigurationAsync(adminClient, configToCreate);

            AnomalyDetectionConfiguration createdConfig = disposableConfig.Configuration;

            Assert.That(createdConfig.Id, Is.Not.Null.And.Not.Empty);
            Assert.That(createdConfig.MetricId, Is.EqualTo(metricId));
            Assert.That(createdConfig.Name, Is.EqualTo(configName));
            Assert.That(createdConfig.Description, Is.EqualTo(description));
            Assert.That(createdConfig.SeriesGroupDetectionConditions, Is.Not.Null.And.Empty);
            Assert.That(createdConfig.SeriesDetectionConditions, Is.Not.Null.And.Empty);

            MetricWholeSeriesDetectionCondition createdWholeConditions = createdConfig.WholeSeriesDetectionConditions;

            Assert.That(createdWholeConditions, Is.Not.Null);
            Assert.That(createdWholeConditions.ConditionOperator, Is.Null);
            Assert.That(createdWholeConditions.ChangeThresholdCondition, Is.Null);
            Assert.That(createdWholeConditions.SmartDetectionCondition, Is.Null);

            ValidateHardThresholdCondition(createdWholeConditions.HardThresholdCondition, AnomalyDetectorDirection.Up, 10.0, null, 1, 2.0);
        }
        public async Task CreateAndDeleteAnomalyDetectionConfigurationAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential);

            string metricId = MetricId;

            #region Snippet:CreateAnomalyDetectionConfigurationAsync
            //@@ string metricId = "<metricId>";
            string configurationName = "Sample anomaly detection configuration";

            var hardThresholdSuppressCondition = new SuppressCondition(1, 100);
            var hardThresholdCondition         = new HardThresholdCondition(AnomalyDetectorDirection.Down, hardThresholdSuppressCondition)
            {
                LowerBound = 5.0
            };

            var smartDetectionSuppressCondition = new SuppressCondition(4, 50);
            var smartDetectionCondition         = new SmartDetectionCondition(10.0, AnomalyDetectorDirection.Up, smartDetectionSuppressCondition);

            var detectionCondition = new MetricWholeSeriesDetectionCondition()
            {
                HardThresholdCondition  = hardThresholdCondition,
                SmartDetectionCondition = smartDetectionCondition,
                CrossConditionsOperator = DetectionConditionsOperator.Or
            };

            var detectionConfiguration = new AnomalyDetectionConfiguration(metricId, configurationName, detectionCondition);

            Response <AnomalyDetectionConfiguration> response = await adminClient.CreateMetricAnomalyDetectionConfigurationAsync(detectionConfiguration);

            detectionConfiguration = response.Value;

            Console.WriteLine($"Anomaly detection configuration ID: {detectionConfiguration.Id}");
            #endregion

            // Delete the created anomaly detection configuration to clean up the Metrics Advisor resource.
            // Do not perform this step if you intend to keep using the configuration.

            await adminClient.DeleteMetricAnomalyDetectionConfigurationAsync(detectionConfiguration.Id);
        }
        public void CreateDetectionConfigurationValidatesArguments()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            var metricId   = "metricId";
            var name       = "configName";
            var conditions = new MetricWholeSeriesDetectionCondition();

            var config = new AnomalyDetectionConfiguration()
            {
                MetricId = null,
                Name     = name,
                WholeSeriesDetectionConditions = conditions
            };

            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(null), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(null), Throws.InstanceOf <ArgumentNullException>());

            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(config), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(config), Throws.InstanceOf <ArgumentNullException>());

            config.MetricId = "";
            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(config), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(config), Throws.InstanceOf <ArgumentException>());

            config.MetricId = metricId;
            config.Name     = null;
            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(config), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(config), Throws.InstanceOf <ArgumentNullException>());

            config.Name = "";
            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(config), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(config), Throws.InstanceOf <ArgumentException>());

            config.Name = name;
            config.WholeSeriesDetectionConditions = null;
            Assert.That(() => adminClient.CreateDetectionConfigurationAsync(config), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateDetectionConfiguration(config), Throws.InstanceOf <ArgumentNullException>());
        }
        public async Task CreateAndGetDetectionConfigurationWithSeriesConditions()
        {
            // Set required parameters of the configuration to be created.

            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            await using DisposableDataFeed disposableDataFeed = await CreateTempDataFeedAsync(adminClient);

            string configName = Recording.GenerateAlphaNumericId("config");
            string metricId   = disposableDataFeed.DataFeed.MetricIds[TempDataFeedMetricName];

            var wholeConditions = new MetricWholeSeriesDetectionCondition()
            {
                HardThresholdCondition = new(AnomalyDetectorDirection.Both, new(1, 2.0))
                {
                    UpperBound = 20.0,
                    LowerBound = 10.0
                }
            };

            var configToCreate = new AnomalyDetectionConfiguration()
            {
                MetricId = metricId,
                Name     = configName,
                WholeSeriesDetectionConditions = wholeConditions
            };

            // Set the series conditions and create the configuration.

            var dimensions = new Dictionary <string, string>()
            {
                { TempDataFeedDimensionNameA, "Delhi" }, { TempDataFeedDimensionNameB, "Handmade" }
            };
            var seriesConditions0 = new MetricSingleSeriesDetectionCondition(new DimensionKey(dimensions))
            {
                SmartDetectionCondition = new(30.0, AnomalyDetectorDirection.Both, new(3, 4.0))
            };

            dimensions = new Dictionary <string, string>()
            {
                { TempDataFeedDimensionNameA, "Kolkata" }, { TempDataFeedDimensionNameB, "Grocery & Gourmet Food" }
            };
            var seriesConditions1 = new MetricSingleSeriesDetectionCondition(new DimensionKey(dimensions))
            {
                ChangeThresholdCondition = new(40.0, 12, false, AnomalyDetectorDirection.Up, new(5, 6.0))
            };

            configToCreate.SeriesDetectionConditions.Add(seriesConditions0);
            configToCreate.SeriesDetectionConditions.Add(seriesConditions1);

            await using var disposableConfig = await DisposableDetectionConfiguration.CreateDetectionConfigurationAsync(adminClient, configToCreate);

            // Get the created configuration and validate top-level members.

            AnomalyDetectionConfiguration createdConfig = disposableConfig.Configuration;

            Assert.That(createdConfig.Id, Is.Not.Null.And.Not.Empty);
            Assert.That(createdConfig.MetricId, Is.EqualTo(metricId));
            Assert.That(createdConfig.Name, Is.EqualTo(configName));
            Assert.That(createdConfig.Description, Is.Empty);
            Assert.That(createdConfig.SeriesGroupDetectionConditions, Is.Not.Null.And.Empty);

            // Validate whole series detection conditions.

            MetricWholeSeriesDetectionCondition createdWholeConditions = createdConfig.WholeSeriesDetectionConditions;

            Assert.That(createdWholeConditions, Is.Not.Null);
            Assert.That(createdWholeConditions.ConditionOperator, Is.Null);
            Assert.That(createdWholeConditions.ChangeThresholdCondition, Is.Null);
            Assert.That(createdWholeConditions.SmartDetectionCondition, Is.Null);

            ValidateHardThresholdCondition(createdWholeConditions.HardThresholdCondition, AnomalyDetectorDirection.Both, 20.0, 10.0, 1, 2.0);

            // Start series conditions validation.

            Assert.That(createdConfig.SeriesDetectionConditions, Is.Not.Null);
            Assert.That(createdConfig.SeriesDetectionConditions.Count, Is.EqualTo(2));

            // Validate first series conditions.

            var createdSeriesConditions0 = createdConfig.SeriesDetectionConditions[0];

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

            ValidateTempDataFeedDimensionKey(createdSeriesConditions0.SeriesKey, "Delhi", "Handmade");

            Assert.That(createdSeriesConditions0.ConditionOperator, Is.Null);
            Assert.That(createdSeriesConditions0.HardThresholdCondition, Is.Null);
            Assert.That(createdSeriesConditions0.ChangeThresholdCondition, Is.Null);

            ValidateSmartDetectionCondition(createdSeriesConditions0.SmartDetectionCondition, 30.0, AnomalyDetectorDirection.Both, 3, 4.0);

            // Validate last series conditions.

            var createdSeriesConditions1 = createdConfig.SeriesDetectionConditions[1];

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

            ValidateTempDataFeedDimensionKey(createdSeriesConditions1.SeriesKey, "Kolkata", "Grocery & Gourmet Food");

            Assert.That(createdSeriesConditions1.ConditionOperator, Is.Null);
            Assert.That(createdSeriesConditions1.HardThresholdCondition, Is.Null);
            Assert.That(createdSeriesConditions1.SmartDetectionCondition, Is.Null);

            ValidateChangeThresholdCondition(createdSeriesConditions1.ChangeThresholdCondition, 40.0, 12, false, AnomalyDetectorDirection.Up, 5, 6.0);
        }
예제 #7
0
        public async Task CreateAndGetDetectionConfigurationWithSeriesGroupConditions()
        {
            // Set required parameters of the configuration to be created.

            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

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

            var wholeConditions = new MetricWholeSeriesDetectionCondition()
            {
                HardThresholdCondition = new(AnomalyDetectorDirection.Down, new(1, 2.0))
                {
                    LowerBound = 10.0
                }
            };

            var configToCreate = new AnomalyDetectionConfiguration()
            {
                MetricId = MetricId,
                Name     = configName,
                WholeSeriesDetectionConditions = wholeConditions
            };

            // Set the series group conditions and create the configuration.

            var groupConditions0 = new MetricSeriesGroupDetectionCondition()
            {
                SmartDetectionCondition = new(30.0, AnomalyDetectorDirection.Both, new(3, 4.0))
            };

            groupConditions0.SeriesGroupKey.AddDimensionColumn("city", "Delhi");

            var groupConditions1 = new MetricSeriesGroupDetectionCondition()
            {
                ChangeThresholdCondition = new(40.0, 12, false, AnomalyDetectorDirection.Up, new(5, 6.0))
            };

            groupConditions1.SeriesGroupKey.AddDimensionColumn("city", "Koltaka");

            configToCreate.SeriesGroupDetectionConditions.Add(groupConditions0);
            configToCreate.SeriesGroupDetectionConditions.Add(groupConditions1);

            await using var disposableConfig = await DisposableDetectionConfiguration.CreateDetectionConfigurationAsync(adminClient, configToCreate);

            // Get the created configuration and validate top-level members.

            AnomalyDetectionConfiguration createdConfig = await adminClient.GetDetectionConfigurationAsync(disposableConfig.Id);

            Assert.That(createdConfig.Id, Is.EqualTo(disposableConfig.Id));
            Assert.That(createdConfig.MetricId, Is.EqualTo(MetricId));
            Assert.That(createdConfig.Name, Is.EqualTo(configName));
            Assert.That(createdConfig.Description, Is.Empty);
            Assert.That(createdConfig.SeriesDetectionConditions, Is.Not.Null.And.Empty);

            // Validate whole series detection conditions.

            MetricWholeSeriesDetectionCondition createdWholeConditions = createdConfig.WholeSeriesDetectionConditions;

            Assert.That(createdWholeConditions, Is.Not.Null);
            Assert.That(createdWholeConditions.CrossConditionsOperator, Is.Null);
            Assert.That(createdWholeConditions.ChangeThresholdCondition, Is.Null);
            Assert.That(createdWholeConditions.SmartDetectionCondition, Is.Null);

            ValidateHardThresholdCondition(createdWholeConditions.HardThresholdCondition, AnomalyDetectorDirection.Down, null, 10.0, 1, 2.0);

            // Start series group conditions validation.

            Assert.That(createdConfig.SeriesGroupDetectionConditions, Is.Not.Null);
            Assert.That(createdConfig.SeriesGroupDetectionConditions.Count, Is.EqualTo(2));

            // Validate first series group conditions.

            var createdGroupConditions0 = createdConfig.SeriesGroupDetectionConditions[0];

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

            ValidateGroupKey(createdGroupConditions0.SeriesGroupKey);

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

            Assert.That(dimensionColumns.Count, Is.EqualTo(1));
            Assert.That(dimensionColumns["city"], Is.EqualTo("Delhi"));

            Assert.That(createdGroupConditions0.CrossConditionsOperator, Is.Null);
            Assert.That(createdGroupConditions0.HardThresholdCondition, Is.Null);
            Assert.That(createdGroupConditions0.ChangeThresholdCondition, Is.Null);

            ValidateSmartDetectionCondition(createdGroupConditions0.SmartDetectionCondition, 30.0, AnomalyDetectorDirection.Both, 3, 4.0);

            // Validate last series group conditions.

            var createdGroupConditions1 = createdConfig.SeriesGroupDetectionConditions[1];

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

            ValidateGroupKey(createdGroupConditions1.SeriesGroupKey);

            dimensionColumns = createdGroupConditions1.SeriesGroupKey.AsDictionary();

            Assert.That(dimensionColumns.Count, Is.EqualTo(1));
            Assert.That(dimensionColumns["city"], Is.EqualTo("Koltaka"));

            Assert.That(createdGroupConditions1.CrossConditionsOperator, Is.Null);
            Assert.That(createdGroupConditions1.HardThresholdCondition, Is.Null);
            Assert.That(createdGroupConditions1.SmartDetectionCondition, Is.Null);

            ValidateChangeThresholdCondition(createdGroupConditions1.ChangeThresholdCondition, 40.0, 12, false, AnomalyDetectorDirection.Up, 5, 6.0);
        }
 public static AnomalyDetectionConfiguration AnomalyDetectionConfiguration(string id = default, string name = default, string description = default, string metricId = default, MetricWholeSeriesDetectionCondition wholeSeriesDetectionConditions = default, IList <MetricSeriesGroupDetectionCondition> seriesGroupDetectionConditions = default, IList <MetricSingleSeriesDetectionCondition> seriesDetectionConditions = default)
 {
     seriesGroupDetectionConditions ??= new List <MetricSeriesGroupDetectionCondition>();
     seriesDetectionConditions ??= new List <MetricSingleSeriesDetectionCondition>();
     return(new AnomalyDetectionConfiguration(id, name, description, metricId, wholeSeriesDetectionConditions, seriesGroupDetectionConditions, seriesDetectionConditions));
 }