This commit is contained in:
Javier Casares 2026-06-02 19:06:05 +00:00
commit 31be5438e7
7 changed files with 293 additions and 195 deletions

View file

@ -198,6 +198,69 @@ class Logger {
$this->track_s3_operation( $operation );
}
/**
* Log and track the result of a batch S3 upload.
*
* Performs a single DB write for all uploads in the batch instead of one
* write per file, reducing database load on bulk media imports.
*
* @since 1.2.0
*
* @param string $operation S3 operation name (e.g. 'putObject').
* @param int $success_count Number of files successfully uploaded.
* @param int $fail_count Number of files that failed.
* @return void
*/
public function s3_batch( string $operation, int $success_count, int $fail_count ): void {
if ( $success_count > 0 ) {
$this->track_bulk_s3_operations( $operation, $success_count );
}
if ( $fail_count > 0 ) {
$this->log(
self::LEVEL_ERROR,
sprintf( 'S3 %s batch: %d failure(s)', $operation, $fail_count )
);
}
}
/**
* Increment S3 operation counters by an arbitrary count in a single DB write.
*
* @since 1.2.0
*
* @param string $operation S3 operation name.
* @param int $count Number of operations to add.
* @return void
*/
private function track_bulk_s3_operations( string $operation, int $count ): void {
$option_key = 'idrivee2_s3_operations';
$raw = get_option( $option_key, array() );
/**
* Daily S3 operation counts, keyed by date and operation name.
*
* @var array<string, array<string, int>> $stats
*/
$stats = is_array( $raw ) ? $raw : array();
$today = gmdate( 'Y-m-d' );
if ( ! isset( $stats[ $today ] ) ) {
$stats[ $today ] = array();
}
if ( ! isset( $stats[ $today ][ $operation ] ) ) {
$stats[ $today ][ $operation ] = 0;
}
$stats[ $today ][ $operation ] += $count;
$cutoff_date = gmdate( 'Y-m-d', time() - ( 30 * DAY_IN_SECONDS ) );
foreach ( array_keys( $stats ) as $date ) {
if ( $date < $cutoff_date ) {
unset( $stats[ $date ] );
}
}
update_option( $option_key, $stats );
}
/**
* Log an authentication failure.
*
@ -280,7 +343,7 @@ class Logger {
*/
private function track_s3_operation( string $operation ): void {
$option_key = 'idrivee2_s3_operations';
$raw = get_option( $option_key, array() );
$raw = get_option( $option_key, array() );
/**
* Daily S3 operation counts, keyed by date and operation name.
*
@ -321,7 +384,7 @@ class Logger {
*/
public function get_s3_stats( int $days = 7 ): array {
$days = min( $days, 30 );
$raw = get_option( 'idrivee2_s3_operations', array() );
$raw = get_option( 'idrivee2_s3_operations', array() );
/**
* Daily S3 operation counts, keyed by date and operation name.
*