Reader.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * @file
  4. * Contains the Redis\Logger\Reader class
  5. */
  6. namespace Redis\Logger;
  7. /**
  8. * This class reads the and erases the logger information stored in Redis:
  9. *
  10. * - messages templates
  11. * - channels used in message templates
  12. * - messages for a given template
  13. *
  14. * @package Redis\Logger
  15. */
  16. class Reader {
  17. /**
  18. * @var \Redis
  19. */
  20. protected $redis;
  21. /**
  22. * @var \Redis\Logger\Settings
  23. */
  24. protected $settings;
  25. /**
  26. * @var string[]
  27. */
  28. protected $templateCache = NULL;
  29. /**
  30. * @param \Redis $redis
  31. * @param \Redis\Logger\Settings $settings
  32. * We pass this constant instance in order to allow overriding it, say for
  33. * tests.
  34. */
  35. public function __construct(\Redis $redis, Settings $settings = NULL) {
  36. $this->redis = $redis;
  37. if (!isset($settings)) {
  38. $settings = new Settings();
  39. }
  40. $this->settings = $settings;
  41. }
  42. /**
  43. * Clear all data in the logger database.
  44. */
  45. public function clear() {
  46. $this->templateCache = NULL;
  47. $this->redis->flushDB();
  48. }
  49. /**
  50. * Load and cache the list of templates in the instance for this page cycle.
  51. *
  52. * Note that the templates are in Redis storage format, including the prefix,
  53. * to link directly to the results.
  54. */
  55. protected function ensureTemplateCache() {
  56. if (!isset($this->templateCache)) {
  57. $this->templateCache = $this->scan($this->settings->getRedisPattern(), 0, $this->settings->getScanBatchSize());
  58. }
  59. }
  60. /**
  61. * Return the list of channels. Assumed to be constant for a given page cycle.
  62. *
  63. * @return string[int]
  64. */
  65. public function getChannels() {
  66. $this->ensureTemplateCache();
  67. $channels = array();
  68. foreach ($this->templateCache as $key) {
  69. preg_match($this->settings->getChannelRegex(), $key, $matches);
  70. $channel = $matches[1];
  71. $channels[$channel] = $channel;
  72. }
  73. ksort($channels);
  74. return $channels;
  75. }
  76. /**
  77. * @param $template
  78. * The Redis key for a template.
  79. * @param int $skip
  80. * The number of pages to skip.
  81. * @param int $entries_per_page
  82. * The maximum number of entries in a page.
  83. */
  84. public function getEntries($template, $skip = 0, $entries_per_page = 0) {
  85. $entries_per_page = $this->settings->getEntriesPerPage($entries_per_page);
  86. // Redis expect actual number of entries in the RANGE parameters.
  87. $start = $skip * $entries_per_page;
  88. $end = $start + $entries_per_page;
  89. $entries = $this->redis->lRange($template, $start, $end);
  90. return $entries;
  91. }
  92. /**
  93. * @param mixed $criterium
  94. * Filter will always pass if criterium is not set, but be checked otherwise.
  95. * @param string $regex
  96. * The regex against which to match to pass the filter.
  97. * @param $string
  98. * The string to match against the regex.
  99. *
  100. * @return bool
  101. */
  102. protected function passesFilter($criterium, $regex, $string) {
  103. if (!isset($criterium)) {
  104. $ret = TRUE;
  105. }
  106. else {
  107. $sts = preg_match($regex, $string, $matches);
  108. $ret = $sts && ($matches[1] == $criterium);
  109. }
  110. return $ret;
  111. }
  112. /**
  113. * @return \Redis\Logger\Settings
  114. */
  115. public function getSettings() {
  116. return $this->settings;
  117. }
  118. public static function getTemplateChannel($template) {
  119. }
  120. /**
  121. * @param null $channel
  122. * @param int $severity
  123. * WATCHDOG_* : 0 to 7
  124. * @param int $skip
  125. * Number of pages to skip.
  126. * @param int $entries_per_page
  127. * Maximum number of entries per page.
  128. */
  129. public function getTemplates($channel = NULL, $severity = NULL, $skip = 0, $entries_per_page = 0) {
  130. $this->ensureTemplateCache();
  131. $matches = array();
  132. $count = 0;
  133. $channelRegex = $this->settings->getChannelRegex();
  134. $severityRegex = $this->settings->getSeverityRegex();
  135. $entries_per_page = $this->settings->getEntriesPerPage($entries_per_page);
  136. $start = $skip * $entries_per_page;
  137. $end = $start + $entries_per_page;
  138. foreach ($this->templateCache as $template) {
  139. if (!$this->passesFilter($channel, $channelRegex, $template)) {
  140. continue;
  141. }
  142. if (!$this->passesFilter($severity, $severityRegex, $template)) {
  143. continue;
  144. }
  145. $count++;
  146. if ($count >= $skip) {
  147. if ($count >= $end) {
  148. break;
  149. }
  150. $matches[] = $template;
  151. }
  152. }
  153. return $matches;
  154. }
  155. /**
  156. * Perform a complete Redis SCAN series.
  157. *
  158. * @param string $pattern
  159. * The Redis SCAN MATCH optional argument.
  160. * @param int $max
  161. * Maximum number of matches to be returned. Cursor is left dangling if $max
  162. * is lower than the maximum number of available results: only the minimum
  163. * number of SCAN iterations needed to reach $max results will be performed.
  164. * @param int $batch_suggestion
  165. * The Redis SCAN COUNT optional argument.
  166. *
  167. * @return string[]
  168. */
  169. public function scan($pattern = '*', $max = 0, $batch_suggestion = 10) {
  170. $redis = $this->redis;
  171. $saved_scan_option = $redis->getOption(\Redis::OPT_SCAN);
  172. $redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
  173. $ret = array();
  174. // Initialize our iterator to NULL.
  175. $scan_it = NULL;
  176. $count = 0;
  177. $request_count = 1;
  178. // Retry when we get no keys back.
  179. while ($arr_keys = $redis->scan($scan_it, $pattern, $batch_suggestion)) {
  180. $request_count++;
  181. foreach ($arr_keys as $str_key) {
  182. $ret[] = $str_key;
  183. if ($max) {
  184. $count++;
  185. if ($count >= $max) {
  186. break 2; // Break out of foreach + while.
  187. }
  188. }
  189. }
  190. }
  191. if ($saved_scan_option != \Redis::SCAN_RETRY) {
  192. $redis->setOption(\Redis::OPT_SCAN, $saved_scan_option);
  193. }
  194. return $ret;
  195. }
  196. }