public async Task <DailyTokensSnapshot> GetTokensSnapshotForDate(DateTime date)
        {
            var today = DateTime.UtcNow.Date;

            if (date.Date == today)
            {
                var syncError = await SyncTotalTokensAsync();

                if (syncError != TokensErrorCodes.None)
                {
                    _log.Warning("Couldn't sync total tokens supply with PBF", context: syncError.ToString());
                }

                return(DailyTokensSnapshot.Create
                           (today, _totalTokensAmount, _totalTokensInCustomersWallets, _todaysEarnedAmount,
                           _todaysBurnedAmount, DateTime.UtcNow));
            }

            var dateAsString = date.ToTokensStatisticsDateString();

            var result = await _tokensSnapshotRepository.GetTokensSnapshotByDate(dateAsString);

            if (result == null)
            {
                result = new DailyTokensSnapshot {
                    ErrorCode = TokensErrorCodes.StatisticsNotFound
                }
            }
            ;

            return(result);
        }
        public async Task SaveTokensSnapshotAsync(DailyTokensSnapshot dailyTokensSnapshot)
        {
            var entity = DailyTokensSnapshotEntity.Create(dailyTokensSnapshot);

            using (var context = _contextFactory.CreateDataContext())
            {
                context.DailyTokensSnapshots.Add(entity);

                await context.SaveChangesAsync();
            }
        }
예제 #3
0
 public static DailyTokensSnapshotEntity Create(DailyTokensSnapshot dailyTokensSnapshot)
 {
     return(new DailyTokensSnapshotEntity
     {
         Date = dailyTokensSnapshot.Date,
         TotalTokens = dailyTokensSnapshot.TotalTokensAmount,
         Timestamp = dailyTokensSnapshot.Timestamp,
         TotalEarnedTokens = dailyTokensSnapshot.TotalEarnedTokensAmount,
         TotalBurnedTokens = dailyTokensSnapshot.TotalBurnedTokensAmount,
         TotalTokensInCustomersWallets = dailyTokensSnapshot.TotalTokensInCustomersWallets,
     });
 }
        public async Task SaveTokensSnapshotAsync()
        {
            var now            = DateTime.UtcNow;
            var dateOfSnapshot = now.Date;

            //This check if the save started after midnight, if not do not subtract one day
            if (now.Hour < 12)
            {
                dateOfSnapshot = dateOfSnapshot.AddDays(-1);
            }

            _log.Info($"Started the snapshot procedure of total tokens statistics in the system for {dateOfSnapshot}");

            var syncError = await SyncTotalTokensAsync();

            if (syncError != TokensErrorCodes.None)
            {
                _log.Warning("Couldn't sync total tokens supply with PBF", context: syncError.ToString());
            }

            try
            {
                var timestampOfSnapshot = DateTime.UtcNow;

                var dailyTokensHistory = DailyTokensSnapshot.Create
                                             (dateOfSnapshot, _totalTokensAmount, _totalTokensInCustomersWallets, _todaysEarnedAmount,
                                             _todaysBurnedAmount, timestampOfSnapshot);

                await _tokensSnapshotRepository.SaveTokensSnapshotAsync(dailyTokensHistory);

                _todaysBurnedAmount = _todaysEarnedAmount = 0;
            }
            catch (Exception e)
            {
                _log.Error(e, "Exception occured during daily snapshot of total tokens amount");
            }

            _log.Info($"Finished the snapshot procedure of total tokens in the system for {dateOfSnapshot}");
        }