public static void Run([ServiceBusTrigger(QUEUE_NAME, Connection = "ConnectionStringKey")] string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

            var bankTransactionRepository           = new TableStorageService <BankTransaction>(log, TABLE_STORAGE_CONNECTION_STRING, $"{MY_NAME}BankTransaction");
            var bankTransactionStatisticsRepository = new TableStorageService <BankTransactionStatistics>(log, TABLE_STORAGE_CONNECTION_STRING, $"{MY_NAME}BankTransactionStatistics");

            var queueMessage = JsonConvert.DeserializeObject <QueueMessage>(myQueueItem);
            var transaction  = new BankTransaction(queueMessage);

            bankTransactionRepository.InsertOrUpdateEntry(transaction);

            var transactions = bankTransactionRepository.GetByPartitionId(transaction.PartitionKey);
            var totalAmmount = transactions.Sum(t => t.Ammount);

            var existingBankTransactionStatistics = bankTransactionStatisticsRepository.GetByPartitionId(transaction.PartitionKey).FirstOrDefault();

            if (existingBankTransactionStatistics != null)
            {
                existingBankTransactionStatistics.Ammount = totalAmmount;
            }
            else
            {
                existingBankTransactionStatistics = new BankTransactionStatistics(Guid.NewGuid().ToString(), transaction.PartitionKey)
                {
                    Ammount = totalAmmount
                };
            }

            bankTransactionStatisticsRepository.InsertOrUpdateEntry(existingBankTransactionStatistics);
        }
        public static void Run([TimerTrigger("0 * * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            var bankTransactionRepository           = new TableStorageService <BankTransaction>(log, ConnectionString, $"{My_Name}BankTransaction");
            var bankTransactionStatisticsRepository = new TableStorageService <BankTransactionStatistics>(log, ConnectionString, $"{My_Name}BankTransactionStatistics");

            SeedData(bankTransactionRepository);

            // Generate BankTransactionStatistics from all senders by aggregating the total sum of their transactions

            var entries     = bankTransactionRepository.GetAllEntries();
            var listOfUsers = new Dictionary <string, double>();

            foreach (var user in entries)
            {
                if (listOfUsers.ContainsKey(user.PartitionKey))
                {
                    listOfUsers[user.PartitionKey] += user.Ammount;
                }
                else
                {
                    listOfUsers.Add(user.PartitionKey, user.Ammount);
                }
            }

            foreach (var stat in listOfUsers)
            {
                bankTransactionStatisticsRepository.InsertOrUpdateEntry(new BankTransactionStatistics());
            }
        }
示例#3
0
        public static void Run([ServiceBusTrigger("myqueue-1", Connection = "ConnectionStringKey")] string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

            var myValuesRepository = new TableStorageService <MyValue>(log, TableStorageConnectionString, MyValuesTableName);
            var mySumRepository    = new TableStorageService <MyValue>(log, TableStorageConnectionString, MySumTableName);

            var myValue = JsonConvert.DeserializeObject <MyValue>(myQueueItem);

            myValuesRepository.InsertOrUpdateEntry(myValue);

            // this is not memory efficient
            var latestValues = myValuesRepository.GetAllEntries().OrderBy(v => v.Timestamp).TakeLast(10);
            var totalSum     = latestValues.Sum(v => v.Value);

            var myNewestSum = new MyValue()
            {
                Value = totalSum
            };

            mySumRepository.InsertOrUpdateEntry(myNewestSum);
        }
        public static void Run([TimerTrigger("0 * * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            var bankTransactionRepository           = new TableStorageService <BankTransaction>(log, ConnectionString, $"{My_Name}BankTransaction");
            var bankTransactionStatisticsRepository = new TableStorageService <BankTransactionStatistics>(log, ConnectionString, $"{My_Name}BankTransactionStatistics");

            SeedData(bankTransactionRepository);

            // Generate BankTransactionStatistics from all senders by aggregating the total sum of their transactions

            var entities = bankTransactionRepository.GetAllEntries();
            var senders  = entities.Select(e => e.PartitionKey).Distinct();

            foreach (var sender in senders)
            {
                var transactions = bankTransactionRepository.GetByPartitionId(sender);
                var totalAmmount = transactions.Sum(t => t.Ammount);

                var existingBankTransactionStatistics = bankTransactionStatisticsRepository.GetByPartitionId(sender).FirstOrDefault();

                if (existingBankTransactionStatistics != null)
                {
                    existingBankTransactionStatistics.Ammount = totalAmmount;
                }
                else
                {
                    existingBankTransactionStatistics = new BankTransactionStatistics(Guid.NewGuid().ToString(), sender)
                    {
                        Ammount = totalAmmount
                    };
                }

                bankTransactionStatisticsRepository.InsertOrUpdateEntry(existingBankTransactionStatistics);
            }
        }
        private static void SeedData(TableStorageService <BankTransaction> tableStorageService)
        {
            var transactions = new BankTransaction[]
            {
                new BankTransaction("1", "John Smith")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Amanda"
                },
                new BankTransaction("2", "John Smith")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Elton"
                },
                new BankTransaction("3", "Elton James")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Amanda"
                },
                new BankTransaction("4", "John Smith")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Elton"
                },
                new BankTransaction("5", "Elton James")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Amanda"
                },
                new BankTransaction("6", "Amanda Jefferson")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "John"
                },
                new BankTransaction("7", "Amanda Jefferson")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Elton"
                },
                new BankTransaction("8", "John Smith")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Elton"
                },
                new BankTransaction("9", "Amanda Jefferson")
                {
                    Ammount            = new Random().Next(1, 100),
                    DestinationAccount = Guid.NewGuid().ToString(),
                    DestinationName    = "Elton"
                },
            };

            foreach (var transaction in transactions)
            {
                tableStorageService.InsertOrUpdateEntry(transaction);
            }
        }