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
175
176
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="assets/javascript/flot/excanvas.min.js"></script>
<script type="text/javascript" src="assets/javascript/flot/jquery.flot.min.js"></script>
<script type="text/javascript" src="assets/javascript/flot/jquery.flot.selection.min.js"></script>
<script type="text/javascript" src="assets/javascript/javascriptrrd/binaryXHR.js"></script>
<script type="text/javascript" src="assets/javascript/javascriptrrd/rrdFile.js"></script>
<script type="text/javascript" src="assets/javascript/MochiKit/Base.js"></script>
<script type="text/javascript" src="assets/javascript/MochiKit/Async.js"></script>
<script type="text/javascript">
var grrd;
function downloadBinary(url) {
var d = new MochiKit.Async.Deferred();
$.ajax({
url: url,
dataType: 'text',
beforeSend: function(request) {
try {
request.overrideMimeType('text/plain; charset=x-user-defined');
} catch(e) {
// IE doesn't support overrideMimeType
}
},
success: function(data) {
try {
d.callback(new BinaryFile(data));
} catch(e) {
d.errback(e);
}
},
error: function(xhr, textStatus, errorThrown) {
// Special case for IE which handles binary data slightly
// differently.
if(textStatus == 'parsererror') {
if (typeof xhr.responseBody != 'undefined') {
return this.success(xhr.responseBody);
}
}
d.errback(new Error(xhr.status));
}
});
return d;
}
var RrdDataWrapper = function(url) {
this.url = url;
this._rrd = null;
}
RrdDataWrapper.prototype = {
getData: function(startTime, endTime) {
var endTimestamp = endTime.getTime()/1000;
var d, self = this;
if(!this._rrd || this._rrd.getLastUpdate() < endTimestamp) {
d = downloadBinary(this.url)
.addCallback(
function(binary) {
var rrd = new RRDFile(binary);
self._rrd = rrd;
return rrd;
});
} else {
d = new MochiKit.Async.Deferred()
d.callback(this._rrd);
}
d.addCallback(
function(rrd) {
return self._getData(startTime, endTime);
});
return d;
},
_getData: function(startTime, endTime) {
var startTimestamp = startTime.getTime()/1000;
var endTimestamp = endTime.getTime()/1000;
var consolidationFunc = 'AVERAGE';
var lastUpdated = this._rrd.getLastUpdate();
var bestRRA = null;
for(var i=0; i<this._rrd.getNrRRAs(); i++) {
// Look through all RRAs looking for the most suitable
// data resolution.
var rra = this._rrd.getRRA(i);
if(rra.getCFName() != consolidationFunc) {
continue;
}
bestRRA = rra;
var step = rra.getStep();
var rraRowCount = rra.getNrRows();
var firstUpdated = lastUpdated - (rraRowCount - 1) * step;
if(firstUpdated <= startTimestamp) {
break;
}
}
if(!bestRRA) {
throw new Error('Unrecognised consolidation function: ' + consolidationFunc);
}
startRow = rraRowCount - parseInt((lastUpdated - startTimestamp)/step);
endRow = rraRowCount - parseInt((lastUpdated - endTimestamp)/step);
returnData = [];
for(var d=this._rrd.getNrDSs()-1; d>=0; d--) {
flotData = [];
timestamp = firstUpdated + (startRow - 1) * step;
for (var i=startRow; i<=endRow; i++) {
var val = bestRRA.getEl(i, d);
flotData.push([timestamp*1000.0, val]);
timestamp += step;
}
returnData.push({label: this._rrd.getDS(d).getName(), data: flotData});
}
return returnData;
}
};
var graphOptions = {
grid: {
clickable: true
},
selection: {
mode: 'x'
},
series: {
points: { show: false },
lines: {
show: true,
steps: false,
shadowSize: 0,
lineWidth: 1
},
shadowSize: 0
},
xaxis: {
mode: "time"
}
};
$(function() {
new RrdDataWrapper('data/localhost/load/load.rrd')
.getData(
new Date('7 April 2010 09:30:00'),
new Date('7 April 2010 11:00:00'))
.addCallback(
function(data) {
var plot = $.plot($('.container'), data, graphOptions);
})
.addErrback(
function(failure) {
$('.container').text('error: ' + failure.message);
});
$('.container').bind('plotclick', function(event, pos, item) {
if (item) {
console.log(['X: ' + new Date(item.datapoint[0]), 'Y: ' + item.datapoint[1]]);
}
});
});
</script>
</head>
<body>
<div class="container" style="width:100%; height:200px;"></div>
</body>
</html>
|