Permalink
Browse files

#61 stats_auto: Automatic stats upload

  • Loading branch information...
1 parent 9c3965a commit 21d731fe2dc99393463cf394cc1487da8c84db4b @azproduction committed Dec 5, 2012
View
@@ -99,3 +99,4 @@
- `log` and `warn` are true by default
- global check of lazy modules is deisabled
- info of unused modules, suspicious globals and off-package modules paths
+ - `"stats_auto": true` automatic statistics push
View
@@ -198,9 +198,24 @@ LmdBuilder.prototype.closeStreams = function () {
* @param {Object} data
*/
LmdBuilder.prototype.template = function (data) {
+ var options;
+
+ if (data.version || data.stats_host) {
+ options = {};
+ if (data.version) {
+ options.version = data.version;
+ }
+
+ if (data.stats_host) {
+ options.stats_host = data.stats_host;
+ }
+
+ options = JSON.stringify(options);
+ }
+
return data.lmd_js + '(' + data.global + ',' + data.lmd_main + ',' + data.lmd_modules + ',' + data.modules_options +
// if version passed
- (data.version ? ',' + data.version : '') +
+ (options ? ',' + options : '') +
')';
};
@@ -629,7 +644,8 @@ LmdBuilder.prototype.render = function (config, lmd_modules, lmd_main, pack, mod
lmd_modules: lmd_modules,
modules_options: JSON.stringify(modules_options),
// if version passed -> module will be cached
- version: config.cache ? JSON.stringify(config.version) : false
+ version: config.cache ? config.version : false,
+ stats_host: config.stats_auto || false
});
return result;
@@ -57,12 +57,15 @@ require.stats.sendTo('http://localhost:8081'); // you may specify report_name to
### Code coverage
- - Flag: `stats`, `stats_coverage`, `stats_sendto`, `stats_coverage_async`
+ - Flag: `stats`, `stats_coverage`, `stats_sendto`, `stats_coverage_async`, `stats_auto`
Add `stats_coverage` flag to your config file or use list of module names to cover only them. Rebuild your package.
Now you can see coverage report in `require.stats()` object. See [src/plugin/stats.js#L46](/azproduction/lmd/blob/master/src/plugin/stats.js#L46) for more information.
You can enable `stats_sendto` flag to push your reports to the Stats Server.
+You can set `stats_auto` flag/property to enable automatic push your reports to specified Stats Server.
+`stats_auto: true` will automatically send your reports to `'http://' + location.hostname + ':8081'`
+you may also define your own stats host url (eg `stats_auto: "http://yourhost:12345"`)
You may also enable `stats_coverage_async` to profile all your async modules without processing them on server. All async modules
will be parsed and processed on client.
View
@@ -105,6 +105,7 @@ var sandbox = {
lmd: lmd, // lmd function itself if $P.CACHE
main: main, // main module code if $P.CACHE
version: version, // module version if $P.CACHE
+ stats_host: stats_host, // stats host if $P.STATS_AUTO
coverage_options: coverage_options, // ... if $P.STATS_COVERAGE
on: lmd_on, // lmd_on
View
@@ -115,7 +115,9 @@
"stats": true,
"stats_coverage": true,
"stats_coverage_async": true,
- "stats_sendto": true
+ "stats_sendto": true,
+ "stats_auto": true // stats host url (eg "http://yourhost:port") or true;
+ // true=default url -> 'http://' + location.hostname + ':8081'
}
```
@@ -30,6 +30,7 @@
"stats_coverage": true,
"stats_coverage_async": false,
"stats_sendto": true,
+ "stats_auto": true,
"warn": true,
"log": true
Oops, something went wrong.
View
@@ -5,7 +5,7 @@
"name": "Mikhail Davydov",
"email": "azazel.private@gmail.com"
},
- "version": "1.9.7",
+ "version": "1.9.8",
"bin": {
"lmd": "./bin/lmd"
},
View
@@ -1,10 +1,12 @@
-(function /*if ($P.CACHE) {*/lmd/*}*/(global, main, modules, modules_options/*if ($P.CACHE) {*/, version/*}*/) {
+(function /*if ($P.CACHE) {*/lmd/*}*/(global, main, modules, modules_options/*if ($P.CACHE || $P.STATS_AUTO) {*/, options/*}*/) {
var initialized_modules = {},
global_eval = function (code) {
return global.Function('return ' + code)();
},
/*if ($P.CSS || $P.JS || $P.ASYNC) {*/global_noop = function () {},/*}*/
/*if ($P.CSS || $P.JS || $P.STATS_SENDTO) {*/global_document = global.document,/*}*/
+ /*if ($P.CACHE) {*/version = options.version,/*}*/
+ /*if ($P.STATS_AUTO) {*/stats_host = options.stats_host,/*}*/
local_undefined,
/**
* @param {String} moduleName module name or path to file
@@ -130,6 +132,7 @@
/*if ($P.CACHE) {*/lmd: lmd,/*}*/
/*if ($P.CACHE) {*/main: main,/*}*/
/*if ($P.CACHE) {*/version: version,/*}*/
+ /*if ($P.STATS_AUTO) {*/stats_host: stats_host,/*}*/
on: lmd_on,
trigger: lmd_trigger,
View
@@ -15,7 +15,9 @@
* - if no cache - it loads fallback script
*/
(function (global, document, scriptId, prefix, getAttribute, removeItem) {
- var globalEval = global.eval,
+ var globalEval = function (code) {
+ return global.Function('return ' + code)();
+ },
scriptElement = document.getElementById(scriptId),
actualVersion = scriptElement[getAttribute](prefix + 'version'),
storageKey = scriptElement[getAttribute](prefix + 'key'),
@@ -55,7 +57,7 @@
} catch (e) {}
if (lmd && main) {
// do not catch module's errors
- lmd(global, main, json.modules, json.options);
+ lmd(global, main, json.modules, json.options, {stats_host: json.host});
return;
}
// if error or version do not match - wipe cache
View
@@ -4,7 +4,7 @@
"require": "async.js"
},
"cache": {
- "preprocess": ["$P.CACHE"],
+ "preprocess": ["$P.CACHE", "$P.CACHE || $P.STATS_AUTO"],
"require": "cache.js"
},
"js": {
@@ -62,6 +62,10 @@
"preprocess": ["$P.STATS_SENDTO", "$P.CSS || $P.JS || $P.STATS_SENDTO"],
"require": "stats_sendto.js"
},
+ "stats_auto": {
+ "preprocess": ["$P.STATS_AUTO", "$P.CACHE || $P.STATS_AUTO"],
+ "require": "stats_auto.js"
+ },
"async_plain": {
"preprocess": ["$P.ASYNC_PLAIN", "$P.ASYNC_PLAIN || $P.ASYNC_PLAINONLY"],
"require": "async_plain.js"
View
@@ -15,6 +15,7 @@
try {
sb.global.localStorage['lmd'] = sb.global.JSON.stringify({
version: sb.version,
+ /*if ($P.STATS_AUTO) {*/host: sb.stats_host,/*}*/
modules: sb.modules,
// main module function
main: '(' + sb.main + ')',
View
@@ -0,0 +1,36 @@
+/**
+ * Coverage for off-package LMD modules
+ *
+ * Flag "stats_auto"
+ *
+ * This plugin depends on stats and stats_sendto
+ */
+/**
+ * @name sandbox
+ */
+(function (sb) {
+ var timeout,
+ host = sb.stats_host;
+
+ if (!host) return;
+
+ if (host === true) {
+ host = 'http://' + sb.global.location.hostname + ':8081';
+ }
+
+ function updateStats() {
+ if (timeout) return;
+
+ timeout = setTimeout(function () {
+ sb.require.stats.sendTo(host);
+ timeout = 0;
+ }, 2000);
+ }
+
+ var events = 'click0keyup0orientationchange0resize0scroll0focus'.split(0);
+ for (var i = 0, c = events.length; i < c; i++) {
+ sb.global.document.body.addEventListener(events[i], updateStats, false);
+ }
+
+ sb.on('lmd-require:before-check', updateStats);
+}(sandbox));

0 comments on commit 21d731f

Please sign in to comment.