Report loaded plugins to analytics

Change-Id: Ic537084dc0304e8a151fe968e6510a04f04e0b6c
This commit is contained in:
Viktar Donich 2018-05-04 15:04:44 -07:00
parent 8eb1599e95
commit c1c5f77fa6
6 changed files with 68 additions and 26 deletions

View File

@ -26,6 +26,13 @@
PAGE_LOADED: 'Page Loaded', PAGE_LOADED: 'Page Loaded',
}; };
// Plugin-related reporting constants.
const PLUGINS = {
TYPE: 'lifecycle',
// Reported events - alphabetize below.
INSTALLED: 'Plugins installed',
};
// Navigation reporting constants. // Navigation reporting constants.
const NAVIGATION = { const NAVIGATION = {
TYPE: 'nav-report', TYPE: 'nav-report',
@ -214,8 +221,10 @@
} }
}, },
pluginsLoaded() { pluginsLoaded(pluginsList) {
this.timeEnd(TIMER.PLUGINS_LOADED); this.timeEnd(TIMER.PLUGINS_LOADED);
this.reporter(
PLUGINS.TYPE, PLUGINS.INSTALLED, (pluginsList || []).join(','));
}, },
/** /**

View File

@ -162,6 +162,14 @@ limitations under the License.
)); ));
}); });
test('pluginsLoaded reports plugins', () => {
Gerrit._arePluginsLoaded.returns(true);
element.pluginsLoaded(['foo', 'bar']);
assert.isTrue(element.defaultReporter.calledWithExactly(
'lifecycle', 'Plugins installed', 'foo,bar'
));
});
test('caches reports if plugins are not loaded', () => { test('caches reports if plugins are not loaded', () => {
Gerrit._arePluginsLoaded.returns(false); Gerrit._arePluginsLoaded.returns(false);
element.timeEnd('foo'); element.timeEnd('foo');

View File

@ -56,18 +56,16 @@ limitations under the License.
stub('gr-endpoint-decorator', { stub('gr-endpoint-decorator', {
_import: sandbox.stub().returns(Promise.resolve()), _import: sandbox.stub().returns(Promise.resolve()),
}); });
// Since _endpoints are global, must reset state. Gerrit._resetPlugins();
Gerrit._endpoints = new GrPluginEndpoints();
container = fixture('basic'); container = fixture('basic');
Gerrit.install(p => plugin = p, '0.1', 'http://some/plugin/url.html'); Gerrit.install(p => plugin = p, '0.1', 'http://some/plugin/url.html');
hooks = [];
// Decoration // Decoration
decorationHook = plugin.registerCustomComponent('first', 'some-module'); decorationHook = plugin.registerCustomComponent('first', 'some-module');
// Replacement // Replacement
replacementHook = plugin.registerCustomComponent( replacementHook = plugin.registerCustomComponent(
'second', 'other-module', {replace: true}); 'second', 'other-module', {replace: true});
// Mimic all plugins loaded. // Mimic all plugins loaded.
Gerrit._setPluginsCount(0); Gerrit._setPluginsPending([]);
flush(done); flush(done);
}); });
@ -88,8 +86,10 @@ limitations under the License.
test('decoration', () => { test('decoration', () => {
const element = const element =
container.querySelector('gr-endpoint-decorator[name="first"]'); container.querySelector('gr-endpoint-decorator[name="first"]');
const module = Polymer.dom(element.root).children.find( const modules = Polymer.dom(element.root).children.filter(
element => element.nodeName === 'SOME-MODULE'); element => element.nodeName === 'SOME-MODULE');
assert.equal(modules.length, 1);
const [module] = modules;
assert.isOk(module); assert.isOk(module);
assert.equal(module['someparam'], 'barbar'); assert.equal(module['someparam'], 'barbar');
return decorationHook.getLastAttached().then(element => { return decorationHook.getLastAttached().then(element => {

View File

@ -52,8 +52,11 @@ breaking changes to gr-change-actions wont be noticed.
suite('early init', () => { suite('early init', () => {
setup(() => { setup(() => {
Gerrit._resetPlugins();
Gerrit.install(p => { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
// Mimic all plugins loaded.
Gerrit._setPluginsPending([]);
changeActions = plugin.changeActions(); changeActions = plugin.changeActions();
element = fixture('basic'); element = fixture('basic');
}); });
@ -71,6 +74,7 @@ breaking changes to gr-change-actions wont be noticed.
suite('normal init', () => { suite('normal init', () => {
setup(() => { setup(() => {
Gerrit._resetPlugins();
element = fixture('basic'); element = fixture('basic');
sinon.stub(element, '_editStatusChanged'); sinon.stub(element, '_editStatusChanged');
element.change = {}; element.change = {};
@ -78,6 +82,8 @@ breaking changes to gr-change-actions wont be noticed.
Gerrit.install(p => { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
changeActions = plugin.changeActions(); changeActions = plugin.changeActions();
// Mimic all plugins loaded.
Gerrit._setPluginsPending([]);
}); });
teardown(() => { teardown(() => {

View File

@ -320,15 +320,19 @@ limitations under the License.
assert.isTrue(Gerrit._arePluginsLoaded()); assert.isTrue(Gerrit._arePluginsLoaded());
}); });
test('_pluginInstalled', done => { test('_pluginInstalled', () => {
const pluginsLoadedStub = sandbox.stub();
stub('gr-reporting', { stub('gr-reporting', {
pluginsLoaded() { pluginsLoaded: (...args) => pluginsLoadedStub(...args),
done();
},
}); });
Gerrit._setPluginsCount(2); const plugins = [
Gerrit._pluginInstalled(); 'http://test.com/plugins/foo/static/test.js',
Gerrit._pluginInstalled(); 'http://test.com/plugins/bar/static/test.js',
];
Gerrit._setPluginsPending(plugins);
Gerrit._pluginInstalled(plugins[0]);
Gerrit._pluginInstalled(plugins[1]);
assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
}); });
test('install calls _pluginInstalled', () => { test('install calls _pluginInstalled', () => {

View File

@ -27,8 +27,12 @@
*/ */
let _pluginsPending = {}; let _pluginsPending = {};
let _pluginsInstalled = [];
let _pluginsPendingCount = -1; let _pluginsPendingCount = -1;
const UNKNOWN_PLUGIN = 'unknown';
const PANEL_ENDPOINTS_MAPPING = { const PANEL_ENDPOINTS_MAPPING = {
CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK: 'change-view-integration', CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK: 'change-view-integration',
CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK: 'change-metadata-item', CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK: 'change-metadata-item',
@ -37,6 +41,7 @@
const PLUGIN_LOADING_TIMEOUT_MS = 10000; const PLUGIN_LOADING_TIMEOUT_MS = 10000;
let _restAPI; let _restAPI;
const getRestAPI = () => { const getRestAPI = () => {
if (!_restAPI) { if (!_restAPI) {
_restAPI = document.createElement('gr-rest-api-interface'); _restAPI = document.createElement('gr-rest-api-interface');
@ -44,6 +49,14 @@
return _restAPI; return _restAPI;
}; };
let _reporting;
const getReporting = () => {
if (!_reporting) {
_reporting = document.createElement('gr-reporting');
}
return _reporting;
};
// TODO (viktard): deprecate in favor of GrPluginRestApi. // TODO (viktard): deprecate in favor of GrPluginRestApi.
function send(method, url, opt_callback, opt_payload) { function send(method, url, opt_callback, opt_payload) {
return getRestAPI().send(method, url, opt_payload).then(response => { return getRestAPI().send(method, url, opt_payload).then(response => {
@ -420,9 +433,13 @@
if (!app) { if (!app) {
// No gr-app found (running tests) // No gr-app found (running tests)
Gerrit._resetPlugins = () => { Gerrit._resetPlugins = () => {
_resolveAllPluginsLoaded = null;
_allPluginsPromise = null; _allPluginsPromise = null;
Gerrit._setPluginsPending([]); _pluginsInstalled = [];
_pluginsPending = {};
_pluginsPendingCount = -1;
_reporting = null;
_resolveAllPluginsLoaded = null;
_restAPI = null;
Gerrit._endpoints = new GrPluginEndpoints(); Gerrit._endpoints = new GrPluginEndpoints();
for (const k of Object.keys(_plugins)) { for (const k of Object.keys(_plugins)) {
delete _plugins[k]; delete _plugins[k];
@ -558,7 +575,8 @@
Gerrit._setPluginsPending = function(plugins) { Gerrit._setPluginsPending = function(plugins) {
_pluginsPending = plugins.reduce((o, url) => { _pluginsPending = plugins.reduce((o, url) => {
o[getPluginNameFromUrl(url)] = url; // TODO(viktard): Remove guard (@see Issue 8962)
o[getPluginNameFromUrl(url) || UNKNOWN_PLUGIN] = url;
return o; return o;
}, {}); }, {});
Gerrit._setPluginsCount(Object.keys(_pluginsPending).length); Gerrit._setPluginsCount(Object.keys(_pluginsPending).length);
@ -567,7 +585,7 @@
Gerrit._setPluginsCount = function(count) { Gerrit._setPluginsCount = function(count) {
_pluginsPendingCount = count; _pluginsPendingCount = count;
if (Gerrit._arePluginsLoaded()) { if (Gerrit._arePluginsLoaded()) {
document.createElement('gr-reporting').pluginsLoaded(); getReporting().pluginsLoaded(_pluginsInstalled);
if (_resolveAllPluginsLoaded) { if (_resolveAllPluginsLoaded) {
_resolveAllPluginsLoaded(); _resolveAllPluginsLoaded();
} }
@ -585,17 +603,14 @@
}; };
Gerrit._pluginInstalled = function(url) { Gerrit._pluginInstalled = function(url) {
const name = getPluginNameFromUrl(url); const name = getPluginNameFromUrl(url) || UNKNOWN_PLUGIN;
if (name && !_pluginsPending[name]) { if (!_pluginsPending[name]) {
console.warn(`Unexpected plugin from ${url}!`); console.warn(`Unexpected plugin ${name} installed from ${url}.`);
} else { } else {
if (name) { delete _pluginsPending[name];
delete _pluginsPending[name]; _pluginsInstalled.push(name);
console.log(`Plugin ${name} installed`);
} else {
console.log(`Plugin installed from ${url}`);
}
Gerrit._setPluginsCount(_pluginsPendingCount - 1); Gerrit._setPluginsCount(_pluginsPendingCount - 1);
console.log(`Plugin ${name} installed.`);
} }
}; };