Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/Logging/AI_Request_Log_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ private function get_date_condition( string $period ): string {
case 'week':
return 'AND timestamp >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 WEEK)';
case 'month':
return 'AND timestamp >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH)';
return 'AND timestamp >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 30 DAY)';
default:
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,51 @@ public function test_get_summary_windowed_period_uses_utc_session_timezone(): vo
}
}

/**
* Tests that the 'month' summary period spans a fixed 30-day window.
*
* The dashboard labels this period "Last 30 Days" and the logs table filters
* it as a fixed 30-day window, so the summary cutoff must also be 30 days.
* A calendar INTERVAL 1 MONTH spans 28–31 days depending on the month, which
* makes the summary cards disagree with the table for the same selection.
*
* @since 1.0.3
*/
public function test_get_summary_month_period_uses_30_day_window(): void {
global $wpdb;
$table = $wpdb->prefix . AI_Request_Log_Schema::TABLE_NAME;

// Just inside the 30-day window (29 days, 23 hours old).
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$table,
array(
'log_id' => wp_generate_uuid4(),
'timestamp' => gmdate( 'Y-m-d H:i:s', strtotime( '-29 days -23 hours' ) ),
'type' => 'ai_client',
'operation' => 'openai:completions',
'status' => 'success',
),
array( '%s', '%s', '%s', '%s', '%s' )
);

// Just outside the 30-day window (30 days, 1 hour old).
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$table,
array(
'log_id' => wp_generate_uuid4(),
'timestamp' => gmdate( 'Y-m-d H:i:s', strtotime( '-30 days -1 hour' ) ),
'type' => 'ai_client',
'operation' => 'openai:completions',
'status' => 'success',
),
array( '%s', '%s', '%s', '%s', '%s' )
);

$summary = $this->repository->get_summary( 'month', true );

$this->assertSame( 1, $summary['total_requests'] );
}

/**
* Tests that get_filter_options returns distinct values from logs.
*
Expand Down
Loading