Beispiel #1
0
        public static async Task RunAsync(string endpoint, string key)
        {
            Console.WriteLine("Sample of detecting whether the latest point in series is anomaly.");

            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            // Detection
            Request request = Program.GetRequest();

            request.MaxAnomalyRatio = 0.25;
            request.Sensitivity     = 95;
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point is detected as anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point is not detected as anomaly.");
            }
        }
Beispiel #2
0
        public async Task DetectLastPointAnomaly()
        {
            //read endpoint and apiKey
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            var endpointUri = new Uri(endpoint);
            var credential  = new AzureKeyCredential(apiKey);

            //create client
            AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential);

            //read data
            string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv");

            List <TimeSeriesPoint> list = File.ReadAllLines(datapath, Encoding.UTF8)
                                          .Where(e => e.Trim().Length != 0)
                                          .Select(e => e.Split(','))
                                          .Where(e => e.Length == 2)
                                          .Select(e => new TimeSeriesPoint(float.Parse(e[1]))
            {
                Timestamp = DateTime.Parse(e[0])
            }).ToList();

            //create request
            DetectRequest request = new DetectRequest(list)
            {
                Granularity = TimeGranularity.Daily
            };

            #region Snippet:DetectLastPointAnomaly

            //detect
            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");

            try
            {
                LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false);

                if (result.IsAnomaly)
                {
                    Console.WriteLine("The latest point was detected as an anomaly.");
                }
                else
                {
                    Console.WriteLine("The latest point was not detected as an anomaly.");
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(String.Format("Last detection failed: {0}", ex.Message));
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
                throw;
            }
            #endregion
        }
Beispiel #3
0
        public static async Task RunAsync(string endpoint, string key)
        {
            Console.WriteLine("Sample of detecting whether the latest point in series is anomaly.");

            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            // Create time series
            var series = new List <Point> {
                new Point(DateTime.Parse("1962-01-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-02-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-03-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-04-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-05-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-06-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-07-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-08-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-09-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-10-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-11-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1962-12-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-01-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-02-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-03-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-04-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-05-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-06-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-07-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-08-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-09-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-10-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-11-01T00:00:00Z"), 1),
                new Point(DateTime.Parse("1963-12-01T00:00:00Z"), 0)
            };

            // Detection
            Request request = new Request(series, Granularity.Monthly);

            request.MaxAnomalyRatio = 0.25;
            request.Sensitivity     = 95;
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point is detected as anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point is not detected as anomaly.");
            }
        }
Beispiel #4
0
        private static void ReportAnomalies(LastDayDetectionContext lastDayDetectionContext, Request orderedCostRequest,
                                            LastDetectResponse result, string azureResource)
        {
            var lastPoint = orderedCostRequest.Series.Last();

            //The anomaly must be reported only if it is detected at the specified in lastDay variable, but not is last known date with cost returned from Azure Cost Management
            if (result.IsAnomaly && lastPoint.Value > lastDayDetectionContext.CostAlertThreshold &&
                lastPoint.Timestamp == lastDayDetectionContext.DayToCheck)
            {
                var anomalyType = result.IsNegativeAnomaly ? AzureCostAnomalyType.Drop : AzureCostAnomalyType.Spike;
                lastDayDetectionContext.OnAnomalyDetected(anomalyType, azureResource, lastPoint.Timestamp, lastPoint.Value);
            }
        }
Beispiel #5
0
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
Beispiel #6
0
        /// <summary>
        /// Detectar o status de anomalias do último ponto de dados
        /// </summary>
        /// <param name="client">Cliente autenticado</param>
        /// <param name="request">Request com os dados</param>
        /// <returns></returns>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("\nDetectando o status de anomalias do último ponto de dados");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("No último registro EXISTE uma anomalia.");
            }
            else
            {
                Console.WriteLine("No último registro NÃO existe uma anomalia.");
            }
        }
Beispiel #7
0
        static async Task LastDetectSampleAsync(string endpoint, string key, Request request)
        {
            Console.WriteLine("Sample of detecting whether the latest point in series is anomaly.");

            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point is detected as anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point is not detected as anomaly.");
            }
        }
Beispiel #8
0
        private async Task <LastDetectResponse> DetectAnomalies(Request orderedCostRequest)
        {
            LastDetectResponse result = await _anomalyDetectorClient.LastDetectAsync(orderedCostRequest).ConfigureAwait(false);

            return(result);
        }