public IList<TrendRoot> GetTrendData()
        {
            int profileId = _parameters.ProfileId;

            var parentArea = new ParentArea(_parameters.ParentAreaCode, _parameters.AreaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            // Do not repository as do not want results cached like this (need to be
            // cached by ID and areatype, i.e. repository by roots)
            GroupData data = new GroupDataBuilderByIndicatorIds
            {
                IndicatorIds = _parameters.IndicatorIds,
                ComparatorMap = comparatorMap,
                AreaTypeId = _parameters.AreaTypeId,
                RestrictSearchProfileIds = _parameters.RestrictResultsToProfileIdList,
                ProfileId = profileId
            }.Build();

            if (data.IsDataOk)
            {
                var groupDataReader = ReaderFactory.GetGroupDataReader();
                data.GroupRoots = new GroupRootFilter(groupDataReader).RemoveRootsWithoutChildAreaData(data.GroupRoots);
            }

            bool isParentAreaCodeNearestNeighbour = Area.IsNearestNeighbour(_parameters.ParentAreaCode);

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap,
                _parameters.AreaTypeId, profileId, data.IndicatorMetadata, isParentAreaCodeNearestNeighbour);

            return trendRoots;
        }
 public void TestBuild()
 {
     ComparatorMapBuilder builder = new ComparatorMapBuilder(new List<ParentArea> { GetRegion102(), GetShaForPct() });
     Assert.AreEqual(4, builder.ComparatorMap.Comparators.Count());
     Assert.AreEqual(2, (from c in builder.ComparatorMap.Comparators where c.ComparatorId == ComparatorIds.Subnational select c).Count());
     Assert.AreEqual(2, (from c in builder.ComparatorMap.Comparators where c.ComparatorId == ComparatorIds.England select c).Count());
 }
        public void TestComparatorMapGetRegionalComparatorByRegion()
        {
            ComparatorMapBuilder builder = new ComparatorMapBuilder(GetShaForPct());

            Assert.AreEqual(builder.ComparatorMap.GetRegionalComparatorByRegion(GetShaForPct()).ChildAreaTypeId,
                AreaTypeIds.Pct);
        }
        public void TestBuild()
        {
            var areaTypeId = AreaTypeIds.CountyAndUnitaryAuthority;
            var profileId = ProfileIds.Phof;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);

            var parentArea = new ParentArea(AreaCodes.Gor_NorthEast, areaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            GroupData data = new GroupDataBuilderByGroupings
            {
                GroupId = profile.GroupIds[0],
                ChildAreaTypeId = areaTypeId,
                ProfileId = profile.Id,
                ComparatorMap = comparatorMap,
                AssignData = true
            }.Build();

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap, areaTypeId, profileId,
                data.IndicatorMetadata, false);
            Assert.IsTrue(trendRoots.Count > 0);
            Assert.IsTrue(trendRoots[0].Periods.Count > 0);
            Assert.IsNotNull(trendRoots[0].Limits);
            Assert.IsTrue(trendRoots[0].ComparatorValueFs.Count > 0);

            string s = trendRoots[0].ComparatorValueFs[0][ComparatorIds.Subnational];
            Assert.AreNotEqual("-", s);
        }
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                DataDownloadParameters parameters = new DataDownloadParameters(context.Request.Params);

                int profileId = parameters.ProfileId;
                List<int> profileIds = parameters.RestrictResultsToProfileIdList;

                var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
                int areaTypeId = parameters.AreaTypeId;
                var parentAreas = GetParentAreas(parameters, areaTypeId);
                var subnationalAreaType = AreaTypeFactory.New(areasReader, parameters.SubnationalAreaTypeId);

                ComparatorMap comparatorMap = new ComparatorMapBuilder(parentAreas).ComparatorMap;
                BaseExcelFileInfo fileInfo;
                IWorkbook workbook = null;
                ExcelFileWriter writer = new ExcelFileWriter
                {
                    UseFileCache = ApplicationConfiguration.UseFileCache
                };
                byte[] bytes = null;
                if (parameters.UseIndicatorIds)
                {
                    fileInfo = new SearchResultsFileInfo();
                    workbook = new ProfileDataBuilder(comparatorMap, profile, profileIds,
                        parameters.IndicatorIds, parentAreas, subnationalAreaType).BuildWorkbook();
                }
                else
                {
                    fileInfo = new ProfileFileInfo(profileId, parentAreas.Select(x => x.AreaCode), areaTypeId, subnationalAreaType.Id);

                    if (ApplicationConfiguration.UseFileCache && fileInfo.DoesFileExist)
                    {
                        bytes = File.ReadAllBytes(fileInfo.FilePath);
                    }
                    else
                    {
                        workbook = new ProfileDataBuilder(comparatorMap, profile, profileIds,
                             parentAreas, subnationalAreaType).BuildWorkbook();
                    }
                }

                if (workbook != null)
                {
                    bytes = writer.Write(fileInfo, workbook);
                }

                HttpResponseBase response = new HttpContextWrapper(context).Response;
                ExportHelper.SetResponseAsExcelFile(response, DownloadedFileName + "." + fileInfo.FileExtension);
                response.BinaryWrite(bytes);
            }
            catch (Exception ex)
            {
                ExceptionLog.LogException(ex, context.Request.Url.AbsoluteUri);
            }

            context.Response.Flush();
        }
        public void TestComparatorMapLimitByAreaType()
        {
            ComparatorMapBuilder builder = new ComparatorMapBuilder(new List<ParentArea> { GetRegion102(), GetShaForPct() });
            Assert.AreEqual(4, builder.ComparatorMap.Count);

            ComparatorMap map = builder.ComparatorMap.LimitByParentArea(GetShaForPct());
            foreach (var comparator in map.Comparators)
            {
                Assert.AreEqual(AreaTypeIds.Pct, comparator.ChildAreaTypeId);
            }
        }
        public static IWorkbook GetWorkbookForProfile(int profileId)
        {
            var region = ReaderFactory.GetAreasReader().GetAreaFromCode(AreaCodes.Gor_EastOfEngland);
            var parentArea = new ParentArea(region.Code, AreaTypeIds.CountyAndUnitaryAuthority);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = AreaTypeIds.GoRegion;

            return new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook();
        }
        private static void InitWorksheets()
        {
            var region = ReaderFactory.GetAreasReader().GetAreaFromCode(AreaCodes.England);
            var parentArea = new ParentArea(region.Code, AreaTypeIds.StrategicClinicalNetwork);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profileId = ProfileIds.CardioVascularDisease;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = AreaTypeIds.Country;

            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)
                ).BuildWorkbook();
        }
        public GroupData GetGroupData(string parentAreaCode, int childAreaTypeId, int profileId, int groupId)
        {
            var parentArea = new ParentArea(parentAreaCode, childAreaTypeId);

            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            return new GroupDataBuilderByGroupings
                {
                    AssignAreas = AssignAreas,
                    AssignChildAreaData = AssignChildAreaData,
                    GroupId = groupId,
                    ProfileId = profileId,
                    ComparatorMap = comparatorMap,
                    ChildAreaTypeId = childAreaTypeId
                }.Build();
        }
        private static void InitWorksheets()
        {
            // Parameters
            var profileId = ProfileIds.Phof;
            var childAreaType = AreaTypeIds.GoRegion;
            var parentArea = new ParentArea(AreaCodes.England, childAreaType);
            var parentAreaTypeId = AreaTypeIds.Country;

            // Create workbook
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);
            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)
                ).BuildWorkbook();
        }
        private static void InitDeprivationDecilesForAllEnglandWorksheets()
        {
            // Parameters
            var profileId = ProfileIds.Phof;
            var childAreaType = AreaTypeIds.DistrictAndUnitaryAuthority;
            var parentArea = new ParentArea(AreaCodes.England, childAreaType);
            var parentAreaTypeId =
                CategoryAreaType.GetAreaTypeIdFromCategoryTypeId(
                    CategoryTypeIds.DeprivationDecileDistrictAndUA2015);

            // Create workbook
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);
            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            _workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook(); ;
        }
        private static void InitOneDecileWorksheets()
        {
            var category = new Category
            {
                Id = 5,
                CategoryTypeId = CategoryTypeIds.DeprivationDecileCountyAndUA2010,
                Name = "",
                ShortName = ""
            };
            var categoryArea = CategoryArea.New(category);

            var parentArea = new ParentArea(categoryArea.Code, AreaTypeIds.CountyAndUnitaryAuthority);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profileId = ProfileIds.Phof;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = CategoryAreaType.GetAreaTypeIdFromCategoryTypeId(
                CategoryTypeIds.DeprivationDecileCountyAndUA2010);
            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook();
        }
        public void TestDiabetesPrevAndRiskHighestQuintileExists()
        {
            var areaTypeId = AreaTypeIds.Ccg;
            var profileId = ProfileIds.Diabetes;

            var parentArea = new ParentArea(AreaCodes.CommissioningRegionLondon, areaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            GroupData data = new GroupDataBuilderByGroupings
            {
                GroupId = GroupIds.Diabetes_PrevalenceAndRisk,
                ChildAreaTypeId = areaTypeId,
                ProfileId = ProfileIds.Diabetes,
                ComparatorMap = comparatorMap,
                AssignData = true
            }.Build();

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap, areaTypeId, profileId,
                data.IndicatorMetadata, false);

            var highestQuintileCount = trendRoots.Select(x => x.DataPoints.Values.FirstOrDefault()[0].Significance).Count(significances => significances.ContainsValue((Significance)5));

            Assert.AreNotEqual(highestQuintileCount, 0);
        }
        public List<SparklineRoot> BuildRoots(string parentAreaCode, int areaTypeId,
            int profileId, int groupId, IList<string> dataTypes, int sexId)
        {
            var groupIds = profileReader.GetProfile(profileId).GroupIds;

            IList<IArea> childAreas = areasReader
                .GetChildAreas(parentAreaCode, areaTypeId).Cast<IArea>().ToList();
            childAreas = IgnoredAreasFilterFactory.New(profileId).RemoveAreasIgnoredEverywhere(childAreas).ToList();

            ComparatorMap comparatorMap = new ComparatorMapBuilder(new ParentArea(parentAreaCode, areaTypeId)).ComparatorMap;
            var parentArea = AreaFactory.NewArea(areasReader,parentAreaCode);
            parentCoreDataSetProvider = new CoreDataSetProviderFactory().New(parentArea);
            var nationalArea = AreaFactory.NewArea(areasReader, AreaCodes.England);
            nationalCoreDataSetProvider = new CoreDataSetProviderFactory().New(nationalArea);

            // Get grouping for time info
            IList<int> indicatorIds = groupDataReader.GetIndicatorIdsByGroupIdAndAreaTypeId(groupId, areaTypeId);

            DoubleOverlappingCIsComparer comparer = new DoubleOverlappingCIsComparer();
            IndicatorMetadataRepository repository = IndicatorMetadataRepository.Instance;

            List<SparklineRoot> roots = new List<SparklineRoot>();

            foreach (var indicatorId in indicatorIds)
            {
                var grouping = GetGrouping(groupIds, indicatorId, areaTypeId, sexId);

                IndicatorMetadata metadata = repository.GetIndicatorMetadata(grouping);

                IList<TimePeriod> timePeriods = grouping.GetTimePeriodIterator(metadata.YearType).TimePeriods;

                SparklineRoot root = new SparklineRoot();
                roots.Add(root);

                List<ValueWithCIsData> allData = new List<ValueWithCIsData>();

                foreach (IArea area in childAreas)
                {
                    SparklineArea sparklineArea = new SparklineArea();
                    int startingAllDataCount = allData.Count;

                    foreach (TimePeriod timePeriod in timePeriods)
                    {
                        SparklineTimePoint timePoint = new SparklineTimePoint();
                        sparklineArea.TimePoints.Add(timePoint);

                        foreach (string dataType in dataTypes)
                        {
                            var data = GetData(area, dataType, grouping, timePeriod, allData);
                            timePoint.Data.Add(dataType, data);
                        }

                        // Are different? (comparatorId=10 “Within area 80/20 by deprivation”)
                        if (timePoint.Data.Count > 0)
                        {
                            ValueWithCIsData p1 = timePoint.Data[dataTypes[0]];
                            ValueWithCIsData p2 = timePoint.Data[dataTypes[1]];

                            Significance sig = comparer.Compare(p1, p2, metadata);
                            timePoint.AreDifferent = sig == Significance.Better || sig == Significance.Worse;
                        }
                    }

                    // Do not include areas with no data
                    if (allData.Count > startingAllDataCount)
                    {
                        root.AreaData.Add(area.Code, sparklineArea);
                    }
                }

                // Add statsPercentiles
                if (allData.Count > 0)
                {
                    IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles
                    {
                        Min = (from d in allData select d.LowerCI).Min<double>(),
                        Max = (from d in allData select d.UpperCI).Max<double>()
                    };

                    var builder = new TimePeriodTextListBuilder(metadata);
                    builder.AddRange(timePeriods);
                    IList<string> xLabels = builder.GetTimePeriodStrings();

                    SparklineStats sparklineStats = new SparklineStats(xLabels);
                    sparklineStats.IndicatorId = grouping.IndicatorId;
                    sparklineStats.Limits = new MinMaxRounder(statsPercentiles.Min, statsPercentiles.Max).Limits;
                    root.Stats = sparklineStats;

                    // Format
                    NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(metadata, statsPercentiles);
                    new ValueWithCIsDataProcessor(formatter).FormatAndTruncateList(allData);

                    // Add comparator data
                    foreach (var comparator in comparatorMap.Comparators)
                    {
                        List<string> values = GetFormattedComparatorValues(timePeriods, metadata, grouping, formatter,
                            comparator.ComparatorId);
                        sparklineStats.ComparatorValues.Add(comparator.ComparatorId, values);
                    }
                }
            }

            return roots;
        }
 public void TestComparatorMapGetNationalComparatorWorksIfTwoNationalComparators()
 {
     ComparatorMapBuilder builder = new ComparatorMapBuilder(new List<ParentArea> { GetRegion102(), GetShaForPct() });
     Assert.AreEqual(builder.ComparatorMap.GetNationalComparator().Area.Code, AreaCodes.England);
 }
        private void CreateExcelFilesForCoreProfiles(int profileId, int areaTypeId, int subnationalAreaTypeId)
        {
            try
            {
                var profile = _profileReader.GetProfile(profileId);
                var parentAreas = new List<ParentArea> { new ParentArea(AreaCodes.England, areaTypeId) };
                var subnationalAreaType = AreaTypeFactory.New(_areasReader, subnationalAreaTypeId);

                var comparatorMap = new ComparatorMapBuilder(parentAreas).ComparatorMap;

                var dummyListOfprofileIds = new List<int>();

                IWorkbook workbook = new ProfileDataBuilder(comparatorMap, profile, dummyListOfprofileIds,
                        parentAreas, subnationalAreaType).BuildWorkbook();

                if (workbook != null)
                {
                    BaseExcelFileInfo fileInfo = new ProfileFileInfo(profileId,
                        parentAreas.Select(x => x.AreaCode), areaTypeId, subnationalAreaType.Id);

                    new ExcelFileWriter
                    {
                        UseFileCache = ApplicationConfiguration.UseFileCache
                    }.Write(fileInfo, workbook);
                }
            }
            catch (Exception ex)
            {
                var message = "Failed to create excel file " +
                    GetProfileDetailsText(profileId, subnationalAreaTypeId, areaTypeId);
                _logger.Error(message);
                NLogHelper.LogException(_logger, ex);
                ExceptionLog.LogException(new FingertipsException(message, ex), null);
            }
        }
 public void TestComparatorMapGetRegionalComparator()
 {
     ComparatorMapBuilder builder = new ComparatorMapBuilder(GetShaForPct());
     Assert.AreEqual(builder.ComparatorMap.GetSubnationalComparator().Area.Code, AreaCodes.Sha_EastOfEngland);
 }
 public void TestComparatorMapGetRegionalComparatorFailsIfTwoRegionalComparators()
 {
     ComparatorMapBuilder builder = new ComparatorMapBuilder(new List<ParentArea> { GetRegion102(), GetShaForPct() });
     builder.ComparatorMap.GetSubnationalComparator();
 }