Problem/Motivation
UsageProviderFilter::getValueOptions() and UsageModelFilter::getValueOptions() run an uncached SELECT DISTINCT against ai_metering_usage on every request. The in-memory guard (if (isset($this->valueOptions))) does not survive between page loads, so every admin user hitting the log view triggers two redundant queries — including a full table scan on model_id, which has no index.
Steps to reproduce
- Enable query logging (Devel or WebProfiler).
- Load
/admin/reports/ai-metering/login two separate sessions. - Observe
SELECT DISTINCT provider_id FROM {ai_metering_usage}running on every load.
Proposed resolution
Cache the options with a Cache::PERMANENT entry tagged ai_metering:usage, invalidated when a new usage record is written:
use Drupal\Core\Cache\Cache; public function getValueOptions(): void { if (isset($this->valueOptions)) { return; } $cid = 'ai_metering:filter_options:providers'; if ($cached = \Drupal::cache()->get($cid)) { $this->valueOptions = $cached->data; return; } // ... existing SELECT DISTINCT query ... \Drupal::cache()->set($cid, $this->valueOptions, Cache::PERMANENT, ['ai_metering:usage']); }
Inject CacheBackendInterface for testability rather than using the static call.
Remaining tasks
- Apply the pattern to both
UsageProviderFilterandUsageModelFilter - Add
Cache::invalidateTags(['ai_metering:usage'])in the usage record writer - Extend
ViewsFilterTestto assert cache hit on second call
UI / API / Data model changes
Internal change only. No visible impact on the filter dropdowns.
AI assistance
Cache strategy and proposed code drafted with AI assistance and reviewed by the module maintainer.
Comments
Comment #3
codeitwisely commented