Example #1
0
        public async Task <ActionResult> Reset()
        {
            var telemetry = new TelemetryClient();

            try
            {
                var token = APIUtil.Authenticate(this.Request, ConfigurationManager.AppSettings[ApplicationMetadataStore.AKAdminToken]);

                using (var wc = new WebClient())
                {
                    wc.Headers.Add($"Authorization: {token}");
                    wc.DownloadString(ConfigurationManager.AppSettings[ApplicationMetadataStore.AKTrainerURL] + "/reset");

                    var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[ApplicationMetadataStore.AKConnectionString]);
                    var blobClient     = storageAccount.CreateCloudBlobClient();
                    var blobContainer  = blobClient.GetContainerReference(ApplicationBlobConstants.OfflineEvalContainerName);
                    await Task.WhenAll(blobContainer
                                       .ListBlobs(useFlatBlobListing : true)
                                       .OfType <CloudBlockBlob>()
                                       .Select(b => b.UploadFromByteArrayAsync(new byte[0] {
                    }, 0, 0)));
                }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (UnauthorizedAccessException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, ex.Message));
            }
            catch (WebException ex)
            {
                telemetry.TrackException(ex);
                string response;
                using (var stream = new StreamReader(ex.Response.GetResponseStream()))
                {
                    response = await stream.ReadToEndAsync();
                }

                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString() + " " + response));
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()));
            }
        }
Example #2
0
        public async Task <ActionResult> Ranker(string defaultActions, string eventId)
        {
            try
            {
                APIUtil.Authenticate(this.Request);

                var client  = DecisionServiceClientFactory.AddOrGetExisting(ModelSuccessNotifier);
                var context = APIUtil.ReadBody(this.Request);
                if (string.IsNullOrEmpty(eventId))
                {
                    eventId = APIUtil.CreateEventId();
                }

                int[] actions;
                if (string.IsNullOrWhiteSpace(defaultActions))
                {
                    actions = await client.ChooseRankingAsync(eventId, context);
                }
                else
                {
                    int[] defaultActionArray = Array.ConvertAll(defaultActions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), Convert.ToInt32);
                    actions = await client.ChooseRankingAsync(eventId, context, defaultActionArray);
                }

                return(Json(new
                {
                    EventId = eventId,
                    Actions = actions,
                    ModelTime = ModelUpdateTime
                }));
            }
            catch (UnauthorizedAccessException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, ex.Message));
            }
            catch (Exception ex)
            {
                new TelemetryClient().TrackException(ex);
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()));
            }
        }
Example #3
0
        public ActionResult Reward(string eventId)
        {
            var telemetry = new TelemetryClient();

            try
            {
                APIUtil.Authenticate(this.Request);

                var client    = DecisionServiceClientFactory.AddOrGetExisting(ModelSuccessNotifier);
                var rewardStr = APIUtil.ReadBody(this.Request);

                var rewardObj = JToken.Parse(rewardStr);

                client.ReportOutcome(rewardObj, eventId);

                if (rewardObj.Type == JTokenType.Float)
                {
                    telemetry.TrackEvent("Reward", metrics: new Dictionary <string, double> {
                        { "Reward", rewardObj.ToObject <float>() }
                    });
                }
                else
                {
                    telemetry.TrackEvent("Reward");
                }

                // telemetry.TrackTrace($"HTTP Endpoint received reward report of: {rewardStr}");

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (UnauthorizedAccessException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, ex.Message));
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()));
            }
        }
        public static DecisionServiceClient <string> AddOrGetExisting(Action <byte[]> modelSuccessNotifier)
        {
            return(DecisionServiceStaticClient.AddOrGetExisting("single", _ =>
            {
                var telemetry = new TelemetryClient();
                string azureStorageConnectionString = ConfigurationManager.AppSettings[ApplicationMetadataStore.AKConnectionString];

                var storageAccount = CloudStorageAccount.Parse(azureStorageConnectionString);
                var blobClient = storageAccount.CreateCloudBlobClient();
                var settingsBlobContainer = blobClient.GetContainerReference(ApplicationBlobConstants.SettingsContainerName);
                var clientSettingsBlob = settingsBlobContainer.GetBlockBlobReference(ApplicationBlobConstants.LatestClientSettingsBlobName);

                //var settingsUrl = clientSettingsBlob.StorageUri.PrimaryUri.ToString();
                var settingsUrl = APIUtil.GetSettingsUrl();

                telemetry.TrackEvent($"DecisionServiceClient created: '{settingsUrl}'");

                var config = new DecisionServiceConfiguration(settingsUrl)
                {
                    InteractionUploadConfiguration = new BatchingConfiguration
                    {
                        MaxDuration = TimeSpan.FromSeconds(2),
                        UploadRetryPolicy = BatchUploadRetryPolicy.ExponentialRetry
                    },
                    ModelPollSuccessCallback = modelSuccessNotifier,
                    ModelPollFailureCallback = e => telemetry.TrackException(e, new Dictionary <string, string> {
                        { "Pool failure", "model" }
                    }),
                    SettingsPollFailureCallback = e => telemetry.TrackException(e, new Dictionary <string, string> {
                        { "Pool failure", "settings" }
                    }),
                    AzureStorageConnectionString = ConfigurationManager.AppSettings[ApplicationMetadataStore.AKConnectionString]
                };

                return DecisionService.CreateJson(config);
            }));
        }