Example #1
0
        public IEnumerable <SalesGroup> GetSales(DateTime start, DateTime end, GroupingPeriod period)
        {
            TimeSpan step;
            DateTime actualStart;
            int      randomStart;
            int      randomEnd;

            switch (period)
            {
            case GroupingPeriod.Hour:
                step        = new TimeSpan(1, 0, 0);
                actualStart = new DateTime(start.Year, start.Month, start.Day, start.Hour, 0, 0);
                randomStart = 50000;
                randomEnd   = 250000;
                break;

            case GroupingPeriod.Day:
                step        = new TimeSpan(24, 0, 0);
                actualStart = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0);
                randomStart = 900000;
                randomEnd   = 150000000;
                break;

            case GroupingPeriod.All:
                return(null);

            default:
                throw new Exception();
            }
            List <SalesGroup> list = new List <SalesGroup>();

            for (DateTime date = actualStart; date <= end; date += step)
            {
                if (period == GroupingPeriod.Hour && (date.Hour < 8 || date.Hour > 18))
                {
                    continue;
                }
                decimal    amount = rnd.Next(randomStart, randomEnd);
                int        units  = (int)amount / 15;
                SalesGroup sg     = new SalesGroup(period.ToString(), amount, units, date, date + step);
                list.Add(sg);
            }
            return(list);
        }
        /// <summary>
        /// Returns data set containing all data for the report.
        /// </summary>
        /// <remarks>
        /// Returned data set contains following tables:
        /// +) Bugfixing periods (Periods)
        /// +) Bugfixing statistics (Statistics)
        /// +) Report information (Info)
        ///
        /// --------------------------------------------------
        /// Bugfixing periods contains information on Added,
        /// Postponed, Reactivated, Removed bugs for each
        /// time period and additionally two balances -
        /// with Postponed/Reactivated and with only Added/Removed
        /// accounted.
        ///
        /// There are following columns in the table:
        /// +) Interval - interval index
        /// +) FromDate - period start date
        /// +) ToDate - period end date
        /// +) Added - number of Add transition during period
        /// +) Postponed - -//-
        /// +) Reactivated - -//-
        /// +) Removed - -//-
        /// +) Balance1 - balance with Postponed/Reactivated
        /// +) Balance2 - balance with only Added/Removed
        ///
        /// Each row contains information for one time period.
        ///
        /// --------------------------------------------------
        /// Bugfixing statistics table contains information on
        /// Sum, Average per period and Average per day statistics.
        ///
        /// There are following columns in the table:
        /// +) Aggregate - name of aggregate (Sum, Avg, AvgDay)
        /// +) Added - value of aggregate for Added through all periods
        /// +) Postponed - -//- for Postponed
        /// +) Reactivated - -//-
        /// +) Removed - -//-
        /// +) Balance1 - -//-
        /// +) Balance2 - -//-
        ///
        /// --------------------------------------------------
        /// Report information table contains parameters
        /// for which report was created:
        /// +) GroupPeriod - Day, Week, etc
        /// +) FromDate - start date of report
        /// +) ToDate - end date of report
        /// +) MinDate - minimum date of handled revision
        /// +) MaxDate - maximum date of handled revision
        /// </remarks>
        public DataSet CreateReport()
        {
            // Create data set
            ProjectStatisticDataSet dataSet = new ProjectStatisticDataSet();

            // Use filter to iterate through revisions
            RevisionTransitionFilter filter = new RevisionTransitionFilter();

            // Select transition which fall into desired time interval
            // Group them by interval index
            // Order groups by interval index
            var query = from t in filter.GetTransitions(m_revisions)
                        where t.CurrentRevision.Date >= m_fromDate &&
                        t.CurrentRevision.Date <= m_toDate
                        group t by GetGroup(m_groupBy, t.CurrentRevision) into g
                        orderby g.Key.Interval
                        select g;

            DateTime minDate = DateTime.MaxValue, maxDate = DateTime.MinValue;
            int      prevInterval = -1;

            // Count transitions
            foreach (var group in query)
            {
                // Initialize values
                int added       = 0;
                int postponed   = 0;
                int reactivated = 0;
                int removed     = 0;

                // Update values for all revisions in group
                foreach (RevisionStatusTransition transition in group)
                {
                    // Update correspondent values
                    added       += transition.Name == "Added" ? 1 : 0;
                    postponed   += transition.Name == "Postponed" ? 1 : 0;
                    reactivated += transition.Name == "Reactivated" ? 1 : 0;
                    removed     += transition.Name == "Removed" ? 1 : 0;

                    // Remember min/max dates
                    if (transition.CurrentRevision.Date < minDate)
                    {
                        minDate = transition.CurrentRevision.Date;
                    }
                    if (transition.CurrentRevision.Date > maxDate)
                    {
                        maxDate = transition.CurrentRevision.Date;
                    }
                }

                // Fill gaps between nonempty periods with zero rows
                if (prevInterval != -1 && group.Key.Interval > (prevInterval + 1))
                {
                    for (int interval = prevInterval + 1; interval < group.Key.Interval; interval++)
                    {
                        DateTime fromDate, toDate;
                        GetIntervalDates(m_groupBy, interval, out fromDate, out toDate);
                        dataSet.Periods.AddPeriodsRow(interval, fromDate, toDate,
                                                      0, 0, 0, 0);
                    }
                }
                // Add row for current group
                dataSet.Periods.AddPeriodsRow(group.Key.Interval,
                                              group.Key.IntervalStart, group.Key.IntervalEnd,
                                              added, postponed, reactivated, removed);

                // Remember current interval
                prevInterval = group.Key.Interval;
            }

            // Fill info table
            dataSet.Info.AddInfoRow(m_fromDate, m_toDate, m_groupBy.ToString(),
                                    minDate, maxDate);

            // Fill statistics table

            return(dataSet);
        }