summaryrefslogtreecommitdiff
path: root/src/viz/index.js
blob: e282026fcc95d8f799d0a71f17a14d2a25732063 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var qintv_ms = 5000;
var uripart = "/updates?";
var lastfrom = "";
var stuff_to_display = {"clients":"none","*":showvalue,"client-count":showvalue, "bytes-sent":showgauge, "fuckups-per-second":showasgraph};


$(document).ready(function()
{
  updateS5Stats();
  setInterval("updateS5Stats()", qintv_ms);
});

function getstreamdiv(sid) {
	var elem = $("#"+sid);
	if (elem.length<1) {
		$("#content").append('<div class="container" id="'+sid+'jump"><main><h2 class="dispstreamheader">Stream: '+sid+'</h2><ul id="'+sid+'" class="controls"></ul></main></div>');
		$("#menu").append('<a href="#'+sid+'jump">'+sid+'</a>&nbsp;');
		elem = $("#"+sid);
	}
	return elem;
}

function showvalue(sid, name, value, jsdate) {
	var divid = sid+"_"+name;
	var elem = $("#"+divid);
	if (elem.length<1) {
		getstreamdiv(sid).append('<li class="dispvalue size1"><div class="dispvalueheader">'+ name +'</div><div class="dispvaluevalue" id='+divid+'></div></li>');
		elem = $("#"+divid);
	}
	elem.html("<strong>"+value+"</strong><br/><span style='font-size:smaller;'>@"+jsdate.toLocaleDateString()+" "+jsdate.toLocaleTimeString()+"</span>");
}

var gauges = {};
function showgauge(sid, name, value) {
	var divid = sid+"_"+name;
	var elem = document.getElementById(divid);
	if (!elem) {
		getstreamdiv(sid).append('<li class="dispvalue quadsize"><div class="dispvalueheader">'+ name +'</div><div class="dispvaluevalue" id='+divid+'></div></li>');
		elem = document.getElementById(divid);
		gauges[divid] = new google.visualization.Gauge(elem);
	}
	var data = google.visualization.arrayToDataTable([["Label", "Value"],[name,value]]);
	gauges[divid].draw(data, {});
}

var graphs = {}
var graphdata = {}
function showasgraph(sid, name, value, jsdate) {
	var divid = sid+"_"+name;
	var elem = document.getElementById(divid);
	if (!elem) {
		getstreamdiv(sid).append('<li class="dispvalue graphsize"><div class="dispvalueheader">'+ name +'</div><div class="dispvaluevalue" id='+divid+'></div></li>');
		elem = document.getElementById(divid);
		graphdata[divid]=[];
		graphs[divid] = new Dygraph(elem,graphdata[divid],{legend:'none',title:sid+"-"+name,labels:["Date/Time",name]});
	}
    graphdata[divid].push([jsdate,value]);
    graphs[divid].updateOptions({'file':graphdata[divid]});
}

function graphS5Status(data) {
	$.each(data, function(index, stream) {
		var sid = stream["streamer-id"].format + '-' + stream["streamer-id"].quality;
		lastfrom = "from="+stream["start-time"];
		var jsdate = new Date(stream["start-time"]);
		qintv_ms = stream["duration-ms"]; //doesnt really change anything yet
		var data = stream.data;
		console.log(data);
		for (var key in data) {
			if (typeof(stuff_to_display[key]) == 'function') {
				stuff_to_display[key](sid, key, data[key], jsdate);
			} else if (! stuff_to_display[key]) {
				stuff_to_display['*'](sid, key, data[key], jsdate);
			}
		}	
	});
}

function updateS5Stats() {
	var uri = uripart + lastfrom;
	//$.getJSON(uri, "", function(data){alert(data);});
	$.ajax({
	dataType: "json",
	url: uri,
	data: undefined,
	success: graphS5Status, //function(data){alert(data);},
	error: function( jqxhr, textStatus, error ) {
		var err = textStatus + ", " + error;
		console.log( "Request Failed: " + err );}
	});
}