Reader.php 5.3 KB

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