forked from dxpr/analyze
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.api.php
More file actions
255 lines (243 loc) · 8.73 KB
/
analyze.api.php
File metadata and controls
255 lines (243 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* @file
* API documentation for the Analyze module.
*
* This file provides information on how to implement and extend the Analyze module
* in Drupal 10 by creating custom analysis plugins that integrate with the Analyze
* tab. The file also provides best practices for implementation, error handling,
* security, and performance.
*/
namespace Drupal\analyze;
/**
* Implements hook_analyze_info_alter().
*
* Allows modules to alter the list of analyzers available in the Analyze module.
*
* @param array $analyzers
* An associative array of analyzer definitions. The keys are the analyzer IDs
* and the values are the analyzer information arrays.
*/
function hook_analyze_info_alter(array &$analyzers): void {
// Example: Change the label of an existing analyzer.
if (isset($analyzers['security_analysis'])) {
$analyzers['security_analysis']['label'] = t('Enhanced Security Analysis');
}
}
/**
* Defines analysis plugins that integrate with the Analyze module.
*
* Modules implementing this hook can provide additional analysis functionality.
* Each plugin should return an array containing up to three analysis data points
* and optionally a link to a full report.
*
* @return array
* An associative array containing:
* - name: (string) A human-readable name for the analysis plugin.
* - description: (string) A brief description of what the analysis does.
* - data: (array) An array containing up to three analysis data points to be
* displayed in the Analyze tab. Each data point should be an associative
* array with the following keys:
* - label: (string) The label for the data point.
* - value: (mixed) The value of the data point, which could be a score,
* message, or other metrics.
* - component: (string) The component type used to render this data point
* (e.g., 'linear_gauge', 'table').
* - settings: (array) An optional array of additional settings specific to
* the component type.
* For a linear gauge, the settings might include:
* - caption: (string) The caption for the gauge, typically the data point
* label.
* - range_min_label: (string) The label for the minimum value range.
* - range_mid_label: (string) The label for the mid value range.
* - range_max_label: (string) The label for the maximum value range.
* - range_min: (string) The minimum value of the gauge.
* - range_max: (string) The maximum value of the gauge.
* - value: (float) The value to be represented on the gauge (as a
* percentage).
* - display_value: (string) The value to display in the gauge.
* - report_link: (optional) An associative array for the full report link,
* with the following keys:
* - title: (string) The title for the link.
* - callback: (callable) A function that returns the full report data to
* be displayed in a second-level tab.
*/
function hook_analyze_info(): array {
return [
'security_analysis' => [
'name' => t('Security Analysis'),
'description' => t('Analyzes potential security issues such as PII exposure
and malicious links.'),
'data' => [
[
'label' => t('PII Detection'),
'value' => 0.01,
'component' => 'linear_gauge',
'settings' => [
'caption' => t('PII (Personally Identifiable Information) Detection'),
'range_min_label' => t('PII Highly Improbable'),
'range_mid_label' => '',
'range_max_label' => t('PII Highly Probable'),
'range_min' => '0',
'range_max' => '1',
'value' => 0.01,
'display_value' => '1%',
],
],
[
'label' => t('Malicious Link Detection'),
'value' => 0.99,
'component' => 'linear_gauge',
'settings' => [
'caption' => t('Malicious Link Detection'),
'range_min_label' => t('Malicious Links Highly Improbable'),
'range_mid_label' => '',
'range_max_label' => t('Malicious Links Highly Probable'),
'range_min' => '0',
'range_max' => '1',
'value' => 0.99,
'display_value' => '99%',
],
],
],
'report_link' => [
'title' => t('View Sitewide Security Report'),
'callback' => 'Drupal\analyze\SecurityAnalyzer::generateFullReport',
],
],
'page_views_analysis' => [
'name' => t('Page Views Analysis'),
'description' => t('Tracks the popularity of content by showing page view
statistics.'),
'data' => [
[
'label' => t('Total Page Views'),
'value' => 3229,
'component' => 'table',
],
[
'label' => t("Today's Page Views"),
'value' => 29,
'component' => 'table',
],
],
'report_link' => [
'title' => t('View Sitewide Page Views Report'),
'callback' => 'Drupal\analyze\PageViewsAnalyzer::generateFullReport',
],
],
];
}
/**
* Class SecurityAnalyzer.
*
* Provides the analysis and report generation for the Security Analysis plugin.
*/
class SecurityAnalyzer {
/**
* Generates a full report for the Security Analysis.
*
* This function should return a renderable array containing the full report
* data for the specified entity, which will be displayed in a second-level tab.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being analyzed.
*
* @return array
* A renderable array representing the full report.
*
* @throws \Exception
* If the report generation fails, an exception is thrown.
*/
public static function generateFullReport(\Drupal\Core\Entity\EntityInterface $entity): array {
try {
// Perform the full report generation logic here.
return [
'#type' => 'markup',
'#markup' => t('Detailed Security report content goes here.'),
];
}
catch (\Exception $e) {
// Log the error and return a user-friendly message.
\Drupal::logger('analyze')->error($e->getMessage());
return [
'#type' => 'markup',
'#markup' => t('An error occurred while generating the report. Please
try again later.'),
];
}
}
}
/**
* Class PageViewsAnalyzer.
*
* Provides the analysis and report generation for the Page Views Analysis plugin.
*/
class PageViewsAnalyzer {
/**
* Generates a full report for the Page Views Analysis.
*
* This function should return a renderable array containing the full report
* data for the specified entity, which will be displayed in a second-level tab.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being analyzed.
*
* @return array
* A renderable array representing the full report.
*
* @throws \Exception
* If the report generation fails, an exception is thrown.
*/
public static function generateFullReport(\Drupal\Core\Entity\EntityInterface $entity): array {
try {
// Perform the full report generation logic here.
return [
'#type' => 'markup',
'#markup' => t('Detailed Page Views report content goes here.'),
];
}
catch (\Exception $e) {
// Log the error and return a user-friendly message.
\Drupal::logger('analyze')->error($e->getMessage());
return [
'#type' => 'markup',
'#markup' => t('An error occurred while generating the report. Please
try again later.'),
];
}
}
}
/**
* Alters the analysis results before they are displayed in the Analyze tab.
*
* @param array $analysis_results
* The array of analysis results to be altered.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being analyzed.
* @param string $plugin_id
* The plugin ID of the analysis being altered.
*
* @return void
*/
function hook_analyze_results_alter(array &$analysis_results, \Drupal\Core\Entity\EntityInterface $entity, string $plugin_id): void {
// Example: Modify existing data points for the page views analysis.
if ($plugin_id === 'page_views_analysis') {
foreach ($analysis_results['data'] as &$data_point) {
if ($data_point['component'] === 'table') {
$data_point['value'] .= ' (' . t('anonymous users only') . ')';
}
}
}
}
/**
* Provides testing and debugging guidelines for custom analysis plugins.
*
* When developing a custom analysis plugin, ensure that you:
* - Clear the Drupal cache (`drush cr`) after implementing or modifying hooks.
* - Use the Devel module's dpm() function or Drupal::logger() for debugging
* outputs.
* - Test the Analyze tab display on multiple entity types to ensure
* compatibility.
* - Check the permissions settings to verify access control.
*/