stats.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. new Request.JSON({
  38. 'url': url + '?start=2014-04-19&end=2014-04-19',
  39. 'onSuccess': graph_stats,
  40. }).get();
  41. function graph_stats(data) {
  42. Object.each(data, function(subfields, field) {
  43. if (field == 'disk')
  44. return;
  45. graph_stat(field, subfields);
  46. });
  47. }
  48. var colors = {
  49. 'cpu': [
  50. ['user', '#33a'],
  51. ['nice', '#55c'],
  52. ['iowait', '#a33'],
  53. ['system', '#aa3'],
  54. ['irq', '#711'],
  55. ['softirq', '#771'],
  56. ['guest', '#555'],
  57. ['guest_nice', '#777'],
  58. ['steal', '#a71'],
  59. ['idle', '#3aa'],
  60. ],
  61. 'mem': [
  62. ['used', '#a33'],
  63. ['buffers', '#3aa'],
  64. ['cached', '#33a'],
  65. ['free', '#3a3'],
  66. ],
  67. 'net': [
  68. ['bit/s_in', '#33a', 'area'],
  69. ['bit/s_out', '#3a3', 'area'],
  70. ['err_in', '#a33', 'line'],
  71. ['err_out', '#aa3', 'line'],
  72. ['drop_in', '#a33', 'line'],
  73. ['drop_out', '#aa3', 'line'],
  74. ],
  75. };
  76. function graph_stat(field, subfields) {
  77. var series = [];
  78. colors[field].each(function(subfield_meta) {
  79. var subfield = subfield_meta[0];
  80. var color = subfield_meta[1];
  81. var array = [];
  82. Object.each(subfields[subfield], function(day_array, k) {
  83. if (day_array == null)
  84. return;
  85. array = array.append(day_array.map(function(val, i) {
  86. return {'x': i, 'y': val};
  87. }));
  88. });
  89. series.push({
  90. 'name': field + '.' + subfield,
  91. 'data': array,
  92. 'color': color,
  93. 'renderer': subfield_meta[2],
  94. });
  95. });
  96. var graph_div = new Element('div');
  97. var legend_div = new Element('div');
  98. var graph = new Rickshaw.Graph({
  99. 'element': graph_div,
  100. 'width': 500,
  101. 'height': 200,
  102. 'renderer': field == 'net' ? 'multi' : 'area',
  103. 'stroke': true,
  104. 'series': series,
  105. });
  106. new Rickshaw.Graph.Legend({
  107. 'graph': graph,
  108. 'element': legend_div,
  109. });
  110. new Rickshaw.Graph.HoverDetail({
  111. 'graph': graph,
  112. });
  113. new Rickshaw.Graph.Axis.Time({
  114. 'graph': graph,
  115. 'timeUnit': new Rickshaw.Fixtures.Time().unit('minute'),
  116. });
  117. new Rickshaw.Graph.Axis.Y.Base1024KMGTP({
  118. 'graph': graph,
  119. 'tickFormat': Rickshaw.Fixtures.Number.formatBase1024KMGTP,
  120. });
  121. graph.render();
  122. $('graphs').adopt(graph_div, legend_div, new Element('br'));
  123. }
  124. });