var totalCan = $("#cycleBar");
var speedCan = $('#cycleSpeed');
var tflagCan = $('#tPostsByFlag');
var tflagPieCan = $('#tPostsByFlagPie');
var tNmaesCan = $('#tNmaes');
var tImagesCan = $('#tImages');
var tThreadsCan = $('#tThreads');
var tIdsCan= $('#tIds');
var tSubjectsCan = $('#tSubjects');
var tsflagCan = $('#tsPostsByFlag');
var tsflagPieCan = $('#tsPostsByFlagPie');
var tsNmaesCan = $('#tsNmaes');
var tsImagesCan = $('#tsImages');
var tsThreadsCan = $('#tsThreads');
var tsIdsCan= $('#tsIds');
var tsSubjectsCan = $('#tsSubjects');
var tsCycleSpeedCan = $('#tsCycleSpeed');
//var tsCycleAverageCan = $('#tsCycleAverage');
var tAgCandleCan = $('#tAgCandle');
const maxEntries= client.preloadCycles;
function setStatus(str)
{
$('#status').text(str);
}
function lerpRGB(a,b,t)
{
return {
r: a.r + (b.r - a.r) * t,
g: a.g + (b.g - a.g) * t,
b: a.b + (b.b - a.b) * t
};
}
function add_new_candle(charts, data,update=true,close=null)
{
//charts.agcandle.data.labels.push(fixtime2(new Date(data.timestamp)));
if(!data._candle.number.t)
data._candle.number.t = new Date(data.timestamp);
if(close!==null)
data._candle.number.o = close;
charts.agcandle.data.datasets[0].data.push(data._candle.number);
if(update) charts.agcandle.update();
}
function createNewCandle(charts, close)
{
if(!!charts.agcandle.data.datasets[0].data[charts.agcandle.data.datasets[0].data.length-1])
charts.agcandle.data.datasets[0].data[charts.agcandle.data.datasets[0].data.length-1].c = close;
charts.agcandle.data.datasets[0].data.push({o: close, h:close, l:close, c:close, t:new Date()});
charts.agcandle.update();
}
function total_add_data(chart, label, data, update=true){
//chart.addData(value, label);
if(chart.data.labels.length>=maxEntries)
{
chart.data.labels.shift();
chart.data.datasets[0].data.shift();
chart.data.datasets[1].data.shift();
chart.data.datasets[2].data.shift();
chart.data.datasets[3].data.shift();
}
chart.data.labels.push(label);
chart.data.datasets[0].data.push(data.number);
chart.data.datasets[1].data.push(data.names);
chart.data.datasets[2].data.push(data.trips);
chart.data.datasets[3].data.push(data.nametrips);
if(update)
chart.update();
}
function fixtime(now){
var
h= now.getHours(),
m= now.getMinutes(),
s= now.getSeconds();
return h+":"+m+":"+s;
}
function fixtime2(now){
var
y = now.getFullYear(),
M = now.getMonth(),
d = now.getUTCDate(),
h= now.getHours(),
m= now.getMinutes(),
s= now.getSeconds();
return y+"."+(M+1)+"."+d+" "+h+":"+m+":"+s;
}
function grapher(data, charts, update=true) {
//cycle graph
total_add_data(charts.total, fixtime(new Date(data.timestamp)), data,update);
}
function updateSection(gr, pfx, allTime, allTimeAverage,data=null)
{
var share = flagShare(allTime.number, allTime.countries);
$('#'+pfx+'last').text(allTime.last);
$('#'+pfx+'posts').text(allTime.number);
$('#'+pfx+'flagshare').text(lcStr(flagShareStr(share),40));
$('#'+pfx+'avg').text(allTimeAverage.number.toFixed(2));
$('#'+pfx+'upcycle').text(allTimeAverage._n);
$('#'+pfx+'flagshare_container > ul').empty();
share.sort(function (a, b) {
return a.total < b.total ? 1 : -1;
});
gr.aflag.data.labels.length =
gr.aflag.data.datasets[0].data.length = 0;
gr.aflagpie.data.labels.length =
gr.aflagpie.data.datasets[0].data.length =
gr.aflagpie.data.datasets[0].backgroundColor.length = 0;
var cole = {r: 0, g: 0,b: 255};
var cols = {r: 255, g: 128, b: 128};
var colstr = function(x) {
return "rgba("+x.r+","+x.g+","+x.b+", 1.0)";
};
for(var i in share) {
$('#'+pfx+'flagshare_container > ul').append("
"+share[i]["key"]+": "+(share[i]["val"]*100.00).toFixed(2)+"% ("+share[i]["total"]+")");
gr.aflag.data.labels.push(share[i]["key"]);
gr.aflag.data.datasets[0].data.push(share[i]["total"]);
gr.aflagpie.data.labels.push(share[i]["key"]);
gr.aflagpie.data.datasets[0].data.push(share[i]["total"]);
gr.aflagpie.data.datasets[0].backgroundColor.push(colstr(lerpRGB(cols,cole,(i/share.length))));
}
gr.anamepie.data.datasets[0].data[0] = allTime.number-(allTime.names+allTime.trips+allTime.nametrips);
gr.anamepie.data.datasets[0].data[1] = allTime.names;
gr.anamepie.data.datasets[0].data[2] = allTime.trips;
gr.anamepie.data.datasets[0].data[3] = allTime.nametrips;
gr.aimagepie.data.datasets[0].data[0] = allTime.images;
gr.aimagepie.data.datasets[0].data[1] = allTime.number-allTime.images;
gr.athreadpie.data.datasets[0].data[0] = allTime.threads;
gr.athreadpie.data.datasets[0].data[1] = allTime.number-allTime.threads;
gr.aidspie.data.datasets[0].data[0] = allTime.ids;
gr.aidspie.data.datasets[0].data[1] = allTime.number-allTime.ids;
gr.asubjectspie.data.datasets[0].data[0] = allTime.subjects;
gr.asubjectspie.data.datasets[0].data[1] = allTime.threads-allTime.subjects;
gr.aflag.update();
gr.aflagpie.update();
gr.anamepie.update();
gr.aimagepie.update();
gr.athreadpie.update();
gr.aidspie.update();
gr.asubjectspie.update();
}
function updateAllTime(gr,data=null)
{
updateSection(gr, "at_", client.allTime, client.allTimeAverage,data);
}
function flagShare(total, ctr)
{
var ret = [];
for(var key in ctr)
{
ret.push({key: key, val: ctr[key]/total, total: ctr[key]});
}
return ret;
}
function lcStr(str, mx)
{
if(str.length>mx)
return str.slice(0, mx)+"...";
else return str;
}
function flagShareStr(share)
{
share.sort(function (a, b) {
return a.total < b.total ? 1 : -1;
});
var strs= [];
for(var shr in share)
{
shr = share[shr];
strs.push(shr["key"]+": "+(shr["val"]*100.00).toFixed(2)+"%");
}
return strs.join(", ");
}
//update session
function graphCycle(data, charts,update=true)
{
$('#cytb > tbody > tr:first').before(""+
""+fixtime(new Date(data.timestamp))+(!update?" (preload)":"")+" | "+
""+data.last+" | "+
""+data.number+" | "+
""+((data.number/client.allTimeAverage.number)*100.00).toFixed(2)+"% "+(update&&(client.sessionAllTimeAverage.number>0)?(((data.number/client.sessionAllTimeAverage.number)*100.00).toFixed(2)+"% (session)"):"")+" | "+
""+flagShareStr(flagShare(data.number, data.countries))+" | "+
""+data.names+" | "+
""+data.trips+" | "+
""+data.nametrips+" | "+
"
");
grapher(data, charts,update);
updateSection(charts, "s_", client.sessionAllTime, client.sessionAllTimeAverage);
//All time average comparison
if(charts.speed.data.labels.length>=maxEntries){
charts.speed.data.labels.shift();
charts.speed.data.datasets[0].data.shift();
charts.speed.data.datasets[1].data.shift();
charts.speed.data.datasets[2].data.shift();
}
charts.speed.data.labels.push(fixtime(new Date(data.timestamp)));
charts.speed.data.datasets[0].data.push( ((data.number/client.allTimeAverage.number)*100.00).toFixed(2));
charts.speed.data.datasets[1].data.push( data.number);
charts.speed.data.datasets[2].data.push( client.allTimeAverage.number);
if(update) {
charts.speed.update();
charts.aspeed.data.labels.push(fixtime(new Date(data.timestamp)));
charts.aspeed.data.datasets[0].data.push( ((data.number/client.sessionAllTimeAverage.number)*100.00).toFixed(2));
charts.aspeed.data.datasets[1].data.push( data.number);
charts.aspeed.data.datasets[2].data.push( client.sessionAllTimeAverage.number );
charts.aspeed.update();
}
//Candle
var lc = charts.agcandle.data.datasets[0].data[charts.agcandle.data.datasets[0].data.length-1];
if(lc) {
lc.c = data.number;
if(data.number>lc.h)
lc.h = data.number;
if(data.number0?client.superBuffer[i-1]._candle.number.c:null));
}
/*if(client.superBuffer.length<1)
{
add_new_candle(g_chartsSuper(), {_candle:{number:{o:0,c:0,h:0,l:0,t:new Date()}}}, false);
}*/
//add last x from buffer
for(var i in client.startupLast5) {
graphCycle(client.startupLast5[i], g_charts(),false);
}
chartTotal.update();
chartSpeed.update();
chartAGCandle.update();
$('#at_flagshare_exp').on("click", function() {
if($('#at_flagshare_container').is(":hidden")) {
$('#at_flagshare_container').show(1000, function() {
$('#at_flagshare_exp').text("-");
});
} else {
$('#at_flagshare_container').hide(1000, function() {
$('#at_flagshare_exp').text("+");
});
}
});
$('#s_flagshare_exp').on("click", function() {
if($('#s_flagshare_container').is(":hidden")) {
$('#s_flagshare_container').show(1000, function() {
$('#s_flagshare_exp').text("-");
});
} else {
$('#s_flagshare_container').hide(1000, function() {
$('#s_flagshare_exp').text("+");
});
}
});
updateAllTime(g_charts());
setStatus("Synced");
}, function(err) {
setStatus("Connection failed");
console.log("Error: "+err.toString());
client.stop();
});
}