public PartitionTrendData GetPartitionTrendData(int profileId,
            string areaCode, int indicatorId, int ageId, int sexId, int categoryTypeId, int areaTypeId)
        {
            InitGrouping(profileId, areaTypeId, indicatorId, sexId, ageId);
            InitMetadata(_grouping);
            var categories = _areasReader.GetCategories(categoryTypeId);
            var dictionaryBuilder = new PartitionTrendDataDictionaryBuilder(
                categories.Cast<INamedEntity>().ToList(), PartitionDataType.Category);

            // Add data for each time period
            var timePeriods = _grouping.GetTimePeriodIterator(_indicatorMetadata.YearType).TimePeriods;
            foreach (var timePeriod in timePeriods)
            {
                IList<CoreDataSet> dataList = _groupDataReader.GetAllCategoryDataWithinParentArea(areaCode,
                indicatorId, sexId, ageId, timePeriod).Where(x => x.CategoryTypeId == categoryTypeId).ToList();
                dictionaryBuilder.AddDataForNextTimePeriod(dataList);
            }

            // Remove entities without data from dictionary
            var allData = dictionaryBuilder.AllDataAsList;

            // Return trend data
            timePeriods = RemoveEarlyEmptyTimePeriods(dictionaryBuilder, timePeriods);
            FormatData(allData);
            var limits = new LimitsBuilder().GetLimits(allData);
            return new PartitionTrendData
            {
                Limits = limits,
                Labels = categories.Cast<INamedEntity>().ToList(),
                TrendData = dictionaryBuilder.Dictionary,
                Periods = GetTimePeriodStrings(timePeriods)
            };
        }
 private static void AddNullData(PartitionTrendDataDictionaryBuilder builder)
 {
     builder.AddDataForNextTimePeriod(new List<CoreDataSet>
     {
         new CoreDataSet {SexId = SexIds.NotApplicable,Value = 1},
         new CoreDataSet {SexId = Sex2,Value = ValueData.NullValue},
     });
 }
 protected static IList<TimePeriod> RemoveEarlyEmptyTimePeriods(PartitionTrendDataDictionaryBuilder dictionaryBuilder,
     IList<TimePeriod> timePeriods)
 {
     int earliestIndexRemoved = dictionaryBuilder.RemoveEarlyEmptyYears();
     return earliestIndexRemoved > -1
         ? timePeriods.Skip(earliestIndexRemoved + 1).ToList()
         : timePeriods;
 }
        public void When_Non_Valid_Early_Data_Then_Remove_Index_Is_First_With_Data()
        {
            var entities = new List<INamedEntity>
            {
                new Sex {Id = Sex1},
                new Sex {Id = Sex2},
            };

            var builder = new PartitionTrendDataDictionaryBuilder(entities, PartitionDataType.Sex);

            AddNullData(builder);
            AddNullData(builder);
            AddValidData(builder);

            var index = builder.RemoveEarlyEmptyYears();

            // Assert
            Assert.AreEqual(1, index);
            Assert.AreEqual(1, builder.Dictionary[Sex2].Count, "Expect 1 data point because two were removed");
        }
        public PartitionTrendData GetPartitionTrendData(int profileId,
            string areaCode, int indicatorId, int ageId, int areaTypeId)
        {
            InitGrouping(profileId, areaTypeId, indicatorId, ageId);
            InitMetadata(_grouping);
            var sexes = _pholioReader.GetAllSexes();
            var dictionaryBuilder = new PartitionTrendDataDictionaryBuilder(
                sexes.Cast<INamedEntity>().ToList(), PartitionDataType.Sex);

            // Add data for each time period
            var timePeriods = _grouping.GetTimePeriodIterator(_indicatorMetadata.YearType).TimePeriods;
            foreach (var timePeriod in timePeriods)
            {
                IList<CoreDataSet> dataList = _groupDataReader.GetCoreDataForAllSexes(
                    indicatorId, timePeriod, areaCode, ageId);
                dictionaryBuilder.AddDataForNextTimePeriod(dataList);
            }

            // Remove entities without data from dictionary
            var allData = dictionaryBuilder.AllDataAsList;
            var sexesWithData = GetSexesFromDataList(allData);
            foreach (var sex in sexes)
            {
                if (sexesWithData.Contains(sex) == false)
                {
                    dictionaryBuilder.RemoveEntity(sex.Id);
                }
            }

            // Return trend data
            timePeriods = RemoveEarlyEmptyTimePeriods(dictionaryBuilder, timePeriods);
            FormatData(allData);
            var limits = new LimitsBuilder().GetLimits(allData);
            return new PartitionTrendData
            {
                Limits = limits,
                Labels = sexesWithData.Cast<INamedEntity>().ToList(),
                TrendData = dictionaryBuilder.Dictionary,
                Periods = GetTimePeriodStrings(timePeriods)
            };
        }
 private static void AddValidData(PartitionTrendDataDictionaryBuilder builder)
 {
     builder.AddDataForNextTimePeriod(new List<CoreDataSet>
     {
         new CoreDataSet {SexId = Sex1,Value = 1},
         new CoreDataSet {SexId = Sex2,Value = 1},
     });
 }
        private static PartitionTrendDataDictionaryBuilder GetPartitionTrendDataDictionaryBuilderWithDataAdded()
        {
            var entities = new List<INamedEntity>
            {
                new Sex {Id = Sex1},
                new Sex {Id = Sex2},
            };

            var builder = new PartitionTrendDataDictionaryBuilder(entities, PartitionDataType.Sex);

            AddValidData(builder);
            AddValidData(builder);

            return builder;
        }