|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Appwrite\Platform\Tasks; |
| 4 | + |
| 5 | +use Utopia\Database\Database; |
| 6 | +use Utopia\Database\Query; |
| 7 | +use Utopia\Platform\Action; |
| 8 | +use Utopia\CLI\Console; |
| 9 | +use Utopia\Database\Validator\Authorization; |
| 10 | +use Utopia\Validator\Boolean; |
| 11 | +use Utopia\Validator\Text; |
| 12 | + |
| 13 | +class MigrateUsage extends Action |
| 14 | +{ |
| 15 | + public static function getName(): string |
| 16 | + { |
| 17 | + return 'migrate-usage'; |
| 18 | + } |
| 19 | + |
| 20 | + public function __construct() |
| 21 | + { |
| 22 | + $this |
| 23 | + ->desc('Migrate old usage metric pattern to new') |
| 24 | + ->param('structure', false, new Boolean(), 'Manipulate usage cols structure', true) |
| 25 | + ->inject('dbForConsole') |
| 26 | + ->inject('getProjectDB') |
| 27 | + ->callback(fn(bool $structure, Database $dbForConsole, callable $getProjectDB) => $this->action($structure, $dbForConsole, $getProjectDB)); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Iterate over every function on every project to make sure there is a schedule. If not, recreate the schedule. |
| 32 | + */ |
| 33 | + public function action(bool $structure, Database $dbForConsole, callable $getProjectDB): void |
| 34 | + { |
| 35 | + Authorization::disable(); |
| 36 | + Authorization::setDefaultStatus(false); |
| 37 | + |
| 38 | + |
| 39 | + $limit = 100; |
| 40 | + $projectCursor = null; |
| 41 | + while (true) { |
| 42 | + $projectsQueries = [Query::limit($limit)]; |
| 43 | + if ($projectCursor !== null) { |
| 44 | + $projectsQueries[] = Query::cursorAfter($projectCursor); |
| 45 | + } |
| 46 | + $projects = $dbForConsole->find('projects', $projectsQueries); |
| 47 | + |
| 48 | + if (count($projects) === 0) { |
| 49 | + break; |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($projects as $project) { |
| 53 | + Console::log("Checking Project " . $project->getAttribute('name') . " (" . $project->getInternalId() . ")"); |
| 54 | + $dbForProject = $getProjectDB($project); |
| 55 | + |
| 56 | + if ($structure) { |
| 57 | + try { |
| 58 | + /** |
| 59 | + * Update 'mimeType' attribute size (127->255) |
| 60 | + */ |
| 61 | + $dbForProject->renameAttribute('stats', 'metric', 'metricOld'); |
| 62 | + $dbForProject->renameAttribute('stats', 'metricV2', 'metric'); |
| 63 | + } catch (\Throwable $th) { |
| 64 | + Console::Error("Error while trying yo rename col: {$th->getMessage()}"); |
| 65 | + } |
| 66 | + |
| 67 | + Console::log("Renaming stats col metric to metricOld, metricV2 to metric"); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Project level |
| 74 | + */ |
| 75 | + $metrics[] = ['from' => 'project.$all.network.outbound', 'to' => 'network.outbound']; |
| 76 | + $metrics[] = ['from' => 'project.$all.network.inbound', 'to' => 'network.inbound']; |
| 77 | + $metrics[] = ['from' => 'project.$all.network.requests', 'to' => 'network.requests']; |
| 78 | + $metrics[] = ['from' => 'executions.$all.compute.total', 'to' => 'executions']; |
| 79 | + $metrics[] = ['from' => 'builds.$all.compute.time', 'to' => 'builds.compute']; |
| 80 | + $metrics[] = ['from' => 'files.$all.storage.size', 'to' => 'files.storage']; |
| 81 | + $metrics[] = ['from' => 'files.$all.count.total', 'to' => 'files']; |
| 82 | + $metrics[] = ['from' => 'buckets.$all.count.total', 'to' => 'buckets']; |
| 83 | + $metrics[] = ['from' => 'collections.$all.count.total', 'to' => 'collections']; |
| 84 | + $metrics[] = ['from' => 'databases.$all.count.total', 'to' => 'databases']; |
| 85 | + $metrics[] = ['from' => 'users.$all.count.total ', 'to' => 'users']; |
| 86 | + $metrics[] = ['from' => 'documents.$all.count.total', 'to' => 'documents']; |
| 87 | + $metrics[] = ['from' => 'documents.$all.count.total', 'to' => 'documents']; |
| 88 | + |
| 89 | + foreach ($metrics as $metric) { |
| 90 | + $stats = $dbForProject->find('stats', [ |
| 91 | + Query::equal('metric', [$metric['from']]), |
| 92 | + Query::greaterThan('value', 0) |
| 93 | + ]); |
| 94 | + foreach ($stats as $stat) { |
| 95 | + $stat->setAttribute('metricV2', $metric['to']); |
| 96 | + $dbForProject->updateDocument('stats', $stat->getId(), $stat); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Databases, collections, documents |
| 102 | + */ |
| 103 | + $databases = $dbForProject->find('databases', []); |
| 104 | + foreach ($databases as $database) { |
| 105 | + $metrics = []; |
| 106 | + $metrics[] = [ |
| 107 | + 'from' => str_replace('{databaseId}', $database->getId(), 'documents.{databaseId}.count.total'), |
| 108 | + 'to' => str_replace('{databaseInternalId}', $database->getInternalId(), '{databaseInternalId}.collections')]; |
| 109 | + $metrics[] = [ |
| 110 | + 'from' => str_replace('{databaseId}', $database->getId(), 'documents.{databaseId}.count.total'), |
| 111 | + 'to' => str_replace('{databaseInternalId}', $database->getInternalId(), '{databaseInternalId}.documents')]; |
| 112 | + foreach ($metrics as $metric) { |
| 113 | + $stats = $dbForProject->find('stats', [ |
| 114 | + Query::equal('metric', [$metric['from']]), |
| 115 | + Query::greaterThan('value', 0) |
| 116 | + ]); |
| 117 | + |
| 118 | + foreach ($stats as $stat) { |
| 119 | + $stat->setAttribute('metricV2', $metric['to']); |
| 120 | + $dbForProject->updateDocument('stats', $stat->getId(), $stat); |
| 121 | + } |
| 122 | + |
| 123 | + $collections = $dbForProject->find('database_' . $database->getInternalId(), []); |
| 124 | + |
| 125 | + foreach ($collections as $collection) { |
| 126 | + $__metric = [ |
| 127 | + 'from' => str_replace(['{databaseId}','{collectionId}'], [$database->getId(), $collection->getId()], 'documents.{databaseId}/{collectionId}.count.total'), |
| 128 | + 'to' => str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$database->getInternalId(), $collection->getInternalId()], '{databaseInternalId}.{collectionInternalId}.documents')]; |
| 129 | + |
| 130 | + $__stats = $dbForProject->find('stats', [ |
| 131 | + Query::equal('metric', [$__metric['from']]), |
| 132 | + Query::greaterThan('value', 0) |
| 133 | + ]); |
| 134 | + |
| 135 | + foreach ($__stats as $__stat) { |
| 136 | + $__stat->setAttribute('metricV2', $__metric['to']); |
| 137 | + $dbForProject->updateDocument('stats', $__stat->getId(), $__stat); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * executions |
| 145 | + */ |
| 146 | + $functions = $dbForProject->find('functions', []); |
| 147 | + foreach ($functions as $function) { |
| 148 | + $metrics = []; |
| 149 | + $metrics[] = [ |
| 150 | + 'from' => str_replace('{functionId}', $function->getId(), 'executions.{functionId}.compute.time'), |
| 151 | + 'to' => str_replace('{functionInternalId}', $function->getInternalId(), '{functionInternalId}.executions.compute')]; |
| 152 | + $metrics[] = [ |
| 153 | + 'from' => str_replace('{functionId}', $function->getId(), 'executions.{functionId}.compute.total'), |
| 154 | + 'to' => str_replace('{functionInternalId}', $function->getInternalId(), '{functionInternalId}.executions')]; |
| 155 | + foreach ($metrics as $metric) { |
| 156 | + $stats = $dbForProject->find('stats', [ |
| 157 | + Query::equal('metric', [$metric['from']]), |
| 158 | + Query::greaterThan('value', 0) |
| 159 | + ]); |
| 160 | + |
| 161 | + foreach ($stats as $stat) { |
| 162 | + $stat->setAttribute('metricV2', $metric['to']); |
| 163 | + $dbForProject->updateDocument('stats', $stat->getId(), $stat); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + $projectCursor = $projects[array_key_last($projects)]; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments