Refactor: Split cost reporting into separate class

Extract read-only reporting methods from PRAutoBlogger_Cost_Tracker into a new
PRAutoBlogger_Cost_Reporter class. This reduces Cost_Tracker from 313 to 206
lines and Reporter to 141 lines, both under the 300-line cap.

Changed files:
- Create includes/core/class-cost-reporter.php with reporting methods:
  * get_monthly_spend(): float
  * get_daily_spend(int $days = 30): array
  * get_spend_by_stage(string $start_date, string $end_date): array
  * get_budget_utilization(): float

- Update includes/core/class-cost-tracker.php:
  * Remove reporting methods
  * Update is_budget_exceeded() and would_exceed_budget() to use Cost_Reporter
  * Keep get_current_run_cost() (tracks current pipeline run only)
  * Add @see cross-reference to Cost_Reporter

- Update templates and admin classes to instantiate Cost_Reporter:
  * templates/admin/metrics-page.php
  * templates/admin/settings-page.php
  * includes/admin/class-admin-notices.php
  * includes/admin/class-dashboard-widget.php

- Split tests/unit/Core/CostTrackerTest.php:
  * Create tests/unit/Core/CostReporterTest.php for reporter methods
  * Keep run ID and budget enforcement tests in CostTrackerTest

Autoloader automatically discovers PRAutoBlogger_Cost_Reporter class.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-16 20:27:57 +07:00
parent 7197249cab
commit 49d094b4f8
8 changed files with 255 additions and 179 deletions

View File

@@ -9,15 +9,15 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
$monthly_spend = $cost_tracker->get_monthly_spend();
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
$monthly_spend = $cost_reporter->get_monthly_spend();
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
$utilization = $cost_tracker->get_budget_utilization();
$daily_spend = $cost_tracker->get_daily_spend( 30 );
$utilization = $cost_reporter->get_budget_utilization();
$daily_spend = $cost_reporter->get_daily_spend( 30 );
$first_of_month = gmdate( 'Y-m-01' );
$today = gmdate( 'Y-m-d' );
$stage_breakdown = $cost_tracker->get_spend_by_stage( $first_of_month, $today );
$stage_breakdown = $cost_reporter->get_spend_by_stage( $first_of_month, $today );
?>
<div class="wrap prautoblogger-metrics">
<h1><?php esc_html_e( 'PRAutoBlogger — Metrics & Costs', 'prautoblogger' ); ?></h1>

View File

@@ -14,8 +14,8 @@ if ( ! defined( 'ABSPATH' ) ) {
}
$active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'prautoblogger_api';
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
$monthly_spend = $cost_tracker->get_monthly_spend();
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
$monthly_spend = $cost_reporter->get_monthly_spend();
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
$utilization = $budget > 0 ? ( $monthly_spend / $budget ) * 100.0 : 0;
$next_run = wp_next_scheduled( 'prautoblogger_daily_generation' );