Пример #1
0
        private static (bool, string, Request) TryPrepareDataForAnomalyDetection(LastDayDetectionContext lastDayDetectionContext, IGrouping <string, AzureCost> costGrouping)
        {
            var azureResource = costGrouping.Key;

            var orderedCost        = costGrouping.OrderBy(rec => rec.Date).ToArray();
            var orderedCostRequest = new Request(orderedCost.Select(rec => rec.Point).ToList(), Granularity.Daily)
            {
                Sensitivity = 65
            };

            //12 is the minimum number of data points that Anomaly Detector API accept
            if (orderedCost.Length < 12)
            {
                var lastCost = orderedCost.Last();
                //If the resource is just created or billed less then 12 times - it should be reported when both:
                //   - It's cost higher then costAlertThreshold
                //   - It's last known date returned from Azure Cost Management is the date specified in lastDay variable
                if (lastCost.Amount > lastDayDetectionContext.CostAlertThreshold && lastCost.Date == lastDayDetectionContext.DayToCheck)
                {
                    lastDayDetectionContext.OnAnomalyDetected(AzureCostAnomalyType.NewResourceWithHighCost, azureResource, lastCost.Date, lastCost.Amount);
                }
                else
                {
                    lastDayDetectionContext.OnNotEnoughValues(azureResource);
                }
                return(false, azureResource, null);
            }

            return(true, azureResource, orderedCostRequest);
        }
Пример #2
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);
            }
        }
Пример #3
0
        public async Task DetectForLastDay(LastDayDetectionContext lastDayDetectionContext)
        {
            CreateAnomalyDetectorClient();
            var costGroupings = await CostAzureCostGroupedByResourceType(lastDayDetectionContext.DayToCheck, lastDayDetectionContext.PeriodDays, lastDayDetectionContext.SubscriptionId);

            foreach (var costGrouping in costGroupings)
            {
                (bool detectionPossible, string azureResource, Request orderedCostRequest) = TryPrepareDataForAnomalyDetection(lastDayDetectionContext, costGrouping);
                if (detectionPossible)
                {
                    var result = await DetectAnomalies(orderedCostRequest);

                    ReportAnomalies(lastDayDetectionContext, orderedCostRequest, result, azureResource);
                }
            }
        }