Skip to content

Commit 3cf3b89

Browse files
committed
Small step toward more generalized recent cmds caching (passing tests)
1 parent 476272c commit 3cf3b89

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

kadi/commands/commands_v2.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ def clear_caches():
9393
OBSERVATIONS.clear()
9494

9595

96-
def _merge_cmds_archive_recent(start, scenario):
96+
def _merge_cmds_archive_recent(start, cache_key, cmds_recent):
9797
"""Merge cmds archive from ``start`` onward with recent cmds for ``scenario``
9898
9999
This assumes:
100-
- CMDS_RECENT cache has been set with that scenario.
101100
- Recent commands overlap the cmds archive
102101
103102
Parameters
@@ -112,11 +111,10 @@ def _merge_cmds_archive_recent(start, scenario):
112111
CommandTable
113112
Commands from cmds archive and all recent commands
114113
"""
115-
cmds_recent = CMDS_RECENT[scenario]
116114

117115
logger.info(f"Merging cmds_recent with archive commands from {start}")
118116

119-
if scenario not in MATCHING_BLOCKS:
117+
if cache_key not in MATCHING_BLOCKS:
120118
# Get index for start of cmds_recent within the cmds archive
121119
i0_arch_recent = IDX_CMDS.find_date(cmds_recent["date"][0])
122120

@@ -130,9 +128,9 @@ def _merge_cmds_archive_recent(start, scenario):
130128
IDX_CMDS[i0_arch_recent:], cmds_recent
131129
)
132130
arch_block_end = i0_arch_recent + arch_recent_offset
133-
MATCHING_BLOCKS[scenario] = arch_block_end, recent_block_end, i0_arch_recent
131+
MATCHING_BLOCKS[cache_key] = arch_block_end, recent_block_end, i0_arch_recent
134132
else:
135-
arch_block_end, recent_block_end, i0_arch_recent = MATCHING_BLOCKS[scenario]
133+
arch_block_end, recent_block_end, i0_arch_recent = MATCHING_BLOCKS[cache_key]
136134

137135
# Get archive commands from the requested start time (or start of the overlap
138136
# with recent commands) to the end of the matching block in recent commands.
@@ -258,6 +256,10 @@ def get_cmds(
258256
# Default stop is either now (typically) or set by env var
259257
default_stop = CxoTime(get_default_stop())
260258

259+
# Cache key used for CMDS_RECENT and MATCHING_BLOCKS.
260+
# TODO: make more complete.
261+
cache_key = scenario
262+
261263
# For flight scenario or no internet or if the query stop time is guaranteed
262264
# to not require recent commands then just use the archive.
263265
before_recent_cmds = stop < default_stop - conf.default_lookback * u.day
@@ -268,7 +270,7 @@ def get_cmds(
268270
f" {scenario=} {before_recent_cmds=} {HAS_INTERNET=}"
269271
)
270272
else:
271-
if scenario not in CMDS_RECENT:
273+
if cache_key not in CMDS_RECENT:
272274
cmds_recent = update_archive_and_get_cmds_recent(
273275
scenario,
274276
cache=True,
@@ -277,7 +279,7 @@ def get_cmds(
277279
event_filter=event_filter,
278280
)
279281
else:
280-
cmds_recent = CMDS_RECENT[scenario]
282+
cmds_recent = CMDS_RECENT[cache_key]
281283

282284
# Get `cmds` as correct mix of recent and archive commands that contains
283285
# the requested date range.
@@ -293,7 +295,7 @@ def get_cmds(
293295
# archive commands. The margin is set at 3 days to ensure that OBS
294296
# command continuity is maintained (there is at least one maneuver).
295297
# See also the DESIGN commentary after the function.
296-
cmds = _merge_cmds_archive_recent(start, scenario)
298+
cmds = _merge_cmds_archive_recent(start, cache_key, cmds_recent)
297299
logger.info(
298300
"Getting commands from archive + recent: start < recent loads start +"
299301
f" 3 days for {scenario=}"
@@ -372,9 +374,11 @@ def update_archive_and_get_cmds_recent(
372374
----------
373375
scenario : str, None
374376
Scenario name
375-
lookback : int, Quantity, None
376-
Lookback time from ``stop`` for recent loads. If None, use
377+
lookback : int, None
378+
Lookback time from ``stop`` for recent loads in days. If None, use
377379
conf.default_lookback.
380+
stop : CxoTime-like, None
381+
Stop time for recent loads. If None, use default_stop.
378382
cache : bool
379383
Cache the result in CMDS_RECENT dict.
380384
pars_dict : dict, None
@@ -385,6 +389,7 @@ def update_archive_and_get_cmds_recent(
385389
Callable function or list of callable functions that takes an Event Table as
386390
input and returns a boolean mask with same length as Table. This is used to
387391
select rows from the Table. If None, no filtering is done.
392+
388393
Returns
389394
-------
390395
CommandTable

0 commit comments

Comments
 (0)