public CcgCoreDataSetProvider(Area area, CcgPopulationProvider ccgPopulationProvider,
     CoreDataSetListProvider coreDataSetListProvider, IGroupDataReader groupDataReader)
     : base(area)
 {
     this.ccgPopulationProvider = ccgPopulationProvider;
     this.coreDataSetListProvider = coreDataSetListProvider;
     this.groupDataReader = groupDataReader;
 }
 private static void GetChildAreaData(Mock<GroupDataReader> reader, IArea area)
 {
     var data = new CoreDataSetListProvider(reader.Object)
         .GetChildAreaData(new Grouping(),area,new TimePeriod());
 }
 private IList<CoreDataSet> GetChildAreaDataList(IArea parentArea,
     GroupRoot groupRoot, Grouping grouping, TimePeriod timePeriod)
 {
     IList<CoreDataSet> dataList;
     if (parentArea.IsCountry)
     {
         if (area.IsGpPractice)
         {
             // No need to calculate rank of practice in whole country
             throw new FingertipsException("Do not use for GP Practices");
         }
         else
         {
             // All data in country
             dataList = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping,
                 parentArea, timePeriod);
             dataList = new CoreDataSetFilter(dataList).RemoveWithAreaCode(ignoredAreaCodes).ToList();
         }
     }
     else
     {
         // Use subnational data
         dataList = groupRoot.Data;
     }
     return dataList;
 }
 private static IEnumerable<CoreDataSet> GetDataList(Grouping grouping, TimePeriod timePeriod)
 {
     var groupDataReader = ReaderFactory.GetGroupDataReader();
     var data = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping, England(), timePeriod);
     return new CoreDataSetFilter(data).RemoveWithAreaCode(IgnoredAreaCodes());
 }
        private ICategoryComparer GetCategoryComparerWithValues(Dictionary<TimePeriod, ICategoryComparer> periodToComparer,
            CoreDataSetListProvider provider, Grouping grouping, TimePeriod period, string areaCode)
        {

            if (periodToComparer.ContainsKey(period) == false)
            {
                Area area = areasReader.GetAreaFromCode(areaCode);
                var childDataList = provider.GetChildAreaData(grouping, area, period);
                var comparatorValues = new CoreDataSetFilter(childDataList).SelectValidValues().ToList();

                // Get comparer
                var categoryComparer = (ICategoryComparer)NewIndicatorComparer();
                categoryComparer.SetDataForCategories(comparatorValues);
                periodToComparer[period] = categoryComparer;

                // Truncate last
                dataProcessor.FormatAndTruncateList(childDataList);
            }

            return periodToComparer[period];
        }
        private CoreDataSet GetFormattedValueData(TimePeriod period, IList<CoreDataSet> coreDataSetList, Grouping grouping,
            IEnumerable<string> childAreaCodes)
        {
            CoreDataSet benchmarkData = GetDataAtSpecificTimePeriod(coreDataSetList, period);

            var parentArea = new Area { Code = coreDataSetList.Select(x => x.AreaCode).FirstOrDefault() };
            if (benchmarkData == null && grouping != null)
            {
                //Get child area data only when necessary
                var childAreaData = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping, parentArea, period);
                var filteredChildAreaData = new CoreDataSetFilter(childAreaData).SelectWithAreaCode(childAreaCodes);
                benchmarkData = AverageCalculatorFactory.New(filteredChildAreaData, IndicatorMetadata).Average;
            }

            if (benchmarkData == null)
            {
                benchmarkData = CoreDataSet.GetNullObject(parentArea.Code);
            }


            dataProcessor.FormatAndTruncate(benchmarkData);
            return benchmarkData;
        }
        protected Dictionary<int, Significance> AssignSignificanceToTrendDataPoint(
            CoreDataSet data, Grouping grouping, TimePeriod period)
        {
            Dictionary<int, Significance> sig = new Dictionary<int, Significance>();

            // Benchmark comparisons
            if (comparer is ICategoryComparer == false)
            {
                // Compare against benchmarks
                foreach (KeyValuePair<int, IList<CoreDataSet>> keyValuePair in comparatorIdToComparatorTrendData)
                {
                    var comparatorId = keyValuePair.Key;
                    CoreDataSet comparatorCoreData =
                        GetDataAtSpecificTimePeriod(keyValuePair.Value, period);
                    var significance = comparer.Compare(data, comparatorCoreData, IndicatorMetadata);
                    sig.Add(comparatorId, significance);
                }
            }

            // Category comparison
            if (comparer is ICategoryComparer)
            {
                var provider = new CoreDataSetListProvider(groupDataReader);

                // Compare against benchmarks
                foreach (KeyValuePair<int, IList<CoreDataSet>> keyValuePair in comparatorIdToComparatorTrendData)
                {
                    var comparatorId = keyValuePair.Key;
                    ICategoryComparer categoryComparer;

                    switch (comparatorId)
                    {
                        case ComparatorIds.England:

                            // Get national comparer
                            categoryComparer = GetCategoryComparerWithValues(timePeriodToNationalCategoryComparer,
                                    provider, grouping, period, AreaCodes.England);
                            break;
                        default:

                            // Get subnational comparer
                            categoryComparer = GetCategoryComparerWithValues(timePeriodToSubnationalCategoryComparer,
                                    provider, grouping, period, data.AreaCode); ;
                            break;
                    }

                    sig.Add(comparatorId, (Significance)categoryComparer.GetCategory(data));
                }
            }

            // Compare against target
            if (targetComparer != null)
            {
                if (targetComparer as BespokeTargetPreviousYearEnglandValueComparer != null)
                {
                    var bespokeComparer = targetComparer as BespokeTargetPreviousYearEnglandValueComparer;

                    // Assign benchmark data to bespoke comparator
                    var comparatorTrendData = comparatorIdToComparatorTrendData[ComparatorIds.England];
                    bespokeComparer.BenchmarkData = GetDataAtSpecificTimePeriod(comparatorTrendData, period.GetTimePeriodForYearBefore());
                }
                else
                {
                    if (targetComparer as BespokeTargetPercentileRangeComparer != null)
                    {
                        var bespokeComparer = targetComparer as BespokeTargetPercentileRangeComparer;

                        var nationalValues = groupDataReader.GetCoreDataForAllAreasOfType(grouping, period);
                        var percentileCalculator = new BespokeTargetPercentileRangeCalculator(nationalValues.Where(x => x.IsValueValid).Select(x => x.Value).ToList());

                        bespokeComparer.LowerTargetPercentileBenchmarkData =
                            new CoreDataSet() { Value = percentileCalculator.GetPercentileValue(bespokeComparer.GetLowerTargetPercentile()) };

                        bespokeComparer.UpperTargetPercentileBenchmarkData =
                            new CoreDataSet() { Value = percentileCalculator.GetPercentileValue(bespokeComparer.GetUpperTargetPercentile()) };
                    }
                }

                sig.Add(ComparatorIds.Target, targetComparer.CompareAgainstTarget(data));
            }

            return sig;
        }
        private IEnumerable<double> GetValuesForStats(Grouping grouping, TimePeriod timePeriod)
        {
            IList<CoreDataSet> data = null;
            IList<double> values;

            if (parentArea.IsCountry)
            {
                // Optimisation for large number of areas
                values = groupDataReader.GetOrderedCoreDataValidValuesForAllAreasOfType(grouping, timePeriod,
                    areaCodesToIgnore);
            }
            else
            {
                data = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping, parentArea, timePeriod);
                data = new CoreDataSetFilter(data).RemoveWithAreaCode(areaCodesToIgnore).ToList();
                data = data.OrderBy(x => x.Value).ToList();
                values = new ValueListBuilder(data).ValidValues;
            }

            // Apply rules
            int areaTypeId = grouping.AreaTypeId;
            if (areaTypeId != AreaTypeIds.GpPractice)
            {
                doEnoughAreasHaveValues = IsRequiredNumberOfAreaValues(values) ||
                    ShouldSpineChartAlwaysBeAvailable(grouping.IndicatorId);

                if (doEnoughAreasHaveValues == false)
                {
                    values = null;
                }
            }
            else if (parentArea.IsCcg)
            {
                // CCG average of GP practices
                if (RuleShouldCcgAverageBeCalculated.Validates(grouping, data, ccgPopulation) == false)
                {
                    values = null;
                }
            }

            return values;
        }