Example #1
0
            public HourlyStats(BinaryReader bR)
            {
                if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "HS") //format
                {
                    throw new InvalidDataException("HourlyStats format is invalid.");
                }

                byte version = bR.ReadByte();

                switch (version)
                {
                case 1:
                    _hourStat = new StatCounter();
                    _hourStat.Lock();

                    for (int i = 0; i < _minuteStats.Length; i++)
                    {
                        _minuteStats[i] = new StatCounter(bR);
                        _hourStat.Merge(_minuteStats[i]);
                    }

                    break;

                default:
                    throw new InvalidDataException("HourlyStats version not supported.");
                }
            }
Example #2
0
        private void DoMaintenance()
        {
            //load new stats counter 5 min ahead of current time
            DateTime currentDateTime = DateTime.UtcNow;

            for (int i = 0; i < 5; i++)
            {
                int minute = currentDateTime.AddMinutes(i).Minute;

                StatCounter statCounter = _lastHourStatCounters[minute];
                if ((statCounter == null) || statCounter.IsLocked)
                {
                    _lastHourStatCounters[minute] = new StatCounter();
                }
            }

            //save data upto last 5 mins
            DateTime last5MinDateTime = currentDateTime.AddMinutes(-5);

            for (int i = 0; i < 5; i++)
            {
                DateTime lastDateTime = last5MinDateTime.AddMinutes(i);

                StatCounter lastStatCounter = _lastHourStatCounters[lastDateTime.Minute];
                if ((lastStatCounter != null) && !lastStatCounter.IsLocked)
                {
                    //load hourly stats data
                    HourlyStats hourlyStats = LoadHourlyStats(lastDateTime);

                    //update hourly stats file
                    lastStatCounter.Lock();
                    hourlyStats.UpdateStat(lastDateTime, lastStatCounter);

                    //save hourly stats
                    SaveHourlyStats(lastDateTime, hourlyStats);
                }
            }
        }
Example #3
0
 public HourlyStats()
 {
     _hourStat = new StatCounter();
     _hourStat.Lock();
 }
Example #4
0
        public Dictionary <string, List <KeyValuePair <string, int> > > GetLastYearStats()
        {
            StatCounter totalStatCounter = new StatCounter();

            totalStatCounter.Lock();

            List <KeyValuePair <string, int> > totalQueriesPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalNoErrorPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalServerFailurePerInterval = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalNameErrorPerInterval     = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalRefusedPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalBlockedPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalClientsPerInterval       = new List <KeyValuePair <string, int> >();

            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
                {
                    DateTime lastDayDateTime = lastMonthDateTime.AddDays(day);

                    for (int hour = 0; hour < 24; hour++) //hours
                    {
                        DateTime    lastDateTime = lastDayDateTime.AddHours(hour);
                        HourlyStats hourlyStats  = LoadHourlyStats(lastDateTime);

                        monthlyStatCounter.Merge(hourlyStats.HourStat);
                    }
                }

                totalStatCounter.Merge(monthlyStatCounter);

                totalQueriesPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalQueries));
                totalNoErrorPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalNoError));
                totalServerFailurePerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalServerFailure));
                totalNameErrorPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalNameError));
                totalRefusedPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalRefused));
                totalBlockedPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalBlocked));
                totalClientsPerInterval.Add(new KeyValuePair <string, int>(label, monthlyStatCounter.TotalClients));
            }

            Dictionary <string, List <KeyValuePair <string, int> > > data = new Dictionary <string, List <KeyValuePair <string, int> > >();

            {
                List <KeyValuePair <string, int> > stats = new List <KeyValuePair <string, int> >(6);

                stats.Add(new KeyValuePair <string, int>("totalQueries", totalStatCounter.TotalQueries));
                stats.Add(new KeyValuePair <string, int>("totalNoError", totalStatCounter.TotalNoError));
                stats.Add(new KeyValuePair <string, int>("totalServerFailure", totalStatCounter.TotalServerFailure));
                stats.Add(new KeyValuePair <string, int>("totalNameError", totalStatCounter.TotalNameError));
                stats.Add(new KeyValuePair <string, int>("totalRefused", totalStatCounter.TotalRefused));
                stats.Add(new KeyValuePair <string, int>("totalBlocked", totalStatCounter.TotalBlocked));
                stats.Add(new KeyValuePair <string, int>("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("totalBlockedPerInterval", totalBlockedPerInterval);
            data.Add("totalClientsPerInterval", totalClientsPerInterval);

            data.Add("topDomains", totalStatCounter.GetTopDomains());
            data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains());
            data.Add("topClients", totalStatCounter.GetTopClients());
            data.Add("queryTypes", totalStatCounter.GetTopQueryTypes());

            return(data);
        }
Example #5
0
        public Dictionary <string, List <KeyValuePair <string, int> > > GetLastDayStats()
        {
            StatCounter totalStatCounter = new StatCounter();

            totalStatCounter.Lock();

            List <KeyValuePair <string, int> > totalQueriesPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalNoErrorPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalServerFailurePerInterval = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalNameErrorPerInterval     = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalRefusedPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalBlockedPerInterval       = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > totalClientsPerInterval       = new List <KeyValuePair <string, int> >();

            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 <string, int>(label, hourlyStatCounter.TotalQueries));
                totalNoErrorPerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalNoError));
                totalServerFailurePerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalServerFailure));
                totalNameErrorPerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalNameError));
                totalRefusedPerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalRefused));
                totalBlockedPerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalBlocked));
                totalClientsPerInterval.Add(new KeyValuePair <string, int>(label, hourlyStatCounter.TotalClients));
            }

            Dictionary <string, List <KeyValuePair <string, int> > > data = new Dictionary <string, List <KeyValuePair <string, int> > >();

            {
                List <KeyValuePair <string, int> > stats = new List <KeyValuePair <string, int> >(6);

                stats.Add(new KeyValuePair <string, int>("totalQueries", totalStatCounter.TotalQueries));
                stats.Add(new KeyValuePair <string, int>("totalNoError", totalStatCounter.TotalNoError));
                stats.Add(new KeyValuePair <string, int>("totalServerFailure", totalStatCounter.TotalServerFailure));
                stats.Add(new KeyValuePair <string, int>("totalNameError", totalStatCounter.TotalNameError));
                stats.Add(new KeyValuePair <string, int>("totalRefused", totalStatCounter.TotalRefused));
                stats.Add(new KeyValuePair <string, int>("totalBlocked", totalStatCounter.TotalBlocked));
                stats.Add(new KeyValuePair <string, int>("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("totalBlockedPerInterval", totalBlockedPerInterval);
            data.Add("totalClientsPerInterval", totalClientsPerInterval);

            data.Add("topDomains", totalStatCounter.GetTopDomains());
            data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains());
            data.Add("topClients", totalStatCounter.GetTopClients());
            data.Add("queryTypes", totalStatCounter.GetTopQueryTypes());

            return(data);
        }
Example #6
0
        public Dictionary <string, List <KeyValuePair <string, int> > > GetLastHourStats()
        {
            StatCounter totalStatCounter = new StatCounter();

            totalStatCounter.Lock();

            List <KeyValuePair <string, int> > totalQueriesPerInterval       = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalNoErrorPerInterval       = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalServerFailurePerInterval = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalNameErrorPerInterval     = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalRefusedPerInterval       = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalBlockedPerInterval       = new List <KeyValuePair <string, int> >(60);
            List <KeyValuePair <string, int> > totalClientsPerInterval       = new List <KeyValuePair <string, int> >(60);

            DateTime lastHourDateTime = DateTime.UtcNow.AddMinutes(-60);

            lastHourDateTime = new DateTime(lastHourDateTime.Year, lastHourDateTime.Month, lastHourDateTime.Day, lastHourDateTime.Hour, lastHourDateTime.Minute, 0, DateTimeKind.Utc);

            for (int minute = 0; minute < 60; minute++)
            {
                DateTime lastDateTime = lastHourDateTime.AddMinutes(minute);
                string   label        = lastDateTime.ToLocalTime().ToString("HH:mm");

                StatCounter statCounter = _lastHourStatCounters[lastDateTime.Minute];
                if ((statCounter != null) && statCounter.IsLocked)
                {
                    totalStatCounter.Merge(statCounter);

                    totalQueriesPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalQueries));
                    totalNoErrorPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalNoError));
                    totalServerFailurePerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalServerFailure));
                    totalNameErrorPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalNameError));
                    totalRefusedPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalRefused));
                    totalBlockedPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalBlocked));
                    totalClientsPerInterval.Add(new KeyValuePair <string, int>(label, statCounter.TotalClients));
                }
                else
                {
                    totalQueriesPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalNoErrorPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalServerFailurePerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalNameErrorPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalRefusedPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalBlockedPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                    totalClientsPerInterval.Add(new KeyValuePair <string, int>(label, 0));
                }
            }

            Dictionary <string, List <KeyValuePair <string, int> > > data = new Dictionary <string, List <KeyValuePair <string, int> > >();

            {
                List <KeyValuePair <string, int> > stats = new List <KeyValuePair <string, int> >(6);

                stats.Add(new KeyValuePair <string, int>("totalQueries", totalStatCounter.TotalQueries));
                stats.Add(new KeyValuePair <string, int>("totalNoError", totalStatCounter.TotalNoError));
                stats.Add(new KeyValuePair <string, int>("totalServerFailure", totalStatCounter.TotalServerFailure));
                stats.Add(new KeyValuePair <string, int>("totalNameError", totalStatCounter.TotalNameError));
                stats.Add(new KeyValuePair <string, int>("totalRefused", totalStatCounter.TotalRefused));
                stats.Add(new KeyValuePair <string, int>("totalBlocked", totalStatCounter.TotalBlocked));
                stats.Add(new KeyValuePair <string, int>("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("totalBlockedPerInterval", totalBlockedPerInterval);
            data.Add("totalClientsPerInterval", totalClientsPerInterval);

            data.Add("topDomains", totalStatCounter.GetTopDomains());
            data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains());
            data.Add("topClients", totalStatCounter.GetTopClients());
            data.Add("queryTypes", totalStatCounter.GetTopQueryTypes());

            return(data);
        }