public async Task <ActionResult <DashboardInformation> > GetDashboard()
        {
            var profile = (ViewApplicationUser)User;

            if (!profile.IsAdmin)
            {
                return(BadRequest());
            }

            var dashboardInfo = new DashboardInformation
            {
                NbVisits              = await _eventTrackingService.Count(e => e.Category == "Navigation" && e.Key == "HomePage"),
                NbRegistar            = await _eventTrackingService.Count(e => e.Category == "Register" && e.Key == "Finished"),
                NbLogin               = await _eventTrackingService.Count(e => e.Category == "Login"),
                NbRequestSubscription = await _eventTrackingService.Count(e => e.Category == "Subscription" && e.Key.StartsWith("started")),
                NbFinishSubscription  = await _eventTrackingService.Count(e => e.Category == "Subscription" && e.Key == "Success"),
                NbAds = await functionalUnitOfWork.AdRepository.CountVisibled()
            };

            var viewFromSocialNetworks = (await _eventTrackingService.Where(c => EF.Functions.Like(c.Category, "%FromSocial%"))).ToList();

            var socialNetworks = functionalUnitOfWork.SocialNetworkRepository.GetNetworks();

            foreach (var socialNetwork in socialNetworks)
            {
                var name = socialNetwork.Name.ToLower();

                if (viewFromSocialNetworks.Any(e => e.Key.EndsWith(name)))
                {
                    CommingFromSocialNetwork commingFromSocialNetwork = new CommingFromSocialNetwork
                    {
                        Name  = name,
                        Value = viewFromSocialNetworks.Count(e => e.Key.EndsWith(name))
                    };

                    dashboardInfo.CommingFromSocialNetworks.Add(commingFromSocialNetwork);
                }
            }

            List <StatisticsDay> StatisticsDays = new List <StatisticsDay>();

            var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            var endDate   = startDate.AddMonths(1).AddDays(-1);

            var date = startDate;

            while (date < endDate)
            {
                var statisticDay = await ComputeDay(date);

                dashboardInfo.StatisticsDays.Add(statisticDay);
                date = date.AddDays(1);
            }

            return(Ok(dashboardInfo));
        }
        public async Task <ActionResult <IList <StatisticDatePoint> > > GetRepartition()
        {
            if (User.Identity.IsAuthenticated == false)
            {
                return(BadRequest());
            }

            var userId         = User.NameIdentifier();
            var socialNetworks = await _functionalUnitOfWork.SocialNetworkRepository.GetSocialNetworkLinks(userId);

            var eventTrackings = (await _eventTrackingService.GetEventTrackingItemsByKey($"ViewProfile#{userId}")).ToList();

            var user = await _userManager.FindByIdAsync(userId);

            if (!string.IsNullOrEmpty(user.PinDigit))
            {
                eventTrackings.AddRange((await _eventTrackingService.GetEventTrackingItemsByKey($"ViewProfile#{user.PinDigit}")));
            }

            var groups = eventTrackings.GroupBy(info => info.CreatedDate.Date)
                         .Select(group => new
            {
                Date  = group.Key,
                Count = group.Count()
            }).OrderBy(x => x.Date);

            var result = groups.Select(g => new StatisticDatePoint {
                Date = g.Date, Value = g.Count
            }).ToArray();

            List <StatisticKeyPoint> repartitions = new List <StatisticKeyPoint>();

            repartitions.AddRange(result.Select(r => new StatisticKeyPoint {
                Date = r.Date, Key = "Total", Value = r.Value
            }));

            var dates = repartitions.Where(r => r.Key == "Total").Select(r => r.Date).ToArray();

            foreach (var socialNetwork in socialNetworks)
            {
                var category = $"ViewProfileFromSocial#{socialNetwork.Name.ToLower()}";

                var eventTrackingsSocialNetwork = await _eventTrackingService.Where(c => c.Category == category && c.Key == userId);

                if (eventTrackingsSocialNetwork.Any() == false)
                {
                    continue;
                }

                var geventTrackingSocialNetwork = eventTrackingsSocialNetwork.GroupBy(info => info.CreatedDate.Date)
                                                  .Select(group => new
                {
                    Date  = group.Key,
                    Count = group.Count()
                }).OrderBy(x => x.Date);

                var resultSocialNetwork = groups.Select(g => new StatisticDatePoint {
                    Date = g.Date, Value = g.Count
                }).ToArray();

                foreach (var date in dates)
                {
                    repartitions.Add(new StatisticKeyPoint
                    {
                        Key   = socialNetwork.Name,
                        Date  = date,
                        Value = resultSocialNetwork.Where(r => r.Date == date).Sum(r => r.Value)
                    });
                }
            }

            foreach (var date in dates)
            {
                var total  = repartitions.First(d => d.Date == date).Value;
                var other  = repartitions.Where(d => d.Date == date && d.Key != "Total").Sum(d => d.Value);
                var direct = total - other;
                repartitions.Add(new StatisticKeyPoint {
                    Date = date, Key = "Direct", Value = direct
                });
            }

            return(Ok(repartitions));
        }
Exemplo n.º 3
0
        public async Task <ActionResult <AdViewModel> > Get(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest());
            }

            if (Request.Query.Any())
            {
                if (QueryHelpers.ParseQuery(Request.QueryString.Value).TryGetValue("social", out var _social))
                {
                    await _eventTrackingService.Create(User.NameIdentifier(), $"ViewAdFromSocial#{_social.ToString().ToLower()}", id.ToString());
                }
            }

            var model = await functionalUnitOfWork.AdRepository.FirstOrDefault(a => a.Id == id);

            if (model == null)
            {
                return(NotFound());
            }

            var ad = (AdViewModel)(model);

            ad.Owner = await _userManager.FindByIdAsync(ad.OwnerId);

            ad.Owner.PhotoUrl ??= new GravatarUrlExtension.Gravatar().GetImageSource(ad.Owner.Email);

            await _eventTrackingService.Create(User.NameIdentifier(), "ViewAd", id.ToString());

            model.ViewCount += 1;
            await functionalUnitOfWork.AdRepository.Update(model);

            ad.ViewCount = model.ViewCount;

            var authorAds = await functionalUnitOfWork.AdRepository.GetVisibledUser(ad.OwnerId);

            ad.Owner.AdsCount = authorAds.Count;

            ad.OtherAds = authorAds.Select(a => new ListAdViewModel {
                Id = a.Id, ImageUrl1 = a.ImageUrl1, Title = a.Title, OrderNumber = a.OrderNumber
            })
                          .OrderBy(a => a.OrderNumber).ToList();

            ad.Owner.SocialNetworkConnected =
                await functionalUnitOfWork.SocialNetworkRepository.GetSocialNetworkLinks(ad.OwnerId);

            ad.Owner.CustomLinks = await functionalUnitOfWork.CustomLinkRepository.GetCustomLinkLinks(ad.OwnerId);

            var categorieIds = await _adsUnitOfWork.CategoryAdRepository.GetAllIdCatByAdId(ad.Id);

            List <string> categories = new();
            string        family     = string.Empty;

            foreach (Guid categoryId in categorieIds)
            {
                var category = await _adsUnitOfWork.CategoriesRepository.Get(categoryId);

                if (string.IsNullOrEmpty(family))
                {
                    var familyId = category.FamilyId;
                    var f        = await _adsUnitOfWork.FamiliesRepository.Get(familyId);

                    family = f.Code;
                }

                categories.Add(category.Code);
            }

            ad.Family         = family;
            ad.Categories     = categories;
            ad.ListIdCategory = categorieIds;

            var events = await _eventTrackingService.Where(c => c.Category == "ViewAd" && c.Key == id.ToString());

            if (events.Any())
            {
                var contactIds = events.OrderByDescending(e => e.CreatedDate).Select(e => e.UserId).Distinct().Take(10).ToArray();
                ad.LastViewers = await _userManager.Users.Where(u => contactIds.Contains(u.Id)).Select(u => (ViewApplicationUser)u).ToListAsync();
            }
            else
            {
                ad.LastViewers = new List <ViewApplicationUser> {
                    User
                };
            }

            return(Ok(ad));
        }