Beispiel #1
0
        public async Task <string> UpdateProducts([FromBody] AddToCartLog logData)
        {
            try
            {
                var host = await _hostCache.GetHostByIdAsync(logData.HostId);

                if (host == null || host.Id == 0)
                {
                    return(CommonStrings.NoHost);
                }
                #region SecurityCheck
                if (_configurations.SecurityCheck)
                {
                    var origin = Request.Headers[CommonStrings.Origin].ToString();
                    if (!string.IsNullOrEmpty(origin))
                    {
                        var aut = new Uri(origin).Host;
                        aut = aut.ToLower();
                        var topdomain = string.Empty;
                        if (aut.IndexOf(CommonStrings.Dot) == aut.LastIndexOf(CommonStrings.Dot))
                        {
                            topdomain = aut;
                        }
                        else
                        {
                            topdomain = aut.Substring(aut.IndexOf(CommonStrings.Dot) + 1);
                        }
                        if (host.Host != topdomain)
                        {
                            return(CommonStrings.NoHostAccess);
                        }
                    }
                }
                #endregion

                var productad = logData.ProductData.OrderByDescending(c => c.ProductId).ToList();
                if (!productad.Any())
                {
                    return(string.Empty);
                }
                await _hostScriptChecker.UpdateCartValidation(logData.HostId);

                await _totalVisitUpdater.UpdateTotalVisit(logData.HostId);

                foreach (var item in productad)
                {
                    var cachedProduct = await _productCache.FindProduct(logData.HostId, item.ProductId);

                    if (cachedProduct.Id != string.Empty)
                    {
                        await _kafkaLogger.SendMessage(new DruidData()
                        {
                            CategoryName = cachedProduct.CategoryName,
                            Date         = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"),
                            HostName     = host.Host,
                            ImageAddress = cachedProduct.ImageAddress,
                            Price        = cachedProduct.Price,
                            ProductName  = cachedProduct.ProductName,
                            PageAddress  = cachedProduct.Url,
                            ProductId    = item.ProductId,
                            StatType     = StatTypes.AddToCart,
                            UserId       = logData.UserId.ToString(),
                            HostId       = logData.HostId.ToString(),
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError("Operations:AdToCart->" + ex.Message);
            }



            return("");
        }
Beispiel #2
0
        public async Task <string> LogUser([FromBody] PageLogRequest pageData)
        {
            try
            {
                var innerAddress = new Uri(pageData.Address).PathAndQuery;
                var host         = await _hostCache.GetHostByIdAsync(pageData.HostId);

                if (host == null || host.Id == 0)
                {
                    return(CommonStrings.NoHost);
                }
                #region SecurityCheck
                if (_configurations.SecurityCheck)
                {
                    var origin = Request.Headers[CommonStrings.Origin].ToString();
                    if (!string.IsNullOrEmpty(origin))
                    {
                        var aut = new Uri(origin).Host;
                        aut = aut.ToLower();
                        var topdomain = string.Empty;
                        if (aut.IndexOf(CommonStrings.Dot) == aut.LastIndexOf(CommonStrings.Dot))
                        {
                            topdomain = aut;
                        }
                        else
                        {
                            topdomain = aut.Substring(aut.IndexOf(CommonStrings.Dot) + 1);
                        }
                        if (host.Host != topdomain)
                        {
                            return(CommonStrings.NoHostAccess);
                        }
                    }
                }
                #endregion

                if (pageData != null && (await _crowlerCache.IsCrowler(pageData.UserId)))
                {
                    return("");
                }
                await _totalVisitUpdater.UpdateTotalVisit(pageData.HostId);

                await _hostScriptChecker.UpdatePageValidation(pageData.HostId);

                await _kafkaLogger.SendMessage(new DruidData()
                {
                    CategoryName = "",
                    Date         = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    HostName     = host.Host,
                    ImageAddress = "",
                    Price        = 0,
                    ProductName  = "",
                    PageAddress  = innerAddress,
                    ProductId    = "",
                    StatType     = StatTypes.PageView,
                    UserId       = pageData.UserId.ToString(),
                    HostId       = pageData.HostId.ToString(),
                });
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError("Operation:PageLogger =>" + "User=>" + pageData.UserId + ":" + ex.Message);
            }
            return("");
        }
        public async Task <string> UpdateProducts([FromBody] ProductLog logData)
        {
            try
            {
                var fiProducts = logData.ProductData.OrderByDescending(c => c.ProductId).Take(20);
                var host       = await _hostCache.GetHostByIdAsync(logData.HostId).ConfigureAwait(false);

                if (host == null || host.Id == 0)
                {
                    return(CommonStrings.NoHost);
                }
                #region SecurityCheck
                if (_configurations.SecurityCheck)
                {
                    var origin = Request.Headers[CommonStrings.Origin].ToString();
                    if (!string.IsNullOrEmpty(origin))
                    {
                        var aut = new Uri(origin).Host;
                        aut = aut.ToLower();
                        var topdomain = string.Empty;
                        if (aut.IndexOf(CommonStrings.Dot) == aut.LastIndexOf(CommonStrings.Dot))
                        {
                            topdomain = aut;
                        }
                        else
                        {
                            topdomain = aut.Substring(aut.IndexOf(CommonStrings.Dot) + 1);
                        }
                        if (host.Host != topdomain)
                        {
                            return(CommonStrings.NoHostAccess);
                        }
                    }
                }
                #endregion
                if (logData != null && (await _crowlerCache.IsCrowler(logData.UserId)))
                {
                    return(string.Empty);
                }
                List <Task> tasks = new List <Task>
                {
                    _totalVisitUpdater.UpdateTotalVisit(logData.HostId)
                };
                await _hostScriptChecker.UpdateProductValidation(logData.HostId);

                var categoriesforlog = logData.ProductData.Where(c => !string.IsNullOrEmpty(c.Category)).GroupBy(c => c.Category).Select(v => new KeyValuePair <string, int>(v.Key, v.Count()));
                foreach (var category in categoriesforlog)
                {
                    tasks.Add(_categoryLogger.LogCategory(host.Id, category.Key, category.Value));
                }
                foreach (var item in fiProducts)
                {
                    var updateedProduct = new HostProduct()
                    {
                        Description  = item.Description,
                        ImageAddress = item.ImageAddress,
                        IsAvailable  = item.Available,
                        Price        = item.Price,
                        Url          = item.PageAddress,
                        Id           = item.ProductId,
                        ProductName  = item.Name,
                        CategoryName = item.Category,
                        UpdateDate   = DateTime.Now
                    };
                    tasks.Add(_productCache.UpdateProduct(logData.HostId, updateedProduct));
                }

                #region Log Into Kafka
                foreach (var item in logData.ProductData)
                {
                    await _kafkaLogger.SendMessage(new DruidData()
                    {
                        CategoryName = item.Category,
                        Date         = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"),
                        HostName     = host.Host,
                        ImageAddress = item.ImageAddress,
                        Price        = item.Price,
                        ProductName  = item.Name,
                        PageAddress  = item.PageAddress,
                        ProductId    = item.ProductId,
                        StatType     = StatTypes.ProductView,
                        UserId       = logData.UserId.ToString(),
                        HostId       = logData.HostId.ToString(),
                    });
                }
                #endregion

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError("Operation:ProductUpdate =>" + ex.Message);
            }
            return(string.Empty);
        }