<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\library\media_jobs;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\models\ImpressionModel;

/**
 * A media job used to periodically delete old raw impression
 * info about  groups, threads, pages, and queries.
 */
class CullOldRawImpressionsJob extends MediaJob
{
    /**
     * Time in current epoch when analytics last updated
     * @var int
     */
    public $update_time;
    /**
     * Used to get statistics from DBMS about wiki and thread views
     *
     * @var object
     */
    public $impression_model;
    /**
     * Initializes the time of last analytics update
     */
    public function init()
    {
        $this->update_time = 0;
        $this->name_server_does_client_tasks = true;
        $this->name_server_does_client_tasks_only = true;
        $this->impression_model = new ImpressionModel();
    }
    /**
     * Only update if its been more than an hour since the last update
     *
     * @return bool whether its been an hour since the last update
     */
    public function checkPrerequisites()
    {
        $time = time();
        $something_updated = false;
        $delta = $time - $this->update_time;
        if ($delta > C\CULL_OLD_IMPRESSIONS_INTERVAL) {
            $this->update_time = $time;
            L\crawlLog("Performing cull of old impressions update");
            return true;
        }
        L\crawlLog("Time since last update not exceeded, skipping cull ".
            " of old impressions");
        return false;
    }
    /**
     * For now analytics update is only done on name server as Yioop
     * currently only supports one DBMS at a time.
     */
    public function nondistributedTasks()
    {
        $this->doTasks([]);
    }
    /**
     * Calls ImpressionModel to do the culling of old impressions
     *
     * @param array $tasks array of info that came from getTasks
     * (in this nothing)
     */
    public function doTasks($tasks)
    {
        $this->impression_model->deleteOldRawImpressions();
    }
}
