-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
174 lines (148 loc) · 4.83 KB
/
client.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var websocket = require('websocket-stream')
, env = require('./env.json')
, ws = websocket('wss://' + env.mountPoint)
, d3 = require('d3')
var lowRequestTime = 1 // millis
, highRequestTime = 100 // millis
, lowBytesSent = 0 // bytes
, highBytesSent = 100000 // bytes
// Control bubble behavior
var minBubbleTravelTime = 1000 // millis
, maxBubbleTravelTime = 15000 // millis
, minBubbleSize = 5 // pixels
, maxBubbleSize = 50 // pixels
, verticalCenterPercentage = .25 // The bubbles wil generally stay in this middle percentage of the screen
var margin = {top: 0, right: -100, bottom: 0, left: 0}
, bubblePadding = 6
, color = d3.scale.category20c().range(['#4ac7f5', '#46c5d2', '#42c4b1', '#3ac271', '#36c056', '#31bf27'])
, id = 0
, nodes = []
, width, height
setWidthHeight()
var speedScaler = d3.scale.linear().domain([lowRequestTime, highRequestTime]).range([minBubbleTravelTime, maxBubbleTravelTime])
, radiusScaler = d3.scale.sqrt().domain([lowBytesSent, highBytesSent]).range([minBubbleSize, maxBubbleSize])
function scaleSpeed (requestTime) {
var t = requestTime
if (requestTime > highRequestTime) {
t = highRequestTime
} else if (requestTime < lowRequestTime) {
t = lowRequestTime
}
return speedScaler(t)
}
function scaleRadius(bytesSent) {
var s = bytesSent
if (bytesSent > highBytesSent) {
s = highBytesSent
} else if (bytesSent < lowBytesSent) {
s = lowBytesSent
}
return radiusScaler(s)
}
ws.on('data', function (line) {
var now = new Date
try {
var req = JSON.parse(line)
} catch (e) {
// bad data, just ignore.
return
}
req.id = id++
req.request_time = scaleSpeed(parseFloat(req.request_time))
req.radius = scaleRadius(req.body_bytes_sent)
req.color = color(req.request)
req.cx = req.x = 0
req.cy = req.y = (height * verticalCenterPercentage * Math.random()) // A Random spot within the verticalCenterPercentage
+ (((1 - verticalCenterPercentage) * height) / 2) // Scooted down so that the top and bottom have even vertical spacing
- 15 // And scooted up to accomodate the height of the bottom label
req.start = now.getTime()
nodes.push(req)
restart()
d3.select('#readout').text(req.request)
})
function setWidthHeight() {
width = window.innerWidth - margin.left - margin.right,
height = window.innerHeight - margin.top - margin.bottom
}
var force = d3.layout.force()
.nodes(nodes)
.size([width, height])
.gravity(0)
.friction(.6)
.charge(0)
.on('tick', tick)
.start()
var svg = d3.select('#animation').append('svg')
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
function restart () {
force.start()
var now = new Date().getTime()
for (var i = nodes.length - 1; i >= 0; i--) {
if (finished(nodes[i], now)) {
nodes.splice(i,1)
}
}
var circle = svg.selectAll('circle').data(nodes, function (d) { return d.id })
circle
.enter().append('circle')
.attr('cx', function (d) { return d.x })
.attr('cy', function (d) { return d.y })
.attr('r', function (d) { return d.radius })
.style('fill', function (d) { return d.color })
circle.exit().remove()
}
function tick (e) {
force.start()
// This code needs to run on tick (setInterval, or requestAnimationFrame)
// Makes things smooth and update
svg.selectAll('circle')
.each(gravity(.2 * e.alpha))
.each(collide(.5))
.attr('cx', function (d) { return d.x })
.attr('cy', function (d) { return d.y })
}
// Move nodes toward cluster focus.
function gravity (alpha) {
return function (d) {
d.y += (d.cy - d.y) * alpha
var now = new Date().getTime()
, pos = width * (now - d.start) / d.request_time
d.x += (pos - d.x) * alpha
}
}
// Resolve collisions between nodes.
function collide (alpha) {
var quadtree = d3.geom.quadtree(nodes)
return function (d) {
var r = d.radius + radiusScaler.domain()[1] + bubblePadding
, nx1 = d.x - r
, nx2 = d.x + r
, ny1 = d.y - r
, ny2 = d.y + r
quadtree.visit(function (quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x
, y = d.y - quad.point.y
, l = Math.sqrt(x * x + y * y)
, r = d.radius + quad.point.radius + (d.color !== quad.point.color) * bubblePadding
if (l < r) {
l = (l - r) / l * alpha
d.x -= x *= l
d.y -= y *= l
quad.point.x += x
quad.point.y += y
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1
})
}
}
function finished (transaction, now) {
return transaction.start + (transaction.request_time || 3000) + 1000 < now
}
var resizeTimer
window.onresize = function () {
window.clearInterval(resizeTimer)
resizeTimer = setInterval(setWidthHeight, 300)
}