Skip to content

Add booking statistics tracking and admin dashboard - #22

Open
datengraben wants to merge 1 commit into
masterfrom
claude/booking-stats-persistence-Kv7kI
Open

Add booking statistics tracking and admin dashboard#22
datengraben wants to merge 1 commit into
masterfrom
claude/booking-stats-persistence-Kv7kI

Conversation

@datengraben

Copy link
Copy Markdown
Owner

Summary

This PR introduces a comprehensive booking statistics system for Commons Booking, including a pre-aggregated stats table, service layer for querying statistics, an admin dashboard page, and a shortcode for displaying stats on the frontend.

Key Changes

New Database Table & Repository

  • BookingStats Repository (src/Repository/BookingStats.php): Manages the cb_booking_stats table with pre-aggregated booking counts and durations per day per entity (global/item/location)
    • Implements recordBooking() and removeBooking() to keep stats in sync when bookings are confirmed/cancelled
    • Provides getAggregated() for efficient range queries
    • Includes recomputeAll() for rebuilding stats from scratch
    • Uses INSERT ... ON DUPLICATE KEY UPDATE for safe concurrent updates

Service Layer

  • BookingStats Service (src/Service/BookingStats.php): High-level API for statistics queries
    • Provides period-specific methods: getDayStats(), getWeekStats(), getMonthStats(), getYearStats()
    • Generic getPeriodStats() method for flexible period selection
    • Handles entity resolution (item/location/global) and date range computation
    • Hooks into transition_post_status to automatically sync stats when booking status changes
    • Returns structured data with current/previous period comparisons and percentage deltas

Admin Dashboard

  • Stats View (src/View/Stats.php): Admin page controller

    • Renders statistics dashboard at CommonsBooking → Statistics
    • Supports filtering by item or location
    • Handles "Recompute Statistics" admin action with nonce verification
    • Generates stat cards for day/week/month/year periods
  • Stats Template (templates/stats-index.php): Admin UI

    • Filter form for selecting items/locations
    • Four stat cards showing current period vs. comparison period with visual indicators
    • Maintenance section with recompute button (admin-only)
    • Success notice after recomputation

Frontend Shortcode

  • BookingStats Shortcode (src/Wordpress/Shortcode/BookingStats.php): [cb_booking_stats]
    • Attributes: period (day/week/month/year), compare (true/false), item/location (ID or "all"), show (count/days/both)
    • Displays booking statistics with optional comparison to previous period
    • Color-coded diff indicators (green for increase, red for decrease)
    • Fully translatable output with proper escaping

Plugin Integration

  • Plugin.php:
    • Initializes stats table on plugin activation
    • Ensures table exists on admin_init (for existing installations)
    • Registers admin menu page for statistics dashboard
    • Registers booking stats shortcode
    • Hooks BookingStatsService::handleStatusTransition() to transition_post_status

Implementation Details

  • Efficient Aggregation: Pre-aggregated daily stats eliminate need for expensive queries over large booking datasets
  • Automatic Sync: Stats table stays in sync via WordPress post status transitions—no manual triggers needed
  • Safe Concurrent Updates: Uses MySQL ON DUPLICATE KEY UPDATE to handle race conditions
  • Flexible Filtering: Supports global, per-item, and per-location statistics
  • Comparison Metrics: Automatically calculates period-over-period deltas with percentage changes
  • Idempotent Table Creation: Uses dbDelta() for safe schema initialization on existing installs
  • Translatable: All UI strings use WordPress i18n functions with proper context

https://claude.ai/code/session_01893WfsffpRupUqYoStyKaG

Introduces a custom `cb_booking_stats` DB table that pre-aggregates
confirmed booking counts and total booked days per day per entity
(global / per-item / per-location). This replaces on-the-fly full
post-meta scans for any aggregate stats query.

- Repository\BookingStats – table init (dbDelta), upsert/decrement rows,
  recomputeAll(), getAggregated() with date-range + entity filter
- Service\BookingStats – period helpers (day/week/month/year, current vs
  previous), diff % calculation, transition_post_status hook handler
- View\Stats – admin page render + "Recompute Statistics" POST handler
- templates/stats-index.php – four stat cards (Today/Week/Month/Year)
  with item/location filter dropdowns and maintenance recompute button
- Wordpress\Shortcode\BookingStats – [cb_booking_stats] shortcode with
  period, compare, item, location, show attributes
- Plugin.php – wires activation init, lifecycle hook, admin submenu,
  admin_post handler, and shortcode registration

https://claude.ai/code/session_01893WfsffpRupUqYoStyKaG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants