checkpoint: pre-audit baseline state

This commit is contained in:
Dwindi Ramadhana
2026-06-06 00:29:10 +07:00
parent 579aab1b2b
commit ae70e4aea9
38 changed files with 4061 additions and 3149 deletions

View File

@@ -640,6 +640,7 @@ class WP_Agentic_Writer_Settings_V2 {
'monthly' => '0.0000',
'today' => '0.0000',
'avg_per_post' => '0.0000',
'action_summary' => array(),
),
'filters' => array(
'models' => array(),
@@ -662,8 +663,8 @@ class WP_Agentic_Writer_Settings_V2 {
$filter_date_from = isset( $_POST['filter_date_from'] ) ? sanitize_text_field( $_POST['filter_date_from'] ) : '';
$filter_date_to = isset( $_POST['filter_date_to'] ) ? sanitize_text_field( $_POST['filter_date_to'] ) : '';
// Build WHERE clause
$where = array( '1=1' );
// Build WHERE clause (OpenRouter-only for this OpenRouter cost log screen).
$where = array( "provider = 'openrouter'" );
if ( $filter_post > 0 ) {
$where[] = $wpdb->prepare( 'post_id = %d', $filter_post );
}
@@ -697,7 +698,7 @@ class WP_Agentic_Writer_Settings_V2 {
FROM {$table_name}
WHERE {$where_clause}
GROUP BY post_id
ORDER BY total_cost DESC
ORDER BY post_id DESC
LIMIT %d OFFSET %d",
$per_page,
$offset
@@ -737,25 +738,80 @@ class WP_Agentic_Writer_Settings_V2 {
);
}
// Get details for visible posts only (on-demand loading).
// For better performance, we skip details here and let frontend request them.
// The details can be loaded via a separate endpoint when user expands a row.
// Load detail rows for visible posts.
// This keeps expand/collapse usable without requiring a second endpoint.
if ( ! empty( $post_ids ) ) {
$placeholders = implode( ',', array_fill( 0, count( $post_ids ), '%d' ) );
$details_sql = $wpdb->prepare(
"SELECT post_id, created_at, model, action, input_tokens, output_tokens, cost
FROM {$table_name}
WHERE provider = 'openrouter' AND post_id IN ({$placeholders})
ORDER BY created_at DESC",
...$post_ids
);
$detail_rows = $wpdb->get_results( $details_sql, ARRAY_A );
$detail_map = array();
foreach ( $detail_rows as $detail_row ) {
$pid = (int) ( $detail_row['post_id'] ?? 0 );
if ( ! isset( $detail_map[ $pid ] ) ) {
$detail_map[ $pid ] = array();
}
$detail_map[ $pid ][] = array(
'created_at' => date_i18n( 'Y-m-d H:i:s', strtotime( $detail_row['created_at'] ) ),
'model' => (string) ( $detail_row['model'] ?? '' ),
'action' => (string) ( $detail_row['action'] ?? '' ),
'input_tokens' => (int) ( $detail_row['input_tokens'] ?? 0 ),
'output_tokens' => (int) ( $detail_row['output_tokens'] ?? 0 ),
'cost' => number_format( (float) ( $detail_row['cost'] ?? 0 ), 4 ),
);
}
foreach ( $formatted_records as $idx => $formatted_record ) {
$pid = (int) ( $formatted_record['post_id'] ?? 0 );
$formatted_records[ $idx ]['details'] = $detail_map[ $pid ] ?? array();
$formatted_records[ $idx ]['details_total'] = count( $formatted_records[ $idx ]['details'] );
}
}
// Get summary stats (all-time aggregation in SQL)
$total_all_time = $wpdb->get_var( "SELECT COALESCE(SUM(cost), 0) FROM {$table_name}" );
$monthly_total = $cost_tracker->get_monthly_total();
$total_all_time = $wpdb->get_var( "SELECT COALESCE(SUM(cost), 0) FROM {$table_name} WHERE provider = 'openrouter'" );
$month_start = date( 'Y-m-01 00:00:00' );
$monthly_total = $wpdb->get_var(
$wpdb->prepare(
"SELECT COALESCE(SUM(cost), 0) FROM {$table_name} WHERE provider = 'openrouter' AND created_at >= %s",
$month_start
)
);
$today_total = $wpdb->get_var(
$wpdb->prepare(
"SELECT COALESCE(SUM(cost), 0) FROM {$table_name} WHERE DATE(created_at) = %s",
"SELECT COALESCE(SUM(cost), 0) FROM {$table_name} WHERE provider = 'openrouter' AND DATE(created_at) = %s",
current_time( 'Y-m-d' )
)
);
$total_posts = $wpdb->get_var( "SELECT COUNT(DISTINCT post_id) FROM {$table_name} WHERE post_id > 0" );
$total_posts = $wpdb->get_var( "SELECT COUNT(DISTINCT post_id) FROM {$table_name} WHERE provider = 'openrouter' AND post_id > 0" );
$avg_per_post = $total_posts > 0 ? $total_all_time / $total_posts : 0;
$action_summary_rows = $wpdb->get_results(
"SELECT action, COUNT(*) AS calls, COALESCE(SUM(cost), 0) AS total_cost, COALESCE(AVG(cost), 0) AS avg_cost
FROM {$table_name}
WHERE provider = 'openrouter'
GROUP BY action
ORDER BY total_cost DESC",
ARRAY_A
);
$action_summary = array();
foreach ( $action_summary_rows as $row ) {
$action_summary[] = array(
'action' => (string) ( $row['action'] ?? '' ),
'calls' => (int) ( $row['calls'] ?? 0 ),
'total' => number_format( (float) ( $row['total_cost'] ?? 0 ), 4 ),
'average' => number_format( (float) ( $row['avg_cost'] ?? 0 ), 4 ),
);
}
// Get filter options (distinct values from DB)
$models = $wpdb->get_col( "SELECT DISTINCT model FROM {$table_name} ORDER BY model LIMIT 100" );
$types = $wpdb->get_col( "SELECT DISTINCT action FROM {$table_name} ORDER BY action" );
$models = $wpdb->get_col( "SELECT DISTINCT model FROM {$table_name} WHERE provider = 'openrouter' ORDER BY model LIMIT 100" );
$types = $wpdb->get_col( "SELECT DISTINCT action FROM {$table_name} WHERE provider = 'openrouter' ORDER BY action" );
wp_send_json_success( array(
'records' => $formatted_records,
@@ -768,6 +824,7 @@ class WP_Agentic_Writer_Settings_V2 {
'monthly' => number_format( (float) $monthly_total, 4 ),
'today' => number_format( (float) $today_total, 4 ),
'avg_per_post' => number_format( (float) $avg_per_post, 4 ),
'action_summary' => $action_summary,
),
'filters' => array(
'models' => $models,
@@ -1042,6 +1099,13 @@ class WP_Agentic_Writer_Settings_V2 {
$sanitized['cost_tracking_enabled'] = isset( $input['cost_tracking_enabled'] ) && '1' === $input['cost_tracking_enabled'];
$sanitized['enable_clarification_quiz'] = isset( $input['enable_clarification_quiz'] ) && '1' === $input['enable_clarification_quiz'];
$sanitized['enable_faq_schema'] = isset( $input['enable_faq_schema'] ) ? '1' === $input['enable_faq_schema'] : false;
$sanitized['allow_openrouter_fallback'] = isset( $input['allow_openrouter_fallback'] ) && '1' === $input['allow_openrouter_fallback'];
$sanitized['openrouter_provider_routing_enabled'] = isset( $input['openrouter_provider_routing_enabled'] ) && '1' === $input['openrouter_provider_routing_enabled'];
$sanitized['openrouter_provider_only'] = isset( $input['openrouter_provider_only'] ) && '1' === $input['openrouter_provider_only'];
$sanitized['openrouter_allow_provider_fallbacks'] = isset( $input['openrouter_allow_provider_fallbacks'] ) && '1' === $input['openrouter_allow_provider_fallbacks'];
$provider_slug = isset( $input['openrouter_provider_slug'] ) ? sanitize_key( $input['openrouter_provider_slug'] ) : 'auto';
$sanitized['openrouter_provider_slug'] = '' !== $provider_slug ? $provider_slug : 'auto';
// Sanitize search options
$sanitized['search_engine'] = in_array( $input['search_engine'] ?? '', array( 'auto', 'native', 'exa' ), true )
@@ -1178,6 +1242,11 @@ class WP_Agentic_Writer_Settings_V2 {
$local_backend_key = $settings['local_backend_key'] ?? 'dummy';
$local_backend_model = $settings['local_backend_model'] ?? 'claude-local';
$task_providers = $settings['task_providers'] ?? array();
$allow_openrouter_fallback = ! empty( $settings['allow_openrouter_fallback'] );
$openrouter_provider_routing_enabled = ! empty( $settings['openrouter_provider_routing_enabled'] );
$openrouter_provider_slug = $settings['openrouter_provider_slug'] ?? 'auto';
$openrouter_provider_only = ! empty( $settings['openrouter_provider_only'] );
$openrouter_allow_provider_fallbacks = ! empty( $settings['openrouter_allow_provider_fallbacks'] );
// Get cost tracking data
$cost_tracker = WP_Agentic_Writer_Cost_Tracker::get_instance();
@@ -1214,6 +1283,11 @@ class WP_Agentic_Writer_Settings_V2 {
'local_backend_key',
'local_backend_model',
'task_providers',
'allow_openrouter_fallback',
'openrouter_provider_routing_enabled',
'openrouter_provider_slug',
'openrouter_provider_only',
'openrouter_allow_provider_fallbacks',
'settings'
);
}