addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'log'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $capsule->schema('default')->create( 'cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('status'); } ); $logSchema = $capsule->schema('log'); $logSchema->create( 'cloud_vm_metric_samples', static function (Blueprint $table): void { $table->integer('instance_id'); $table->integer('user_id'); $table->integer('sampled_at'); foreach (['cpu_percent', 'memory_used_mib', 'memory_total_mib', 'disk_read_bps', 'disk_write_bps', 'network_rx_bps', 'network_tx_bps'] as $field) { $table->double($field)->nullable(); } $table->integer('network_rx_bytes')->nullable(); $table->integer('network_tx_bytes')->nullable(); $table->primary(['instance_id', 'sampled_at']); } ); $logSchema->create( 'cloud_vm_metric_rollups', static function (Blueprint $table): void { $table->integer('instance_id'); $table->integer('user_id'); $table->integer('interval_seconds'); $table->integer('bucket_start'); $table->integer('sample_count'); $table->integer('last_sampled_at'); foreach (['cpu_percent', 'memory_used_mib', 'memory_total_mib', 'disk_read_bps', 'disk_write_bps', 'network_rx_bps', 'network_tx_bps'] as $field) { $table->double($field . '_sum')->nullable(); $table->integer($field . '_count'); } $table->primary(['instance_id', 'interval_seconds', 'bucket_start']); } ); $main = $capsule->connection('default'); $log = $capsule->connection('log'); $main->table('cloud_instances')->insert(['id' => 1, 'status' => 'active']); $now = 20_000_000; $sample = static function (int $at, ?float $cpu): array { return [ 'instance_id' => 1, 'user_id' => 9, 'sampled_at' => $at, 'cpu_percent' => $cpu, 'memory_used_mib' => 1024.0, 'memory_total_mib' => 2048.0, 'disk_read_bps' => 10.0, 'disk_write_bps' => 20.0, 'network_rx_bps' => 30.0, 'network_tx_bps' => 40.0, ]; }; $at = intdiv($now - 600, 300) * 300; foreach ([$sample($at, 10.0), $sample($at + 60, null), $sample($at + 120, 30.0)] as $row) { $log->table('cloud_vm_metric_samples')->insert($row + [ 'network_rx_bytes' => 1, 'network_tx_bytes' => 1, ]); CloudMetricHistoryStore::recordRollups($log, $row); } $day = CloudMetricHistoryStore::series( $log, 1, 9, '24h', $now ); $month = CloudMetricHistoryStore::series( $log, 1, 9, '30d', $now ); $assert(count($day['points']) === 1 && abs((float) $day['points'][0]['cpu_percent'] - 20.0) < 0.0001 && count($month['points']) === 1 && abs((float) $month['points'][0]['memory_used_mib'] - 1024.0) < 0.0001, 'Tiered metric history did not preserve non-null weighted averages.'); $oldRaw = $sample( $now - CloudMetricHistoryStore::RAW_RETENTION_SECONDS - 60, 1.0 ); $log->table('cloud_vm_metric_samples')->insert($oldRaw + [ 'network_rx_bytes' => 1, 'network_tx_bytes' => 1, ]); $rollupFixture = static function ( int $instanceId, int $interval, int $bucket ): array { $values = [ 'instance_id' => $instanceId, 'user_id' => 9, 'interval_seconds' => $interval, 'bucket_start' => $bucket, 'sample_count' => 1, 'last_sampled_at' => $bucket, ]; foreach (['cpu_percent', 'memory_used_mib', 'memory_total_mib', 'disk_read_bps', 'disk_write_bps', 'network_rx_bps', 'network_tx_bps'] as $field) { $values[$field . '_sum'] = 1.0; $values[$field . '_count'] = 1; } return $values; }; $oldFive = intdiv( $now - CloudMetricHistoryStore::FIVE_MINUTE_RETENTION_SECONDS - 300, 300 ) * 300; $oldHour = intdiv( $now - 36 * 86400, 3600 ) * 3600; $keptHour = intdiv($now - 34 * 86400, 3600) * 3600; $log->table('cloud_vm_metric_rollups')->insert([ $rollupFixture(1, 300, $oldFive), $rollupFixture(1, 3600, $oldHour), $rollupFixture(1, 3600, $keptHour), $rollupFixture(2, 300, intdiv($now, 300) * 300), ]); $deletedSample = $sample($now - 60, 1.0); $deletedSample['instance_id'] = 2; $log->table('cloud_vm_metric_samples')->insert($deletedSample + [ 'network_rx_bytes' => 1, 'network_tx_bytes' => 1, ]); $pruned = CloudMetricHistoryStore::prune($log, $now); $assert($pruned['raw'] === 1 && $pruned['five_minute'] === 1 && $pruned['hourly'] === 1 && $pruned['deleted_instance'] === 2 && !$log->table('cloud_vm_metric_rollups') ->where('instance_id', 2)->exists() && !$log->table('cloud_vm_metric_samples') ->where('instance_id', 2)->exists() && !$log->table('cloud_vm_metric_rollups') ->where('interval_seconds', 3600) ->where('bucket_start', $oldHour)->exists() && $log->table('cloud_vm_metric_rollups') ->where('interval_seconds', 3600) ->where('bucket_start', $keptHour)->exists(), 'Metric history retention did not prune every bounded tier safely.'); echo "Cloud metric history store tests passed.\n";