public void TestBaselineCannotBeOvershotIdenticalBaselineAndDatapoint()
        {
            DataPointOffsetCalculator calc = new DataPointOffsetCalculator(
                new Grouping
                {
                    DataPointYear = 2010,
                    BaselineYear = 2010,
                    YearRange = 1
                }, 1, calendarYearType);

            Assert.AreEqual(DataPointOffsetCalculator.InvalidYear, calc.TimePeriod.Year);
        }
        public void TestDataPointCannotBeExceeded()
        {
            DataPointOffsetCalculator calc = new DataPointOffsetCalculator(
                new Grouping
                {
                    DataPointYear = 2010,
                    BaselineYear = 2009,
                    YearRange = 1
                }, -100, calendarYearType);

            Assert.AreEqual(2010, calc.TimePeriod.Year);
            Assert.AreEqual(1, calc.TimePeriod.YearRange);
        }
        public void TestMonthOffset()
        {
            DataPointOffsetCalculator calc = new DataPointOffsetCalculator(
                new Grouping
                {
                    DataPointYear = 2010,
                    DataPointMonth = 3,
                    BaselineYear = 2009,
                    BaselineMonth = 1
                }, 1, calendarYearType);

            Assert.AreEqual(2010, calc.TimePeriod.Year);
            Assert.AreEqual(2, calc.TimePeriod.Month);
        }
        public QofListSizeProvider(IGroupDataReader groupDataReader, Area area, int groupId, int dataPointOffset,
            YearType yearType)
        {
            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(groupId, IndicatorId);
            var period = new DataPointOffsetCalculator(grouping, dataPointOffset, yearType).TimePeriod;

            if (area.IsCcg)
            {
                //Note: zeroes may occur and should be included
                Value = groupDataReader
                    .GetCoreDataListForChildrenOfArea(grouping, period, area.Code)
                    .Where(x => x.IsValueValid)
                    .Average(x => x.Value);
            }
            else
            {
                SetSingleAreaValue(groupDataReader, area, grouping, period);
            }
        }
        public PracticeValueCollection Build(PracticeAxis axis1, PracticeAxis axis2)
        {
            bool isAreacode = !string.IsNullOrEmpty(AreaCode);

            // Indicator 1
            IndicatorMetadata indicatorMetadata1 = indicatorMetadataRepository.GetIndicatorMetadata(IndicatorId1);
            Grouping grouping1 = reader.GetGroupings(GroupId1, IndicatorId1, AreaTypeIds.GpPractice, SexId1, AgeId1).FirstOrDefault();
            TimePeriod period1 = new DataPointOffsetCalculator(grouping1, DataPointOffset, indicatorMetadata1.YearType).TimePeriod;

            if (axis1 == null)
            {
                axis1 = GetPracticeAxis(IndicatorId1, period1, SexId1, indicatorMetadata1);
                if (axis1 == null)
                {
                    return collection;
                }
            }
            collection.PracticeAxis1 = axis1;

            if (isAreacode)
            {
                CoreDataSet practiceData = reader.GetCoreData(grouping1, period1, AreaCode).FirstOrDefault();
                if (practiceData != null && practiceData.IsValueValid)
                {
                    collection.PracticeValue1 = practiceData.Value;
                }
            }

            // Indicator 2
            IndicatorMetadata indicatorMetadata2 = indicatorMetadataRepository.GetIndicatorMetadata(IndicatorId2);
            Grouping grouping2 = reader.GetGroupings(GroupId2, IndicatorId2, AreaTypeIds.GpPractice, SexId2, AgeId2).FirstOrDefault();
            TimePeriod period2 = new DataPointOffsetCalculator(grouping2, DataPointOffset, indicatorMetadata2.YearType).TimePeriod;

            if (axis2 == null)
            {
                axis2 = GetPracticeAxis(IndicatorId2, period2, SexId2, indicatorMetadata2);
                if (axis2 == null)
                {
                    return collection;
                }
            }
            collection.PracticeAxis2 = axis2;

            if (isAreacode)
            {
                CoreDataSet practiceData = reader.GetCoreData(grouping2, period2, AreaCode).FirstOrDefault();
                if (practiceData != null && practiceData.IsValueValid)
                {
                    collection.PracticeValue2 = practiceData.Value;
                }
            }

            AssignChildAreas(grouping1);

            return collection;
        }
        public Dictionary<int, IndicatorStatsResponse> GetIndicatorStats()
        {
            profileIds = _parameters.RestrictResultsToProfileIdList;
            parentArea = AreaFactory.NewArea(areasReader, _parameters.ParentAreaCode);

            var roots = GetRoots();
            var responseObjects = new Dictionary<int, IndicatorStatsResponse>();

            SetAreaCodesToIgnore();

            if (parentArea.IsCcg)
            {
                ccgPopulation = new CcgPopulationProvider(pholioReader).GetPopulation(parentArea.Code);
            }

            childAreaCount = new ChildAreaCounter(areasReader)
                .GetChildAreasCount(parentArea, _parameters.ChildAreaTypeId);

            int rootIndex = 0;
            foreach (var root in roots)
            {
                Grouping grouping = root.Grouping[0];
                IndicatorMetadata metadata = indicatorMetadataRepository.GetIndicatorMetadata(grouping);
                TimePeriod timePeriod = new DataPointOffsetCalculator(grouping, _parameters.DataPointOffset, metadata.YearType).TimePeriod;

                IEnumerable<double> values = GetValuesForStats(grouping, timePeriod);

                IndicatorStatsResponse statsAndStatF;
                if (values != null)
                {
                    IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsCalculator(values).GetStats();
                    var formatter = NumericFormatterFactory.New(metadata, groupDataReader);
                    indicatorStatsProcessor.Truncate(statsPercentiles);

                    statsAndStatF = new IndicatorStatsResponse
                    {
                        IID = metadata.IndicatorId,
                        Stats = statsPercentiles,
                        StatsF = formatter.FormatStats(statsPercentiles),
                        HaveRequiredValues = doEnoughAreasHaveValues
                    };
                }
                else
                {
                    // No stats calculated
                    statsAndStatF = new IndicatorStatsResponse
                    {
                        IID = metadata.IndicatorId,
                        HaveRequiredValues = doEnoughAreasHaveValues
                    };
                }
                responseObjects[rootIndex] = statsAndStatF;

                rootIndex++;
            }

            return responseObjects;
        }