public BenchmarkCriteria BuildFromSpecialComparisonCriteria(FinancialDataModel benchmarkSchoolData, SpecialCriteria specialCriteria, int tryCount = 0) { var criteria = new BenchmarkCriteria(); criteria.SchoolOverallPhase = new[] { "Special" }; if (specialCriteria.SimilarPupils.GetValueOrDefault()) { criteria.MinLowestAgePupils = benchmarkSchoolData.LowestAgePupils - CriteriaSearchConfig.SPECIALS_AGE_EXP_RANGE; criteria.MaxLowestAgePupils = benchmarkSchoolData.LowestAgePupils + CriteriaSearchConfig.SPECIALS_AGE_EXP_RANGE; criteria.MinHighestAgePupils = benchmarkSchoolData.HighestAgePupils - CriteriaSearchConfig.SPECIALS_AGE_EXP_RANGE; criteria.MaxHighestAgePupils = benchmarkSchoolData.HighestAgePupils + CriteriaSearchConfig.SPECIALS_AGE_EXP_RANGE; } foreach (var sen in specialCriteria.TopSenCriteria) { criteria.FindAndSetMaxMinSenInCriteria(sen, tryCount); } return(criteria); }
public async Task <ComparisonResult> GenerateBenchmarkListWithSpecialComparisonAsync(BenchmarkCriteria benchmarkCriteria, SpecialCriteria specialCriteria, FinancialDataModel defaultSchoolFinancialDataModel) { //STEP 1: Straight search with prefilled criteria var benchmarkSchools = await _financialDataService.SearchSchoolsByCriteriaAsync(benchmarkCriteria, EstablishmentType.All, true); if (benchmarkSchools.Count > ComparisonListLimit.SPECIALS) //Original query returns more than required. Clip to closest schools to the top SEN criteria. { var highestSENName = specialCriteria.TopSenCriteria[0].DataName; var highestSENValue = specialCriteria.TopSenCriteria[0].Original.GetValueOrDefault(); benchmarkSchools = benchmarkSchools.OrderBy(b => Math.Abs(b.getValueByCriteriaName(highestSENName).GetValueOrDefault() - highestSENValue)).Take(ComparisonListLimit.SPECIALS).ToList(); //Updating the criteria to reflect the final min and max used values used on top SEN var minValue = benchmarkSchools.Min(s => s.getValueByCriteriaName(highestSENName)); var maxValue = benchmarkSchools.Max(s => s.getValueByCriteriaName(highestSENName)); benchmarkCriteria.FindAndSetMaxMinSenInCriteria(highestSENName, minValue, maxValue); } //STEP 2: Original query returns less than required. Expand criteria values gradually and try this max 10 times var tryCount = 0; while (benchmarkSchools.Count < ComparisonListLimit.SPECIALS) { if (++tryCount > CriteriaSearchConfig.MAX_TRY_LIMIT) //Max query try reached. Return whatever is found. { break; } benchmarkCriteria = _benchmarkCriteriaBuilderService.BuildFromSpecialComparisonCriteria(defaultSchoolFinancialDataModel, specialCriteria, tryCount); benchmarkSchools = await _financialDataService.SearchSchoolsByCriteriaAsync(benchmarkCriteria, EstablishmentType.All, true); if (benchmarkSchools.Count > ComparisonListLimit.SPECIALS) //Number jumping to more than ideal. Clip to closest schools to the top SEN criteria. { var highestSENName = specialCriteria.TopSenCriteria[0].DataName; var highestSENValue = specialCriteria.TopSenCriteria[0].Original.GetValueOrDefault(); benchmarkSchools = benchmarkSchools.OrderBy(b => Math.Abs(b.getValueByCriteriaName(highestSENName).GetValueOrDefault() - highestSENValue)).Take(ComparisonListLimit.SPECIALS).ToList(); //Updating the criteria to reflect the final min and max used values used on top SEN var minValue = benchmarkSchools.Min(s => s.getValueByCriteriaName(highestSENName)); var maxValue = benchmarkSchools.Max(s => s.getValueByCriteriaName(highestSENName)); benchmarkCriteria.FindAndSetMaxMinSenInCriteria(highestSENName, minValue, maxValue); break; } } return(new ComparisonResult() { BenchmarkSchools = benchmarkSchools, BenchmarkCriteria = benchmarkCriteria }); }