get_url()); exit; } private static function load_registry() { if (!class_exists('AIEO_Signal_Registry') && defined('AIEO_DIR')) { $f = AIEO_DIR . 'includes/class-aieo-signal-registry.php'; if (is_readable($f)) { require_once $f; } } return class_exists('AIEO_Signal_Registry'); } public function render_subtab() { list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'signals-what-this-site-measures', 'title' => __('What this site measures', 'ai-eshop-optimizer'), 'description' => __('Everything this shop measures about itself, in one list: what each metric is, the table it lands in, and the code that produces and reads it. The breadth is the point. Recommendations, audiences, pricing decisions and the answers an AI assistant gives about your shop are only as good as what is measured underneath them — a shop that measures this much can be asked real questions and give real answers, while one that measures a handful can only guess. Check here before building anything new; most of what you need already exists. "Dormant" means the code ships but has never produced a result, and "unconsumed" means rows are written every night that nothing reads — both are worth clearing, because a measurement nobody uses is cost without benefit.', 'ai-eshop-optimizer'), )); echo $open; if (!self::load_registry()) { $aieo_sg_failed = \Rooster\Admin\States::state('failed', array('title' => __('The signal registry is not available in this build.', 'ai-eshop-optimizer'), 'compact' => true)); echo $aieo_sg_failed; echo $close; return; } $this->render_signals_card(); echo $close; } private function render_signals_card() { $status = AIEO_Signal_Registry::status(); $signals = isset($status['signals']) ? $status['signals'] : array(); $counts = isset($status['counts']) ? $status['counts'] : array(); if (empty($signals)) { $aieo_sg_none = \Rooster\Admin\States::state('first-use', array('title' => __('No signals are catalogued yet.', 'ai-eshop-optimizer'), 'compact' => true)); echo $aieo_sg_none; return; } $remeasure = \Rooster\Admin\Ui::button( __('Re-measure now', 'ai-eshop-optimizer'), array('href' => wp_nonce_url($this->get_form_action_url(self::ACTION_REMEASURE), self::ACTION_REMEASURE), 'size' => 'sm') ); $aieo_sg_head = \Rooster\Admin\States::result_head( sprintf( _n('%d signal', '%d signals', count($signals), 'ai-eshop-optimizer'), count($signals) ), array( 'facts' => array(sprintf( __('measured %s ago', 'ai-eshop-optimizer'), human_time_diff((int) $status['generated'], time()) )), 'actions' => $remeasure, ) ); echo $aieo_sg_head; echo '
'; foreach (AIEO_Signal_Registry::STATES as $st) { $n = isset($counts[$st]) ? (int) $counts[$st] : 0; if ($n < 1) { continue; } echo ' ' . self::signal_pill(AIEO_Signal_Registry::state_label($st) . ' ' . $n, $st); } $unscheduled = 0; foreach ($signals as $sig) { if (!empty($sig['health']['cron_missing'])) { $unscheduled++; } } if ($unscheduled > 0) { echo ' ' . self::signal_pill( sprintf( _n('%d unscheduled cron', '%d unscheduled crons', $unscheduled, 'ai-eshop-optimizer'), $unscheduled ), 'dormant' ); } $thin = 0; foreach ($signals as $sig) { if (!empty($sig['health']['underpopulated'])) { $thin++; } } if ($thin > 0) { echo ' ' . self::signal_pill( sprintf( _n('%d signal barely populated', '%d signals barely populated', $thin, 'ai-eshop-optimizer'), $thin ), 'dormant' ); } echo '
'; $signal_rows = array(); $grains = array(); foreach (AIEO_Signal_Registry::by_grain($signals) as $grain => $rows) { $grains[(string) $grain] = (string) $grain; usort($rows, static function ($a, $b) { $ai = array_search($a['state'], AIEO_Signal_Registry::STATES, true); $bi = array_search($b['state'], AIEO_Signal_Registry::STATES, true); if ($ai === $bi) { return strcmp($a['label'], $b['label']); } return $ai - $bi; }); foreach ($rows as $s) { $signal_rows[] = self::signal_row($s, (string) $grain); } } $states = array(); foreach (AIEO_Signal_Registry::STATES as $st) { $states[$st] = AIEO_Signal_Registry::state_label($st); } $grain_opts = array(); foreach ($grains as $g) { $n = 0; foreach ($signal_rows as $sr) { $n += ((string) ($sr['grain'] ?? '') === $g) ? 1 : 0; } $grain_opts[$g] = $g . ' (' . $n . ')'; } $state_opts = array(); foreach ($states as $sk => $sl) { $n = 0; foreach ($signal_rows as $sr) { $n += ((string) ($sr['state_key'] ?? '') === (string) $sk) ? 1 : 0; } $state_opts[$sk] = $sl . ' (' . $n . ')'; } $signal_cell = static function ($key) { return array( 'render' => static function ($row) use ($key) { return $row[$key]; }, ); }; AIEO_Table::render(array( 'id' => 'aieo-signals-inventory', 'caption' => __('What this site measures', 'ai-eshop-optimizer'), 'density' => 'compact', 'caps' => array('search' => true), 'columns' => array( array('key' => 'metric', 'label' => __('Metric', 'ai-eshop-optimizer'), 'type' => 'control', 'ra_type' => 'text', 'sort_key' => 'label', 'format' => $signal_cell('metric')), array('key' => 'storage', 'label' => __('Storage', 'ai-eshop-optimizer'), 'width' => 'md', 'type' => 'control', 'ra_type' => 'html', 'format' => $signal_cell('storage')), array('key' => 'state', 'label' => __('State', 'ai-eshop-optimizer'), 'type' => 'control', 'ra_type' => 'status', 'sort_key' => 'state_rank', 'format' => $signal_cell('state')), array('key' => 'producer', 'label' => __('Produced by', 'ai-eshop-optimizer'), 'type' => 'control', 'ra_type' => 'html', 'format' => $signal_cell('producer')), array('key' => 'consumers', 'label' => __('Read by', 'ai-eshop-optimizer'), 'type' => 'control', 'ra_type' => 'html', 'format' => $signal_cell('consumers')), ), 'filters' => array( array('key' => 'grain', 'label' => __('Grain', 'ai-eshop-optimizer'), 'kind' => 'values', 'control' => 'select', 'options' => $grain_opts), array('key' => 'state_key', 'label' => __('State', 'ai-eshop-optimizer'), 'kind' => 'values', 'control' => 'select', 'options' => $state_opts), ), 'rows' => $signal_rows, 'row_key' => 'id', 'row_class' => static function ($row) { return $row['alarm'] ? 'aieo-t-row--warning' : ''; }, 'page_id' => 'signals', )); } private static function signal_row($s, $grain) { $ui = '\Rooster\Admin\Ui'; $sub = static function ($html) { return '' . $html . ''; }; $alarm = AIEO_Signal_Registry::state_is_alarming($s['state']) || !empty($s['health']['cron_missing']) || !empty($s['health']['underpopulated']); $metric_html = '' . esc_html($s['label']) . '' . $sub('' . esc_html($s['id']) . '')
. $sub(esc_html(self::signal_trim($s['measures'], 190)))
. $sub(esc_html__('grain:', 'ai-eshop-optimizer') . ' ' . esc_html($s['grain'])
. ' · ' . esc_html__('profiles:', 'ai-eshop-optimizer') . ' ' . esc_html(implode(', ', $s['site_profiles'])));
$storage_html = '';
if (empty($s['health']['tables'])) {
$storage_html = $sub(esc_html__('computed on demand — no table', 'ai-eshop-optimizer'));
} else {
foreach ($s['health']['tables'] as $t) {
$rowtxt = (null === $t['rows'])
? ('VIEW' === $t['type'] ? __('view', 'ai-eshop-optimizer') : '—')
: number_format_i18n((int) $t['rows']);
$absent = empty($t['exists']);
$is_tmp = !empty($t['transient']);
$note = '';
$tooltip = '';
if (!empty($t['lazy']) && $absent) {
$note = esc_html__('(not used yet)', 'ai-eshop-optimizer');
$tooltip = __('This table is created the first time the feature is used, so it does not exist until then. Nothing is broken.', 'ai-eshop-optimizer');
} elseif ($is_tmp && $absent) {
$note = esc_html__('(cleared)', 'ai-eshop-optimizer');
$tooltip = __('Working table. It exists only while the stage is running, so this is the normal resting state.', 'ai-eshop-optimizer');
} elseif ($is_tmp) {
$note = esc_html($rowtxt) . ' ' . $ui::badge(trim(__('(left over)', 'ai-eshop-optimizer'), '() '), 'warning', array('sm' => true));
$tooltip = __('Working table still present. The stage that owns it drops it on its last batch, so this one was interrupted. Drop the table and re-run the stage.', 'ai-eshop-optimizer');
} elseif ($absent && !empty($s['health']['unconfigured'])) {
$note = esc_html__('(not set up)', 'ai-eshop-optimizer');
$tooltip = __('This integration has not been set up yet, so its table has never been created. Nothing is broken.', 'ai-eshop-optimizer');
} elseif ($absent) {
$note = $ui::badge(trim(__('(missing)', 'ai-eshop-optimizer'), '() '), 'destructive', array('sm' => true));
} else {
$note = esc_html($rowtxt);
}
$storage_html .= $sub('' . esc_html($t['name']) . ' ' . $note
. ('' !== $tooltip ? ' ' . \Rooster\Admin\Guidance::tip($tooltip, $t['name']) : ''));
}
}
$state_html = self::signal_pill(AIEO_Signal_Registry::state_label($s['state']), $s['state'], true)
. $sub(esc_html($s['state_reason']));
if (!empty($s['health']['underpopulated'])) {
$state_html .= $sub($ui::badge(
sprintf(
__('barely populated — %1$s rows, expected at least %2$s', 'ai-eshop-optimizer'),
number_format_i18n((int) $s['health']['rows']),
number_format_i18n((int) $s['health']['floor'])
),
'warning',
array('sm' => true)
));
}
if (!empty($s['health']['last_run'])) {
$state_html .= $sub(esc_html(sprintf(
__('last run %s ago', 'ai-eshop-optimizer'),
human_time_diff((int) $s['health']['last_run'], time())
)));
}
$producer_html = esc_html(self::signal_trim($s['producer'], 170));
if (empty($s['health']['crons'])) {
$producer_html .= $sub(esc_html__('no cron — on write / on demand', 'ai-eshop-optimizer'));
} else {
foreach ($s['health']['crons'] as $c) {
$producer_html .= $sub('' . esc_html($c['hook']) . ' ' . (!empty($c['scheduled'])
? self::signal_pill($c['via'] ? $c['via'] : __('scheduled', 'ai-eshop-optimizer'), 'healthy')
: self::signal_pill(__('not scheduled', 'ai-eshop-optimizer'), 'dormant')));
}
}
$consumers_html = '' === trim((string) $s['consumers'])
? $ui::badge(__('NOTHING READS THIS', 'ai-eshop-optimizer'), 'warning', array('sm' => true))
: esc_html(self::signal_trim($s['consumers'], 220));
if ('' !== trim((string) $s['notes'])) {
$consumers_html .= $sub(esc_html(self::signal_trim($s['notes'], 260)));
}
$rank = array_search($s['state'], AIEO_Signal_Registry::STATES, true);
return array(
'id' => (string) $s['id'],
'label' => (string) $s['label'],
'grain' => $grain,
'state_key' => (string) $s['state'],
'state_rank' => false === $rank ? 99 : (int) $rank,
'alarm' => (bool) $alarm,
'metric' => $metric_html,
'storage' => $storage_html,
'state' => $state_html,
'producer' => $producer_html,
'consumers' => $consumers_html,
);
}
private static function signal_pill($text, $state, $dot = false) {
$tone = 'mono';
if ('healthy' === $state) {
$tone = 'success';
} elseif ('dormant' === $state || 'unconsumed' === $state) {
$tone = 'warning';
}
return \Rooster\Admin\Ui::badge((string) $text, $tone, $dot ? array('dot' => true) : array('sm' => true));
}
private static function signal_trim($s, $max) {
$s = trim((string) $s);
if (mb_strlen($s) <= $max) { return $s; }
return mb_substr($s, 0, $max - 1) . '…';
}
}