-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PerformanceAudit.php
173 lines (155 loc) · 4.49 KB
/
PerformanceAudit.php
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
<?php
namespace Piwik\Plugins\PerformanceAudit;
require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';
use Exception;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Log\Logger;
use Piwik\Plugin;
use Piwik\Plugins\PerformanceAudit\Exceptions\InstallationFailedException;
use ReflectionClass;
use ReflectionException;
class PerformanceAudit extends Plugin
{
/**
* Register plugin events.
*
* @return array
*/
public function registerEvents()
{
return [
'Db.getTablesInstalled' => 'getTablesInstalled',
'Updater.componentUpdated' => 'updated',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles'
];
}
/**
* Create database table(s).
*
* @return void
* @throws Exception
*/
public function install()
{
$this->createDatabaseTable();
}
/**
* Delete database table(s).
*
* @return void
*/
public function uninstall()
{
$this->deleteDatabaseTable();
}
/**
* Activate plugin.
*
* @return bool
*/
public function activate()
{
try {
(new NodeDependencyInstaller())->install();
} catch (Exception $exception) {
StaticContainer::get(Logger::class)->error('Unable to activate plugin.', ['exception' => $exception]);
// Throw new exception so the plugin doesn't get activated in Matomo
throw new InstallationFailedException('PerformanceAudit plugin activation failed due to the following error: ' . PHP_EOL . $exception->getMessage());
}
return true;
}
/**
* Deactivate plugin.
*
* @return bool
*/
public function deactivate()
{
return (new NodeDependencyInstaller())->uninstall();
}
/**
* Called event after component update.
*
* @param string $componentName
* @param string $updatedVersion
* @return void
* @throws ReflectionException
*/
public function updated($componentName, $updatedVersion) {
// Only perform action if this plugin got updated
if ((new ReflectionClass($this))->getShortName() === $componentName) {
StaticContainer::get(Logger::class)->info($componentName . ' plugin was updated to version: ' . $updatedVersion);
// Since an plugin update removes all installed Node dependencies,
// we re-add them by running the dependencies installer via activate()
// Nice side effect: Dependencies get directly updated
$this->activate();
}
}
/**
* Create log database table.
*
* @return void
* @throws Exception
*/
private function createDatabaseTable()
{
Db::exec('
CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('log_performance') . '` (
`idreport` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`idsite` INT UNSIGNED NOT NULL,
`emulated_device` TINYINT UNSIGNED NOT NULL,
`idaction` INT UNSIGNED NOT NULL,
`key` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,
`min` INT UNSIGNED NOT NULL,
`median` INT UNSIGNED NOT NULL,
`max` INT UNSIGNED NOT NULL,
`created_at` DATE NOT NULL,
PRIMARY KEY (`idreport`),
INDEX (`idsite`),
INDEX (`idaction`),
INDEX (`emulated_device`),
INDEX (`key`),
INDEX (`created_at`)
) DEFAULT CHARSET=utf8 COLLATE utf8_general_ci
');
}
/**
* Delete log database table.
*
* @return void
*/
private function deleteDatabaseTable()
{
Db::dropTables(Common::prefixTable('log_performance'));
}
/**
* Register the new tables.
*
* @param array $tablesInstalled
* @retrun void
*/
public function getTablesInstalled(&$tablesInstalled)
{
$tablesInstalled[] = Common::prefixTable('log_performance');
}
/**
* Sets array of required CSS files.
*
* @param array $cssFiles
* @retrun void
*/
public function getStylesheetFiles(&$cssFiles)
{
$cssFiles[] = 'plugins/PerformanceAudit/stylesheets/pluginCheck.css';
}
/**
* Return if internet connection is required.
*
* @return bool
*/
public function requiresInternetConnection() {
return true;
}
}