Ejemplo n.º 1
0
        public async Task <PredictionResponse> Post(PredictionRequest request)
        {
            var year         = DateTime.Now.Year;
            var tasks        = new Dictionary <int, AsyncUnaryCall <CalculatePredictionResponse> >();
            var yearAverages = new Dictionary <int, double>();

            for (int y = year; y >= year - 4; y--)
            {
                tasks.Add(y, agents[y % agents.Count].GetProbabilityAsync(request.ToAgentRequest(y)));
            }

            foreach (var task in tasks)
            {
                yearAverages.Add(task.Key, (await task.Value).Probability);
            }

            var expectation  = calculator.GetAverageOfPreviousYears(yearAverages);
            var recentCrimes = await statisticProvider.GetCrimesOneMonthBack();

            var lastMonthData = calculator.FindDaysSinceLastCrime(CaseSimple.FromApiRequest(request), recentCrimes);
            var probability   = calculator.CalculateCrimeProbability(expectation, lastMonthData);

            return(new PredictionResponse {
                Probability = probability
            });
        }