public async Task <ActionResult> Index(Donater donater)
        {
            var response = await this.donationExperienceService.GetValuesForDonator(donater);

            TempData["response"] = response;
            return(RedirectToAction("Suggestions"));
        }
Example #2
0
 public static void AddDonater(Donater Donater)
 {
     using (BodonateDbContext db = new BodonateDbContext())
     {
         db.Donaters.Add(Donater);
         db.SaveChanges();
     }
 }
        public async Task <DonationExperienceValues> GetValuesForDonator(Donater donater)
        {
            var ai       = new TelemetryClient();
            var response = await donationPredictionService.InvokeRequestResponseService(donater);

            if (response.Success)
            {
                try
                {
                    var json            = JObject.Parse(response.Response);
                    var columnNames     = json["Results"]["output1"]["value"]["ColumnNames"].ToArray();
                    var scoreArrayIndex = Array.IndexOf(columnNames, "Scored Labels");
                    var score           = json["Results"]["output1"]["value"]["Values"].ToArray()[0].ToArray()[scoreArrayIndex];

                    return(new DonationExperienceValues
                    {
                        Score = score.ToString(),
                        Values = SuggestedValuesFromScore((double)score, donationExperienceSettings.SuggestionCount, donationExperienceSettings.Multiplier),
                        Success = true,
                        Message = response.Response,
                        ExperienceSettings = donationExperienceSettings,
                        RequestTime = response.RequestTime
                    });
                }
                catch (Exception ex)
                {
                    ai.TrackException(ex);
                    return(new DonationExperienceValues
                    {
                        Success = false,
                        Message = ex.ToString()
                    });
                }
            }
            ai.TrackException(new Exception(response.Response));
            return(new DonationExperienceValues
            {
                Success = false,
                Message = response.Response
            });
        }
        public async Task <DonationPredictionResponse> InvokeRequestResponseService(Donater donater)
        {
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary <string, StringTable>
                    {
                        {
                            "input1",
                            new StringTable
                            {
                                ColumnNames =
                                    new[]
                                {
                                    "Age", "Sex", "Income", "Zip", "Education", "MaritalStatus", "Children",
                                    "Interactions", "Amount", "WeekDay", "Month", "Hour", "Location", "Device",
                                    "Profile"
                                },
                                Values =
                                    new[, ]
                                {
                                    {
                                        donater.Age.ToString(),
                    donater.Sex,
                    donater.Income.ToString(),
                    donater.Zip,
                    donater.Education,
                    donater.MaritalStatus,
                    donater.Children,
                    donater.Interactions.ToString(),
                    donater.Amount.ToString(),
                    donater.WeekDay.ToString(),
                    donater.Month.ToString(),
                    donater.Hour.ToString(),
                    donater.Location,
                    donater.Device,
                    donater.Profile
                                    }
                                }
                            }
                        }
                    },
                    GlobalParameters = new Dictionary <string, string>()
                    {
                        { "Allow unknown categorical levels", "true" }
                    }
                };
                var apiKey = configuration.ApiKey; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                client.BaseAddress = configuration.Uri;
                HttpResponseMessage response = null;
                var ai        = new TelemetryClient();
                var startTime = DateTime.UtcNow;
                var timer     = System.Diagnostics.Stopwatch.StartNew();
                try
                {
                    response = await client.PostAsJsonAsync("", scoreRequest);
                }
                finally
                {
                    timer.Stop();
                    ai.TrackDependency("MachineLearning", "ApiCall", startTime, timer.Elapsed, response != null && response.IsSuccessStatusCode);
                }

                if (!response.IsSuccessStatusCode)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();;
                    return(new DonationPredictionResponse()
                    {
                        RequestTime = timer.Elapsed, Success = false, Response =
                            $"status:{response.StatusCode}\r\nheaders:{response.Headers}\r\ncontent:{responseContent}\r\n"
                    });
                }
                var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(new DonationPredictionResponse()
                {
                    RequestTime = timer.Elapsed, Success = true, Response = result
                });
            }
        }