<commit_before><?php
/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2022 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Metabase Reports plugin
 *
 * @package  Application
 * @subpackage Plugins
 * @since    5.14
 */
class metabaseReportsPlugin extends Billrun_Plugin_BillrunPluginBase {
	
	/**
	 * plugin name
	 *
	 * @var string
	 */
	protected $name = 'metabaseReports';
	
	/**
	 * Holds the file export details
	 * @var array 
	 */
	protected $export_details;
	
	/**
	 * Holds the metabase details
	 * @var array 
	 */
	protected $metabase_details;
	
	/**
	 * Holds the reports details
	 * @var array 
	 */
	protected $reports_details;
	
	/**
	 * Holds the domain of the metabase's APIs
	 * @var string
	 */
	protected $domain;
	
	/**
	 * Holds the plugin's configuration values
	 * @var array
	 */
	protected $values;
	
	protected static $type = 'ssh';
	
	protected $port = '22';
	
	public function __construct($options = array()) {
		if (!$this->validateReportsConfStructure($options)) {
			Billrun_Factory::log("Metabase reports - missing reports/metbase details/export info/all the reports are disabled. No action was done.", Zend_Log::WARN);
			return;
		}
		$this->reports_details = $options['reports'];
		$this->metabase_details = $options['metbase_details'];
		$this->export_details = $options['export'];
		$this->values = isset($options['added_data']) ? $options['added_data'] : [];
		
	}
	
	/**
	 * Check if Metabase reports actions are needed.
	 * @param array $options
	 */
	public function validateReportsConfStructure($options) {
		$reports_exist = !empty($options['reports']);
		$all_disable = true;
		if ($reports_exist) {
			foreach ($options['reports'] as $report) {
				if(!isset($report['enable']) || ($report['enable'] == true)) {
					$all_disable = false;
				}
			}
		}
		$metabase_details = !empty($options['metbase_details']);
		$export_details = !empty($options['export']);
		return $reports_exist && !$all_disable && $metabase_details && $export_details;
	}

	/**
	 * Function to fetch the reports that should run in the current day and hour.
	 */
	public function runReports () {
		$reports = $this->getReportsToRun();
		Billrun_Factory::log("Found " . count($reports) . " reports to run."  , Zend_Log::INFO);
		foreach ($reports as $index => $report_settings) {
			if (class_exists($report_class = 'Report_' . $report_settings['name'])) {
				$report = new $report_class($report_settings);
			} else {
				$report = new Report($report_settings);
			}
			$metabase_url = rtrim($this->metabase_details['url'], "/");
			try {<commit_msg>BRCD-3432:Added hourly cron function to Billrun Metabase reports manager. Updated the plugin' constructor respectively<commit_after><?php
/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2022 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Metabase Reports plugin
 *
 * @package  Application
 * @subpackage Plugins
 * @since    5.14
 */
class metabaseReportsPlugin extends Billrun_Plugin_BillrunPluginBase {
	
	/**
	 * plugin name
	 *
	 * @var string
	 */
	protected $name = 'metabaseReports';
	
	/**
	 * Holds the file export details
	 * @var array 
	 */
	protected $export_details;
	
	/**
	 * Holds the metabase details
	 * @var array 
	 */
	protected $metabase_details;
	
	/**
	 * Holds the reports details
	 * @var array 
	 */
	protected $reports_details;
	
	/**
	 * Holds the domain of the metabase's APIs
	 * @var string
	 */
	protected $domain;
	
	/**
	 * Holds the plugin's configuration values
	 * @var array
	 */
	protected $values;
	
	protected static $type = 'ssh';
	
	protected $port = '22';
	
	public function __construct($options = array()) {		
		$this->reports_details = isset($options['reports']) ? $options['reports'] : [];
		$this->metabase_details = isset($options['metbase_details']) ? $options['metbase_details'] : [];
		$this->export_details = isset($options['export']) ? $options['export'] : [];
		$this->values = isset($options['added_data']) ? $options['added_data'] : [];
		
	}
	
	/**
	 * Check if Metabase reports actions are needed.
	 */
	public function validateReportsConfStructure() {
		$reports_exist = !empty($this->reports_details);
		$all_disable = true;
		if ($reports_exist) {
			foreach ($this->reports_details as $report) {
				if(!isset($report['enable']) || ($report['enable'] == true)) {
					$all_disable = false;
				}
			}
		}
		$metabase_details = !empty($this->metabase_details);
		$export_details = !empty($this->export_details);
		return $reports_exist && !$all_disable && $metabase_details && $export_details;
	}

	public function cronHour () {
		if (!$this->validateReportsConfStructure()) {
			Billrun_Factory::log("Metabase reports - missing reports/metbase details/export info/all the reports are disabled. No action was done.", Zend_Log::WARN);
			return;
		}
		$this->runReports();
	}
	
	/**
	 * Function to fetch the reports that should run in the current day and hour.
	 */
	public function runReports () {
		$reports = $this->getReportsToRun();
		Billrun_Factory::log("Found " . count($reports) . " reports to run."  , Zend_Log::INFO);
		foreach ($reports as $index => $report_settings) {
			if (class_exists($report_class = 'Report_' . $report_settings['name'])) {
				$report = new $report_class($report_settings);
			} else {
				$report = new Report($report_settings);
			}
			$metabase_url = rtrim($this->metabase_details['url'], "/");
			try {
{'fraction_non_alphanumeric': 0.1497134670487106, 'fraction_numerical': 0.0057306590257879654, 'mean_word_length': 3.7410865874363326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '6526051', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1678, 'n_words': 429}