strlen( $question ) ) { continue; } $ungrounded = empty( $row['grounded'] ); $disliked = isset( $row['rating'] ) && -1 === (int) $row['rating']; if ( ! $ungrounded && ! $disliked ) { continue; } $key = self::fingerprint( $question ); if ( '' === $key ) { continue; } if ( ! isset( $groups[ $key ] ) ) { $groups[ $key ] = array( 'question' => $question, 'asked' => 0, 'ungrounded' => 0, 'disliked' => 0, 'last' => 0, ); } ++$groups[ $key ]['asked']; if ( $ungrounded ) { ++$groups[ $key ]['ungrounded']; } if ( $disliked ) { ++$groups[ $key ]['disliked']; } $time = isset( $row['time'] ) ? (int) $row['time'] : 0; if ( $time > $groups[ $key ]['last'] ) { $groups[ $key ]['last'] = $time; // Show the most recent phrasing, which is usually the clearest. $groups[ $key ]['question'] = $question; } } // Asked most often first, then a reader complaint, then most recent. uasort( $groups, static function ( $a, $b ) { if ( $a['asked'] !== $b['asked'] ) { return $b['asked'] - $a['asked']; } if ( $a['disliked'] !== $b['disliked'] ) { return $b['disliked'] - $a['disliked']; } return $b['last'] - $a['last']; } ); return array_slice( array_values( $groups ), 0, $limit ); } /** * Content-gap rows for editorial planning, in the same order as the panel. * * Counts describe the exchanges contributing to a gap, not every answer * about the subject. Grounded contributors were marked unhelpful. * * @param array $args Optional limit and days, as for content_gaps(). * @return array CSV header followed by data rows. */ public static function export_rows( $args = array() ) { $rows = array( array( 'question', 'occurrences', 'last_asked_utc', 'grounded_count', 'ungrounded_count' ) ); foreach ( self::content_gaps( $args ) as $gap ) { $rows[] = array( $gap['question'], (int) $gap['asked'], gmdate( 'Y-m-d H:i:s', (int) $gap['last'] ), (int) $gap['asked'] - (int) $gap['ungrounded'], (int) $gap['ungrounded'], ); } return $rows; } /** * Periods, in days, that the content gaps panel and its download offer. */ const GAP_WINDOWS = array( 7, 30, 90 ); /** * Period used when none, or an unrecognised one, is asked for. It is what 1.47.6 always used. */ const DEFAULT_GAP_WINDOW = 30; /** * The content gaps period a request asked for, or the default. * * Only a PHP int or a string made of digits alone is read, and only 7, 30 or 90 are * accepted. \z, unlike $, does not let a trailing newline through, and nothing is * coerced: "-7", "7abc", " 90" and "90.5" are all the default, not a nearby window. * * @param mixed $raw Requested period, typically straight from the request. * @return int 7, 30 or 90. */ public static function gap_window( $raw ) { if ( is_int( $raw ) ) { $value = $raw; } elseif ( is_string( $raw ) && 1 === preg_match( '/\A[0-9]+\z/', $raw ) ) { $value = (int) $raw; } else { return self::DEFAULT_GAP_WINDOW; } return in_array( $value, self::GAP_WINDOWS, true ) ? $value : self::DEFAULT_GAP_WINDOW; } /** * File name for a content gaps download. * * The default period keeps the name it always had, so anything that collects these * files keeps working; the other periods say which window the file covers. * * @param mixed $days Period in days; validated with gap_window(). * @param int|null $time Unix time for the name, in UTC. Defaults to now. * @return string */ public static function export_filename( $days, $time = null ) { $days = self::gap_window( $days ); $time = null === $time ? time() : (int) $time; $range = self::DEFAULT_GAP_WINDOW === $days ? '' : $days . 'd-'; return 'ai-chat-bedrock-content-gaps-' . $range . gmdate( 'Ymd-His', $time ) . '.csv'; } /** * Counts for the panel heading. * * @param int $days Window in days. * @return array */ public static function summary( $days = 30 ) { $days = max( 1, min( 365, (int) $days ) ); $since = time() - ( $days * DAY_IN_SECONDS ); $asked = 0; $ungrounded = 0; if ( class_exists( 'AI_Chat_Bedrock_Conversations' ) ) { foreach ( AI_Chat_Bedrock_Conversations::recent( AI_Chat_Bedrock_Conversations::MAX_ENTRIES ) as $row ) { if ( ! is_array( $row ) || ( isset( $row['time'] ) && (int) $row['time'] < $since ) ) { continue; } $source = isset( $row['source'] ) ? (string) $row['source'] : 'chat'; if ( ! in_array( $source, array( 'chat', 'stream', 'wechat' ), true ) ) { continue; } ++$asked; if ( empty( $row['grounded'] ) ) { ++$ungrounded; } } } return array( 'days' => $days, 'asked' => $asked, 'ungrounded' => $ungrounded, 'percent' => $asked > 0 ? (int) round( $ungrounded * 100 / $asked ) : 0, ); } /** * A link that opens the content generator with this subject filled in. * * The gap is where the loop starts; the draft is written by the tool that already * exists, with a human reviewing it. * * @param string $question Question to write about. * @return string */ public static function draft_link( $question ) { $question = trim( wp_strip_all_tags( (string) $question ) ); if ( '' === $question ) { return ''; } return add_query_arg( array( 'page' => 'ai-chat-for-amazon-bedrock-generator', // add_query_arg does not encode values, so an ampersand in the question would // otherwise start a new parameter and truncate the subject. PHP decodes this // once when reading $_GET, so the receiving screen does not decode again. 'aicfab_topic' => rawurlencode( AI_Chat_Bedrock_Security::string_substr( $question, 0, 200 ) ), ), admin_url( 'admin.php' ) ); } }