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

feat: add ignore_cache to fetch_args_list #301

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open

Conversation

dshemetov
Copy link
Contributor

@dshemetov dshemetov commented Feb 5, 2025

Checklist

Please:

  • Make sure this PR is against "dev", not "main" (unless this is a release
    PR).
  • Request a review from one of the current epidatr main reviewers:
    brookslogan, dshemetov, nmdefries, dsweber2.
  • Makes sure to bump the version number in DESCRIPTION. Always increment
    the patch version number (the third number), unless you are making a
    release PR from dev to main, in which case increment the minor version
    number (the second number).
  • Describe changes made in NEWS.md, making sure breaking changes
    (backwards-incompatible changes to the documented interface) are noted.
    Collect the changes under the next release number (e.g. if you are on
    1.7.2, then write your changes under the 1.8 heading).

Change explanations for reviewer

  • Adds the ignore_cache argument to fetch_args_list() to make it easier to temporarily ignore the cache
  • Adds some documentation to the is_cachable property getter for clarity
  • Refactors a few of the internals of fetch functions
    • Disassemble cache_epidata_call into pieces, move most of the pieces over to fetch
    • Remove fetch_tbl and simplify fetch logic
    • Consolidate cache utility functions in cache.R (check_is_cachable moved from utils.R)

Magic GitHub syntax to mark associated Issue(s) as resolved when this is merged into the default branch

@brookslogan
Copy link
Contributor

I'm getting some tests failing when running locally where check_is_cachable gives unexpected results. There was also a CHECK warning above from not having @importFrom tibble tibble that seems like it still applies but might not be caught in tests.

Copy link
Contributor

@brookslogan brookslogan left a comment

Choose a reason for hiding this comment

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

I have some minor suggestions that probably don't relate to the refactor itself but also include some similar cleanup, but I did not spot why my local tests might be failing; looked good to me but seems that I missed something (hence just commenting). Here's the output if helpful:

ℹ Testing epidatr
! epidatr cache is being used (set env var EPIDATR_USE_CACHE=FALSE if not intended).
ℹ The cache directory is ~/.cache/R/epidatr.
ℹ The cache will be cleared after 14 days and will be pruned if it exceeds 4096 MB.
ℹ The log of cache transactions is stored at ~/.cache/R/epidatr/logfile.txt.
✔ | F W  S  OK | Context
✔ |          1 | auth
✖ | 1       42 | cache
──────────────────────────────────────────────────────────────────────────────────────────────
Failure (test-cache.R:128:7): check_is_cachable
check_is_cachable(epidata_call, fetch_args) is not FALSE

`actual`:   TRUE 
`expected`: FALSE
Backtrace:
    ▆
 1. └─epidatr (local) check_fun(...) at test-cache.R:142:3
 2.   └─testthat::expect_false(check_is_cachable(epidata_call, fetch_args)) at test-cache.R:128:7
──────────────────────────────────────────────────────────────────────────────────────────────
✔ |         19 | check
✔ |          3 | covidcast [1.2s]
✔ |      1  74 | endpoints
✔ |         12 | epidatacall
✔ |          6 | epirange
✔ |         42 | model
✔ |      1   0 | request
✔ |      1  28 | utils

══ Results ═══════════════════════════════════════════════════════════════════════════════════
Duration: 3.2 s

── Skipped tests (3) ─────────────────────────────────────────────────────────────────────────
• empty test (2): test-endpoints.R:1:1, test-utils.R:24:1
• This site is down. (1): test-request.R:2:3

── Failed tests ──────────────────────────────────────────────────────────────────────────────
Failure (test-cache.R:128:7): check_is_cachable
check_is_cachable(epidata_call, fetch_args) is not FALSE

`actual`:   TRUE 
`expected`: FALSE
Backtrace:
    ▆
 1. └─epidatr (local) check_fun(...) at test-cache.R:142:3
 2.   └─testthat::expect_false(check_is_cachable(epidata_call, fetch_args)) at test-cache.R:128:7

[ FAIL 1 | WARN 0 | SKIP 3 | PASS 227 ]

dshemetov and others added 4 commits February 5, 2025 17:06
Co-authored-by: brookslogan <lcbrooks@cs.cmu.edu>
Co-authored-by: brookslogan <lcbrooks@cs.cmu.edu>
@dshemetov
Copy link
Contributor Author

Fixed a bunch of issues:

  • test caught a bug (if fields is not null, then we don't cache; lost that check in the refactor), fixed that
  • rewrote a few other tests
  • improved tests so they actually clear and disable the cache after every test using withr::defer
  • fixed incorrect parsing of only_supports_classic endpoints
  • addressed other minor changes

Thanks for the review!

@dshemetov dshemetov requested a review from brookslogan February 6, 2025 02:19
@brookslogan
Copy link
Contributor

brookslogan commented Feb 12, 2025

Sorry, somehow thought this was already merged. Taking a look now. Quick first not: I think running tests deleted my actual cache instead of the test cache somehow? And then I get output like:

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning messages:
1: Using cached results with `as_of` within the past week (or the future!). This will likely result in an
invalid cache. Consider
ℹ disabling the cache for this session with `disable_cache` or permanently with environmental variable
  `EPIDATR_USE_CACHE=FALSE`
ℹ setting `EPIDATR_CACHE_MAX_AGE_DAYS=1` to e.g. `3/24` (3 hours).

from trying the pub_covidcast examples + applying an as_of = Sys.Date() - 7 (should have been 8 I guess to not get this much spam).

Though some other attempts are easier to interpret:

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file '/home/<username>/.cache/R/epidatr/logfile.txt': No such file or directory

expect_message(test_set_cache())
# Delete cache files after the test
withr::defer(clear_cache(disable = TRUE))
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how, but one of these clear_cache() calls seems to have cleared my real cache rather than the test cache when I ran devtools::test().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, thanks for reminding me, this happened to me too... Current guess: if you have a cache activated before tests starts, subsequent set_cache calls just reuse that cache, so then a later clear_cache destroys it. Will check.

Copy link
Contributor

@brookslogan brookslogan left a comment

Choose a reason for hiding this comment

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

Nice catches. I've noted a couple minor issues + request (maybe in some separate Issue/PR) to improve messaging when a cache has been cleared unexpectedly and we still try to use it.

Co-authored-by: brookslogan <lcbrooks@cs.cmu.edu>
@dshemetov
Copy link
Contributor Author

Also, I'm now thinking that ignore_cache is kinda weird... what doesn't seem natural is that if there is a cache and we ignore_cache, then we never update the cache. What's probably better is something like refresh_cache, which will fetch from API and overwrite what's in the cache currently, if the cache is on.

@dshemetov dshemetov self-assigned this Feb 25, 2025
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.

Refactor idea: the fetch call chain is too complex
2 participants