speed.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. function Chart (id) {
  2. this.id = id;
  3. this.timezoneOffset = (new Date()).getTimezoneOffset() * 60 * 1000;
  4. this.startDate = null;
  5. this.endDate = null;
  6. this.step = null;
  7. this.stationLogs = null;
  8. this.data = {};
  9. this.liveUpdateEnabled = false;
  10. }
  11. Chart.prototype.draw = function () {
  12. var self = this;
  13. $(this.id).highcharts('StockChart', {
  14. rangeSelector : {
  15. selected : 1
  16. },
  17. chart: {
  18. type: 'spline'
  19. },
  20. title: {
  21. text: 'WLAN Capacity'
  22. },
  23. xAxis: {
  24. type: 'datetime',
  25. dateTimeLabelFormats: {
  26. month: '%e. %b',
  27. year: '%b'
  28. },
  29. title: {
  30. text: 'Date'
  31. }
  32. },
  33. yAxis: {
  34. title: {
  35. text: 'Bandwidth in MBit/s'
  36. },
  37. min: 0
  38. },
  39. tooltip: {
  40. //pointFormat: '{point.x:%e. %b}: {point.y:.2f} MBit/s'
  41. formatter: function () {
  42. var timestamp = new Date(this.x);
  43. var send = Math.round(this.points[0].y * 100) / 100;
  44. var capacity = Math.round(this.points[1].y * 100) / 100;
  45. var index = this.points[0].point.index;
  46. var frequency = self.data.frequency && self.data.frequency[index] && self.data.frequency[index][1];
  47. frequency = frequency ? frequency + ' MHz' : '';
  48. return '<em>' + timestamp.toISOString() + '</em><br>' +
  49. send + ' MBit/s ' +
  50. '<b>' + capacity + 'MBit/s </b> ' +
  51. frequency;
  52. },
  53. shared: true
  54. },
  55. plotOptions: {
  56. spline: {
  57. marker: {
  58. enabled: true
  59. }
  60. }
  61. },
  62. series: [{
  63. name: 'send',
  64. data: this.data.send
  65. }, {
  66. name: 'capacity',
  67. data: this.data.capacity
  68. }]
  69. });
  70. this.chart = $(this.id).highcharts();
  71. return Promise.resolve();
  72. };
  73. Chart.prototype.append = function (offset) {
  74. var self = this;
  75. var send = this.chart.series[0];
  76. this.data.send.slice(offset).forEach(function (point) {
  77. send.addPoint(point, true, true);
  78. });
  79. var capacity = this.chart.series[1];
  80. this.data.capacity.slice(offset).forEach(function (point) {
  81. capacity.addPoint(point, true, true);
  82. });
  83. };
  84. Chart.prototype.update = function (append) {
  85. var self = this;
  86. if (!append) {
  87. this.data = {
  88. send: [],
  89. capacity: [],
  90. frequency: []
  91. };
  92. }
  93. var offset = (this.data.send && this.data.send.length) || 0;
  94. return this.fetchStationLog().then(function () {
  95. self.buildSeries();
  96. }).then(function () {
  97. if (!append) return self.draw();
  98. else return self.append(offset);
  99. }).then(function () {
  100. return self.buildData();
  101. });
  102. };
  103. Chart.prototype.liveUpdate = function () {
  104. var that = this;
  105. if (!this.liveUpdateEnabled) return;
  106. this.startDate = this.endDate;
  107. this.endDate = new Date();
  108. this.update(true).then(function () {
  109. setTimeout(that.liveUpdate.bind(that), 30 * 1000);
  110. }).catch(function (error) {
  111. console.error(error);
  112. });
  113. };
  114. Chart.prototype.enableLiveUpdate = function (enable) {
  115. this.liveUpdateEnabled = enable;
  116. this.liveUpdate();
  117. };
  118. Chart.prototype.fetchStationLog = function () {
  119. var that = this;
  120. this.endDate = this.endDate || new Date();
  121. var params = {
  122. startDate: this.startDate.toISOString(),
  123. endDate: this.endDate.toISOString(),
  124. step: this.step
  125. };
  126. return new Promise(function (resolve) {
  127. $.getJSON('station?' + jQuery.param(params), function (stationLogs) {
  128. that.stationLogs = stationLogs;
  129. resolve();
  130. });
  131. });
  132. };
  133. Chart.prototype.buildData = function () {
  134. var that = this;
  135. this.data.frequency = this.data.frequency.concat(this.stationLogs.map(function (entry) {
  136. var frequency = entry.frequency ? parseInt(entry.frequency.split(' ').shift()) : null;
  137. return [ Date.parse(entry.timestamp) - that.timezoneOffset, frequency ];
  138. }));
  139. return Promise.resolve();
  140. };
  141. Chart.prototype.buildSeries = function () {
  142. var that = this;
  143. this.data.send = this.data.send.concat(this.stationLogs.map(function (entry) {
  144. return [ Date.parse(entry.timestamp) - that.timezoneOffset, entry.sendSpeed ];
  145. }));
  146. this.data.capacity = this.data.capacity.concat(this.stationLogs.map(function (entry) {
  147. return [ Date.parse(entry.timestamp) - that.timezoneOffset, entry.capacity ];
  148. }));
  149. return Promise.resolve();
  150. };
  151. var chart = new Chart('#speed-diagram');
  152. function update () {
  153. $('#status').text('loading...');
  154. var past = 24*60*60*1000 * parseFloat($('#speed-diagram-range').val());
  155. chart.step = 0.1 * parseFloat($('#speed-diagram-detail').val());
  156. chart.startDate = new Date(Date.now() - past);
  157. chart.update().then(function () {
  158. $('#status').text('');
  159. });
  160. }
  161. $(document).ready(function () {
  162. update();
  163. $('#speed-diagram-range').change(update);
  164. $('#speed-diagram-detail').change(update);
  165. $('#update').click(update);
  166. $('#live-update').change(function () {
  167. var enable = $(this).is(':checked');
  168. chart.enableLiveUpdate(enable);
  169. });
  170. });