Ejemplo n.º 1
0
        private float[] GetHumanMatchesCountNormalizedMarks(GetAdvertisementByEnvironmentRequest request, Advertisement[] ads)
        {
            int[] humanMatchesCounts = ads.Select(ad => ad.CountTargetAudience(request.Faces)).ToArray();
            int maxMatchesCount = humanMatchesCounts.Max();

            return humanMatchesCounts.Select(humanMatchesCount => (float)humanMatchesCount / maxMatchesCount).ToArray();
        }
Ejemplo n.º 2
0
        private Advertisement GetMostSuitableAdvertisemet(GetAdvertisementByEnvironmentRequest request, Advertisement[] ads)
        {
            float[] humanMatchesNormalizedMarks = GetHumanMatchesCountNormalizedMarks(request, ads);
            float[] remainingViewsNormalizedMarks = GetRemainingViewsNormalizedMarks(ads);

            int recommendedAdIndex = GetMostValuableAdvertisementIndex(humanMatchesNormalizedMarks, remainingViewsNormalizedMarks);
            return ads[recommendedAdIndex];
        }
Ejemplo n.º 3
0
 private Advertisement[] GetSuitableAdvertisements(int localTimeInMinutes, GetAdvertisementByEnvironmentRequest request)
 {
     return _dbContext.Advertisements.AsQueryable()
         .FilterAdvertisementsByStatus()
         .FilterAdvertisementsByLocation(request)
         .FilterAdvertisementsByTime(localTimeInMinutes)
         .LoadAdvertisementsInMemory()
         .FilterAdvertisementsByHumanLimit(request)
         .FilterAdvertisementsByViewsCountLimit()
         .ToArray();
 }
Ejemplo n.º 4
0
 private async Task StoreAdvertisementView(
     GetAdvertisementByEnvironmentRequest request, Guid smartDeviceId, Advertisement advertisement)
 {
     _dbContext.AdvertisementViews.Add(new AdvertisementView(
         advertisement.Id,
         smartDeviceId,
         request.Country,
         request.City,
         advertisement.CountTargetAudience(request.Faces)
     ));
     await _dbContext.SaveChangesAsync();
 }
        public async Task <IActionResult> GetAdvertisementByEnvironmentAsync([FromBody] GetAdvertisementByEnvironmentRequest request)
        {
            int  localTimeInMinutes = GetUserLocalTimeInMinutes((int)request.TimeZoneOffset);
            Guid currentUserId      = Guid.Parse(User.Identity.Name);

            var response = await _advertisementService.GetAdvertisementByEnvironmentAsync(
                localTimeInMinutes, request, currentUserId);

            if (response == null)
            {
                return(NoContent());
            }
            return(Ok(response));
        }
Ejemplo n.º 6
0
        public async Task<AdvertisementForSmartDeviceResponse> GetAdvertisementByEnvironmentAsync(
            int localTimeInMinutes, GetAdvertisementByEnvironmentRequest request, Guid smartDeviceId)
        {
            Advertisement[] ads = GetSuitableAdvertisements(localTimeInMinutes, request);
            if (ads.Length == 0)
            {
                return null;
            }

            Advertisement advertisement = GetMostSuitableAdvertisemet(request, ads); ;
            await StoreAdvertisementView(request, smartDeviceId, advertisement);

            var response = new AdvertisementForSmartDeviceResponse(
                advertisement.Name,
                _fileStorageService.GetUrlForFile(advertisement.GetFileName())
            );
            return response;
        }
Ejemplo n.º 7
0
        public async Task <AdvertisementResponse> GetAdvertisementByEnvironmentAsync(
            GetAdvertisementByEnvironmentRequest envRequest, string accessToken)
        {
            using var request = new HttpRequestMessage(
                      HttpMethod.Post, cvAdsAPIConfiguration.GetAdvertisementByEnvironmentURL())
                                .AddJsonContent(envRequest)
                                .SetAuthorizationHeader(accessToken);

            using var response = await httpClient.SendAsync(request);

            response.EnsureSuccessStatusCode();

            if (response.StatusCode == HttpStatusCode.NoContent)
            {
                return(null);
            }
            var advertisementResponse = await response.ReadResponseAsync <AdvertisementResponse>();

            return(advertisementResponse);
        }
 public static IEnumerable <Advertisement> FilterAdvertisementsByHumanLimit(
     this IEnumerable <Advertisement> ads, GetAdvertisementByEnvironmentRequest request) =>
 ads.Where(ad =>
           ad.HumanLimits.Any(humanLimit =>
                              request.Faces.Any(face => humanLimit.IsMatch(face))));
 public static IQueryable <Advertisement> FilterAdvertisementsByLocation(
     this IQueryable <Advertisement> ads, GetAdvertisementByEnvironmentRequest request) =>
 ads.Where(ad => ad.CountryScope == null ||
           (ad.CountryScope == request.Country &&
            (ad.CityScope == null || ad.CityScope == request.City)));