Esempio n. 1
0
        protected override void Load(
            ContainerBuilder builder)
        {
            var logFactory = LogFactory.Create().AddConsole();

            builder
            .RegisterInstance(logFactory)
            .As <ILogFactory>();

            builder
            .RegisterType <MeLogReader>()
            .WithParameter(TypedParameter.From(_meLogsFolderPath))
            .SingleInstance()
            .AsSelf();

            builder
            .Register(ctx => BalanceUpdateRepository.Create(
                          _neoGasDistributorSettings.ConnectionString(x => x.NeoGasDistributor.Db.DataConnString),
                          ctx.Resolve <ILogFactory>()
                          ))
            .As <IBalanceUpdateRepository>()
            .SingleInstance();

            builder
            .Register(ctx => new BalanceUpdateImporter
                      (
                          _neoGasDistributorSettings.CurrentValue.NeoGasDistributor.NeoAssetId,
                          ctx.Resolve <IBalanceUpdateRepository>(),
                          ctx.Resolve <ILogFactory>(),
                          ctx.Resolve <MeLogReader>()
                      ));
        }
        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.");
            }
        }
        protected override void Load(
            ContainerBuilder builder)
        {
            builder
            .Register(ctx => BalanceUpdateRepository.Create(
                          _connectionString,
                          ctx.Resolve <ILogFactory>()
                          ))
            .As <IBalanceUpdateRepository>()
            .SingleInstance();

            builder
            .Register(ctx => ClaimedGasAmountRepository.Create(
                          _connectionString,
                          ctx.Resolve <ILogFactory>()
                          ))
            .As <IClaimedGasAmountRepository>()
            .SingleInstance();

            builder
            .Register(ctx => DistributionPlanRepository.Create(
                          _connectionString,
                          ctx.Resolve <ILogFactory>(),
                          ctx.Resolve <IChaosKitty>()
                          ))
            .As <IDistributionPlanRepository>()
            .SingleInstance();

            builder
            .Register(ctx => SnapshotRepository.Create(
                          _connectionString,
                          ctx.Resolve <ILogFactory>(),
                          ctx.Resolve <IChaosKitty>()
                          ))
            .As <ISnapshotRepository>()
            .SingleInstance();
        }