From bcbcc4365fe62a461df190db8eca53f07b5b6689 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 24 Sep 2025 20:16:33 -0400 Subject: [PATCH 1/3] remove ttl --- doc/cp.txt | 5 ----- lua/cp/cache.lua | 32 -------------------------------- 2 files changed, 37 deletions(-) diff --git a/doc/cp.txt b/doc/cp.txt index f3f97be..3e816a4 100644 --- a/doc/cp.txt +++ b/doc/cp.txt @@ -373,11 +373,6 @@ Picker Controls ~ / Navigate to previous item / Start filtering/searching items -Notes ~ - • Contest lists are fetched dynamically using scrapers with a TTL of 1 day - • Use to force refresh - • Large contest lists may take time to load - ============================================================================== RUN PANEL *cp-run* diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index 90c2c2b..2c01be5 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -12,12 +12,10 @@ ---@class ContestListData ---@field contests table[] ---@field cached_at number ----@field expires_at number ---@class ContestData ---@field problems Problem[] ---@field scraped_at string ----@field expires_at? number ---@field test_cases? CachedTestCase[] ---@field test_cases_cached_at? number ---@field timeout_ms? number @@ -39,28 +37,6 @@ local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json' local cache_data = {} local loaded = false -local CONTEST_LIST_TTL = { - cses = 7 * 24 * 60 * 60, - codeforces = 24 * 60 * 60, - atcoder = 24 * 60 * 60, -} - ----@param contest_data ContestData ----@param platform string ----@return boolean -local function is_cache_valid(contest_data, platform) - vim.validate({ - contest_data = { contest_data, 'table' }, - platform = { platform, 'string' }, - }) - - if contest_data.expires_at and os.time() >= contest_data.expires_at then - return false - end - - return true -end - function M.load() if loaded then return @@ -125,10 +101,6 @@ function M.get_contest_data(platform, contest_id) return nil end - if not is_cache_valid(contest_data, platform) then - return nil - end - return contest_data end @@ -146,11 +118,9 @@ function M.set_contest_data(platform, contest_id, problems) cache_data[platform] = {} end - local ttl = CONTEST_LIST_TTL[platform] or (24 * 60 * 60) cache_data[platform][contest_id] = { problems = problems, scraped_at = os.date('%Y-%m-%d'), - expires_at = os.time() + ttl, } M.save() @@ -316,11 +286,9 @@ function M.set_contest_list(platform, contests) cache_data.contest_lists = {} end - local ttl = CONTEST_LIST_TTL[platform] or (24 * 60 * 60) cache_data.contest_lists[platform] = { contests = contests, cached_at = os.time(), - expires_at = os.time() + ttl, } M.save() From 71b827fe95acf4927e31e44a4ef28f58ec80fb9f Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 24 Sep 2025 20:34:35 -0400 Subject: [PATCH 2/3] fix: set test cases first --- lua/cp/setup.lua | 59 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index 21dba2c..a53c12e 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -41,7 +41,7 @@ function M.setup_contest(platform, contest_id, problem_id, language) return end - logger.log(('fetching contest %s %s...'):format(platform, contest_id)) + logger.log('fetching contests problems...', vim.log.levels.INFO, true) scraper.scrape_contest_metadata(platform, contest_id, function(result) if not result.success then @@ -99,35 +99,6 @@ function M.setup_problem(contest_id, problem_id, language) state.set_contest_id(contest_id) state.set_problem_id(problem_id) - - local cached_tests = cache.get_test_cases(platform, contest_id, problem_id) - if cached_tests then - state.set_test_cases(cached_tests) - logger.log(('using cached test cases (%d)'):format(#cached_tests)) - elseif vim.tbl_contains(config.scrapers, platform) then - logger.log('loading test cases...') - - scraper.scrape_problem_tests(platform, contest_id, problem_id, function(result) - if result.success then - logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id)) - if state.get_problem_id() == problem_id then - state.set_test_cases(result.tests) - end - else - logger.log( - 'failed to load tests: ' .. (result.error or 'unknown error'), - vim.log.levels.ERROR - ) - if state.get_problem_id() == problem_id then - state.set_test_cases({}) - end - end - end) - else - logger.log(('scraping disabled for %s'):format(platform)) - state.set_test_cases({}) - end - state.set_run_panel_active(false) vim.schedule(function() @@ -180,6 +151,34 @@ function M.setup_problem(contest_id, problem_id, language) logger.log(('setup error: %s'):format(err), vim.log.levels.ERROR) end end) + + local cached_tests = cache.get_test_cases(platform, contest_id, problem_id) + if cached_tests then + state.set_test_cases(cached_tests) + logger.log(('using cached test cases (%d)'):format(#cached_tests)) + elseif vim.tbl_contains(config.scrapers, platform) then + logger.log('loading test cases...') + + scraper.scrape_problem_tests(platform, contest_id, problem_id, function(result) + if result.success then + logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id)) + if state.get_problem_id() == problem_id then + state.set_test_cases(result.tests) + end + else + logger.log( + 'failed to load tests: ' .. (result.error or 'unknown error'), + vim.log.levels.ERROR + ) + if state.get_problem_id() == problem_id then + state.set_test_cases({}) + end + end + end) + else + logger.log(('scraping disabled for %s'):format(platform)) + state.set_test_cases({}) + end end function M.scrape_remaining_problems(platform, contest_id, problems) From 170021af8e22e72e12adb7dde992274b0941683e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 24 Sep 2025 20:46:43 -0400 Subject: [PATCH 3/3] no more ttl --- lua/cp/cache.lua | 7 +------ spec/cache_spec.lua | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index 2c01be5..58bbaf0 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -266,12 +266,7 @@ function M.get_contest_list(platform) return nil end - local contest_list_data = cache_data.contest_lists[platform] - if os.time() >= contest_list_data.expires_at then - return nil - end - - return contest_list_data.contests + return cache_data.contest_lists[platform].contests end ---@param platform string diff --git a/spec/cache_spec.lua b/spec/cache_spec.lua index 2f5053a..50f74a3 100644 --- a/spec/cache_spec.lua +++ b/spec/cache_spec.lua @@ -85,7 +85,6 @@ describe('cp.cache', function() local result = cache.get_contest_data('cses', 'test_contest') assert.is_not_nil(result) - assert.is_not_nil(result.expires_at) end) end)