72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class AlertModel extends Model
|
|
{
|
|
protected $table = 'alerts';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = true;
|
|
protected $protectFields = true;
|
|
|
|
protected $allowedFields = [
|
|
'workspace_id',
|
|
'chart_id',
|
|
'name',
|
|
'metric_field',
|
|
'condition',
|
|
'threshold',
|
|
'check_interval',
|
|
'notify_email',
|
|
'email_addresses',
|
|
'notify_slack',
|
|
'slack_webhook',
|
|
'is_active',
|
|
'is_muted_until',
|
|
'last_checked_at',
|
|
'last_triggered_at',
|
|
'created_by',
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
protected $validationRules = [
|
|
'workspace_id' => 'required|integer',
|
|
'chart_id' => 'required|integer',
|
|
'name' => 'required|min_length[2]|max_length[200]',
|
|
'metric_field' => 'required|max_length[150]',
|
|
'condition' => 'required|in_list[gt,lt,eq,gte,lte]',
|
|
'threshold' => 'required|decimal',
|
|
];
|
|
|
|
public function forWorkspace(int $workspaceId): array
|
|
{
|
|
return $this->select('alerts.*, charts.name AS chart_name')
|
|
->join('charts', 'charts.id = alerts.chart_id', 'left')
|
|
->where('alerts.workspace_id', $workspaceId)
|
|
->orderBy('alerts.id', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Active alerts across all workspaces (for cron).
|
|
*
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function allActiveForEngine(): array
|
|
{
|
|
return $this->where('is_active', 1)->findAll();
|
|
}
|
|
}
|