Skip to content

Commit

Permalink
Update to new backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cramforce committed Sep 30, 2011
1 parent f0c2eaa commit 4768963
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 56 deletions.
50 changes: 22 additions & 28 deletions public/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

(function() {

var Feed_Url_Prefix = 'http://127.0.0.1:5984';
var Feed_Url_Prefix = 'http://192.168.1.24:2911';

var options = {
xaxis: { mode: 'time' }
Expand Down Expand Up @@ -31,9 +31,13 @@

function fetch() {
$.getJSON(self.url, function(data) {
self.data = data.rows.map(function(row) {
return [row.key[1], row.value];
});
self.data = [];
function aggregate(row) {
return [row[0], row[1] + row[2]];
}
self.longTerm = data.longTerm.map(aggregate);
self.shortTerm = data.shortTerm.map(aggregate);
console.log(self.data);
});
}

Expand All @@ -56,22 +60,18 @@
return item.active;
});

$.plot(longTermPlaceholder, active, options);

var shortTermData = active.map(function(feed) { return feed.shortTermCopy(); })

$.plot(shortTermPlaceholder, shortTermData, options);
$.plot(longTermPlaceholder, active.map(function(feed) { return { data: feed.longTerm } }), options);
$.plot(shortTermPlaceholder, active.map(function(feed) { return { data: feed.shortTerm } }), options);
}

function addFeed(name, key) {

var url = '/traffic/_design/total_traffic/_view/10minutes?group=true&startkey=[%22' + encodeURIComponent(key) + '%22]&endkey=[%22' + encodeURIComponent(key) + '.%22]&callback=?';
function addFeed(name, screenName) {
var url = '/traffic/total' + (screenName ? '/'+encodeURIComponent(screenName) : '') + '?callback=?';

var feed = new Feed(name, Feed_Url_Prefix + url, draw);
Data.push(feed);
feed.attach();

var id = 'check-' + key;
var id = 'check-' + name;
var ul = $('#followList');
var li = $('<li />');
var checkbox = $('<input />').attr({
Expand All @@ -93,12 +93,11 @@
}
setInterval(draw, 500);

addFeed('All Inbound', 'in-N');
addFeed('All Outbound', 'out-N');
addFeed('All');

function addNamedFeed(name) {
addFeed('In ' + name, 'in-' + name);
addFeed('Out ' + name, 'out-' + name);
addFeed('In ' + name, name);
//addFeed('Out ' + name, 'out-' + name);
}

$('#addUser').click(function() {
Expand All @@ -124,21 +123,17 @@
}
}

function drawRanking(type) {
function drawRanking() {

var All_Time_Url = Feed_Url_Prefix +
'/traffic/_design/total_traffic/_view/10minutes?group=true&startkey=["' + type + '"]&endkey=["' + type + 'X"]&callback=?';
'/traffic/top10?count=500&callback=?';

$.getJSON(All_Time_Url, function(data) {
var rows = data.rows.sort(function(a, b) {
return b.value - a.value;
});

$.getJSON(All_Time_Url, function(rows) {
var html = rows.map(function(row) {
return '<tr><td class="name">' + row.key[0].replace(type, '') + '</td><td>' + formatBytes(row.value) + '</td></tr>';
return '<tr><td class="name">' + row.screenName + '</td><td>' + formatBytes(row.inTotal + row.outTotal) + '</td></tr>';
}).join('\n');

$('#' + type + 'traffic tbody').html(html);
$('#user-traffic tbody').html(html);
});

};
Expand All @@ -151,8 +146,7 @@

$(function() {
function d() {
drawRanking('totalout-');
drawRanking('totalin-');
drawRanking();
}
setInterval(d, 10000);
d();
Expand Down
14 changes: 2 additions & 12 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,10 @@ <h3>Following</h3>
<section>
<div id="totals">
<h2>Total Traffic</h2>
<table id="totalout-traffic" class="statTable">
<table id="user-traffic" class="statTable">
<thead>
<tr>
<th>User</th><th>Total Outbound</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<table id="totalin-traffic" class="statTable">
<thead>
<tr>
<th>User</th><th>Total Inbound</th>
<th>User</th><th>Total</th>
</tr>
</thead>
<tbody>
Expand Down
73 changes: 57 additions & 16 deletions queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ function(doc) {

var timeRound = 10 * 60 * 1000;

var tp = doc._id.match(/(\d+)-(\d+)-(\d+)\.(\d\d)(\d\d)(\d\d)/);

var date = new Date(tp[1], parseInt(tp[2])-1, tp[3], tp[4], tp[5], tp[6]);

var time = date.getTime();

time = time - time % timeRound;



for(var i = 0, len = doc.tuples.length; i < len; i++) {
var tuple = doc.tuples[i];
if (!tuple.srcIP || !tuple.dstIP) continue;

var tp = doc._id.match(/(\d+)-(\d+)-(\d+)\.(\d\d)(\d\d)(\d\d)/);


if(tp) {

var date = new Date(tp[1], parseInt(tp[2])-1, tp[3], tp[4], tp[5], tp[6]);

var time = date.getTime();

time = time - time % timeRound;

var val = parseInt(tuple.octets, 10);

var key;
Expand All @@ -37,11 +38,51 @@ function(doc) {



function(keys, values, rereduce) {
var total = 0
for(var i = 0, len = values.length; i < len; i++) {
var octets = values[i]
total += parseInt(octets, 10);
function(doc) {

var timeRound = 10 * 60 * 1000;

var tp = doc._id.match(/(\d+)-(\d+)-(\d+)\.(\d\d)(\d\d)(\d\d)/);

if(!tp) {
return;
}

var date = new Date(tp[1], parseInt(tp[2])-1, tp[3], tp[4], tp[5], tp[6]);

var time = date.getTime();

time = time - time % timeRound;

var group = {};

for(var i = 0, len = doc.tuples.length; i < len; i++) {
var tuple = doc.tuples[i];
if (!tuple.srcIP || !tuple.dstIP) continue;

var val = parseInt(tuple.octets, 10);

var key;
if (tuple.srcIP.indexOf('.') == -1) { // No IP :)
key = 'out-' + tuple.srcIP;
}
else if (tuple.dstIP.indexOf('.') == -1) { // No IP :)
key = 'in-' + tuple.dstIP;
}

if(key) {
if(!group[key]) {
group[key] = val;
} else {
group[key] += val;
}
}
}

for(var key in group) {
var val = group[key];
emit(['total' + key], val);
emit([key, time], val);
emit([key.replace(/-.*$/, '-N'), time], val);
}
return total;
}

0 comments on commit 4768963

Please sign in to comment.