0, 'tokens' => 0 ) ); $entry['calls'] = (int) $entry['calls'] + 1; $entry['tokens'] = (int) $entry['tokens'] + max( 0, $tokens ); $usage[ $month ][ $task ] = $entry; // Parallel per-model token aggregation (M45, cost estimation). The // '__models' key is reserved — task keys are sanitize_key()-ed field // names and can never start with '__'. $model_key = sanitize_key( $provider ) . '|' . sanitize_text_field( $model ); $models = (array) ( $usage[ $month ]['__models'] ?? array() ); $models[ $model_key ] = (int) ( $models[ $model_key ] ?? 0 ) + max( 0, $tokens ); $usage[ $month ]['__models'] = $models; // Keep only the newest months — this option must never grow unbounded. krsort( $usage ); $usage = array_slice( $usage, 0, self::MONTHS_KEPT, true ); update_option( self::OPTION_USAGE, $usage, false ); $log = get_option( self::OPTION_LOG, array() ); $log = is_array( $log ) ? $log : array(); array_unshift( $log, array( 'time' => time(), 'task' => sanitize_key( $task ), 'provider' => sanitize_key( $provider ), 'model' => sanitize_text_field( $model ), 'ok' => $ok, 'note' => mb_substr( sanitize_text_field( $note ), 0, 120 ), 'duration_ms' => max( 0, $duration_ms ), ) ); update_option( self::OPTION_LOG, array_slice( $log, 0, self::LOG_MAX ), false ); } /** * @return array task => totals for the current month */ public static function current_month_summary(): array { $usage = get_option( self::OPTION_USAGE, array() ); $month = (array) ( is_array( $usage ) ? ( $usage[ gmdate( 'Y-m' ) ] ?? array() ) : array() ); $summary = array(); foreach ( $month as $task => $entry ) { // Reserved aggregation keys (e.g. '__models') are not tasks. if ( str_starts_with( (string) $task, '__' ) ) { continue; } $summary[ (string) $task ] = array( 'calls' => (int) ( $entry['calls'] ?? 0 ), 'tokens' => (int) ( $entry['tokens'] ?? 0 ), ); } return $summary; } /** * @return array "provider|model" => approximate tokens this month */ public static function current_month_models(): array { $usage = get_option( self::OPTION_USAGE, array() ); $month = (array) ( is_array( $usage ) ? ( $usage[ gmdate( 'Y-m' ) ] ?? array() ) : array() ); $models = array(); foreach ( (array) ( $month['__models'] ?? array() ) as $key => $tokens ) { $models[ (string) $key ] = (int) $tokens; } return $models; } public static function calls_this_month(): int { return array_sum( array_column( self::current_month_summary(), 'calls' ) ); } /** * @return array> newest first */ public static function recent(): array { $log = get_option( self::OPTION_LOG, array() ); return is_array( $log ) ? array_values( $log ) : array(); } /** M46 — "Wyczyść dziennik generowań": drops the recent-log rows only. */ public static function clear_log(): void { update_option( self::OPTION_LOG, array(), false ); } /** * M46 — "Wyzeruj liczniki bieżącego miesiąca": drops only the CURRENT * month's entry (incl. its '__models' aggregation) — past months stay, * they are historical accounting covered by the 3-month retention. */ public static function clear_current_month(): void { $usage = get_option( self::OPTION_USAGE, array() ); $usage = is_array( $usage ) ? $usage : array(); unset( $usage[ gmdate( 'Y-m' ) ] ); update_option( self::OPTION_USAGE, $usage, false ); } }