WYSIJA_module_statistics_model

The MailPoet Newsletters WYSIJA module statistics model class.

Defined (1)

The class is defined in the following location(s).

/core/module/statistics_model.php  
  1. class WYSIJA_module_statistics_model extends WYSIJA_model { 
  2.  
  3. const STATS_DATE_OF_CREATION = 'STATS_DATE_OF_CREATION'; 
  4. /** 
  5. * Time to live of stats data 
  6. */ 
  7. const STATS_DATA_LIFE_TIME = 3600; // 1h * 60mins * 60s 
  8. /** 
  9. * Time to live of stats's table (structure) 
  10. */ 
  11. const STATS_TABLE_LIFE_TIME = 86400; // 24h * 60mins * 60s 
  12.  
  13. const STATS_PREFIX = 'sc_'; // prefix of stat tables 
  14.  
  15. public function __construct() { 
  16. parent::__construct(); 
  17. $this->clean_up_out_of_date_tables(); 
  18.  
  19. /** 
  20. * @param array $params array of input params 
  21. */ 
  22.  
  23. public function get_hash(Array $params) { 
  24. $hash = md5(get_class($this) . json_encode($params)); 
  25. return substr($hash, 0, 8);// Only get 8 first characters to not make table name too long (less than 64) 
  26.  
  27. /** 
  28. * set a date of creating temporary tables, useful for caching 
  29. * @return type 
  30. */ 
  31. protected function set_date_of_creation() { 
  32. $config = WYSIJA::get('config', 'model'); 
  33. return $config->save(array(self::STATS_DATE_OF_CREATION => time())); 
  34.  
  35. /** 
  36. * Check if cache life time is out of date 
  37. * @return boolean 
  38. */ 
  39. protected function is_data_out_of_date() { 
  40. $config = WYSIJA::get('config', 'model'); 
  41. $date_of_creation = $config->getValue(self::STATS_DATE_OF_CREATION); 
  42. return (time() - $date_of_creation >= self::STATS_DATA_LIFE_TIME); 
  43.  
  44. /** 
  45. * Get the list of tables which are out of date, based on create_time 
  46. * @return type 
  47. */ 
  48. protected function get_out_of_date_tables() { 
  49. $query = ' 
  50. SELECT 
  51. TABLE_NAME as table_name 
  52. FROM 
  53. INFORMATION_SCHEMA.TABLES 
  54. WHERE 
  55. TABLE_SCHEMA IN (SELECT DATABASE()) 
  56. AND (TABLE_NAME LIKE "[wysija]'.self::STATS_PREFIX.'%" OR TABLE_NAME LIKE "[wysija]stats_cache_%") 
  57. AND TIMESTAMPDIFF(SECOND, CREATE_TIME, NOW()) >= '.self::STATS_TABLE_LIFE_TIME.'; 
  58. '; 
  59. return $this->get_results($query); 
  60.  
  61. /** 
  62. * Auto cleanup out-of-date tables 
  63. */ 
  64. protected function clean_up_out_of_date_tables() { 
  65. $tables = $this->get_out_of_date_tables(); 
  66. if (!empty($tables) && is_array($tables)) { 
  67. $_temp = array(); 
  68. foreach ($tables as $table) 
  69. if (!empty($table['table_name'])) 
  70. $_temp[] = $table['table_name']; 
  71. if (!empty($_temp)) { 
  72. $query = 'DROP TABLE IF EXISTS `'. implode('`, `', $_temp).'`'; 
  73. $this->get_results($query); 
  74.  
  75. /** 
  76. * Check if a table exists 
  77. * @param string $table_name table name 
  78. * @return boolean 
  79. */ 
  80. protected function does_table_exists($table_name = null) { 
  81. if (empty($table_name) OR !is_string($table_name)) 
  82. return false; 
  83. $query = "SHOW TABLES LIKE '$table_name'"; 
  84. $result = $this->get_results($query); 
  85. return !empty($result) ? true : false; 
  86.  
  87. /** 
  88. * Generate a table name, based on input params 
  89. * @param type $params 
  90. */ 
  91. protected function get_table_name($params) { 
  92. $hash = $this->get_hash($params); 
  93. return '[wysija]' . self::STATS_PREFIX . $hash; 
  94.  
  95. /** 
  96. * Generate a cached table 
  97. * @param type $table_name 
  98. * @param array $queries_create_table query to create a new cached table 
  99. * @param array $queries_insert_data query to collect and insert data to the newly created/truncated table 
  100. * @return boolean 
  101. */ 
  102. protected function generate_table($table_name, Array $queries_create_table, Array $queries_insert_data) { 
  103. $is_out_of_date = $this->is_data_out_of_date(); 
  104. $does_table_exists = $this->does_table_exists($table_name); 
  105.  
  106. if (!$is_out_of_date && $does_table_exists) 
  107. return true; 
  108. if ($does_table_exists) { 
  109. $this->query('TRUNCATE TABLE `' . $table_name . '`'); 
  110. } else { 
  111. foreach ($queries_create_table as $query_create_table) 
  112. $this->query($query_create_table); 
  113.  
  114. foreach ($queries_insert_data as $query_insert_data) 
  115. $this->query($query_insert_data); 
  116.  
  117. if ($is_out_of_date) { 
  118. $this->set_date_of_creation(); 
  119.  
  120. return true; 
  121.