stats.js 3.3 KB

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