Reader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * @return \Redis\Logger\Settings
  107. */
  108. public function getSettings() {
  109. return $this->settings;
  110. }
  111. public static function getTemplateChannel($template) {
  112. }
  113. /**
  114. * @param null $channel
  115. * @param int $severity
  116. * WATCHDOG_* : 0 to 7
  117. * @param int $skip
  118. * Number of pages to skip.
  119. * @param int $entries_per_page
  120. * Maximum number of entries per page.
  121. */
  122. public function getTemplates($channel = NULL, $severity = NULL, $skip = 0, $entries_per_page = 0) {
  123. $this->ensureTemplateCache();
  124. $matches = array();
  125. $count = 0;
  126. $channelRegex = $this->settings->getChannelRegex();
  127. $severityRegex = $this->settings->getSeverityRegex();
  128. $entries_per_page = $this->settings->getEntriesPerPage($entries_per_page);
  129. $start = $skip * $entries_per_page;
  130. $end = $start + $entries_per_page;
  131. foreach ($this->templateCache as $template) {
  132. if (!$this->passesFilter($channel, $channelRegex, $template)) {
  133. continue;
  134. }
  135. if (!$this->passesFilter($severity, $severityRegex, $template)) {
  136. continue;
  137. }
  138. $count++;
  139. if ($count >= $skip) {
  140. if ($count >= $end) {
  141. break;
  142. }
  143. $matches[] = $template;
  144. }
  145. }
  146. return $matches;
  147. }
  148. /**
  149. * Perform a complete Redis SCAN series.
  150. *
  151. * @param string $pattern
  152. * The Redis SCAN MATCH optional argument.
  153. * @param int $max
  154. * Maximum number of matches to be returned. Cursor is left dangling if $max
  155. * is lower than the maximum number of available results: only the minimum
  156. * number of SCAN iterations needed to reach $max results will be performed.
  157. * @param int $batch_suggestion
  158. * The Redis SCAN COUNT optional argument.
  159. *
  160. * @return string[]
  161. */
  162. public function scan($pattern = '*', $max = 0, $batch_suggestion = 10) {
  163. $redis = $this->redis;
  164. $saved_scan_option = $redis->getOption(\Redis::OPT_SCAN);
  165. $redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
  166. $ret = array();
  167. // Initialize our iterator to NULL.
  168. $scan_it = NULL;
  169. $count = 0;
  170. $request_count = 1;
  171. // Retry when we get no keys back.
  172. while ($arr_keys = $redis->scan($scan_it, $pattern, $batch_suggestion)) {
  173. $request_count++;
  174. foreach ($arr_keys as $str_key) {
  175. $ret[] = $str_key;
  176. if ($max) {
  177. $count++;
  178. if ($count >= $max) {
  179. break 2; // Break out of foreach + while.
  180. }
  181. }
  182. }
  183. }
  184. if ($saved_scan_option != \Redis::SCAN_RETRY) {
  185. $redis->setOption(\Redis::OPT_SCAN, $saved_scan_option);
  186. }
  187. return $ret;
  188. }
  189. }