Implement Phase 9: Prometheus Metrics (v0.9.0)
All checks were successful
Create Release Package / build-release (push) Successful in 1m8s

- Add Prometheus metrics integration via wp-prometheus hooks
- Create Grafana dashboard JSON with 24 panels
- Add metrics settings tab with enable/disable toggle
- Expose inventory, booking, occupancy, revenue, and guest metrics

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-03 20:40:54 +01:00
parent b6d7eeb5ec
commit 13fd25f84c
8 changed files with 2805 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ use Magdev\WpBnb\Booking\EmailNotifier;
use Magdev\WpBnb\Frontend\Search;
use Magdev\WpBnb\Frontend\Shortcodes;
use Magdev\WpBnb\Integration\CF7;
use Magdev\WpBnb\Integration\Prometheus;
use Magdev\WpBnb\Frontend\Widgets\AvailabilityCalendar;
use Magdev\WpBnb\Frontend\Widgets\BuildingRooms;
use Magdev\WpBnb\Frontend\Widgets\SimilarRooms;
@@ -142,6 +143,9 @@ final class Plugin {
CF7::init();
}
// Initialize Prometheus metrics integration.
Prometheus::init();
// Initialize admin components.
if ( is_admin() ) {
$this->init_admin();
@@ -610,6 +614,10 @@ final class Plugin {
class="nav-tab <?php echo 'updates' === $active_tab ? 'nav-tab-active' : ''; ?>">
<?php esc_html_e( 'Updates', 'wp-bnb' ); ?>
</a>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=wp-bnb-settings&tab=metrics' ) ); ?>"
class="nav-tab <?php echo 'metrics' === $active_tab ? 'nav-tab-active' : ''; ?>">
<?php esc_html_e( 'Metrics', 'wp-bnb' ); ?>
</a>
</nav>
<div class="tab-content">
@@ -624,6 +632,9 @@ final class Plugin {
case 'updates':
$this->render_updates_settings();
break;
case 'metrics':
$this->render_metrics_settings();
break;
default:
$this->render_general_settings();
break;
@@ -1265,6 +1276,163 @@ final class Plugin {
<?php
}
/**
* Render metrics settings tab.
*
* @return void
*/
private function render_metrics_settings(): void {
$metrics_enabled = Prometheus::is_enabled();
$prometheus_active = class_exists( '\Magdev\WpPrometheus\Plugin' ) || defined( 'WP_PROMETHEUS_VERSION' );
$dashboard_file = WP_BNB_PATH . 'assets/grafana/wp-bnb-dashboard.json';
$dashboard_available = file_exists( $dashboard_file );
?>
<form method="post" action="">
<?php wp_nonce_field( 'wp_bnb_save_settings', 'wp_bnb_settings_nonce' ); ?>
<h2><?php esc_html_e( 'Prometheus Metrics', 'wp-bnb' ); ?></h2>
<?php if ( ! $prometheus_active ) : ?>
<div class="notice notice-warning inline" style="margin: 0 0 20px 0;">
<p>
<span class="dashicons dashicons-warning" style="color: #dba617;"></span>
<strong><?php esc_html_e( 'WP Prometheus not detected', 'wp-bnb' ); ?></strong><br>
<?php
printf(
/* translators: %s: Plugin URL */
esc_html__( 'The WP Prometheus plugin is required to expose metrics. Please install and activate it from %s.', 'wp-bnb' ),
'<a href="https://src.bundespruefstelle.ch/magdev/wp-prometheus" target="_blank">wp-prometheus</a>'
);
?>
</p>
</div>
<?php else : ?>
<div class="notice notice-success inline" style="margin: 0 0 20px 0;">
<p>
<span class="dashicons dashicons-yes-alt" style="color: #00a32a;"></span>
<strong><?php esc_html_e( 'WP Prometheus is active', 'wp-bnb' ); ?></strong>
<?php esc_html_e( 'Metrics will be exposed via the /metrics/ endpoint.', 'wp-bnb' ); ?>
</p>
</div>
<?php endif; ?>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><?php esc_html_e( 'Enable Metrics', 'wp-bnb' ); ?></th>
<td>
<label>
<input type="checkbox" name="wp_bnb_metrics_enabled"
value="yes" <?php checked( $metrics_enabled ); ?>>
<?php esc_html_e( 'Expose BnB metrics via Prometheus', 'wp-bnb' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'When enabled, occupancy, booking, revenue, and guest metrics will be available for Prometheus scraping.', 'wp-bnb' ); ?>
</p>
</td>
</tr>
</table>
<h2><?php esc_html_e( 'Available Metrics', 'wp-bnb' ); ?></h2>
<p class="description"><?php esc_html_e( 'The following metrics are exposed when enabled:', 'wp-bnb' ); ?></p>
<table class="widefat striped" style="max-width: 800px; margin-top: 10px;">
<thead>
<tr>
<th><?php esc_html_e( 'Metric', 'wp-bnb' ); ?></th>
<th><?php esc_html_e( 'Type', 'wp-bnb' ); ?></th>
<th><?php esc_html_e( 'Description', 'wp-bnb' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>wp_bnb_buildings_total</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total number of buildings', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_rooms_total</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total rooms by status (available, occupied, maintenance, inactive)', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_bookings_total</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total bookings by status (pending, confirmed, checked_in, checked_out, cancelled)', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_occupancy_rate_current</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Current room occupancy rate (percentage)', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_occupancy_rate_this_month</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Room occupancy rate for current month (percentage)', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_checkins_today</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Number of check-ins scheduled for today', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_checkouts_today</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Number of check-outs scheduled for today', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_revenue_this_month</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total revenue for current month', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_revenue_ytd</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total revenue year to date', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_booking_avg_value</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Average booking value', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_guests_total</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Total number of registered guests', 'wp-bnb' ); ?></td>
</tr>
<tr>
<td><code>wp_bnb_guests_repeat</code></td>
<td><?php esc_html_e( 'Gauge', 'wp-bnb' ); ?></td>
<td><?php esc_html_e( 'Number of repeat guests (more than one booking)', 'wp-bnb' ); ?></td>
</tr>
</tbody>
</table>
<h2 style="margin-top: 30px;"><?php esc_html_e( 'Grafana Dashboard', 'wp-bnb' ); ?></h2>
<?php if ( $dashboard_available ) : ?>
<p>
<?php esc_html_e( 'A pre-configured Grafana dashboard is available for visualizing WP BnB metrics.', 'wp-bnb' ); ?>
</p>
<p>
<strong><?php esc_html_e( 'Dashboard file:', 'wp-bnb' ); ?></strong>
<code>assets/grafana/wp-bnb-dashboard.json</code>
</p>
<p class="description">
<?php esc_html_e( 'If WP Prometheus is installed, this dashboard will be automatically registered and available for export in the Prometheus settings.', 'wp-bnb' ); ?>
</p>
<?php else : ?>
<div class="notice notice-warning inline">
<p><?php esc_html_e( 'Grafana dashboard file not found.', 'wp-bnb' ); ?></p>
</div>
<?php endif; ?>
<p class="submit">
<?php submit_button( __( 'Save Metrics Settings', 'wp-bnb' ), 'primary', 'submit', false ); ?>
</p>
</form>
<?php
}
/**
* Render license status badge.
*
@@ -1336,6 +1504,9 @@ final class Plugin {
case 'updates':
$this->save_updates_settings();
break;
case 'metrics':
$this->save_metrics_settings();
break;
default:
$this->save_general_settings();
break;
@@ -1465,6 +1636,19 @@ final class Plugin {
settings_errors( 'wp_bnb_settings' );
}
/**
* Save metrics settings.
*
* @return void
*/
private function save_metrics_settings(): void {
$metrics_enabled = isset( $_POST['wp_bnb_metrics_enabled'] ) ? 'yes' : 'no';
update_option( Prometheus::OPTION_ENABLED, $metrics_enabled );
add_settings_error( 'wp_bnb_settings', 'settings_saved', __( 'Metrics settings saved.', 'wp-bnb' ), 'success' );
settings_errors( 'wp_bnb_settings' );
}
/**
* AJAX handler for checking room availability.
*