Ejemplo n.º 1
0
        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);
            }));
        }
Ejemplo n.º 2
0
        public async Task <HttpResponseMessage> Post(string eventId)
        {
            var userToken = Request.Headers.SingleOrDefault(x => x.Key == "Authorization").Value.First();

            if (string.IsNullOrWhiteSpace(userToken))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }
            if (userToken != ConfigurationManager.AppSettings["UserToken"])
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            var telemetry = new TelemetryClient();
            var stopwatch = Stopwatch.StartNew();

            try
            {
                telemetry.Context.Operation.Name = "Reward";
                telemetry.Context.Operation.Id   = eventId;

                // support simply float and complex JSON outcomes
                var rewardStr = await Request.Content.ReadAsStringAsync();

                var rewardObj = JToken.Parse(rewardStr);

                // parse input
                var guid = Guid.ParseExact(eventId, "N");

                var url           = ConfigurationManager.AppSettings["DecisionServiceSettingsUrl"];
                var eventUploader = DecisionServiceStaticClient.AddOrGetExisting("uploader" + url,
                                                                                 _ =>
                {
                    telemetry.TrackEvent("EventUploader creation");

                    var metaData = ApplicationMetadataUtil.DownloadMetadata <ApplicationClientMetadata>(url);
                    return(new EventUploaderASA(
                               metaData.EventHubObservationConnectionString,
                               new BatchingConfiguration
                    {
                        // TODO: these are not production ready configurations. do we need to move those to C&C as well?
                        MaxBufferSizeInBytes = 1,
                        MaxDuration = TimeSpan.FromSeconds(1),
                        MaxEventCount = 1,
                        MaxUploadQueueCapacity = 1,
                        UploadRetryPolicy = BatchUploadRetryPolicy.ExponentialRetry
                    }));
                });

                eventUploader.Upload(new Observation
                {
                    Key   = guid.ToString("N", CultureInfo.InvariantCulture),
                    Value = rewardObj
                });

                stopwatch.Stop();
                telemetry.TrackRequest("ReportReward", DateTime.Now, stopwatch.Elapsed, "200", true);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                telemetry.TrackException(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
 public static DecisionServiceClient <string> AddOrGetExisting(string settingsUrl)
 {
     return(DecisionServiceStaticClient.AddOrGetExisting(settingsUrl, _ => DecisionService.CreateJson(CreateConfiguration(settingsUrl))));
 }