stats.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. window.addEvent('domready', function() {
  2. 'use strict';
  3. Rickshaw.Graph.Axis.Y.Base1024KMGTP = Rickshaw.Class.create(Rickshaw.Graph.Axis.Y, {
  4. '_drawAxis': function($super, scale) {
  5. var axis = d3.svg.axis().scale(scale).orient(this.orientation);
  6. var domain = axis.scale().domain();
  7. var tickSpacing = 1;
  8. while (tickSpacing * 1024 < domain[1])
  9. tickSpacing *= 1024;
  10. while (tickSpacing * 20 < domain[1])
  11. tickSpacing *= 10;
  12. while (tickSpacing * 5 < domain[1])
  13. tickSpacing *= 2;
  14. var min = Math.ceil(domain[0] / tickSpacing);
  15. var max = Math.floor(domain[1] / tickSpacing) + 1;
  16. var tickValues = [];
  17. for (var i = min * tickSpacing; i < max; i += 1)
  18. tickValues.push(i * tickSpacing);
  19. axis.tickValues(tickValues);
  20. axis.tickFormat(this.tickFormat);
  21. if (this.orientation == 'left') {
  22. var berth = this.height * berthRate;
  23. var transform = 'translate(' + this.width + ', ' + berth + ')';
  24. }
  25. if (this.element)
  26. this.vis.selectAll('*').remove();
  27. this.vis
  28. .append('svg:g')
  29. .attr('class', ['y_ticks', this.ticksTreatment].join(' '))
  30. .attr('transform', transform)
  31. .call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize));
  32. return axis;
  33. },
  34. });
  35. var split = document.location.pathname.split('/');
  36. var url = 'http://localhost:8892/v1/' + split[2] + '/stats/' + split[3];
  37. function get_stats(start, end, interval) {
  38. new Request.JSON({
  39. 'url': url + '?start=' + start + '&end=' + end + '&interval=' + interval,
  40. 'onSuccess': function(data) {
  41. Object.each(data, function(subfields, field) {
  42. if (field == 'disk')
  43. return;
  44. graph_stat(field, subfields, interval);
  45. });
  46. },
  47. }).get();
  48. }
  49. var intervals = {
  50. 1: 15,
  51. 7: 60,
  52. 30: 360,
  53. 90: 1440,
  54. }
  55. function change_res() {
  56. var days = $('resolution').get('value');
  57. var start = moment.utc().subtract('days', days - 1).format('YYYY-MM-DD');
  58. var end = moment.utc().format('YYYY-MM-DD');
  59. get_stats(start, end, intervals[days]);
  60. }
  61. $('resolution').addEvent('change', change_res);
  62. change_res();
  63. var colors = {
  64. 'cpu': [
  65. ['user', '#33a'],
  66. ['nice', '#55c'],
  67. ['iowait', '#a33'],
  68. ['system', '#aa3'],
  69. ['irq', '#711'],
  70. ['softirq', '#771'],
  71. ['guest', '#555'],
  72. ['guest_nice', '#777'],
  73. ['steal', '#a71'],
  74. ['idle', '#3aa'],
  75. ],
  76. 'mem': [
  77. ['used', '#a33'],
  78. ['buffers', '#3aa'],
  79. ['cached', '#33a'],
  80. ['free', '#3a3'],
  81. ],
  82. 'net': [
  83. ['bit/s_in', '#33a', 'area'],
  84. ['bit/s_out', '#3a3', 'area'],
  85. ['err_in', '#a33', 'line'],
  86. ['err_out', '#aa3', 'line'],
  87. ['drop_in', '#a33', 'line'],
  88. ['drop_out', '#aa3', 'line'],
  89. ],
  90. };
  91. function graph_stat(field, subfields, interval) {
  92. var series = [];
  93. colors[field].each(function(subfield_meta) {
  94. var subfield = subfield_meta[0];
  95. var color = subfield_meta[1];
  96. var days = Object.keys(subfields[subfield]).sort();
  97. var array = [];
  98. var index_start = 0;
  99. days.each(function(day) {
  100. var day_array = subfields[subfield][day];
  101. if (day_array != null) {
  102. array = array.append(day_array.map(function(val, i) {
  103. return {'x': index_start + i * interval, 'y': val};
  104. }));
  105. }
  106. index_start += 1440;
  107. });
  108. series.push({
  109. 'name': field + '.' + subfield,
  110. 'data': array,
  111. 'color': color,
  112. 'renderer': subfield_meta[2],
  113. });
  114. });
  115. var graph_div = new Element('div');
  116. var legend_div = new Element('div');
  117. var graph = new Rickshaw.Graph({
  118. 'element': graph_div,
  119. 'width': 400,
  120. 'height': 200,
  121. 'renderer': field == 'net' ? 'multi' : 'area',
  122. 'stroke': true,
  123. 'series': series,
  124. 'interpolation': 'step-after',
  125. });
  126. new Rickshaw.Graph.Legend({
  127. 'graph': graph,
  128. 'element': legend_div,
  129. });
  130. new Rickshaw.Graph.HoverDetail({
  131. 'graph': graph,
  132. });
  133. new Rickshaw.Graph.Axis.Time({
  134. 'graph': graph,
  135. //'timeUnit': new Rickshaw.Fixtures.Time().unit('minute'),
  136. });
  137. new Rickshaw.Graph.Axis.Y.Base1024KMGTP({
  138. 'graph': graph,
  139. 'tickFormat': Rickshaw.Fixtures.Number.formatBase1024KMGTP,
  140. });
  141. graph.render();
  142. $(field).empty().adopt(graph_div, legend_div);
  143. }
  144. });