Esempio n. 1
0
        public IEnumerable <ChartStatsViewModel> GetBlockSizeStats(ChartFilterViewModel filter)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            long latestBlockDate = this.GetLatestTimestamp();

            filter.StartDate = latestBlockDate.ToUnixDate();

            List <ChartStatsViewModel> result = new List <ChartStatsViewModel>();

            foreach (var endStamp in filter.GetPeriodStamps())
            {
                var avgSize = this.db.Blocks
                              .AsNoTracking()
                              .Where(x => x.Timestamp <= latestBlockDate && x.Timestamp >= endStamp)
                              .Average(x => x.Size);

                result.Add(new ChartStatsViewModel
                {
                    Value      = (decimal)avgSize,
                    StartDate  = DateOrderFilter.GetDateTime(endStamp, filter.UnitOfTime),
                    UnitOfTime = filter.UnitOfTime
                });

                latestBlockDate = endStamp;
            }

            stopwatch.Stop();
            Log.Information("GetBlockSizeStats time - " + stopwatch.ElapsedMilliseconds);
            return(result);
        }
        public IEnumerable<ChartStatsViewModel> Filter<T>(
            ChartFilterViewModel chartFilter,
            Expression<Func<T, ValueExtractionModel>> projection = null,
            Expression<Func<T, bool>> queryFilter = null)
                where T : StampedEntity
        {
            this.ConfirmStartDateValue<T>(chartFilter);

            var query = this.db.Set<T>().AsQueryable();

            if (queryFilter != null)
            {
                query = query.Where(queryFilter);
            }

            var filteredQuery = query.Select(projection ?? (x => new ValueExtractionModel
            {
                Size = 1,
                Timestamp = x.Timestamp
            }));

            var result = filteredQuery
                .GroupBy(x => DateOrderFilter.GetGroupBy(x.Timestamp, chartFilter.UnitOfTime))
                .Select(x => new ChartStatsViewModel
                {
                    StartDate = DateOrderFilter.GetDateTime(x.First().Timestamp, chartFilter.UnitOfTime),
                    UnitOfTime = chartFilter.UnitOfTime,
                    Value = projection == null ? x.Count() : x.Sum(z => z.Size) / x.Count()
                })
                .OrderByDescending(x => x.StartDate)
                .Take(chartFilter.EndPeriod)
                .ToList();

            return result;
        }
        public IEnumerable <ChartStatsViewModel> GetStats(ChartFilterViewModel filter)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var latestDate = this.db.Transactions
                             .OrderByDescending(x => x.Timestamp)
                             .Select(x => x.Timestamp)
                             .First();

            filter.StartDate = latestDate.ToUnixDate();

            List <ChartStatsViewModel> result = new List <ChartStatsViewModel>();
            var periods = filter.GetPeriodStamps();

            foreach (var endStamp in periods)
            {
                var count = this.db.Transactions
                            .Where(x => x.Timestamp <= latestDate && x.Timestamp >= endStamp)
                            .Count();

                result.Add(new ChartStatsViewModel
                {
                    Value      = (decimal)count,
                    StartDate  = DateOrderFilter.GetDateTime(endStamp, filter.UnitOfTime),
                    UnitOfTime = filter.UnitOfTime
                });

                latestDate = endStamp;
            }

            stopwatch.Stop();
            Log.Information($"{this.GetType().FullName} - GetStats time - " + stopwatch.ElapsedMilliseconds);
            return(result);
        }
Esempio n. 4
0
        private ICollection <ChartStatsViewModel> GetBlockTimesStats(ChartFilterViewModel filter)
        {
            var latestBlockDate = this.GetLatestTimestamp();

            filter.StartDate  = latestBlockDate.ToUnixDate();
            filter.StartStamp = latestBlockDate;

            var result  = this.GetChartEntries(filter.UnitOfTime, ChartEntryType.BlockTimes);
            var periods = filter.GetPeriodStamps().Where(x => !result.Any(y => y.Timestamp == x));

            foreach (var endStamp in periods)
            {
                var blocksQuery = this.db.Blocks
                                  .Where(x => x.Timestamp <= latestBlockDate && x.Timestamp >= endStamp);
                var count = blocksQuery.Count();
                var sum   = count > 0 ? blocksQuery.Sum(x => x.TimeInSeconds) : 0;

                result.Add(new ChartStatsViewModel
                {
                    Label            = endStamp.ToUnixDate().ToString(),
                    AccumulatedValue = (decimal)sum,
                    Value            = (decimal)count,
                    StartDate        = DateOrderFilter.GetDateTime(endStamp, filter.UnitOfTime),
                    UnitOfTime       = filter.UnitOfTime
                });

                var exists = this.db.ChartEntries.Any(
                    x =>
                    x.Timestamp == endStamp &&
                    x.Type == ChartEntryType.BlockTimes &&
                    x.UnitOfTime == filter.UnitOfTime);

                latestBlockDate = endStamp;

                if (exists || !endStamp.IsPeriodOver(filter.UnitOfTime))
                {
                    continue;
                }

                this.db.ChartEntries.Add(new Data.Models.ChartEntry
                {
                    UnitOfTime       = filter.UnitOfTime,
                    Timestamp        = endStamp,
                    Type             = ChartEntryType.BlockTimes,
                    AccumulatedValue = (decimal)sum,
                    Value            = count
                });

                this.db.SaveChanges();
            }

            return(result);
        }
Esempio n. 5
0
        private ICollection <ChartStatsViewModel> GetConsensusRewardsStats(ChartFilterViewModel filter)
        {
            var latestBlockDate = this.GetLatestTimestamp();

            filter.StartStamp = latestBlockDate;
            filter.StartDate  = latestBlockDate.ToUnixDate();

            var result  = this.GetChartEntries(filter.UnitOfTime, ChartEntryType.ConsensusRewards);
            var periods = filter.GetPeriodStamps().Where(x => !result.Any(y => y.Timestamp == x)).ToArray();

            for (int i = 1; i < periods.Length; i++)
            {
                var sum = this.db.Transactions
                          .Where(x => x.Type == TransactionType.MinerTransaction)
                          .Where(x => x.GlobalOutgoingAssets.Any())
                          .Where(x => x.Timestamp < periods[i - 1] && x.Timestamp >= periods[i])
                          .SelectMany(x => x.GlobalOutgoingAssets)
                          .Sum(x => x.Amount);

                result.Add(new ChartStatsViewModel
                {
                    Value      = (decimal)sum,
                    StartDate  = DateOrderFilter.GetDateTime(periods[i], filter.UnitOfTime),
                    UnitOfTime = filter.UnitOfTime
                });

                var exists = this.db.ChartEntries.Any(
                    x =>
                    x.Timestamp == periods[i] &&
                    x.Type == ChartEntryType.ConsensusRewards &&
                    x.UnitOfTime == filter.UnitOfTime);

                if (exists || !periods[i].IsPeriodOver(filter.UnitOfTime))
                {
                    continue;
                }

                this.db.ChartEntries.Add(new Data.Models.ChartEntry
                {
                    UnitOfTime = filter.UnitOfTime,
                    Timestamp  = periods[i],
                    Type       = ChartEntryType.ConsensusRewards,
                    Value      = sum
                });

                this.db.SaveChanges();
            }

            return(result);
        }
Esempio n. 6
0
        private ICollection <ChartStatsViewModel> GetActiveAddressesStats(ChartFilterViewModel filter)
        {
            var latestBlockDate = this.GetLatestTimestamp();

            filter.StartDate  = latestBlockDate.ToUnixDate();
            filter.StartStamp = latestBlockDate;

            var result  = this.GetChartEntries(filter.UnitOfTime, ChartEntryType.ActiveAddresses);
            var periods = filter.GetPeriodStamps().Where(x => !result.Any(y => y.Timestamp == x)).ToArray();

            for (int i = 1; i < periods.Length; i++)
            {
                var count = this.db.Addresses
                            .Count(x => x.AddressesInTransaction.Any(at => at.Timestamp < periods[i - 1] && at.Timestamp >= periods[i]));

                result.Add(new ChartStatsViewModel
                {
                    Value      = (decimal)count,
                    StartDate  = DateOrderFilter.GetDateTime(periods[i], filter.UnitOfTime),
                    UnitOfTime = filter.UnitOfTime
                });

                var exists = this.db.ChartEntries.Any(
                    x =>
                    x.Timestamp == periods[i] &&
                    x.Type == ChartEntryType.ActiveAddresses &&
                    x.UnitOfTime == filter.UnitOfTime);

                if (exists || !periods[i].IsPeriodOver(filter.UnitOfTime))
                {
                    continue;
                }

                this.db.ChartEntries.Add(new Data.Models.ChartEntry
                {
                    UnitOfTime = filter.UnitOfTime,
                    Timestamp  = periods[i],
                    Type       = ChartEntryType.ActiveAddresses,
                    Value      = count
                });

                this.db.SaveChanges();
            }

            return(result);
        }