popoto.fields.access_tracker¶
popoto.fields.access_tracker
¶
AccessTrackerMixin — read pattern tracking with staged vs confirmed reads.
This module provides a mixin class that adds read access tracking to any Model. It uses a staging pattern: reads are first recorded to a staging list, then atomically promoted to the confirmed access log via a Lua script.
Design
- on_read() appends a timestamp to a staging list (cheap, fire-and-forget)
- confirm_access() atomically promotes staged timestamps to the confirmed log
- discard_staged_access() discards staged reads without affecting confirmed data
- Access log is capped at max_access_log entries (default 100)
Redis Key Patterns
- $AT:{ClassName}:staged:{redis_key} — staging list (RPUSH timestamps)
- $AT:{ClassName}:access_log:{redis_key} — confirmed access timestamps
- $AT:{ClassName}:meta:{redis_key} — hash with access_count and last_accessed
Example
class Memory(AccessTrackerMixin, Model): key = UniqueKeyField() content = StringField()
memory = Memory.query.get(key="important") # auto-stages on_read memory.confirm_access() # promote staged reads to confirmed log print(memory.access_count) # total confirmed read count print(memory.last_accessed) # timestamp of most recent confirmed read
AccessTrackerMixin
¶
Mixin that adds read access tracking to any Model.
Add this as a base class alongside Model to enable read tracking:
class MyModel(AccessTrackerMixin, Model):
...
Class Attributes
_max_access_log: Maximum number of timestamps to keep in the access log. Older entries are trimmed on confirm. Default 100. _track_reads: Whether to automatically track reads from queries. Default True. _staged_ttl_seconds: TTL applied to the staging list on every on_read() call. Default 86400 (24h). Magic-number tuning knob — increase if your stage→confirm cadence exceeds 24h. See on_read() docstring for the staged-read TTL contract.
Note: Attributes are prefixed with underscore to avoid conflict with Popoto's ModelBase metaclass, which requires public attributes to be Fields.
Source code in src/popoto/fields/access_tracker.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
access_count
property
¶
Total number of confirmed read accesses.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The cumulative access count, or 0 if never confirmed. |
last_accessed
property
¶
Timestamp of the most recent confirmed read access.
Returns:
| Type | Description |
|---|---|
|
float or None: Unix timestamp, or None if never confirmed. |
export_state(model_instance, **kwargs)
classmethod
¶
Export the access-tracker meta counters and confirmed log.
Returns:
| Type | Description |
|---|---|
|
``{"access_count": int, "last_accessed": float, |
|
|
"access_log": [float, ...]} |
|
|
never had a confirmed access. Each key is omitted when its source |
|
|
structure is absent, so an instance with counters but an empty log |
|
|
exports exactly the shape #554-era code produced. |
Source code in src/popoto/fields/access_tracker.py
import_state(model_instance, state, **kwargs)
classmethod
¶
Restore the access-tracker meta counters and confirmed log.
access_count and last_accessed are exposed only as read-only
properties, so this writes the meta hash directly -- the same hash
CONFIRM_ACCESS_LUA maintains. The log is restored by a raw
DELETE + RPUSH rather than by staging the timestamps and calling
confirm_access(): that path would HINCRBY the count a second time
on top of the carried value and overwrite last_accessed, and it
would push the timestamps through a staging list whose TTL contract
has nothing to do with a transfer.
The log is trimmed to the destination's _max_access_log, keeping
the most recent entries -- the same trim CONFIRM_ACCESS_LUA
applies at :47. access_log absent from state is tolerated:
a file exported by #554-era code has no such key, and must import
cleanly rather than raise.
Source code in src/popoto/fields/access_tracker.py
on_read(pipeline=None)
¶
Record a read access by staging a timestamp.
Appends the current timestamp to the staging list and refreshes the TTL on the staged key. This is a cheap operation suitable for fire-and-forget use from query hooks.
Staged-read TTL contract
Applications MUST call confirm_access() within
_staged_ttl_seconds (default 24h) of staging reads via
on_read(). Reads left unconfirmed past the TTL window are
permanently dropped from access_count / last_accessed by
design. Set _staged_ttl_seconds higher if a longer
stage→confirm cadence is required. The _staged_ttl_seconds
constant is a magic-number tuning knob — it is not user-facing
configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Optional Redis pipeline for batch operations. When provided, the RPUSH and EXPIRE are queued in the same pipeline call so they execute atomically. |
None
|
Source code in src/popoto/fields/access_tracker.py
confirm_access(pipeline=None)
¶
Atomically promote staged reads to the confirmed access log.
Uses a Lua script to: 1. Move all staged timestamps to the access log 2. Trim the log to max_access_log entries 3. Update access_count and last_accessed in the meta hash 4. Delete the staging list
TTL contract (confirm side):
A staged key that expired before confirm contributes 0 to
access_count and does not update last_accessed — this is
by design (see on_read() staged-read TTL contract). When this
happens a DEBUG log is emitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Optional Redis pipeline (not used for Lua eval, reserved for future use). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
Number of staged reads that were promoted (0 if the staged key was empty or had already expired). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the model instance has not been saved to Redis. |
Source code in src/popoto/fields/access_tracker.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
discard_staged_access(pipeline=None)
¶
Discard all staged reads without affecting confirmed data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Optional Redis pipeline for batch operations. |
None
|