<commit_before><?php defined('SYSPATH') or die('No direct script access.');

/**
 * Common model class managing the branches table.
 */
class Report_Model extends Model {

    /**
     * The Web Service class.
     */
    protected $service = null;

    public function __construct()
    {
        parent::__construct();

        $config = array();
        $credentials = Kohana::config('webserviceclient.basic_auth');
        if ($credentials) {
            $config['basic_auth'] = $credentials;
        }

        $this->service = new Web_Service($config);
    }

     /**
      * Checks to see if this crash report has been processed and if so
      * what was the date/time. If crash has not been processed yet
      * NULL is returned.
      *
      * @param  string UUID by which to look up report
      * @param  string uri for retrieving the crash dump
      * @return object Report data and dump data OR NULL
      */
    public function getByUUID($uuid, $crash_uri)
    {

        $crashReportDump = new CrashReportDump;
        $crash_report_json = $crashReportDump->getJsonZ($crash_uri);
            $raw_json = $this->getRawJson($uuid);

        if($crash_report_json === false){
                Kohana::log('info', "$uuid could not fetch processed JSON");
                return false;
        } else if ($crash_report_json === true) {
                Kohana::log('info', "$uuid was reported but not processed; a priority job has been scheduled.");
                return true;
        } else if( !is_bool($crash_report_json) ){
			/* Note: 99% of our data comes from the processed crash dump
			         jsonz file. Only select columns that aren't in the json file
			         such as email which is SENSITIVE and should never appear in
		                 the publically accessable jsonz file. Anything here will be
		                 merged into the model object + jsonz data */
		        // Added addons_checked since that's not in the jsonz - bug 590245
                        // Extract the date of crash so we can limit query to the appropriate partition - bug 718224 
                        $day = substr($uuid, -2);
                        $month = substr($uuid, -4, 2);
                        $year = substr($uuid, -6, 2);
                        $report_date_sql = '20'.$year.'-'.$month.'-'.$day; // assume we won't be using this code in 88 years :D
		        $report = $this->db->query(
		            "/* soc.web report.dateProcessed */
		                SELECT reports.email, reports.url, reports.addons_checked,
			            ( SELECT reports_duplicates.duplicate_of FROM reports_duplicates WHERE reports_duplicates.uuid = reports.uuid) as duplicate_of
		                FROM reports
		                WHERE reports.uuid=?
		                AND reports.success
		                IS NOT NULL
                                AND tstz_between( reports.date_processed,  ( date ? - 1 ), ( date ? + 1 ))
		            ", $uuid, $report_date_sql, $report_date_sql)->current();
	            if(!$report) {
                        Kohana::log('info', "$uuid processed crash exists in HBase but does not exist in the reports table");
                        return false;
                }

                $crashReportDump->populate($report, $crash_report_json, $raw_json);
                return $report;
        } else {
        Kohana::log('info', "$uuid doesn't exist (404)");
                return NULL;
        }

    }

    public function getRawJson($uuid){
        $uri = Kohana::config('webserviceclient.socorro_hostname').'/crash/meta/by/uuid/'.$uuid;
        $result = $this->service->get($uri);
        return $result;
    }



    /**
     * Determine whether or not the raw dumps are still on the system and return the URLs
     * by which they may be downloaded.  UUID must contain a timestamp that is within the given
     * acceptable timeframe found in Kohana::config('application.raw_dump_availability').
     *
     * @param   string  The $uuid for the dump
     * @return  array|bool  Return an array containing the dump and json urls for download; else
     *                  return false if unavailable
     */
    public function formatRawDumpURLs ($uuid) {
        if ($uuid_timestamp = $this->uuidTimestamp($uuid)) {
            if ($uuid_timestamp > (time() - Kohana::config('application.raw_dump_availability'))) {
                return array(
                    Kohana::config('application.raw_dump_url') . $uuid . '.dump',
                    Kohana::config('application.raw_dump_url') . $uuid . '.json',
                );
            }
        }
        return false;
    }

    /**
     * Check the UUID to determine if this report is still valid.
     *
     * @access  public
     * @param   string  The $uuid for this report
     * @return  bool    Return TRUE if valid; return FALSE if invalid
     */
    public function isReportValid ($uuid)
    {
        if ($uuid_timestamp = $this->uuidTimestamp($uuid)) {
            if ($uuid_timestamp < (mktime(0, 0, 0, date("m"), date("d"), date("Y")-3))) {
                return false;
            } else {
                return true;
            }
        }

        // Can't determine just by looking at the UUID. Return TRUE.
        return true;
    }

    /**
     * Determine whether or not this signature exists within the `reports` table.
     *
     * @param   ?       The signature for the report
     * @return  bool    Return TRUE if exists; return FALSE if does not exist
     */
    public function sig_exists($signature)
    {
        $rs = $this->db->query(
                "/* soc.web report sig exists */
                    SELECT signature
                    FROM reports
                    WHERE signature = ?
                    LIMIT 1
                ", $signature)->current();
        if ($rs) {
        return TRUE;
        } else {
        return FALSE;
    }
    }

    /**
     * Determine the timestamp for this report by the given UUID.
     *
     * @access  public
     * @param   string  The $uuid for this report
     * @return  int     The timestamp for this report.
     */
    public function uuidTimestamp ($uuid)
    {
        $uuid_chunks = str_split($uuid, 6);
        if (isset($uuid_chunks[5]) && is_numeric($uuid_chunks[5])) {
            $uuid_date = str_split($uuid_chunks[5], 2);
            if (isset($uuid_date[0]) && isset($uuid_date[1]) && isset($uuid_date[2])) {
                return mktime(0, 0, 0, $uuid_date[1], $uuid_date[2], $uuid_date[0]);
            }
        }
    return false;
    }<commit_msg>Fixes bug 728194 - Removed obsolete function `sig_exists` from report model.<commit_after><?php defined('SYSPATH') or die('No direct script access.');

/**
 * Common model class managing the branches table.
 */
class Report_Model extends Model {

    /**
     * The Web Service class.
     */
    protected $service = null;

    public function __construct()
    {
        parent::__construct();

        $config = array();
        $credentials = Kohana::config('webserviceclient.basic_auth');
        if ($credentials) {
            $config['basic_auth'] = $credentials;
        }

        $this->service = new Web_Service($config);
    }

     /**
      * Checks to see if this crash report has been processed and if so
      * what was the date/time. If crash has not been processed yet
      * NULL is returned.
      *
      * @param  string UUID by which to look up report
      * @param  string uri for retrieving the crash dump
      * @return object Report data and dump data OR NULL
      */
    public function getByUUID($uuid, $crash_uri)
    {

        $crashReportDump = new CrashReportDump;
        $crash_report_json = $crashReportDump->getJsonZ($crash_uri);
            $raw_json = $this->getRawJson($uuid);

        if($crash_report_json === false){
                Kohana::log('info', "$uuid could not fetch processed JSON");
                return false;
        } else if ($crash_report_json === true) {
                Kohana::log('info', "$uuid was reported but not processed; a priority job has been scheduled.");
                return true;
        } else if( !is_bool($crash_report_json) ){
			/* Note: 99% of our data comes from the processed crash dump
			         jsonz file. Only select columns that aren't in the json file
			         such as email which is SENSITIVE and should never appear in
		                 the publically accessable jsonz file. Anything here will be
		                 merged into the model object + jsonz data */
		        // Added addons_checked since that's not in the jsonz - bug 590245
                        // Extract the date of crash so we can limit query to the appropriate partition - bug 718224 
                        $day = substr($uuid, -2);
                        $month = substr($uuid, -4, 2);
                        $year = substr($uuid, -6, 2);
                        $report_date_sql = '20'.$year.'-'.$month.'-'.$day; // assume we won't be using this code in 88 years :D
		        $report = $this->db->query(
		            "/* soc.web report.dateProcessed */
		                SELECT reports.email, reports.url, reports.addons_checked,
			            ( SELECT reports_duplicates.duplicate_of FROM reports_duplicates WHERE reports_duplicates.uuid = reports.uuid) as duplicate_of
		                FROM reports
		                WHERE reports.uuid=?
		                AND reports.success
		                IS NOT NULL
                                AND tstz_between( reports.date_processed,  ( date ? - 1 ), ( date ? + 1 ))
		            ", $uuid, $report_date_sql, $report_date_sql)->current();
	            if(!$report) {
                        Kohana::log('info', "$uuid processed crash exists in HBase but does not exist in the reports table");
                        return false;
                }

                $crashReportDump->populate($report, $crash_report_json, $raw_json);
                return $report;
        } else {
        Kohana::log('info', "$uuid doesn't exist (404)");
                return NULL;
        }

    }

    public function getRawJson($uuid){
        $uri = Kohana::config('webserviceclient.socorro_hostname').'/crash/meta/by/uuid/'.$uuid;
        $result = $this->service->get($uri);
        return $result;
    }



    /**
     * Determine whether or not the raw dumps are still on the system and return the URLs
     * by which they may be downloaded.  UUID must contain a timestamp that is within the given
     * acceptable timeframe found in Kohana::config('application.raw_dump_availability').
     *
     * @param   string  The $uuid for the dump
     * @return  array|bool  Return an array containing the dump and json urls for download; else
     *                  return false if unavailable
     */
    public function formatRawDumpURLs ($uuid) {
        if ($uuid_timestamp = $this->uuidTimestamp($uuid)) {
            if ($uuid_timestamp > (time() - Kohana::config('application.raw_dump_availability'))) {
                return array(
                    Kohana::config('application.raw_dump_url') . $uuid . '.dump',
                    Kohana::config('application.raw_dump_url') . $uuid . '.json',
                );
            }
        }
        return false;
    }

    /**
     * Check the UUID to determine if this report is still valid.
     *
     * @access  public
     * @param   string  The $uuid for this report
     * @return  bool    Return TRUE if valid; return FALSE if invalid
     */
    public function isReportValid ($uuid)
    {
        if ($uuid_timestamp = $this->uuidTimestamp($uuid)) {
            if ($uuid_timestamp < (mktime(0, 0, 0, date("m"), date("d"), date("Y")-3))) {
                return false;
            } else {
                return true;
            }
        }

        // Can't determine just by looking at the UUID. Return TRUE.
        return true;
    }

    /**
     * Determine the timestamp for this report by the given UUID.
     *
     * @access  public
     * @param   string  The $uuid for this report
     * @return  int     The timestamp for this report.
     */
    public function uuidTimestamp ($uuid)
    {
        $uuid_chunks = str_split($uuid, 6);
        if (isset($uuid_chunks[5]) && is_numeric($uuid_chunks[5])) {
            $uuid_date = str_split($uuid_chunks[5], 2);
            if (isset($uuid_date[0]) && isset($uuid_date[1]) && isset($uuid_date[2])) {
                return mktime(0, 0, 0, $uuid_date[1], $uuid_date[2], $uuid_date[0]);
            }
        }
    return false;
    }
{'fraction_non_alphanumeric': 0.1066995544239338, 'fraction_numerical': 0.00787714831317632, 'mean_word_length': 1.837886656130052, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941110', 'n_tokens_mistral': 3587, 'n_tokens_neox': 3334, 'n_words': 1081}