|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Controller; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class TopController implements the Top403/Top404 controllers. |
|
7
|
|
|
*/ |
|
8
|
|
|
class TopController { |
|
9
|
|
|
public function top403() { |
|
|
|
|
|
|
10
|
|
|
return ['#markup' => "403"]; |
|
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function top404() { |
|
|
|
|
|
|
14
|
|
|
return ['#markup' => "404"]; |
|
|
|
|
|
|
15
|
|
|
} |
|
16
|
|
|
/** |
|
17
|
|
|
* Page callback for "admin/reports/[access-denied|page-not-found]". |
|
18
|
|
|
* |
|
19
|
|
|
* @return array |
|
|
|
|
|
|
20
|
|
|
*/ |
|
21
|
|
|
function mongodb_watchdog_page_top($type) { |
|
|
|
|
|
|
22
|
|
|
$ret = array(); |
|
23
|
|
|
$type_param = array('%type' => $type); |
|
24
|
|
|
$limit = 50; |
|
25
|
|
|
|
|
26
|
|
|
// Safety net |
|
|
|
|
|
|
27
|
|
|
$types = array( |
|
28
|
|
|
'page not found', |
|
29
|
|
|
'access denied', |
|
30
|
|
|
); |
|
31
|
|
|
if (!in_array($type, $types)) { |
|
32
|
|
|
drupal_set_message(t('Unknown top report type: %type', $type_param), 'error'); |
|
33
|
|
|
watchdog('mongodb_watchdog', 'Unknown top report type: %type', $type_param, WATCHDOG_WARNING); |
|
34
|
|
|
$ret = ''; |
|
35
|
|
|
return $ret; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Find _id for the error type. |
|
39
|
|
|
$watchdog = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
|
40
|
|
|
$template = $watchdog->findOne(array('type' => $type), array('_id')); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
// findOne() will return NULL if no row is found |
|
|
|
|
|
|
43
|
|
|
if (empty($template)) { |
|
44
|
|
|
$ret['empty'] = array( |
|
45
|
|
|
'#markup' => t('No "%type" message found', $type_param), |
|
46
|
|
|
'#prefix' => '<div class="mongodb-watchdog-message">', |
|
47
|
|
|
'#suffix' => '</div>', |
|
48
|
|
|
); |
|
49
|
|
|
$ret = drupal_render($ret); |
|
50
|
|
|
return $ret; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Find occurrences of error type. |
|
54
|
|
|
$key = $template['_id']; |
|
55
|
|
|
$event_collection = mongodb_collection('watchdog_event_' . $key); |
|
56
|
|
|
$reduce = <<<EOT |
|
57
|
|
|
function (doc, accumulator) { |
|
58
|
|
|
accumulator.count++; |
|
59
|
|
|
} |
|
60
|
|
|
EOT; |
|
61
|
|
|
|
|
62
|
|
|
$counts = $event_collection->group( |
|
|
|
|
|
|
63
|
|
|
array('variables.@param' => 1), |
|
64
|
|
|
array('count' => array()), |
|
65
|
|
|
$reduce |
|
66
|
|
|
); |
|
67
|
|
|
if (!$counts['ok']) { |
|
68
|
|
|
drupal_set_message(t('No "%type" occurrence found', $type_param), 'error'); |
|
69
|
|
|
$ret = ''; |
|
70
|
|
|
return $ret; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
$counts = $counts['retval']; |
|
73
|
|
|
usort($counts, '_mongodb_watchdog_sort_top'); |
|
74
|
|
|
$counts = array_slice($counts, 0, $limit); |
|
75
|
|
|
|
|
76
|
|
|
$header = array( |
|
77
|
|
|
t('#'), |
|
78
|
|
|
t('Paths'), |
|
79
|
|
|
); |
|
80
|
|
|
$rows = array(); |
|
81
|
|
|
foreach ($counts as $count) { |
|
82
|
|
|
$rows[] = array( |
|
83
|
|
|
$count['variables.@param'], |
|
84
|
|
|
$count['count'], |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$ret = array( |
|
89
|
|
|
'#theme' => 'table', |
|
90
|
|
|
'#header' => $header, |
|
91
|
|
|
'#rows' => $rows, |
|
92
|
|
|
); |
|
93
|
|
|
return $ret; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* usort() helper function to sort top entries returned from a group query. |
|
|
|
|
|
|
98
|
|
|
* |
|
99
|
|
|
* @param array $x |
|
|
|
|
|
|
100
|
|
|
* @param array $y |
|
|
|
|
|
|
101
|
|
|
* |
|
102
|
|
|
* @return boolean |
|
|
|
|
|
|
103
|
|
|
*/ |
|
104
|
|
|
function _mongodb_watchdog_sort_top($x, $y) { |
|
|
|
|
|
|
105
|
|
|
$cx = $x['count']; |
|
|
|
|
|
|
106
|
|
|
$cy = $y['count']; |
|
|
|
|
|
|
107
|
|
|
return $cy - $cx; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
|
|
|
|
|
111
|
|
|
|