Example #1
0
        public async Task <IReadOnlyCollection <BalanceUpdateAggregate> > GetAsync(
            DateTime from,
            DateTime to)
        {
            var balanceUpdates = new List <BalanceUpdateAggregate>();

            foreach (var rangeQuery in GetRangeQueries(from, to))
            {
                var continuationToken = (string)null;

                do
                {
                    IEnumerable <BalanceUpdateEntity> entities;

                    (entities, continuationToken) = await _balanceUpdateTable
                                                    .GetDataWithContinuationTokenAsync(rangeQuery, 1000, continuationToken);

                    balanceUpdates.AddRange(entities.Select(x => BalanceUpdateAggregate.CreateOrRestore
                                                            (
                                                                x.EventTimestamp,
                                                                x.NewBalance,
                                                                x.WalletId
                                                            )));
                } while (continuationToken != null);
            }

            return(balanceUpdates);
        }
Example #2
0
 public Task RegisterBalanceUpdateAsync(
     Guid walletId,
     DateTime eventTimestamp,
     decimal newBalance)
 {
     return(_balanceUpdateRepository.SaveAsync(BalanceUpdateAggregate.CreateOrRestore
                                               (
                                                   eventTimestamp: eventTimestamp,
                                                   newBalance: newBalance,
                                                   walletId: walletId)
                                               ));
 }
        public async Task ImportBalancesAsync()
        {
            var balanceUpdateBatches = _meLogReader.GetBalanceUpdates()
                                       .SelectMany
                                       (
                x => x.BalanceUpdates
                .Where(b => b.AssetId == _neoAssetId)
                .Select(b => new
            {
                Date       = x.Header.Timestamp.UtcDateTime,
                WalletId   = b.WalletId,
                NewBalance = b.NewBalance
            })
                                       )
                                       .GroupBy(x => BalanceUpdateRepository.GetPartitionKey(x.Date));

            _log.Info($"Starting asset {_neoAssetId} balance updates import...");

            var batchesCounter        = 0;
            var balanceUpdatesCounter = 0;

            try
            {
                foreach (var balanceUpdatesGroup in balanceUpdateBatches)
                {
                    foreach (var balanceUpdatesBatch in balanceUpdatesGroup.Batch(1000))
                    {
                        var aggregates = balanceUpdatesBatch
                                         .Select(x => BalanceUpdateAggregate.CreateOrRestore
                                                 (
                                                     eventTimestamp: x.Date,
                                                     newBalance: x.NewBalance,
                                                     walletId: x.WalletId
                                                 ))
                                         .Distinct(BalanceUpdateAggregateComparer.Instance)
                                         .ToArray();

                        await _balanceUpdateRepository.SaveBatchAsync(aggregates);

                        balanceUpdatesCounter += aggregates.Count();

                        _log.Info($"{++batchesCounter} balance update batches and {balanceUpdatesCounter} balances have been imported.");
                    }
                }

                _log.Info($"Balance updates import has been completed. {balanceUpdatesCounter} balance updates have been imported.");
            }
            catch (Exception e)
            {
                _log.Error(e, "Balance updates import failed.");
            }
        }
Example #4
0
        public Task SaveAsync(
            BalanceUpdateAggregate balanceUpdate)
        {
            var entity = new BalanceUpdateEntity
            {
                EventTimestamp = balanceUpdate.EventTimestamp,
                NewBalance     = balanceUpdate.NewBalance,
                WalletId       = balanceUpdate.WalletId,

                PartitionKey = GetPartitionKey(balanceUpdate.EventTimestamp),
                RowKey       = GetRowKey(balanceUpdate.EventTimestamp, balanceUpdate.WalletId)
            };

            return(_balanceUpdateTable.InsertOrReplaceAsync(entity));
        }