public async Task <SummarizedDnsServerStatistics> SummarizeTimeSpanForSingleServer(TimeSpan timeSpanToSummarize, string serverName) { if (timeSpanToSummarize <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException($"TimeSpan must be positive ant non-zero, got: {timeSpanToSummarize}"); } if (serverName == "all" || serverName == "blitz") { return((await SummarizeTimeSpan(timeSpanToSummarize)).First(result => result.ServerName == serverName)); } var fromDate = DateTime.UtcNow.Subtract(timeSpanToSummarize); var serverStatistics = await _dnsServerStatisticsStore.GetServerStatisticsFromDate(serverName, fromDate); return(new SummarizedDnsServerStatistics(serverStatistics)); }
/// <summary> /// Get DNS server statistics for all servers /// </summary> /// <returns></returns> private async Task <List <DnsServerStatistics> > GetAllDnsServerStatistics() { var allDnsServerStatistics = new List <DnsServerStatistics>(); foreach (var server in _dnsServersSettings.DnsServers) { allDnsServerStatistics.AddRange(await _dnsServerStatisticsStore.GetServerStatisticsFromDate(server.ServerName, DateTime.UtcNow.AddHours(-HoursToSummarize))); } return(allDnsServerStatistics); }
/// <summary> /// Get DNS server statistics for a single server. /// First, try get statistics from memory cache, and if not exist, query storage /// </summary> /// <param name="server"></param> /// <returns></returns> private async Task <List <DnsServerStatistics> > GetDnsServerStatistics(string server) { if (!_memoryCache.TryGetValue(server, out IEnumerable <DnsServerStatistics> dnsServerStatistics)) { dnsServerStatistics = await _dnsServerStatisticsStore.GetServerStatisticsFromDate(server, DateTime.UtcNow.AddDays(-1)); var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(20)).SetSlidingExpiration(TimeSpan.FromMinutes(10)); _memoryCache.Set(server, dnsServerStatistics, cacheEntryOptions); } return(dnsServerStatistics?.ToList()); }
public static async Task Run( [TimerTrigger("0 0 19 * * *")] TimerInfo myTimer, [Inject] IOptions <DnsServersSettings> dnsServersSettings, [Inject] IDnsServerStatisticsStore dnsServerStatisticsStore, [Inject] IEnumerable <IAlertClient> alertClients) { var logger = Log.ForContext("SourceContext", FunctionName); try { var hoursOfIncrements = DateTime.UtcNow.GetHoursOfIncrements(); var printableTimeSpan = hoursOfIncrements.PrintableTimeSpan(); // Get statistics for all servers var allDnsServerStatistics = new List <DnsServerStatistics>(); foreach (var server in dnsServersSettings.Value.DnsServers) { allDnsServerStatistics.AddRange(await dnsServerStatisticsStore.GetServerStatisticsFromDate(server.ServerName, DateTime.UtcNow.AddHours(-hoursOfIncrements))); } if (allDnsServerStatistics.Count == 0) { throw new InvalidOperationException($"Did not get any statistical increments for pidns servers for hours {hoursOfIncrements}"); } var totalNumQueries = allDnsServerStatistics.Sum(s => s.TotalNumQueries); var totalAdsBlocked = allDnsServerStatistics.Sum(s => s.NumAnswerNXDOMAIN); foreach (var alertClient in alertClients) { logger.Information("Sending pidns statistics totalNumQueries {TotalNumQueries} and totalAdsBlocked {TotalAdsBlocked} to {Integration} client", totalNumQueries, totalAdsBlocked, alertClient.Integration); if (!(await alertClient.TrySendPiDnsStatistics(totalNumQueries, totalAdsBlocked, printableTimeSpan))) { logger.Warning("AlertClient {Client} returned false when sending pidns statistics alert, indicating a failure", alertClient.Integration); } } } catch (Exception e) { logger.Error(e, "Got an exception while processing pidns statistics alert"); } }
private async Task <IOrderedEnumerable <DnsServerStatistics> > GetStatisticsForSingleServer(string serverName, DateTime fromDate) { return(await _dnsServerStatisticsStore.GetServerStatisticsFromDate(serverName, fromDate)); }