private async Task <bool> DeserializeAsync <TState>(ILog log, IHaveState <TState> stateHolder, ISnapshotRepository <TState> repository)
        {
            await log.WriteInfoAsync(nameof(DeserializeAsync), "", "Loading state...");

            var state = await repository.TryGetAsync();

            if (state == null)
            {
                await log.WriteWarningAsync("SnapshotSerializer", nameof(DeserializeAsync),
                                            stateHolder.GetType().Name, "No snapshot found to deserialize");

                return(false);
            }

            string stateDescription;

            try
            {
                stateDescription = stateHolder.DescribeState(state);
            }
            catch (NotSupportedException)
            {
                await log.WriteWarningAsync(nameof(DeserializeAsync), "", "Not supported, skipping");

                return(false);
            }

            await log.WriteInfoAsync(nameof(DeserializeAsync), stateDescription, "Settings state...");

            try
            {
                stateHolder.SetState(state);
            }
            catch (NotSupportedException)
            {
                await log.WriteWarningAsync(nameof(DeserializeAsync), "", "Not supported, skipping");

                return(false);
            }

            await log.WriteInfoAsync(nameof(DeserializeAsync), "", "State was set");

            return(true);
        }
        public async Task DeserializeAsync()
        {
            await _log.WriteInfoAsync(nameof(DeserializeAsync), "", "Loading state...");

            var state = await _repository.TryGetAsync();

            if (state == null)
            {
                await _log.WriteWarningAsync("SnapshotSerializer", nameof(DeserializeAsync),
                                             _stateHolder.GetType().Name, "No snapshot found to deserialize");

                return;
            }

            await _log.WriteInfoAsync(nameof(DeserializeAsync), _stateHolder.DescribeState(state), "Settings state...");

            _stateHolder.SetState(state);

            await _log.WriteInfoAsync(nameof(DeserializeAsync), "", "State was set");
        }
Beispiel #3
0
        public async Task CreateSnapshotAsync(
            DateTime from,
            DateTime to)
        {
            _log.Info($"Creating balance snapshot for {from:s} - {to:s}...");

            var snapshotBalances = (await _balanceUpdateRepository
                                    .GetAsync(from, to))
                                   .OrderBy(x => x.EventTimestamp)
                                   .GroupBy(x => x.WalletId)
                                   .ToDictionary(x => x.Key, x => x.Select(y => y.NewBalance).Last());

            _log.Info($"{snapshotBalances.Count} balance updates found");

            var previousSnapshot = await _snapshotRepository.TryGetAsync(from);

            if (previousSnapshot != null)
            {
                _log.Info($"Previous snapshot found with {previousSnapshot.Balances.Count} balances");

                foreach (var balance in previousSnapshot.Balances)
                {
                    if (!snapshotBalances.ContainsKey(balance.WalletId))
                    {
                        snapshotBalances[balance.WalletId] = balance.Value;
                    }
                }
            }

            var newSnapshot = SnapshotAggregate.CreateOrRestore
                              (
                balances: snapshotBalances.Select(x => SnapshotAggregate.Balance.CreateOrRestore
                                                  (
                                                      balance: x.Value,
                                                      walletId: x.Key
                                                  )),
                timestamp: to
                              );

            await _snapshotRepository.SaveAsync(newSnapshot);
        }