<commit_before><?php

namespace alojaweb\inc;

class DBUtils
{
    public static $TASK_METRICS = [
        'Duration',
        'Bytes Read',
        'Bytes Written',
        'FILE_BYTES_WRITTEN',
        'FILE_BYTES_READ',
        'HDFS_BYTES_WRITTEN',
        'HDFS_BYTES_READ',
        'Spilled Records',
        'SPLIT_RAW_BYTES',
        'Map input records',
        'Map output records',
        'Map input bytes',
        'Map output bytes',
        'Map output materialized bytes',
        'Reduce input groups',
        'Reduce input records',
        'Reduce output records',
        'Reduce shuffle bytes',
        'Combine input records',
        'Combine output records',
    ];

    private $dbConn;
    private $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function init()
    {
        $this->dbConn = new \PDO($this->container['config']['db_conn_chain'], $this->container['config']['mysql_user'], $this->container['config']['mysql_pwd']);

        $this->dbConn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->dbConn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
    }

    public function get_rows($sql, $params = array())
    {
        $md5_sql = md5($sql.http_build_query($params, '', ','));
        $file_path = "{$this->container['config']['db_cache_path']}/CACHE_$md5_sql.sql";

        if ($this->container['env'] == 'dev' || $_SERVER['HTTP_HOST'] == 'localhost' || (isset($_GET['NO_CACHE']) && strlen($_GET['NO_CACHE']) > 0)) {
            $use_cache = false;
        } else {
            $use_cache = true;
        }

        //check for cache first
        if ($use_cache &&
                file_exists($file_path) &&
                ($rows = file_get_contents($file_path)) &&
                ($rows = unserialize(gzuncompress($rows)))
        ) {
            $this->container['log']->addDebug('CACHED: '.$sql);
        } else {
            if (!$this->dbConn) $this->init();

            $this->container['log']->addDebug('NO CACHE: '.$sql);

            try {
                $sth = $this->dbConn->prepare($sql);
                $sth->execute($params);
            } catch (Exception $e) {
                throw new \Exception($e->getMessage(). " SQL: $sql");
            }

            $rows = $sth->fetchAll(\PDO::FETCH_ASSOC);

            //save cache
            if ($rows) {
                file_put_contents($file_path, gzcompress(serialize($rows), 9));
            }
        }

        return $rows;
    }

    public function get_execs($filter_execs = null)
    {
        if($filter_execs === null)<commit_msg>Fix: Only save query cache file if cache is active<commit_after><?php

namespace alojaweb\inc;

class DBUtils
{
    public static $TASK_METRICS = [
        'Duration',
        'Bytes Read',
        'Bytes Written',
        'FILE_BYTES_WRITTEN',
        'FILE_BYTES_READ',
        'HDFS_BYTES_WRITTEN',
        'HDFS_BYTES_READ',
        'Spilled Records',
        'SPLIT_RAW_BYTES',
        'Map input records',
        'Map output records',
        'Map input bytes',
        'Map output bytes',
        'Map output materialized bytes',
        'Reduce input groups',
        'Reduce input records',
        'Reduce output records',
        'Reduce shuffle bytes',
        'Combine input records',
        'Combine output records',
    ];

    private $dbConn;
    private $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function init()
    {
        $this->dbConn = new \PDO($this->container['config']['db_conn_chain'], $this->container['config']['mysql_user'], $this->container['config']['mysql_pwd']);

        $this->dbConn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->dbConn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
    }

    public function get_rows($sql, $params = array())
    {
        $md5_sql = md5($sql.http_build_query($params, '', ','));
        $file_path = "{$this->container['config']['db_cache_path']}/CACHE_$md5_sql.sql";

        if ($this->container['env'] == 'dev' || $_SERVER['HTTP_HOST'] == 'localhost' || (isset($_GET['NO_CACHE']) && strlen($_GET['NO_CACHE']) > 0)) {
            $use_cache = false;
        } else {
            $use_cache = true;
        }

        //check for cache first
        if ($use_cache &&
                file_exists($file_path) &&
                ($rows = file_get_contents($file_path)) &&
                ($rows = unserialize(gzuncompress($rows)))
        ) {
            $this->container['log']->addDebug('CACHED: '.$sql);
        } else {
            if (!$this->dbConn) $this->init();

            $this->container['log']->addDebug('NO CACHE: '.$sql);

            try {
                $sth = $this->dbConn->prepare($sql);
                $sth->execute($params);
            } catch (Exception $e) {
                throw new \Exception($e->getMessage(). " SQL: $sql");
            }

            $rows = $sth->fetchAll(\PDO::FETCH_ASSOC);

            //save cache
            if ($use_cache && $rows) {
                file_put_contents($file_path, gzcompress(serialize($rows), 9));
            }
        }

        return $rows;
    }

    public function get_execs($filter_execs = null)
    {
        if($filter_execs === null)
{'fraction_non_alphanumeric': 0.15979092775807355, 'fraction_numerical': 0.0018667164457718873, 'mean_word_length': 2.1151162790697673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '5416270', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1608, 'n_words': 301}