// Helpers

        private IEnumerable <CountryMetricDetails> GetCountriesData(LogDbContext context, string countryCodeFilter = null)
        {
            IQueryable <LogRecord> data = context.LogRecords;

            if (!string.IsNullOrEmpty(countryCodeFilter))
            {
                data = data.Where(x => x.Country.ToLower() == countryCodeFilter.ToLower());
            }

            var groupedData = data
                              .GroupBy(x => new
            {
                x.Country,
                x.MessageClass,
                x.TimestampUTC.Year,
                x.TimestampUTC.Month,
                x.TimestampUTC.Day     // Group by Day
            })
                              .Select(x => new { x.Key, Total = x.Count() });

            var results = groupedData
                          .ToList() // DB Call
                          .Select(x => new
            {
                Country      = x.Key.Country,
                Date         = new DateTime(x.Key.Year, x.Key.Month, x.Key.Day, 0, 0, 0), // Grouped by Day
                MessageClass = x.Key.MessageClass,
                Total        = x.Total,
            })
                          .GroupBy(x => x.Country) // Group by Country to get a child list of messages/totals
                          .Select(x => new CountryMetricDetails()
            {
                CountryCode = x.Key,
                Name        = CountryCodes.GetCountryNameFromCode(x.Key),
                Data        = x.OrderBy(d => d.Date).Select(y => new CountryMessageDetails()
                {
                    Timestamp = y.Date,
                    Name      = y.MessageClass,
                    Total     = y.Total
                })
            });

            return(results);
        }
Пример #2
0
        // Helpers

        private IEnumerable <RecordMetrics> GetMessageMetricData(LogDbContext context, string messageFilter = null)
        {
            IQueryable <LogRecord> data = context.LogRecords;

            if (!string.IsNullOrEmpty(messageFilter))
            {
                data = data.Where(x => x.MessageClass.ToLower() == messageFilter.ToLower());
            }

            var results = data.GroupBy(x => new
            {
                x.Country,
                x.MessageClass,
                x.TimestampUTC.Year,
                x.TimestampUTC.Month,
                x.TimestampUTC.Day,
            })
                          .Select(x => new { x.Key, Count = x.Count() })
                          .ToList() // DB Calls
                          .Select(x => new
            {
                Date         = new DateTime(x.Key.Year, x.Key.Month, x.Key.Day, 0, 0, 0),
                MessageClass = x.Key.MessageClass,
                Total        = x.Count,
                Country      = x.Key.Country
            })
                          .GroupBy(x => x.MessageClass)
                          .Select(x => new RecordMetrics(x.OrderBy(v => v.Date).Select(v => new RecordDetails()
            {
                Timestamp = v.Date,
                Name      = CountryCodes.GetCountryNameFromCode(v.Country),
                Total     = v.Total
            }))
            {
                Name = x.Key
            })
                          .ToList();

            return(results);
        }