Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jormungandr: Cache Co2 emission value instead of pb object #3965

Merged
merged 1 commit into from
Apr 4, 2023

Conversation

kadhikari
Copy link
Contributor

Two problems occurred while caching pb object Co2Emission

  • Caching for key = uri, request_id: as request_id is unique for a request global to navitia, caching on uri and request_id is useless.
  • Caching pb object raises error (see ticket for more details)

A little adjustment is made to pass request_id without including it's value as key while caching.

For more details: https://navitia.atlassian.net/browse/NAV-1667

@kadhikari kadhikari requested review from xlqian, pbench, woshilapin, azime and SGrenet and removed request for xlqian April 3, 2023 14:21
@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

83.3% 83.3% Coverage
0.0% 0.0% Duplication

@woshilapin woshilapin removed their request for review April 4, 2023 08:32
Copy link
Contributor

@woshilapin woshilapin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be fixed differently in a much simpler way.

current_app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 30)
)
@cache.memoize(current_app.config[str('CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 300))
def inner(mode_str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using this convoluted solution of an inner function, to ignore the request_id from the cache key, @cache.memoize has an option to ignore some function parameters with args_to_ignore (see documentation).

So the PR could only be

        @memory_cache.memoize(
            current_app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 30),
            args_to_ignore=["request_id"],
        )
        @cache.memoize(
            current_app.config[str('CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 300), 
            args_to_ignore=["request_id"],
        )

s.co2_emission.CopyFrom(co2_emission)
co2_emission = instance.georef.get_car_co2_emission(request_id)
pb_co2_emission = s.co2_emission
pb_co2_emission.value = (co2_emission * s.street_network.length) / 1000.0 if co2_emission > 0 else 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that the if co2_emission > 0 else 0 is useful. In case co2_emission == 0, the calculation will already be 0. Can co2_emission be negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will avoid negative value

@kadhikari kadhikari merged commit 796ba42 into dev Apr 4, 2023
@kadhikari kadhikari deleted the tma_fix_cache_physical_mode_car branch April 4, 2023 15:43
@pbougue pbougue changed the title Cache Co2 emission value instead of pb object Jormungandr: Cache Co2 emission value instead of pb object Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants