From a49697e5f9b48136da3853bd7b11aa3e9b484053 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sun, 14 Feb 2021 15:18:54 +0530 Subject: [PATCH] StatsManager: implemented custom date range stats and top stats methods. code refactoring done to reuse same methods. --- DnsServerCore/Dns/StatsManager.cs | 590 +++++++++++++----------------- 1 file changed, 255 insertions(+), 335 deletions(-) diff --git a/DnsServerCore/Dns/StatsManager.cs b/DnsServerCore/Dns/StatsManager.cs index 1b1ad673..79f60139 100644 --- a/DnsServerCore/Dns/StatsManager.cs +++ b/DnsServerCore/Dns/StatsManager.cs @@ -556,7 +556,7 @@ namespace DnsServerCore.Dns _queue.Add(item); } - public Dictionary>> GetLastHourStats() + public Dictionary>> GetLastHourMinuteWiseStats() { StatCounter totalStatCounter = new StatCounter(); totalStatCounter.Lock(); @@ -659,264 +659,22 @@ namespace DnsServerCore.Dns return data; } - public Dictionary>> GetLastDayStats() + public Dictionary>> GetLastDayHourWiseStats() { - StatCounter totalStatCounter = new StatCounter(); - totalStatCounter.Lock(); - - List> totalQueriesPerInterval = new List>(); - List> totalNoErrorPerInterval = new List>(); - List> totalServerFailurePerInterval = new List>(); - List> totalNameErrorPerInterval = new List>(); - List> totalRefusedPerInterval = new List>(); - - List> totalAuthHitPerInterval = new List>(); - List> totalRecursionsPerInterval = new List>(); - List> totalCacheHitPerInterval = new List>(); - List> totalBlockedPerInterval = new List>(); - - List> totalClientsPerInterval = new List>(); - - DateTime lastDayDateTime = DateTime.UtcNow.AddHours(-24); - lastDayDateTime = new DateTime(lastDayDateTime.Year, lastDayDateTime.Month, lastDayDateTime.Day, lastDayDateTime.Hour, 0, 0, DateTimeKind.Utc); - - for (int hour = 0; hour < 24; hour++) - { - DateTime lastDateTime = lastDayDateTime.AddHours(hour); - string label = lastDateTime.ToLocalTime().ToString("MM/dd HH") + ":00"; - - HourlyStats hourlyStats = LoadHourlyStats(lastDateTime); - StatCounter hourlyStatCounter = hourlyStats.HourStat; - - totalStatCounter.Merge(hourlyStatCounter); - - totalQueriesPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalQueries)); - totalNoErrorPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalNoError)); - totalServerFailurePerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalServerFailure)); - totalNameErrorPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalNameError)); - totalRefusedPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalRefused)); - - totalAuthHitPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalAuthoritative)); - totalRecursionsPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalRecursive)); - totalCacheHitPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalCached)); - totalBlockedPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalBlocked)); - - totalClientsPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalClients)); - } - - Dictionary>> data = new Dictionary>>(); - - { - List> stats = new List>(6); - - stats.Add(new KeyValuePair("totalQueries", totalStatCounter.TotalQueries)); - stats.Add(new KeyValuePair("totalNoError", totalStatCounter.TotalNoError)); - stats.Add(new KeyValuePair("totalServerFailure", totalStatCounter.TotalServerFailure)); - stats.Add(new KeyValuePair("totalNameError", totalStatCounter.TotalNameError)); - stats.Add(new KeyValuePair("totalRefused", totalStatCounter.TotalRefused)); - - stats.Add(new KeyValuePair("totalAuthoritative", totalStatCounter.TotalAuthoritative)); - stats.Add(new KeyValuePair("totalRecursive", totalStatCounter.TotalRecursive)); - stats.Add(new KeyValuePair("totalCached", totalStatCounter.TotalCached)); - stats.Add(new KeyValuePair("totalBlocked", totalStatCounter.TotalBlocked)); - - stats.Add(new KeyValuePair("totalClients", totalStatCounter.TotalClients)); - - data.Add("stats", stats); - } - - data.Add("totalQueriesPerInterval", totalQueriesPerInterval); - data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval); - data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval); - data.Add("totalNameErrorPerInterval", totalNameErrorPerInterval); - data.Add("totalRefusedPerInterval", totalRefusedPerInterval); - - data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval); - data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval); - data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval); - data.Add("totalBlockedPerInterval", totalBlockedPerInterval); - - data.Add("totalClientsPerInterval", totalClientsPerInterval); - - data.Add("topDomains", totalStatCounter.GetTopDomains(10)); - data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10)); - data.Add("topClients", totalStatCounter.GetTopClients(10)); - data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(5)); - - return data; + return GetHourWiseStats(DateTime.UtcNow.AddHours(-24), 24); } - public Dictionary>> GetLastWeekStats() + public Dictionary>> GetLastWeekDayWiseStats() { - StatCounter totalStatCounter = new StatCounter(); - totalStatCounter.Lock(); - - List> totalQueriesPerInterval = new List>(); - List> totalNoErrorPerInterval = new List>(); - List> totalServerFailurePerInterval = new List>(); - List> totalNameErrorPerInterval = new List>(); - List> totalRefusedPerInterval = new List>(); - - List> totalAuthHitPerInterval = new List>(); - List> totalRecursionsPerInterval = new List>(); - List> totalCacheHitPerInterval = new List>(); - List> totalBlockedPerInterval = new List>(); - - List> totalClientsPerInterval = new List>(); - - DateTime lastWeekDateTime = DateTime.UtcNow.AddDays(-7); - lastWeekDateTime = new DateTime(lastWeekDateTime.Year, lastWeekDateTime.Month, lastWeekDateTime.Day, 0, 0, 0, DateTimeKind.Utc); - - for (int day = 0; day < 7; day++) //days - { - DateTime lastDayDateTime = lastWeekDateTime.AddDays(day); - string label = lastDayDateTime.ToLocalTime().ToString("MM/dd"); - - StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime); - totalStatCounter.Merge(dailyStatCounter); - - totalQueriesPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalQueries)); - totalNoErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNoError)); - totalServerFailurePerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalServerFailure)); - totalNameErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNameError)); - totalRefusedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRefused)); - - totalAuthHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalAuthoritative)); - totalRecursionsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRecursive)); - totalCacheHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalCached)); - totalBlockedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalBlocked)); - - totalClientsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalClients)); - } - - Dictionary>> data = new Dictionary>>(); - - { - List> stats = new List>(6); - - stats.Add(new KeyValuePair("totalQueries", totalStatCounter.TotalQueries)); - stats.Add(new KeyValuePair("totalNoError", totalStatCounter.TotalNoError)); - stats.Add(new KeyValuePair("totalServerFailure", totalStatCounter.TotalServerFailure)); - stats.Add(new KeyValuePair("totalNameError", totalStatCounter.TotalNameError)); - stats.Add(new KeyValuePair("totalRefused", totalStatCounter.TotalRefused)); - - stats.Add(new KeyValuePair("totalAuthoritative", totalStatCounter.TotalAuthoritative)); - stats.Add(new KeyValuePair("totalRecursive", totalStatCounter.TotalRecursive)); - stats.Add(new KeyValuePair("totalCached", totalStatCounter.TotalCached)); - stats.Add(new KeyValuePair("totalBlocked", totalStatCounter.TotalBlocked)); - - stats.Add(new KeyValuePair("totalClients", totalStatCounter.TotalClients)); - - data.Add("stats", stats); - } - - data.Add("totalQueriesPerInterval", totalQueriesPerInterval); - data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval); - data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval); - data.Add("totalNameErrorPerInterval", totalNameErrorPerInterval); - data.Add("totalRefusedPerInterval", totalRefusedPerInterval); - - data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval); - data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval); - data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval); - data.Add("totalBlockedPerInterval", totalBlockedPerInterval); - - data.Add("totalClientsPerInterval", totalClientsPerInterval); - - data.Add("topDomains", totalStatCounter.GetTopDomains(10)); - data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10)); - data.Add("topClients", totalStatCounter.GetTopClients(10)); - data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(5)); - - return data; + return GetDayWiseStats(DateTime.UtcNow.AddDays(-7).Date, 7); } - public Dictionary>> GetLastMonthStats() + public Dictionary>> GetLastMonthDayWiseStats() { - StatCounter totalStatCounter = new StatCounter(); - totalStatCounter.Lock(); - - List> totalQueriesPerInterval = new List>(); - List> totalNoErrorPerInterval = new List>(); - List> totalServerFailurePerInterval = new List>(); - List> totalNameErrorPerInterval = new List>(); - List> totalRefusedPerInterval = new List>(); - - List> totalAuthHitPerInterval = new List>(); - List> totalRecursionsPerInterval = new List>(); - List> totalCacheHitPerInterval = new List>(); - List> totalBlockedPerInterval = new List>(); - - List> totalClientsPerInterval = new List>(); - - DateTime lastMonthDateTime = DateTime.UtcNow.AddDays(-31); - lastMonthDateTime = new DateTime(lastMonthDateTime.Year, lastMonthDateTime.Month, lastMonthDateTime.Day, 0, 0, 0, DateTimeKind.Utc); - - for (int day = 0; day < 31; day++) //days - { - DateTime lastDayDateTime = lastMonthDateTime.AddDays(day); - string label = lastDayDateTime.ToLocalTime().ToString("MM/dd"); - - StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime); - totalStatCounter.Merge(dailyStatCounter); - - totalQueriesPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalQueries)); - totalNoErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNoError)); - totalServerFailurePerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalServerFailure)); - totalNameErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNameError)); - totalRefusedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRefused)); - - totalAuthHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalAuthoritative)); - totalRecursionsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRecursive)); - totalCacheHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalCached)); - totalBlockedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalBlocked)); - - totalClientsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalClients)); - } - - Dictionary>> data = new Dictionary>>(); - - { - List> stats = new List>(6); - - stats.Add(new KeyValuePair("totalQueries", totalStatCounter.TotalQueries)); - stats.Add(new KeyValuePair("totalNoError", totalStatCounter.TotalNoError)); - stats.Add(new KeyValuePair("totalServerFailure", totalStatCounter.TotalServerFailure)); - stats.Add(new KeyValuePair("totalNameError", totalStatCounter.TotalNameError)); - stats.Add(new KeyValuePair("totalRefused", totalStatCounter.TotalRefused)); - - stats.Add(new KeyValuePair("totalAuthoritative", totalStatCounter.TotalAuthoritative)); - stats.Add(new KeyValuePair("totalRecursive", totalStatCounter.TotalRecursive)); - stats.Add(new KeyValuePair("totalCached", totalStatCounter.TotalCached)); - stats.Add(new KeyValuePair("totalBlocked", totalStatCounter.TotalBlocked)); - - stats.Add(new KeyValuePair("totalClients", totalStatCounter.TotalClients)); - - data.Add("stats", stats); - } - - data.Add("totalQueriesPerInterval", totalQueriesPerInterval); - data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval); - data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval); - data.Add("totalNameErrorPerInterval", totalNameErrorPerInterval); - data.Add("totalRefusedPerInterval", totalRefusedPerInterval); - - data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval); - data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval); - data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval); - data.Add("totalBlockedPerInterval", totalBlockedPerInterval); - - data.Add("totalClientsPerInterval", totalClientsPerInterval); - - data.Add("topDomains", totalStatCounter.GetTopDomains(10)); - data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10)); - data.Add("topClients", totalStatCounter.GetTopClients(10)); - data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(5)); - - return data; + return GetDayWiseStats(DateTime.UtcNow.AddDays(-31).Date, 31); } - public Dictionary>> GetLastYearStats() + public Dictionary>> GetLastYearMonthWiseStats() { StatCounter totalStatCounter = new StatCounter(); totalStatCounter.Lock(); @@ -1011,6 +769,182 @@ namespace DnsServerCore.Dns return data; } + public Dictionary>> GetHourWiseStats(DateTime startDate, DateTime endDate) + { + return GetHourWiseStats(startDate, Convert.ToInt32((endDate - startDate).TotalHours) + 1); + } + + public Dictionary>> GetHourWiseStats(DateTime startDate, int hours) + { + StatCounter totalStatCounter = new StatCounter(); + totalStatCounter.Lock(); + + List> totalQueriesPerInterval = new List>(); + List> totalNoErrorPerInterval = new List>(); + List> totalServerFailurePerInterval = new List>(); + List> totalNameErrorPerInterval = new List>(); + List> totalRefusedPerInterval = new List>(); + + List> totalAuthHitPerInterval = new List>(); + List> totalRecursionsPerInterval = new List>(); + List> totalCacheHitPerInterval = new List>(); + List> totalBlockedPerInterval = new List>(); + + List> totalClientsPerInterval = new List>(); + + for (int hour = 0; hour < hours; hour++) + { + DateTime lastDateTime = startDate.AddHours(hour); + string label = lastDateTime.ToLocalTime().ToString("MM/dd HH") + ":00"; + + HourlyStats hourlyStats = LoadHourlyStats(lastDateTime); + StatCounter hourlyStatCounter = hourlyStats.HourStat; + + totalStatCounter.Merge(hourlyStatCounter); + + totalQueriesPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalQueries)); + totalNoErrorPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalNoError)); + totalServerFailurePerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalServerFailure)); + totalNameErrorPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalNameError)); + totalRefusedPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalRefused)); + + totalAuthHitPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalAuthoritative)); + totalRecursionsPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalRecursive)); + totalCacheHitPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalCached)); + totalBlockedPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalBlocked)); + + totalClientsPerInterval.Add(new KeyValuePair(label, hourlyStatCounter.TotalClients)); + } + + Dictionary>> data = new Dictionary>>(); + + { + List> stats = new List>(6); + + stats.Add(new KeyValuePair("totalQueries", totalStatCounter.TotalQueries)); + stats.Add(new KeyValuePair("totalNoError", totalStatCounter.TotalNoError)); + stats.Add(new KeyValuePair("totalServerFailure", totalStatCounter.TotalServerFailure)); + stats.Add(new KeyValuePair("totalNameError", totalStatCounter.TotalNameError)); + stats.Add(new KeyValuePair("totalRefused", totalStatCounter.TotalRefused)); + + stats.Add(new KeyValuePair("totalAuthoritative", totalStatCounter.TotalAuthoritative)); + stats.Add(new KeyValuePair("totalRecursive", totalStatCounter.TotalRecursive)); + stats.Add(new KeyValuePair("totalCached", totalStatCounter.TotalCached)); + stats.Add(new KeyValuePair("totalBlocked", totalStatCounter.TotalBlocked)); + + stats.Add(new KeyValuePair("totalClients", totalStatCounter.TotalClients)); + + data.Add("stats", stats); + } + + data.Add("totalQueriesPerInterval", totalQueriesPerInterval); + data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval); + data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval); + data.Add("totalNameErrorPerInterval", totalNameErrorPerInterval); + data.Add("totalRefusedPerInterval", totalRefusedPerInterval); + + data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval); + data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval); + data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval); + data.Add("totalBlockedPerInterval", totalBlockedPerInterval); + + data.Add("totalClientsPerInterval", totalClientsPerInterval); + + data.Add("topDomains", totalStatCounter.GetTopDomains(10)); + data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10)); + data.Add("topClients", totalStatCounter.GetTopClients(10)); + data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(5)); + + return data; + } + + public Dictionary>> GetDayWiseStats(DateTime startDate, DateTime endDate) + { + return GetDayWiseStats(startDate, Convert.ToInt32((endDate - startDate).TotalDays) + 1); + } + + public Dictionary>> GetDayWiseStats(DateTime startDate, int days) + { + StatCounter totalStatCounter = new StatCounter(); + totalStatCounter.Lock(); + + List> totalQueriesPerInterval = new List>(); + List> totalNoErrorPerInterval = new List>(); + List> totalServerFailurePerInterval = new List>(); + List> totalNameErrorPerInterval = new List>(); + List> totalRefusedPerInterval = new List>(); + + List> totalAuthHitPerInterval = new List>(); + List> totalRecursionsPerInterval = new List>(); + List> totalCacheHitPerInterval = new List>(); + List> totalBlockedPerInterval = new List>(); + + List> totalClientsPerInterval = new List>(); + + for (int day = 0; day < days; day++) //days + { + DateTime lastDayDateTime = startDate.AddDays(day); + string label = lastDayDateTime.ToLocalTime().ToString("MM/dd"); + + StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime); + totalStatCounter.Merge(dailyStatCounter); + + totalQueriesPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalQueries)); + totalNoErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNoError)); + totalServerFailurePerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalServerFailure)); + totalNameErrorPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalNameError)); + totalRefusedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRefused)); + + totalAuthHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalAuthoritative)); + totalRecursionsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalRecursive)); + totalCacheHitPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalCached)); + totalBlockedPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalBlocked)); + + totalClientsPerInterval.Add(new KeyValuePair(label, dailyStatCounter.TotalClients)); + } + + Dictionary>> data = new Dictionary>>(); + + { + List> stats = new List>(6); + + stats.Add(new KeyValuePair("totalQueries", totalStatCounter.TotalQueries)); + stats.Add(new KeyValuePair("totalNoError", totalStatCounter.TotalNoError)); + stats.Add(new KeyValuePair("totalServerFailure", totalStatCounter.TotalServerFailure)); + stats.Add(new KeyValuePair("totalNameError", totalStatCounter.TotalNameError)); + stats.Add(new KeyValuePair("totalRefused", totalStatCounter.TotalRefused)); + + stats.Add(new KeyValuePair("totalAuthoritative", totalStatCounter.TotalAuthoritative)); + stats.Add(new KeyValuePair("totalRecursive", totalStatCounter.TotalRecursive)); + stats.Add(new KeyValuePair("totalCached", totalStatCounter.TotalCached)); + stats.Add(new KeyValuePair("totalBlocked", totalStatCounter.TotalBlocked)); + + stats.Add(new KeyValuePair("totalClients", totalStatCounter.TotalClients)); + + data.Add("stats", stats); + } + + data.Add("totalQueriesPerInterval", totalQueriesPerInterval); + data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval); + data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval); + data.Add("totalNameErrorPerInterval", totalNameErrorPerInterval); + data.Add("totalRefusedPerInterval", totalRefusedPerInterval); + + data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval); + data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval); + data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval); + data.Add("totalBlockedPerInterval", totalBlockedPerInterval); + + data.Add("totalClientsPerInterval", totalClientsPerInterval); + + data.Add("topDomains", totalStatCounter.GetTopDomains(10)); + data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10)); + data.Add("topClients", totalStatCounter.GetTopClients(10)); + data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(5)); + + return data; + } + public List> GetLastHourTopStats(TopStatsType type, int limit) { StatCounter totalStatCounter = new StatCounter(); @@ -1045,16 +979,75 @@ namespace DnsServerCore.Dns } public List> GetLastDayTopStats(TopStatsType type, int limit) + { + return GetHourWiseTopStats(DateTime.UtcNow.AddHours(-24), 24, type, limit); + } + + public List> GetLastWeekTopStats(TopStatsType type, int limit) + { + return GetDayWiseTopStats(DateTime.UtcNow.AddDays(-7).Date, 7, type, limit); + } + + public List> GetLastMonthTopStats(TopStatsType type, int limit) + { + return GetDayWiseTopStats(DateTime.UtcNow.AddDays(-31).Date, 31, type, limit); + } + + public List> GetLastYearTopStats(TopStatsType type, int limit) { StatCounter totalStatCounter = new StatCounter(); totalStatCounter.Lock(); - DateTime lastDayDateTime = DateTime.UtcNow.AddHours(-24); - lastDayDateTime = new DateTime(lastDayDateTime.Year, lastDayDateTime.Month, lastDayDateTime.Day, lastDayDateTime.Hour, 0, 0, DateTimeKind.Utc); + DateTime lastYearDateTime = DateTime.UtcNow.AddMonths(-12); + lastYearDateTime = new DateTime(lastYearDateTime.Year, lastYearDateTime.Month, 1, 0, 0, 0, DateTimeKind.Utc); - for (int hour = 0; hour < 24; hour++) + for (int month = 0; month < 12; month++) //months { - DateTime lastDateTime = lastDayDateTime.AddHours(hour); + StatCounter monthlyStatCounter = new StatCounter(); + monthlyStatCounter.Lock(); + + DateTime lastMonthDateTime = lastYearDateTime.AddMonths(month); + + int days = DateTime.DaysInMonth(lastMonthDateTime.Year, lastMonthDateTime.Month); + + for (int day = 0; day < days; day++) //days + { + StatCounter dailyStatCounter = LoadDailyStats(lastMonthDateTime.AddDays(day)); + monthlyStatCounter.Merge(dailyStatCounter); + } + + totalStatCounter.Merge(monthlyStatCounter); + } + + switch (type) + { + case TopStatsType.TopDomains: + return totalStatCounter.GetTopDomains(limit); + + case TopStatsType.TopBlockedDomains: + return totalStatCounter.GetTopBlockedDomains(limit); + + case TopStatsType.TopClients: + return totalStatCounter.GetTopClients(limit); + + default: + throw new NotSupportedException(); + } + } + + public List> GetHourWiseTopStats(DateTime startDate, DateTime endDate, TopStatsType type, int limit) + { + return GetHourWiseTopStats(startDate, Convert.ToInt32((endDate - startDate).TotalHours) + 1, type, limit); + } + + public List> GetHourWiseTopStats(DateTime startDate, int hours, TopStatsType type, int limit) + { + StatCounter totalStatCounter = new StatCounter(); + totalStatCounter.Lock(); + + for (int hour = 0; hour < hours; hour++) + { + DateTime lastDateTime = startDate.AddHours(hour); HourlyStats hourlyStats = LoadHourlyStats(lastDateTime); StatCounter hourlyStatCounter = hourlyStats.HourStat; @@ -1078,17 +1071,19 @@ namespace DnsServerCore.Dns } } - public List> GetLastWeekTopStats(TopStatsType type, int limit) + public List> GetDayWiseTopStats(DateTime startDate, DateTime endDate, TopStatsType type, int limit) + { + return GetDayWiseTopStats(startDate, Convert.ToInt32((endDate - startDate).TotalDays) + 1, type, limit); + } + + public List> GetDayWiseTopStats(DateTime startDate, int days, TopStatsType type, int limit) { StatCounter totalStatCounter = new StatCounter(); totalStatCounter.Lock(); - DateTime lastWeekDateTime = DateTime.UtcNow.AddDays(-7); - lastWeekDateTime = new DateTime(lastWeekDateTime.Year, lastWeekDateTime.Month, lastWeekDateTime.Day, 0, 0, 0, DateTimeKind.Utc); - - for (int day = 0; day < 7; day++) //days + for (int day = 0; day < days; day++) //days { - DateTime lastDayDateTime = lastWeekDateTime.AddDays(day); + DateTime lastDayDateTime = startDate.AddDays(day); StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime); totalStatCounter.Merge(dailyStatCounter); @@ -1110,81 +1105,6 @@ namespace DnsServerCore.Dns } } - public List> GetLastMonthTopStats(TopStatsType type, int limit) - { - StatCounter totalStatCounter = new StatCounter(); - totalStatCounter.Lock(); - - DateTime lastMonthDateTime = DateTime.UtcNow.AddDays(-31); - lastMonthDateTime = new DateTime(lastMonthDateTime.Year, lastMonthDateTime.Month, lastMonthDateTime.Day, 0, 0, 0, DateTimeKind.Utc); - - for (int day = 0; day < 31; day++) //days - { - DateTime lastDayDateTime = lastMonthDateTime.AddDays(day); - - StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime); - totalStatCounter.Merge(dailyStatCounter); - } - - switch (type) - { - case TopStatsType.TopDomains: - return totalStatCounter.GetTopDomains(limit); - - case TopStatsType.TopBlockedDomains: - return totalStatCounter.GetTopBlockedDomains(limit); - - case TopStatsType.TopClients: - return totalStatCounter.GetTopClients(limit); - - default: - throw new NotSupportedException(); - } - } - - public List> GetLastYearTopStats(TopStatsType type, int limit) - { - StatCounter totalStatCounter = new StatCounter(); - totalStatCounter.Lock(); - - DateTime lastYearDateTime = DateTime.UtcNow.AddMonths(-12); - lastYearDateTime = new DateTime(lastYearDateTime.Year, lastYearDateTime.Month, 1, 0, 0, 0, DateTimeKind.Utc); - - for (int month = 0; month < 12; month++) //months - { - StatCounter monthlyStatCounter = new StatCounter(); - monthlyStatCounter.Lock(); - - DateTime lastMonthDateTime = lastYearDateTime.AddMonths(month); - string label = lastMonthDateTime.ToLocalTime().ToString("MM/yyyy"); - - int days = DateTime.DaysInMonth(lastMonthDateTime.Year, lastMonthDateTime.Month); - - for (int day = 0; day < days; day++) //days - { - StatCounter dailyStatCounter = LoadDailyStats(lastMonthDateTime.AddDays(day)); - monthlyStatCounter.Merge(dailyStatCounter); - } - - totalStatCounter.Merge(monthlyStatCounter); - } - - switch (type) - { - case TopStatsType.TopDomains: - return totalStatCounter.GetTopDomains(limit); - - case TopStatsType.TopBlockedDomains: - return totalStatCounter.GetTopBlockedDomains(limit); - - case TopStatsType.TopClients: - return totalStatCounter.GetTopClients(limit); - - default: - throw new NotSupportedException(); - } - } - public List> GetLastHourEligibleQueries(int minimumHitsPerHour) { StatCounter totalStatCounter = new StatCounter();