<commit_before><?php
namespace Grav\Plugin;

use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Plugins;
use Grav\Common\Themes;
use Grav\Common\Page\Page;
use Grav\Common\Data;
use Grav\Common\GravTrait;

class Popularity
{
    use GravTrait;

    /** @var Config */
    protected $config;
    protected $data_path;

    protected $daily_file;
    protected $monthly_file;
    protected $totals_file;
    protected $visitors_file;

    protected $daily_data;
    protected $monthly_data;
    protected $totals_data;
    protected $visitors_data;

    const DAILY_FORMAT = 'd-m-Y';
    const MONTHLY_FORMAT = 'm-Y';
    const DAILY_FILE = 'daily.json';
    const MONTHLY_FILE = 'monthly.json';
    const TOTALS_FILE = 'totals.json';
    const VISITORS_FILE = 'visitors.json';

    public function __construct()
    {
        $this->config = self::getGrav()['config'];

        $this->data_path = self::$grav['locator']->findResource('log://popularity', true, true);
        $this->daily_file = $this->data_path.'/'.self::DAILY_FILE;
        $this->monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
        $this->totals_file = $this->data_path.'/'.self::TOTALS_FILE;
        $this->visitors_file = $this->data_path.'/'.self::VISITORS_FILE;

    }

    public function trackHit()
    {
        /** @var Page $page */
        $page = self::getGrav()['page'];
        $relative_url = str_replace(self::getGrav()['base_url_relative'], '', $page->url());

        // Don't track error pages or pages that have no route
        if ($page->template() == 'error' || !$page->route()) {
            return;
        }

        // Make sure no 'widcard-style' ignore matches this url
        foreach ((array) $this->config->get('plugins.admin.popularity.ignore') as $ignore) {
            if (fnmatch($ignore, $relative_url)) {
                return;
            }
        }

        // initial creation if it doesn't exist
        if (!file_exists($this->data_path)) {
            mkdir($this->data_path);
            $this->flushPopularity();
        }

        // Update the data we want to track
        $this->updateDaily();
        $this->updateMonthly();
        $this->updateTotals($page->route());
        $this->updateVisitors(self::getGrav()['uri']->ip());

    }

    protected function updateDaily()
    {

        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }

        $day_month_year = date(self::DAILY_FORMAT);

        // get the daily access count
        if (array_key_exists($day_month_year, $this->daily_data)) {
            $this->daily_data[$day_month_year] = intval($this->daily_data[$day_month_year]) + 1;
        } else {
            $this->daily_data[$day_month_year] = 1;
        }

        // keep correct number as set by history
        $count = intval($this->config->get('plugins.admin.popularity.history.daily', 30));
        $total = count($this->daily_data);

        if ($total > $count) {
            $this->daily_data = array_slice($this->daily_data, -$count, $count, true);
        }

        file_put_contents($this->daily_file, json_encode($this->daily_data));
    }

    public function getDailyChartData()
    {
        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }

        $limit = intval($this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7));
        $chart_data = array_slice($this->daily_data, -$limit, $limit);

        $labels = array();
        $data = array();

        foreach ($chart_data as $date => $count) {
            $labels[] = self::getGrav()['grav']['language']->translate(['PLUGIN_ADMIN_' . strtoupper(date('D', strtotime($date)))]);
            $data[] = $count;
        }

        return array('labels' => json_encode($labels), 'data' => json_encode($data));
    }

    public function getDailyTotal()
    {
        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }<commit_msg>Fix popularity class string<commit_after><?php
namespace Grav\Plugin;

use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Plugins;
use Grav\Common\Themes;
use Grav\Common\Page\Page;
use Grav\Common\Data;
use Grav\Common\GravTrait;

class Popularity
{
    use GravTrait;

    /** @var Config */
    protected $config;
    protected $data_path;

    protected $daily_file;
    protected $monthly_file;
    protected $totals_file;
    protected $visitors_file;

    protected $daily_data;
    protected $monthly_data;
    protected $totals_data;
    protected $visitors_data;

    const DAILY_FORMAT = 'd-m-Y';
    const MONTHLY_FORMAT = 'm-Y';
    const DAILY_FILE = 'daily.json';
    const MONTHLY_FILE = 'monthly.json';
    const TOTALS_FILE = 'totals.json';
    const VISITORS_FILE = 'visitors.json';

    public function __construct()
    {
        $this->config = self::getGrav()['config'];

        $this->data_path = self::$grav['locator']->findResource('log://popularity', true, true);
        $this->daily_file = $this->data_path.'/'.self::DAILY_FILE;
        $this->monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
        $this->totals_file = $this->data_path.'/'.self::TOTALS_FILE;
        $this->visitors_file = $this->data_path.'/'.self::VISITORS_FILE;

    }

    public function trackHit()
    {
        /** @var Page $page */
        $page = self::getGrav()['page'];
        $relative_url = str_replace(self::getGrav()['base_url_relative'], '', $page->url());

        // Don't track error pages or pages that have no route
        if ($page->template() == 'error' || !$page->route()) {
            return;
        }

        // Make sure no 'widcard-style' ignore matches this url
        foreach ((array) $this->config->get('plugins.admin.popularity.ignore') as $ignore) {
            if (fnmatch($ignore, $relative_url)) {
                return;
            }
        }

        // initial creation if it doesn't exist
        if (!file_exists($this->data_path)) {
            mkdir($this->data_path);
            $this->flushPopularity();
        }

        // Update the data we want to track
        $this->updateDaily();
        $this->updateMonthly();
        $this->updateTotals($page->route());
        $this->updateVisitors(self::getGrav()['uri']->ip());

    }

    protected function updateDaily()
    {

        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }

        $day_month_year = date(self::DAILY_FORMAT);

        // get the daily access count
        if (array_key_exists($day_month_year, $this->daily_data)) {
            $this->daily_data[$day_month_year] = intval($this->daily_data[$day_month_year]) + 1;
        } else {
            $this->daily_data[$day_month_year] = 1;
        }

        // keep correct number as set by history
        $count = intval($this->config->get('plugins.admin.popularity.history.daily', 30));
        $total = count($this->daily_data);

        if ($total > $count) {
            $this->daily_data = array_slice($this->daily_data, -$count, $count, true);
        }

        file_put_contents($this->daily_file, json_encode($this->daily_data));
    }

    public function getDailyChartData()
    {
        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }

        $limit = intval($this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7));
        $chart_data = array_slice($this->daily_data, -$limit, $limit);

        $labels = array();
        $data = array();

        foreach ($chart_data as $date => $count) {
            $labels[] = self::getGrav()['grav']['language']->translate(['PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]);
            $data[] = $count;
        }

        return array('labels' => json_encode($labels), 'data' => json_encode($data));
    }

    public function getDailyTotal()
    {
        if (!$this->daily_data) {
            $this->daily_data = $this->getData($this->daily_file);
        }
{'fraction_non_alphanumeric': 0.16205875092569735, 'fraction_numerical': 0.0012342631449024932, 'mean_word_length': 2.5711767298369326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5862978', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2515, 'n_words': 459}