Beispiel #1
0
        public async Task <ActionResult <IpDetailDTO> > GetIpDetail(string ip)
        {
            try
            {
                // ensure that I will only search for IPs and I will not have random strings in my Database
                if (!System.Text.RegularExpressions.Regex.IsMatch(ip, @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"))
                {
                    return(NotFound());
                }

                // first check the cache
                var ipDetailsFromCache = IpRequestHandlerService.CheckCacheForIp(ip);
                if (ipDetailsFromCache != null)
                {
                    var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromCache);
                    return(ipdDTO);
                }

                // then check the db. If it is in there then update cache and return
                var ipDetailsFromDb = await IpDetailsRepository.GetIpDetailAsync(ip);

                if (ipDetailsFromDb != null)
                {
                    await IpRequestHandlerService.UpdateCacheAsync(ipDetailsFromDb);

                    var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromDb);
                    return(ipdDTO);
                }

                // lastly get the IP from library. If its found then update both db and cache
                var ipDetailsFromLibrary = await IpRequestHandlerService.CheckLibraryForIPAsync(ip);

                if (ipDetailsFromLibrary != null)
                {
                    await IpDetailsRepository.AddIpDetailsAsync(ipDetailsFromLibrary);

                    await IpRequestHandlerService.UpdateCacheAsync(ipDetailsFromLibrary);

                    var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromLibrary);
                    return(ipdDTO);
                }

                return(NotFound());
            }
            catch (Exception)
            {
                return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError));
            }
        }
        public Guid IpDetailsUpdateJob(IEnumerable <IpDetail> ipDetails)
        {
            Guid guid = Guid.NewGuid();

            // initialize the progress
            BatchJobHelperService.UpdateJobProgress(guid, $"0/{ipDetails.Count()}");

            var t = Task.Run(async() =>
            {
                using var scope = ServiceScopeFactory.CreateScope();

                var _ipDetailsRepository = scope.ServiceProvider.GetRequiredService <IIPDetailsRepository>();

                var currentIds = ipDetails;

                int progress = 0;

                while (currentIds.Any())
                {
                    // update entries in DB
                    await _ipDetailsRepository.UpdateIpDetailsAsync(currentIds.Take(batchSize));

                    // if the request has less items that the batchsize then i want the progress to write it correctly
                    progress += batchSize > currentIds.Count() ? currentIds.Count() : batchSize;

                    currentIds = currentIds.Skip(batchSize);

                    // update progress
                    BatchJobHelperService.UpdateJobProgress(guid, $"{progress}/{ipDetails.Count()}");

                    // update cache
                    await IpRequestHandlerService.UpdateManyCacheAsync(currentIds.Take(batchSize));
                }
            });

            return(guid);
        }