public async Task <Product[]> GetRecommendationsAsync(RecommendationEvalContext context)
        {
            var dynamicAssociationsContext = context as DynamicAssociationsEvalContext;

            if (dynamicAssociationsContext == null)
            {
                throw new InvalidCastException(nameof(context));
            }

            var cacheKey = CacheKey.With(GetType(), nameof(GetRecommendationsAsync), context.GetCacheKey());

            return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(RecommendationsCacheRegion.CreateChangeToken());
                cacheEntry.AddExpirationToken(_apiChangesWatcher.CreateChangeToken());

                var result = new List <Product>();
                var recommendedProductIds = await _associationsApi.EvaluateDynamicAssociationsAsync(dynamicAssociationsContext.ToContextDto());

                if (recommendedProductIds != null)
                {
                    result.AddRange(await _catalogService.GetProductsAsync(recommendedProductIds.ToArray(), ItemResponseGroup.Seo | ItemResponseGroup.Outlines | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory));
                }

                return result.ToArray();
            }));
        }
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }

            if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var retVal   = new RecommendationEvalContext();
            var provider = bindingContext.ValueProvider.GetValue("provider");

            if (provider != null && provider.RawValue != null && provider.RawValue.ToString().EqualsInvariant("Cognitive"))
            {
                retVal = new CognitiveRecommendationEvalContext();
            }
            controllerContext.HttpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
            var reader   = new StreamReader(controllerContext.HttpContext.Request.InputStream);
            var bodyText = reader.ReadToEnd();
            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            JsonConvert.PopulateObject(bodyText, retVal, settings);
            return(retVal);
        }
Exemplo n.º 3
0
        public async Task <Product[]> GetRecommendationsAsync(RecommendationEvalContext context)
        {
            var products = await _catalogService.GetProductsAsync(context.ProductIds.ToArray(), ItemResponseGroup.ItemInfo);

            var result = products.SelectMany(x => x.Associations).Take(context.Take).Select(x => x.Product).ToArray();

            return(result);
        }
        public async Task <Product[]> GetRecommendationsAsync(RecommendationEvalContext context)
        {
            Product[] products = await _catalogService.GetProductsAsync(context.ProductIds.ToArray(), ItemResponseGroup.ItemAssociations);

            //Need to load related products from associated product and categories
            var retVal = products.SelectMany(p => p.Associations.OfType <ProductAssociation>().OrderBy(x => x.Priority))
                         .Select(a => a.Product).ToList();

            retVal.AddRange(products.SelectMany(p => p.Associations.OfType <CategoryAssociation>().OrderBy(x => x.Priority))
                            .SelectMany(a => a.Category.Products.ToArray()));

            return(retVal.Take(context.Take).ToArray());
        }
        public async Task <ActionResult> GetRecommendations([FromBody] RecommendationEvalContext evalContext)
        {
            var recommendationService = _providerFactory.GetProvider(evalContext.Provider);

            if (recommendationService == null)
            {
                throw new NotSupportedException(evalContext.Provider);
            }
            evalContext.StoreId = WorkContext.CurrentStore.Id;
            evalContext.UserId  = WorkContext.CurrentUser.Id;
            var result = await recommendationService.GetRecommendationsAsync(evalContext);

            return(Json(result));
        }
        public async Task <ActionResult> GetRecommendations(RecommendationEvalContext evalContext)
        {
            var recommendationService = _services.FirstOrDefault(x => x.ProviderName.EqualsInvariant(evalContext.Provider));

            if (recommendationService == null)
            {
                throw new NotSupportedException(evalContext.Provider);
            }
            evalContext.StoreId = WorkContext.CurrentStore.Id;
            evalContext.UserId  = WorkContext.CurrentCustomer.Id;
            var result = await recommendationService.GetRecommendationsAsync(evalContext);

            return(Json(result));
        }
Exemplo n.º 7
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object retVal       = new RecommendationEvalContext();
            var    obj          = JObject.Load(reader);
            var    providerName = obj.SelectToken("provider");

            if (providerName != null)
            {
                var provider = _providerFactory.GetProvider(providerName.Value <string>());
                if (provider != null)
                {
                    retVal = provider.CreateEvalContext();
                }
            }
            serializer.Populate(obj.CreateReader(), retVal);
            return(retVal);
        }
        public async Task <string[]> GetRecommendationsAsync(RecommendationEvalContext context)
        {
            AzureRecommendationType azureRecommendationType;
            var isAzureRecommendations = Enum.TryParse(context.Type, out azureRecommendationType);

            if (!isAzureRecommendations)
            {
                throw new NotSupportedException();
            }

            var settins = GetRecommendationsSettings(context.StoreId);

            string[] result;
            if (azureRecommendationType == AzureRecommendationType.User2Item)
            {
                result = await _azureRecommendationsClient.GetCustomerRecommendationsAsync(settins.ApiKey, settins.BaseUrl, context.ModelId,
                                                                                           context.UserId, context.BuildId, context.Take, context.ProductIds.ToArray());
            }
            else
            {
                throw new NotSupportedException();
            }
            return(result);
        }
Exemplo n.º 9
0
        public async Task <IHttpActionResult> GetRecommendations(RecommendationEvalContext context)
        {
            var result = await _recommendationsService.GetRecommendationsAsync(context);

            return(Ok(result));
        }