| @@ -22,8 +22,10 @@ | ||
| 22 | 22 | |
| 23 | 23 | const ERROR_LENGTH = 500; |
| 24 | 24 | |
| 25 | 25 | const SHOWN = 10; |
| 26 | + | |
| 27 | + const USAGE_DAYS = 2; | |
| 26 | 28 | // phpcs:enable PSR12.Properties.ConstantVisibility.NotFound |
| 27 | 29 | |
| 28 | 30 | /** |
| 29 | 31 | * @return string |
| @@ -78,8 +80,44 @@ | ||
| 78 | 80 | (int) $userId, |
| 79 | 81 | (string) $connection, |
| 80 | 82 | (int) $limit |
| 81 | 83 | ), ARRAY_A) ?: []; |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Unknown names become `ability`: an unknown-tool call logs the client's free text. | |
| 88 | + * | |
| 89 | + * @return array | |
| 90 | + */ | |
| 91 | + public static function usage() | |
| 92 | + { | |
| 93 | + if (!self::exists()) { | |
| 94 | + return []; | |
| 95 | + } | |
| 96 | + | |
| 97 | + $wpdb = $GLOBALS['wpdb']; | |
| 98 | + $table = self::table(); | |
| 99 | + $tools = array_merge(array_keys(Tools::all()), Surface::abilityNames()); | |
| 100 | + $in = implode(',', array_fill(0, count($tools), '%s')); | |
| 101 | + $since = gmdate('Y-m-d', time() - ((self::USAGE_DAYS - 1) * DAY_IN_SECONDS)); | |
| 102 | + | |
| 103 | + // Binary, or the collation lets `LIST_POSTS` or a trailing space pass as ours. | |
| 104 | + $rows = $wpdb->get_results($wpdb->prepare( | |
| 105 | + "SELECT DATE(created_at), IF(CAST(tool AS BINARY) IN ({$in}), tool, 'ability'), COUNT(*)," | |
| 106 | + . " SUM(outcome = 'error'), SUM(outcome = 'refused') FROM {$table}" | |
| 107 | + . ' WHERE created_at >= %s GROUP BY 1, 2 ORDER BY 1, 2', | |
| 108 | + array_merge($tools, [$since]) | |
| 109 | + ), ARRAY_N) ?: []; | |
| 110 | + | |
| 111 | + return array_map(function ($row) { | |
| 112 | + return [ | |
| 113 | + 'day' => $row[0], | |
| 114 | + 'tool' => $row[1], | |
| 115 | + 'calls' => (int) $row[2], | |
| 116 | + 'errors' => (int) $row[3], | |
| 117 | + 'refused' => (int) $row[4], | |
| 118 | + ]; | |
| 119 | + }, $rows); | |
| 82 | 120 | } |
| 83 | 121 | |
| 84 | 122 | /** |
| 85 | 123 | * @param integer $userId - The user the connection belonged to. |