private static void UpdateStatus(VirtualCurrencyBalanceAlert gameStatus, VirtualCurrencyBalanceEvent currentStatus, string gameTitle, ILogger log)
        {
            if (currentStatus.VirtualCurrencyCount > 0)
            {
                gameStatus.VirtualCurrencyCounter = 0;
            }
            else
            {
                gameStatus.VirtualCurrencyCounter += 1;
            }

            if (gameStatus.VirtualCurrencyCounter < WarningThreshold)
            {
                gameStatus.Status = GameStatus.Available;
            }

            if (gameStatus.VirtualCurrencyCounter >= WarningThreshold && gameStatus.VirtualCurrencyCounter < CriticalThreshold)
            {
                gameStatus.Status = GameStatus.Warning;
                if (gameStatus.VirtualCurrencyCounter == WarningThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached WARNING level on Virtual Currency Balance Event count.");
                }
            }

            if (gameStatus.VirtualCurrencyCounter >= CriticalThreshold && gameStatus.VirtualCurrencyCounter < DownThreshold)
            {
                gameStatus.Status = GameStatus.Critical;
                if (gameStatus.VirtualCurrencyCounter == CriticalThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached CRITICAL level on Virtual Currency Balance Event count.");
                }
            }

            if (gameStatus.VirtualCurrencyCounter >= DownThreshold)
            {
                gameStatus.Status = GameStatus.Down;
                if (gameStatus.VirtualCurrencyCounter == DownThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached DOWN level on Virtual Currency Balance Event count.");
                }
            }

            gameStatus.VirtualCurrencyCount = currentStatus.VirtualCurrencyCount;
        }
        public static async void Run(
            [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
            ExecutionContext context,
            ILogger log)
        {
            config = Helpers.GetConfig(context);

            WarningThreshold  = Int32.Parse(config["WarningThreshold"]);
            CriticalThreshold = Int32.Parse(config["CriticalThreshold"]);
            DownThreshold     = Int32.Parse(config["DownThreshold"]);

            var gameBlob = Helpers.GetBlob(context, log, config["CloudStorageContainer"], config["CloudStorageBlobFile"]);
            var gameList = Helpers.GetBlobAsList(gameBlob, log);

            CloudStorageAccount storageAccount = Helpers.GetCloudStorageAccount(context);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference(config["CloudStorageBlobFile"]);

            var blobs = container.ListBlobs(prefix: "vcEvents", useFlatBlobListing: true).OfType <CloudBlockBlob>().ToList();

            if (blobs.Count == 0)
            {
                foreach (Game savedGame in gameList)
                {
                    UpdateStatus(savedGame.VirtualCurrencyAlert, new VirtualCurrencyBalanceEvent()
                    {
                        TitleId = savedGame.TitleId
                    }, savedGame.Title, log);
                    UpdateAverageStatus(savedGame.VirtualCurrencyAlert, new VirtualCurrencyBalanceEvent()
                    {
                        TitleId = savedGame.TitleId
                    }, savedGame.Title, log);
                }
            }
            else
            {
                foreach (var blobEvent in blobs)
                {
                    List <VirtualCurrencyBalanceEvent> currentList = BlobHelpers <VirtualCurrencyBalanceEvent> .GetBlobContentLineByLine(blobEvent, log);

                    foreach (Game savedGame in gameList)
                    {
                        log.LogInformation($"{DateTime.UtcNow} - Processing game {savedGame.Title}");

                        if (currentList.Exists(x => x.TitleId == savedGame.TitleId))
                        {
                            VirtualCurrencyBalanceEvent currentStatus = currentList.Find(x => x.TitleId == savedGame.TitleId);
                            log.LogInformation($"{DateTime.UtcNow} - Game {savedGame.Title} has recorded events. Updating status.");
                            UpdateStatus(savedGame.VirtualCurrencyAlert, currentStatus, savedGame.Title, log);
                            UpdateAverageStatus(savedGame.VirtualCurrencyAlert, currentStatus, savedGame.Title, log);
                        }
                        else
                        {
                            log.LogInformation($"{DateTime.UtcNow} - Game {savedGame.Title} does not have any recorded events. Adding and updating status.");
                            UpdateStatus(savedGame.VirtualCurrencyAlert, new VirtualCurrencyBalanceEvent()
                            {
                                TitleId = savedGame.TitleId
                            }, savedGame.Title, log);
                            UpdateAverageStatus(savedGame.VirtualCurrencyAlert, new VirtualCurrencyBalanceEvent()
                            {
                                TitleId = savedGame.TitleId
                            }, savedGame.Title, log);
                        }
                    }

                    blobEvent.Delete();
                }
            }

            await Helpers.UploadGame(gameBlob, gameList, log);
        }
        private static void UpdateAverageStatus(VirtualCurrencyBalanceAlert gameStatus, VirtualCurrencyBalanceEvent currentStatus, string gameTitle, ILogger log)
        {
            if (getAverageDifference(gameStatus.VirtualCurrencyCount, currentStatus.VirtualCurrencyCount) < float.Parse(config["AveragePercentile"]))
            {
                gameStatus.VirtualCurrencyAverageCounter = 0;
            }
            else
            {
                gameStatus.VirtualCurrencyAverageCounter += 1;
            }

            if (gameStatus.VirtualCurrencyAverageCounter < WarningThreshold)
            {
                gameStatus.AverageStatus = GameStatus.Available;
            }

            if (gameStatus.VirtualCurrencyAverageCounter >= WarningThreshold && gameStatus.VirtualCurrencyAverageCounter < CriticalThreshold)
            {
                gameStatus.AverageStatus = GameStatus.Warning;
                if (gameStatus.VirtualCurrencyAverageCounter == WarningThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached WARNING level on Virtual Currency Balance Event Count via Average Count..");
                }
            }

            if (gameStatus.VirtualCurrencyAverageCounter >= CriticalThreshold && gameStatus.VirtualCurrencyAverageCounter < DownThreshold)
            {
                gameStatus.AverageStatus = GameStatus.Critical;
                if (gameStatus.VirtualCurrencyAverageCounter == CriticalThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached CRITICAL level on Virtual Currency Balance Event Count via Average Count..");
                }
            }

            if (gameStatus.VirtualCurrencyAverageCounter >= DownThreshold)
            {
                gameStatus.AverageStatus = GameStatus.Down;
                if (gameStatus.VirtualCurrencyAverageCounter == DownThreshold)
                {
                    log.LogWarning($"{DateTime.UtcNow} - Game {gameTitle} has reached DOWN level on Virtual Currency Balance Event Count via Average Count..");
                }
            }

            gameStatus.VirtualCurrencyCount = currentStatus.VirtualCurrencyCount;
        }