Skip to content

Commit 1dce1ab

Browse files
Copilotpeterjaap
andcommitted
Make thresholds configurable per indexer with sane defaults
Co-authored-by: peterjaap <[email protected]>
1 parent 19810ab commit 1dce1ab

File tree

2 files changed

+83
-24
lines changed

2 files changed

+83
-24
lines changed

Checks/IndexerBacklog.php

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,41 @@ public function run(): CheckResultInterface
4646
$checkResult->setName('indexer_backlog');
4747
$checkResult->setLabel('Indexer Backlog');
4848

49-
$warningThreshold = $this->getWarningThreshold();
50-
$criticalThreshold = $this->getCriticalThreshold();
51-
5249
$backlogData = $this->getAllIndexerBacklogs();
5350
$totalProducts = $this->getTotalProducts();
5451

55-
// Calculate statistics
52+
// Calculate statistics and determine per-indexer status
5653
$maxBacklog = 0;
5754
$totalBacklog = 0;
5855
$indexersWithBacklog = 0;
56+
$worstStatus = CheckStatus::STATUS_OK;
57+
$criticalIndexers = [];
58+
$warningIndexers = [];
5959

6060
foreach ($backlogData as $indexerId => $data) {
6161
$backlog = $data['backlog'];
62+
$warningThreshold = $this->getWarningThreshold($indexerId);
63+
$criticalThreshold = $this->getCriticalThreshold($indexerId);
64+
65+
// Add threshold info to backlog data
66+
$backlogData[$indexerId]['warning_threshold'] = $warningThreshold;
67+
$backlogData[$indexerId]['critical_threshold'] = $criticalThreshold;
68+
69+
// Determine per-indexer status
70+
if ($backlog >= $criticalThreshold) {
71+
$backlogData[$indexerId]['check_status'] = CheckStatus::STATUS_FAILED;
72+
$worstStatus = CheckStatus::STATUS_FAILED;
73+
$criticalIndexers[] = $data['title'];
74+
} elseif ($backlog >= $warningThreshold) {
75+
$backlogData[$indexerId]['check_status'] = CheckStatus::STATUS_WARNING;
76+
if ($worstStatus !== CheckStatus::STATUS_FAILED) {
77+
$worstStatus = CheckStatus::STATUS_WARNING;
78+
}
79+
$warningIndexers[] = $data['title'];
80+
} else {
81+
$backlogData[$indexerId]['check_status'] = CheckStatus::STATUS_OK;
82+
}
83+
6284
if ($backlog > 0) {
6385
$indexersWithBacklog++;
6486
$totalBacklog += $backlog;
@@ -75,31 +97,27 @@ public function run(): CheckResultInterface
7597
'max_backlog' => $maxBacklog,
7698
'total_backlog' => $totalBacklog,
7799
'indexers_with_backlog' => $indexersWithBacklog,
78-
'warning_threshold' => $warningThreshold,
79-
'critical_threshold' => $criticalThreshold,
80100
]);
81101

82-
// Determine status based on backlog size
83-
if ($maxBacklog >= $criticalThreshold) {
84-
$checkResult->setStatus(CheckStatus::STATUS_FAILED);
102+
// Determine overall status based on worst per-indexer status
103+
$checkResult->setStatus($worstStatus);
104+
105+
if ($worstStatus === CheckStatus::STATUS_FAILED) {
85106
$checkResult->setNotificationMessage(
86-
sprintf('Critical indexer backlog detected: %d items in backlog', $maxBacklog)
107+
sprintf('Critical backlog in indexer(s): %s', implode(', ', $criticalIndexers))
87108
);
88-
$checkResult->setShortSummary(sprintf('Critical backlog: %d items', $maxBacklog));
89-
} elseif ($maxBacklog >= $warningThreshold) {
90-
$checkResult->setStatus(CheckStatus::STATUS_WARNING);
109+
$checkResult->setShortSummary(sprintf('%d critical indexer(s)', count($criticalIndexers)));
110+
} elseif ($worstStatus === CheckStatus::STATUS_WARNING) {
91111
$checkResult->setNotificationMessage(
92-
sprintf('High indexer backlog detected: %d items in backlog', $maxBacklog)
112+
sprintf('High backlog in indexer(s): %s', implode(', ', $warningIndexers))
93113
);
94-
$checkResult->setShortSummary(sprintf('High backlog: %d items', $maxBacklog));
114+
$checkResult->setShortSummary(sprintf('%d warning indexer(s)', count($warningIndexers)));
95115
} elseif ($indexersWithBacklog > 0) {
96-
$checkResult->setStatus(CheckStatus::STATUS_OK);
97116
$checkResult->setNotificationMessage(
98117
sprintf('%d indexer(s) have backlog but within acceptable range', $indexersWithBacklog)
99118
);
100119
$checkResult->setShortSummary(sprintf('%d indexer(s) with minor backlog', $indexersWithBacklog));
101120
} else {
102-
$checkResult->setStatus(CheckStatus::STATUS_OK);
103121
$checkResult->setNotificationMessage('All indexers are up to date');
104122
$checkResult->setShortSummary('All indexers up to date');
105123
}
@@ -208,10 +226,22 @@ private function getIndexerIds(): array
208226
/**
209227
* Get the warning threshold from configuration or default
210228
*
229+
* @param string|null $indexerId Indexer ID for per-indexer threshold
211230
* @return int
212231
*/
213-
private function getWarningThreshold(): int
232+
private function getWarningThreshold(?string $indexerId = null): int
214233
{
234+
// Check for per-indexer threshold first
235+
if ($indexerId !== null) {
236+
$perIndexerConfig = $this->deploymentConfig->get(
237+
'ohdear/Elgentos\OhDearChecks\Checks\IndexerBacklog/thresholds/' . $indexerId . '/warning'
238+
);
239+
if (is_numeric($perIndexerConfig) && $perIndexerConfig > 0) {
240+
return (int) $perIndexerConfig;
241+
}
242+
}
243+
244+
// Fall back to global threshold
215245
$config = $this->deploymentConfig->get('ohdear/Elgentos\OhDearChecks\Checks\IndexerBacklog/warning_threshold');
216246

217247
if (is_numeric($config) && $config > 0) {
@@ -224,10 +254,22 @@ private function getWarningThreshold(): int
224254
/**
225255
* Get the critical threshold from configuration or default
226256
*
257+
* @param string|null $indexerId Indexer ID for per-indexer threshold
227258
* @return int
228259
*/
229-
private function getCriticalThreshold(): int
260+
private function getCriticalThreshold(?string $indexerId = null): int
230261
{
262+
// Check for per-indexer threshold first
263+
if ($indexerId !== null) {
264+
$perIndexerConfig = $this->deploymentConfig->get(
265+
'ohdear/Elgentos\OhDearChecks\Checks\IndexerBacklog/thresholds/' . $indexerId . '/critical'
266+
);
267+
if (is_numeric($perIndexerConfig) && $perIndexerConfig > 0) {
268+
return (int) $perIndexerConfig;
269+
}
270+
}
271+
272+
// Fall back to global threshold
231273
$config = $this->deploymentConfig->get('ohdear/Elgentos\OhDearChecks\Checks\IndexerBacklog/critical_threshold');
232274

233275
if (is_numeric($config) && $config > 0) {

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,36 @@ You can disable any check by adding configuration to your `env.php`:
6363
'catalogsearch_fulltext',
6464
// ... add or remove indexer IDs as needed
6565
],
66-
// Optional: customize warning threshold (default: 1000)
66+
// Optional: global default thresholds (used when no per-indexer threshold is set)
6767
'warning_threshold' => 1000,
68-
// Optional: customize critical threshold (default: 10000)
69-
'critical_threshold' => 10000
68+
'critical_threshold' => 10000,
69+
// Optional: per-indexer thresholds (override global defaults)
70+
'thresholds' => [
71+
'catalog_product_price' => [
72+
'warning' => 500,
73+
'critical' => 5000
74+
],
75+
'catalogsearch_fulltext' => [
76+
'warning' => 2000,
77+
'critical' => 15000
78+
],
79+
// ... configure thresholds for specific indexers
80+
]
7081
]
7182
]
7283
```
7384

7485
### Indexer Backlog Configuration Options
7586

7687
- **`indexer_ids`** (array): List of indexer IDs to monitor. If not specified, a default list of 12 common indexers is used.
77-
- **`warning_threshold`** (int): Backlog size that triggers a WARNING status. Default: 1,000 items.
78-
- **`critical_threshold`** (int): Backlog size that triggers a FAILED status. Default: 10,000 items.
88+
- **`warning_threshold`** (int): Global warning threshold. Default: 1,000 items. Used when no per-indexer threshold is configured.
89+
- **`critical_threshold`** (int): Global critical threshold. Default: 10,000 items. Used when no per-indexer threshold is configured.
90+
- **`thresholds`** (array): Per-indexer threshold configuration. Each indexer can have its own `warning` and `critical` values that override the global defaults.
91+
92+
**Priority Order:**
93+
1. Per-indexer threshold (if configured)
94+
2. Global threshold (if configured)
95+
3. Default threshold (1,000 for warning, 10,000 for critical)
7996

8097
## Contributing
8198

0 commit comments

Comments
 (0)