Skip to content

Commit

Permalink
Add usage stats gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Nov 22, 2018
1 parent 90d1428 commit 4a87fa6
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 88 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
--ui-highlight highlight words or lines if defined string found in logs, default preset
--ui-highlight-preset <path> custom preset for highlighting (see ./preset/default.json)
--path <path> prefix path for the running application, default /
--disable-usage-stats disable gathering usage statistics

Web interface runs on **http://127.0.0.1:[port]**.

Expand Down Expand Up @@ -121,3 +122,10 @@ http {
}
}
```

### Usage statistics

`frontail` by default (from `v4.5.0`) gathers __anonymous__ usage statistics in Google Analytics. It can be disabled with
`--disable-usage-stats`.

The data is used to help me understand how `frontail` is used and I can make it better.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const connectBuilder = require('./lib/connect_builder');
const program = require('./lib/options_parser');
const serverBuilder = require('./lib/server_builder');
const daemonize = require('./lib/daemonize');
const usageStats = require('./lib/stats');

/**
* Parse args
Expand All @@ -22,6 +23,13 @@ if (program.args.length === 0) {
process.exit();
}

/**
* Init usage statistics
*/
const stats = usageStats(!program.disableUsageStats);
stats.track('runtime', 'init');
stats.time('runtime', 'runtime');

/**
* Validate params
*/
Expand Down Expand Up @@ -143,10 +151,13 @@ if (program.daemonize) {
filesSocket.emit('line', line);
});

stats.track('runtime', 'started');

/**
* Handle signals
*/
const cleanExit = () => {
stats.timeEnd('runtime', 'runtime');
process.exit();
};
process.on('SIGINT', cleanExit);
Expand Down
3 changes: 2 additions & 1 deletion lib/options_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ program
.option(
'--ui-highlight-preset <path>',
'custom preset for highlighting (see ./preset/default.json)'
);
)
.option('--disable-usage-stats', 'disable gathering usage statistics');

module.exports = program;
65 changes: 65 additions & 0 deletions lib/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const ua = require('universal-analytics');
const isDocker = require('is-docker');
const Configstore = require('configstore');
const uuidv4 = require('uuid/v4');
const pkg = require('../package.json');

const trackingID = 'UA-129582046-1';

// Usage stats
function Stats(enabled) {
this.timer = {};

if (enabled === true) {
const config = new Configstore(pkg.name);
let clientID = uuidv4();
if (config.has('clientID')) {
clientID = config.get('clientID');
} else {
config.set('clientID', clientID);
}

const tracker = ua(trackingID, clientID);
tracker.set('aip', 1); // Anonymize IP
tracker.set('an', 'frontail'); // Application Name
tracker.set('av', pkg.version); // Application Version
tracker.set('ds', 'app'); // Data Source
tracker.set('cd1', process.platform); // platform
tracker.set('cd2', process.arch); // arch
tracker.set('cd3', process.version.match(/^v(\d+\.\d+)/)[1]); // Node version
tracker.set('cd4', isDocker()); // is Docker
this.tracker = tracker;
}

return this;
}

Stats.prototype.track = function track(category, action) {
if (!this.tracker) {
return;
}
this.tracker.event(category, action).send();
};

Stats.prototype.time = function time(category, action) {
if (!this.tracker) {
return;
}

if (!this.timer[category]) {
this.timer[category] = {};
}
this.timer[category][action] = Date.now();
};

Stats.prototype.timeEnd = function timeEnd(category, action) {
if (!this.tracker) {
return;
}

this.tracker.timing(category, action, Date.now() - this.timer[category][action]).send();
};

module.exports = enabled => new Stats(enabled);
Loading

0 comments on commit 4a87fa6

Please sign in to comment.