HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/heifetz/heifetz-app/crons/UserLogRotation.php
<?php

namespace Crons;

use Helpers\Data;
use Models\UserLogHistory;
use Models\UserLogs;

class UserLogRotation extends AbstractCron
{

    protected string $title = 'Переносим устаревшие данные из таблицы user_logs';

    private int $limit = 10000;

    static array $help = [
        '--limit' => 'Ограничение количества обрабатываемых записей (по-умолчанию 10000)',
    ];

    public function __construct(?int $limit = null)
    {
        parent::__construct();

        if (isset($limit)) {
            $this->limit = $limit;
        }
    }

    protected function move(): void
    {
        $minDate = UserLogs::$db->select('MIN(created_at) as minDate')->from(UserLogs::$tableName)
            ->where('created_at <= ?', date('Y-m-d', strtotime('-3 month')))
            ->group('created_at')->limit(1)->fetchOne();

        if (false === $minDate) {
            $this->log('Нечего переносить');
            $this->limit -= $this->limit;

            return;
        }

        $sql = UserLogs::$db->select()->from(UserLogs::$tableName)->where('created_at <= ?', date('Y-m-d', strtotime('-3 month')));

        $totalCount = $sql->totalCount();
        /** @var UserLogs[] $usersLogs */
        $usersLogs = $sql->where('DATE(created_at) = ?', $minDate)->order('created_at')->fetchAll();

        $count = count($usersLogs);
        $this->log('Переносим логи за ' . date('d.m.Y', strtotime($minDate)));
        $this->log($count . '/' . $totalCount . ' записей');

        $deletedCount = 0;
        foreach ($usersLogs as $index => $userLog) {
            if ($this->debug) {
                $this->progressBar(Data::getPercent($index + 1, $count));
            }
            $data = [
                'user_id' => $userLog->user_id,
                'entity' => $userLog->entity,
                'entity_id' => $userLog->entity_id,
                'column' => $userLog->column,
                'value' => $userLog->value,
                'created_at' => $userLog->created_at,
                'action' => $userLog->action,
                'company_id' => $userLog->company_id,
            ];

            UserLogHistory::add($data);

            $data['id'] = $userLog->id;
            $deletedCount += 1;
        }
        if ($this->debug) {
            $this->write('');
        }

        $deletedCount = UserLogs::$db->query('DELETE FROM ' . UserLogs::$tableName . " WHERE DATE(created_at) = '" . $minDate . "';");
        $this->log('Удалено: ' . $deletedCount . ' записей');

        $this->limit -= $count;
    }


    public function execute()
    {
        while ($this->limit > 0) {
            $this->move();
        }
    }

}