示例#1
0
        /// <summary>
        /// Group and concatenate all DNS server statistics increments on CreatedDate
        /// </summary>
        /// <param name="serverStatistics"></param>
        /// <returns></returns>
        private List <DnsServerStatistics> AggregateAllServerStatistics(List <DnsServerStatistics> serverStatistics)
        {
            // Hack, remove seconds and milliseconds from DateTime when grouping
            var groupedByTimestamp     = serverStatistics.GroupBy(s => new DateTime(s.CreatedDate.AddSeconds(-s.CreatedDate.Second).Ticks - (s.CreatedDate.Ticks % TimeSpan.TicksPerSecond), s.CreatedDate.Kind));
            var concatenatedStatistics = new List <DnsServerStatistics>();

            foreach (var group in groupedByTimestamp)
            {
                var newStat = new DnsServerStatistics
                {
                    ServerName  = "all",
                    CreatedDate = group.Key
                };

                foreach (var property in typeof(DnsServerStatistics).GetProperties())
                {
                    if (property.PropertyType == typeof(int))
                    {
                        var value = group.Sum(s => (int)s.GetType().GetProperty(property.Name).GetValue(s));
                        property.SetValue(newStat, value);
                    }
                    else if (property.PropertyType == typeof(double))
                    {
                        var value = group.Sum(s => (double)s.GetType().GetProperty(property.Name).GetValue(s));
                        property.SetValue(newStat, value);
                    }
                }

                newStat.DomainsOnBlocklist /= group.Count();
                concatenatedStatistics.Add(newStat);
            }

            return(concatenatedStatistics.OrderBy(stats => stats.CreatedDate)?.ToList());
        }
        /// <summary>
        /// Group and concatenate DNS server statistics increments on CreatedDate
        /// </summary>
        /// <param name="serverStatistics"></param>
        /// <returns></returns>
        private IOrderedEnumerable <DnsServerStatistics> AggregateMultiServerStatistics(string serverName, IEnumerable <DnsServerStatistics> serverStatistics)
        {
            foreach (var serverStatistic in serverStatistics)
            {
                serverStatistic.CreatedDate = RoundUp(serverStatistic.CreatedDate, TimeSpan.FromMinutes(15))
                                              .Subtract(TimeSpan.FromMinutes(15)); // Round up time 15 minutes to count for time difference between servers
            }

            var groupedByTimestamp     = serverStatistics.GroupBy(s => s.CreatedDate);
            var concatenatedStatistics = new List <DnsServerStatistics>();

            foreach (var group in groupedByTimestamp)
            {
                var newStat = new DnsServerStatistics
                {
                    ServerName  = serverName,
                    CreatedDate = group.Key
                };

                foreach (var property in typeof(DnsServerStatistics).GetProperties())
                {
                    if (property.PropertyType == typeof(int))
                    {
                        var value = group.Sum(s => (int)s.GetType().GetProperty(property.Name).GetValue(s));
                        property.SetValue(newStat, value);
                    }
                    else if (property.PropertyType == typeof(double))
                    {
                        var value = group.Sum(s => (double)s.GetType().GetProperty(property.Name).GetValue(s));
                        property.SetValue(newStat, value);
                    }
                    else if (property.PropertyType == typeof(long))
                    {
                        var value = group.Sum(s => (long)s.GetType().GetProperty(property.Name).GetValue(s));
                        property.SetValue(newStat, value);
                    }
                }

                newStat.DomainsOnBlockList /= group.Count();
                concatenatedStatistics.Add(newStat);
            }

            return(concatenatedStatistics.OrderBy(stats => stats.CreatedDate));
        }
        /// <summary>
        /// Try get DNS server statistics from a given AhaDNS server
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="apiKey"></param>
        /// <returns></returns>
        public async Task <(DnsServerStatistics, bool)> TryGetStatistics(string serverName, string apiKey, string controller)
        {
            _logger.Information("Getting DNS server statistics from server {Server}", serverName);
            DnsServerStatistics result = null;
            var success = true;

            try
            {
                var queryParameters = new Dictionary <string, string>
                {
                    { "api_key", apiKey }
                };

                var apiUrl     = $"https://{serverName}.ahadns.net/{controller}";
                var requestUri = QueryHelpers.AddQueryString(apiUrl, queryParameters);
                _logger.Debug("Sending GET -> {Url}", apiUrl);

                var httpResponse = await _httpClient.GetAsync(requestUri);

                httpResponse.EnsureSuccessStatusCode();

                result             = JsonConvert.DeserializeObject <DnsServerStatistics>(await httpResponse.Content.ReadAsStringAsync());
                result.ServerName  = serverName;      // Override server name
                result.CreatedDate = DateTime.UtcNow; // Override this time since we don't want to rely on server times being configured correctly
            }
            catch (HttpRequestException hre)
            {
                success = false;
                _logger.Error(hre, "Unsuccessful response when retrieving statistics for server {ServerName}", serverName);
            }
            catch (Exception e)
            {
                success = false;
                _logger.Error(e, "Got an unhandled exception while getting DNS statistics for server {Server}", serverName);
            }

            return(result, success);
        }
 /// <summary>
 /// Add a DNS statistics entity to table storage
 /// </summary>
 /// <param name="dnsServerStatistics"></param>
 /// <returns></returns>
 public async Task Add(DnsServerStatistics dnsServerStatistics)
 {
     await InsertTableRecordAsync(_cloudTable, new DnsServerStatisticsEntity(dnsServerStatistics));
 }
示例#5
0
 public DnsServerStatisticsEntity(DnsServerStatistics dnsServerStatistics) : base($"{dnsServerStatistics.ServerName}:{dnsServerStatistics.CreatedDate:yyyy-MM}", $"{dnsServerStatistics.CreatedDate:o}")
 {
     DnsServerStatistics = dnsServerStatistics;
     CreatedDate         = DnsServerStatistics.CreatedDate;
 }