コード例 #1
0
        public async void AsyncUpdateComputerSessionInformation()
        {
            IsModelInformationBeingUpdated = true;

            IServiceFactory sf = ServiceFactoryProducer.GetServiceFactory();

            IComputerSessionService css = sf.CreateComputerSessionService();

            css.UpdateProgress += UpdateModelInformation;

            ModelInformationUpdateProgress = "Loading computer session data...";

            ComputerSessions = await Task.Run(() =>
            {
                return(css.GetComputerSessions());
            });

            RaisePropertyChanged("ComputerSessions");

            if (ComputerSessions != null && ComputerSessions.Count > 0)
            {
                StartupShutdownSeriesCollection          = new SeriesCollection();
                SessionDurationSeriesCollection          = new SeriesCollection();
                SessionDurationByWeekDaySeriesCollection = new SeriesCollection();

                StartupHoursCounts    = new int[12];
                ShutdownHoursCounts   = new int[12];
                SessionDurationCounts = new int[24];

                _sessionDurationByDayOfWeek = new Dictionary <int, List <int> >();

                for (int i = 0; i < 7; i++)
                {
                    _sessionDurationByDayOfWeek.Add(i, new List <int>());
                }
                foreach (ComputerSession session in ComputerSessions)
                {
                    int day = (int)session.Beginning.DayOfWeek;


                    //TO-DO clean up and optimize
                    int logonHour  = session.Beginning.Hour;
                    int logoffHour = session.End.Hour;

                    int durationDays = (int)Math.Floor((session.Beginning.Hour + (session.Beginning.Minute / 60) + session.Duration.TotalHours) / 24);

                    int firstDayDuration    = (logoffHour < logonHour || session.Duration.TotalHours >= 24) ? firstDayDuration = 24 - logonHour: 0;
                    int leftoverDayDuration = (int)Math.Floor((session.Duration.TotalHours - firstDayDuration) % 24);

                    int logonHourIndex  = (int)Math.Ceiling(logonHour / 2.0) - 1;
                    int logoffHourIndex = (int)Math.Ceiling(logoffHour / 2.0) - 1;

                    logonHourIndex  = logonHourIndex < 0 ? 0 : logonHourIndex;
                    logoffHourIndex = logoffHourIndex < 0 ? 0 : logoffHourIndex;


                    if (session.Duration.TotalHours < 23)
                    {
                        SessionDurationCounts[(int)Math.Floor((double)session.Duration.Hours)]++;
                    }
                    else
                    {
                        SessionDurationCounts[23]++;
                    }

                    StartupHoursCounts[logonHourIndex]++;
                    ShutdownHoursCounts[logoffHourIndex]++;

                    for (int i = 0; i < durationDays; i++)
                    {
                        if (i == 0)
                        {
                            _sessionDurationByDayOfWeek[(day + i) % 7].Add(firstDayDuration);
                        }
                        else
                        {
                            _sessionDurationByDayOfWeek[(day + i) % 7].Add(24);
                        }
                    }

                    _sessionDurationByDayOfWeek[(day + durationDays) % 7].Add(leftoverDayDuration);
                }

                ColumnSeries cs = new ColumnSeries();
                cs.Title  = "Hours";
                cs.Values = new ChartValues <int>();

                foreach (KeyValuePair <int, List <int> > day in _sessionDurationByDayOfWeek)
                {
                    day.Value.Sort();

                    int median = 0;
                    //TO-DO fix displaying hours for outliers. Activity in some week days may not be representative if it is not repeated often enough
                    if (day.Value.Count > 0)
                    {
                        median = day.Value[(int)Math.Floor((double)(day.Value.Count / 2))];
                    }

                    cs.Values.Add(median);
                }

                SessionDurationByWeekDaySeriesCollection.Add(cs);

                ColumnSeries cs_startup = new ColumnSeries();
                cs_startup.Title  = "Startups";
                cs_startup.Values = new ChartValues <int>();
                foreach (int day in StartupHoursCounts)
                {
                    cs_startup.Values.Add(day);
                }

                StartupShutdownSeriesCollection.Add(cs_startup);

                ColumnSeries cs_shutdown = new ColumnSeries();
                cs_shutdown.Title  = "Shutdowns";
                cs_shutdown.Values = new ChartValues <int>();
                foreach (int day in ShutdownHoursCounts)
                {
                    cs_shutdown.Values.Add(day);
                }

                StartupShutdownSeriesCollection.Add(cs_shutdown);

                ColumnSeries cs_sessions = new ColumnSeries();
                cs_sessions.Title = "Sessions";

                cs_sessions.Values = new ChartValues <int>();
                foreach (int day in SessionDurationCounts)
                {
                    cs_sessions.Values.Add(day);
                }

                SessionDurationSeriesCollection.Add(cs_sessions);
            }

            IsModelInformationBeingUpdated = false;
        }