Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> Post()
        {
            var header = this.Request.Headers.SingleOrDefault(x => x.Key == "Authorization");

            if (header.Value == null)
            {
                throw new UnauthorizedAccessException("AuthorizationToken missing");
            }

            var userToken = header.Value.First();

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

            if (this.metaData == null || lastDownload + TimeSpan.FromMinutes(1) < DateTime.Now)
            {
                var url = ConfigurationManager.AppSettings["DecisionServiceSettingsUrl"];
                this.metaData = ApplicationMetadataUtil.DownloadMetadata <ApplicationClientMetadata>(url);
                lastDownload  = DateTime.Now;
            }

            using (var vw = new VowpalWabbit(new VowpalWabbitSettings(metaData.TrainArguments)
            {
                EnableStringExampleGeneration = true,
                EnableStringFloatCompact = true
            }))
                using (var serializer = new VowpalWabbitJsonSerializer(vw))
                    using (var example = serializer.ParseAndCreate(new JsonTextReader(new StreamReader(await Request.Content.ReadAsStreamAsync()))))
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, example.VowpalWabbitString));
                    }
        }
Ejemplo n.º 2
0
        public ActionResult Validate()
        {
            try
            {
                APIUtil.Authenticate(this.Request);

                if (this.metaData == null || lastDownload + TimeSpan.FromMinutes(1) < DateTime.Now)
                {
                    var url = APIUtil.GetSettingsUrl();
                    this.metaData = ApplicationMetadataUtil.DownloadMetadata <ApplicationClientMetadata>(url);
                    lastDownload  = DateTime.Now;
                }

                var context = APIUtil.ReadBody(this.Request);
                using (var vw = new VowpalWabbit(new VowpalWabbitSettings(metaData.TrainArguments)
                {
                    EnableStringExampleGeneration = true,
                    EnableStringFloatCompact = true
                }))
                    using (var serializer = new VowpalWabbitJsonSerializer(vw))
                        using (var example = serializer.ParseAndCreate(context))
                        {
                            return(Json(new { VWExample = example.VowpalWabbitString }));
                        }
            }
            catch (UnauthorizedAccessException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, ex.Message));
            }
            catch (Exception ex)
            {
                new TelemetryClient().TrackException(ex);
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()));
            }
        }
Ejemplo n.º 3
0
        private static ApplicationClientMetadata DownloadMetadata(DecisionServiceConfiguration config, ApplicationClientMetadata metaData)
        {
            if (!config.OfflineMode || metaData == null)
            {
                metaData = ApplicationMetadataUtil.DownloadMetadata <ApplicationClientMetadata>(config.SettingsBlobUri);
                if (config.LogAppInsights)
                {
                    Trace.Listeners.Add(new ApplicationInsights.TraceListener.ApplicationInsightsTraceListener(metaData.AppInsightsKey));
                }
            }

            return(metaData);
        }
Ejemplo n.º 4
0
        protected void ConfigureDecisionService(string trainArguments = null, float?initialExplorationEpsilon = null, bool?isExplorationEnabled = null)
        {
            using (var wc = new WebClient())
            {
                wc.Headers.Add($"Authorization: {managementPassword}");
                var query = new List <string>();
                if (trainArguments != null)
                {
                    query.Add("trainArguments=" + HttpUtility.UrlEncode(trainArguments));
                }
                if (initialExplorationEpsilon != null)
                {
                    query.Add("initialExplorationEpsilon=" + initialExplorationEpsilon);
                }
                if (isExplorationEnabled != null)
                {
                    query.Add("isExplorationEnabled=" + isExplorationEnabled);
                }

                var url = managementCenterUrl + "/Automation/UpdateSettings";
                if (query.Count > 0)
                {
                    url += "?" + string.Join("&", query);
                }

                wc.DownloadString(url);
            }

            // validate
            var metaData = ApplicationMetadataUtil.DownloadMetadata <ApplicationClientMetadata>(settingsUrl);

            if (trainArguments != null)
            {
                Assert.AreEqual(trainArguments, metaData.TrainArguments);
            }

            if (initialExplorationEpsilon != null)
            {
                Assert.AreEqual((float)initialExplorationEpsilon, metaData.InitialExplorationEpsilon);
            }

            if (isExplorationEnabled != null)
            {
                Assert.AreEqual((bool)isExplorationEnabled, metaData.IsExplorationEnabled);
            }
        }
Ejemplo n.º 5
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));
            }
        }