Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Added url module to parse URLs, added list of hostnames with most t…
Browse files Browse the repository at this point in the history
…raffic from Varnish log
  • Loading branch information
rangoo94 committed Mar 23, 2015
1 parent fa356a0 commit 36b5d13
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
</nav>
<div class="box-inner">
<section id="log" class="tab current">
<h2>5 hostnames with most traffic</h2>
<div id="varnish-most-traffic"></div>
<h2>5 most requested files</h2>
<div id="varnish-most-requested"></div>
<h2>Full log grouped by remote host</h2>
Expand Down
40 changes: 40 additions & 0 deletions src/js/modules/log/varnish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var log = require('../log'),
url = require('../url'),
VarnishGrid = require('../varnish-grid'),
COLS = [ 'remote', 'date', 'method', 'url', 'http', 'status', 'size', 'referer', 'userAgent' ],
FORMAT = '^([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) - - ' +
Expand Down Expand Up @@ -29,6 +30,7 @@
*/
VarnishLog.prototype.transform = function(row) {
row.date = new Date(row.date.replace(':', ' '));
row.size = +row.size;
return row;
};

Expand Down Expand Up @@ -123,5 +125,43 @@
return list;
};

/**
* Find specified number of hostnames with most traffic
*
* @param {Number} limit
* @returns {Object[]}
*/
VarnishLog.prototype.findHostnamesWithMostTraffic = function(limit) {
var map = {},
list = [],
hostname,
idx;

for (idx = 0; idx < this.data.length; idx++) {
hostname = url.getHostname(this.data[idx].url);
if (!map[hostname]) {
map[hostname] = 0;
}

map[hostname] += this.data[idx].size;
}

for (hostname in map) {
if (map.hasOwnProperty(hostname)) {
for (idx = 0; idx < limit; idx++) {
if (list[idx] === void 0 || list[idx].traffic < map[hostname]) {
list[idx] = {
traffic: map[hostname],
url: hostname
};
break;
}
}
}
}

return list;
};

module.exports = VarnishLog;
}());
18 changes: 18 additions & 0 deletions src/js/modules/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function() {
'use strict';

module.exports = {
/**
* Get hostname by specified URL
*
* @param {String} url
* @returns {String}
*/
getHostname: function(url) {
var a = document.createElement('a');
a.href = url;

return a.hostname;
}
}
}());
12 changes: 12 additions & 0 deletions src/js/views/varnish.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
var log = new VarnishLog(data),
list;

// Prepare list of hostnames with most traffic
list = log.findHostnamesWithMostTraffic(5);
document.getElementById('varnish-most-traffic').appendChild(
buildList(list, function(el, item) {
var traffic = document.createElement('span');
traffic.innerText = item.traffic + ' bytes';

el.innerText = item.url;
el.appendChild(traffic);
})
);

// Prepare list of most requested files
list = log.findMostRequestedFiles(5);
document.getElementById('varnish-most-requested').appendChild(
Expand Down
5 changes: 5 additions & 0 deletions test/spec/modules/url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function() {
'use strict';

var url = require('../../../src/js/modules/url');
}());

0 comments on commit 36b5d13

Please sign in to comment.