示例#1
0
        // Get total users (12)
        public async Task <TimeValueChartData> GetTotalUsers(double milliseconds, string period)
        {
            TimeSpan dateTimeSpan   = TimeSpan.FromMilliseconds(milliseconds);
            DateTime dateAfterEpoch = new DateTime(1970, 1, 1) + dateTimeSpan;
            var      data           = new TimeValueChartData();

            string[] dates;
            int[]    values;
            switch (period)
            {
            case "quarterly":
                dates  = new string[4];
                values = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    var date  = dateAfterEpoch.ToLocalTime().AddMonths(-3 * i);
                    var total = await GetQuarterlyTotalUsers(date);

                    dates[3 - i]  = $@"Q{date.GetQuarter()} {date:yy}";
                    values[3 - i] = total;
                }
                data.Dates  = dates;
                data.Values = values;
                break;

            case "daily":
                dates  = new string[7];
                values = new int[7];
                for (int i = 0; i < 7; i++)
                {
                    var date  = dateAfterEpoch.ToLocalTime().AddDays(-i);
                    var total = await _context.Users
                                .Where(u => u.Created <= date).CountAsync();

                    dates[6 - i]  = date.DayOfWeek.ToString();
                    values[6 - i] = total;
                }
                data.Dates  = dates;
                data.Values = values;
                break;

            default:
                dates  = new string[12];
                values = new int[12];
                for (int i = 0; i < 12; i++)
                {
                    var date  = dateAfterEpoch.ToLocalTime().AddMonths(-i);
                    var total = await _context.Users
                                .Where(u => u.Created <= date).CountAsync();

                    dates[11 - i]  = date.ToString("MMM yy");
                    values[11 - i] = total;
                }
                data.Dates  = dates;
                data.Values = values;
                break;
            }
            return(data);
        }
示例#2
0
        // Get new users per month (12)
        public async Task <TimeValueChartData> GetNewUsersPerMonth(double milliseconds)
        {
            TimeSpan dateTimeSpan   = TimeSpan.FromMilliseconds(milliseconds);
            DateTime dateAfterEpoch = new DateTime(1970, 1, 1) + dateTimeSpan;
            var      data           = new TimeValueChartData();
            var      dates          = new string[12];
            var      values         = new int[12];

            for (int i = 0; i < 12; i++)
            {
                var month = dateAfterEpoch.ToLocalTime().AddMonths(-i);
                var total = await _context.Users
                            .Where(u => u.Created.Month == month.Month && u.Created.Year == month.Year).CountAsync();

                dates[11 - i]  = month.ToString("MMM yy");
                values[11 - i] = total;
            }
            data.Dates  = dates;
            data.Values = values;
            return(data);
        }