Ejemplo n.º 1
0
        public async Task InsertAsync(SawmillGeocodeRequest location)
        {
            // TODO: When .Net Standard 2.1 is released, change the following to TryAdd instead of ContainsKey and Add.
            // if (Locations.TryAdd(location.SourceKey, location))
            if (!Locations.ContainsKey(location.SourceKey))
            {
                Locations.Add(location.SourceKey, location);
                logger?.LogDebug((int)LogEventIds.L1MemoryCacheInserted, "Location '{sourceKey}' added to L1 cache", location.SourceKey);
                await Task.CompletedTask;
            }

            logger?.LogDebug((int)LogEventIds.L1MemoryCacheAlreadyExists, "Location '{sourceKey}' already exists in L1 cache", location.SourceKey);
            await Task.CompletedTask;
        }
        public async Task InsertAsync(SawmillGeocodeRequest location)
        {
            await EnsureLocationsLoaded();

            // TODO: When .Net Standard 2.1 is released, change the following to TryAdd instead of ContainsKey and Add.
            // if (Locations.TryAdd(location.SourceKey, location))
            if (!locations.ContainsKey(location.SourceKey))
            {
                locations.Add(location.SourceKey, location);
                logger?.LogDebug((int)LogEventIds.L2DiskCacheInserted, "Location '{sourceKey}' added to L2 cache", location.SourceKey);

                await SaveToDiskAsync();

                return;
            }

            logger?.LogDebug((int)LogEventIds.L2DiskCacheAlreadyExists, "Location '{sourceKey}' already exists in L2 cache", location.SourceKey);
            await Task.CompletedTask;
        }
        // TODO: I don't like that this func mutates the passed parameter, create a model and return it.
        private async Task <SawmillGeocodeRequest> GeocodeLocationAsync(SawmillGeocodeRequest location)
        {
            if (location.Status == SawmillStatus.RequiresLookup)
            {
                // TODO: Test that this caches.
                logger?.LogInformation((int)LogEventIds.CheckingCache, "Looking up locations in L1/L2 caches");
                location = await locationCache.LookupAsync(location.Source);
            }

            var qualityStatus = addressQualityChecker.StatusGuessFromSourceQuality(location.Source);

            if (qualityStatus != AddressQualityStatus.OK)
            {
                location.Status = MapQualityStatus(qualityStatus);
            }

            if (location.Status == SawmillStatus.RequiresGeocoding || location.Status == SawmillStatus.TemporaryGeocodeError)
            {
                // TODO: We might need to record the last time a geocoding was attempted and how many tries if this was a temp error so we don't keep retrying.
                var result = await geocodeManager.GeocodeAddressAsync(location.Source);

                // TODO: Insert additional responses into cache here plus expiry times dependent on the status.
                if (result.Status == AddressLookupStatus.Geocoded || result.Status == AddressLookupStatus.ZeroResults)
                {
                    // TODO: Add cache inserting.
                    // locationCache.InsertAsync
                }

                // TODO: Geocoder should return a text description of the errors so we can present to the user.
                location.Status       = MapGeocoderStatus(result.Status);
                location.GeocoderUsed = result.GeocoderId;
                if (location.Status == SawmillStatus.Geocoded)
                {
                    location.Responses = result.Locations;
                }
            }

            return(location);
        }