diff options
author | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-04-08 19:13:45 +0100 |
---|---|---|
committer | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-04-08 19:33:44 +0100 |
commit | 53de8bcf7e1288fceb84f62bb2b603cf8b182fbd (patch) | |
tree | fa01ce94c5ce05467148d992402beedf2bd7b833 | |
parent | dfa264df606e3943026c333c341c05d3d066ab24 (diff) | |
download | gitlab-ce-add-promjs.tar.gz |
Added prompusheradd-promjs
-rw-r--r-- | app/assets/javascripts/main.js | 16 | ||||
-rw-r--r-- | app/assets/javascripts/prom/prom_metrics.js | 11 | ||||
-rw-r--r-- | app/assets/javascripts/prom/prom_pusher.js | 58 | ||||
-rw-r--r-- | app/assets/javascripts/right_sidebar.js | 3 |
4 files changed, 88 insertions, 0 deletions
diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index 3f92d4ee6cf..27776290f6e 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -11,6 +11,8 @@ import Pikaday from 'pikaday'; import Dropzone from 'dropzone'; import Sortable from 'vendor/Sortable'; +import PromPusher from './prom/prom_pusher'; + // libraries with import side-effects import 'mousetrap'; import 'mousetrap/plugins/pause/mousetrap-pause'; @@ -181,6 +183,11 @@ import './visibility_select'; import './wikis'; import './zen_mode'; +gon.gateway_endpoint = 'https://gatewayEndpoint'; // TEMPORARY +PromPusher.init({ + gatewayEndpoint: gon.gateway_endpoint, +}); + // eslint-disable-next-line global-require if (process.env.NODE_ENV !== 'production') require('./test_utils/'); @@ -288,7 +295,15 @@ $(function () { return buttons.enable(); } }); + + $(document).ajaxSend(function (e, xhrObj, options) { + const xhr = xhrObj; + xhr.url = options.url; + }); + $(document).ajaxError(function (e, xhrObj) { + if (xhrObj.url === gon.gateway_endpoint) return; + var ref = xhrObj.status; if (xhrObj.status === 401) { return new Flash('You need to be logged in.', 'alert'); @@ -296,6 +311,7 @@ $(function () { return new Flash('Something went wrong on our end.', 'alert'); } }); + $('.account-box').hover(function () { // Show/Hide the profile menu when hovering the account box return $(this).toggleClass('hover'); diff --git a/app/assets/javascripts/prom/prom_metrics.js b/app/assets/javascripts/prom/prom_metrics.js new file mode 100644 index 00000000000..0090ccfff63 --- /dev/null +++ b/app/assets/javascripts/prom/prom_metrics.js @@ -0,0 +1,11 @@ +import PromPusher from './prom_pusher'; + +const PromMetrics = { + rightSidebar: { + toggle() { + PromPusher.getMetric('counter', 'toggle_sidebar', 'Counts right sidebar toggles').inc(); + }, + }, +}; + +export default PromMetrics; diff --git a/app/assets/javascripts/prom/prom_pusher.js b/app/assets/javascripts/prom/prom_pusher.js new file mode 100644 index 00000000000..195d95f8478 --- /dev/null +++ b/app/assets/javascripts/prom/prom_pusher.js @@ -0,0 +1,58 @@ +import prom from 'promjs'; + +const PromPusher = { + init(opts = {}) { + this.pushInterval = opts.interval || 15000; + this.gatewayEndpoint = opts.gatewayEndpoint; + + this.registry = prom(); + + this.startInterval(); + }, + + startInterval() { + if (this.timeout) clearTimeout(this.timeout); + + const onSuccess = this.onSuccess.bind(this); + const onError = this.onError.bind(this); + + this.timeout = setTimeout(() => { + this.pushMetrics() + .then(onSuccess, onError) + .catch(onError); + }, this.pushInterval); + }, + + pushMetrics() { + const promise = new Promise((resolve, reject) => { + const metrics = this.registry.metrics(); + + if (!metrics) return reject(new Error('No metrics available')); + + return $.post(this.gatewayEndpoint, metrics) + .then(resolve, reject) + .fail(reject); + }); + + return promise; + }, + + onSuccess() { + this.registry.clear(); + + this.startInterval(); + }, + + onError(error) { + // Handle error. + // Just let sentry catch? Not much we can "handle" here. + + this.startInterval(); + }, + + getMetric(type, name, help) { + return this.registry.get(name) || this.registry.create(type, name, help); + }, +}; + +export default PromPusher; diff --git a/app/assets/javascripts/right_sidebar.js b/app/assets/javascripts/right_sidebar.js index a9b3de281e1..da33d818729 100644 --- a/app/assets/javascripts/right_sidebar.js +++ b/app/assets/javascripts/right_sidebar.js @@ -1,6 +1,7 @@ /* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-unused-vars, consistent-return, one-var, one-var-declaration-per-line, quotes, prefer-template, object-shorthand, comma-dangle, no-else-return, no-param-reassign, max-len */ import Cookies from 'js-cookie'; +import PromMetrics from './prom/prom_metrics'; (function() { var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; }; @@ -32,6 +33,8 @@ import Cookies from 'js-cookie'; $(window).on('resize', () => throttledSetSidebarHeight()); $document.on('scroll', () => throttledSetSidebarHeight()); $document.on('click', '.js-sidebar-toggle', function(e, triggered) { + PromMetrics.rightSidebar.toggle(); + var $allGutterToggleIcons, $this, $thisIcon; e.preventDefault(); $this = $(this); |