log_metrics.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * @typedef {Ember.Object} LogLevelItemObject
  21. * @property {string} level level name
  22. * @property {number} counter
  23. */
  24. /**
  25. * @typedef {Object} ServiceLogMetricsObject
  26. * @property {App.Service} service model instance
  27. * @property {LogLevelItemObject[]} logs
  28. */
  29. App.MainHostLogMetrics = Em.View.extend({
  30. templateName: require('templates/main/host/log_metrics'),
  31. classNames: ['host-log-metrics'],
  32. /**
  33. * @type {ServiceLogMetricsObject[]}
  34. */
  35. logsData: function() {
  36. var services = this.get('content').get('hostComponents').mapProperty('service').uniq();
  37. var logLevels = ['fatal', 'critical', 'error', 'warning', 'info', 'debug'];
  38. return services.map(function(service) {
  39. var levels = logLevels.map(function(level) {
  40. return Em.Object.create({
  41. level: level,
  42. counter: Math.ceil(Math.random()*10)
  43. });
  44. });
  45. return Em.Object.create({
  46. service: service,
  47. logs: levels
  48. });
  49. });
  50. }.property('content'),
  51. /**
  52. * @type {Ember.View} Pie Chart view
  53. * @extends App.PieChartView
  54. */
  55. chartView: App.ChartPieView.extend({
  56. classNames: ['log-metrics-chart'],
  57. w: 150,
  58. h: 150,
  59. stroke: '#fff',
  60. strokeWidth: 1,
  61. levelColors: {
  62. FATAL: '#B10202',
  63. CRITICAL: '#E00505',
  64. ERROR: App.healthStatusRed,
  65. INFO: App.healthStatusGreen,
  66. WARNING: App.healthStatusOrange,
  67. DEBUG: '#1e61f7'
  68. },
  69. innerR: 36,
  70. donut: d3.layout.pie().sort(null).value(function(d) { return d.get('counter'); }),
  71. prepareChartData: function(content) {
  72. this.set('data', content.get('logs'));
  73. },
  74. didInsertElement: function() {
  75. this.prepareChartData(this.get('content'));
  76. this._super();
  77. this.appendLabels();
  78. this.formatCenterText();
  79. this.attachArcEvents();
  80. this.colorizeArcs();
  81. },
  82. attachArcEvents: function() {
  83. var self = this;
  84. this.get('svg').selectAll('.arc')
  85. .on('mouseover', function(d) {
  86. self.get('svg').select('g.center-text').select('text')
  87. .text(d.data.get('level').capitalize() + ": " + d.data.get('counter'));
  88. })
  89. .on('mouseout', function() {
  90. self.get('svg').select('g.center-text').select('text').text('');
  91. });
  92. },
  93. formatCenterText: function() {
  94. this.get('svg')
  95. .append('svg:g')
  96. .attr('class', 'center-text')
  97. .attr('render-order', 1)
  98. .append('svg:text')
  99. .attr('transform', "translate(0,0)")
  100. .attr('text-anchor', 'middle')
  101. .attr('stroke', '#000')
  102. .attr('stroke-width', 0)
  103. },
  104. appendLabels: function() {
  105. var labelArc = d3.svg.arc()
  106. .outerRadius(this.get('outerR') - 15)
  107. .innerRadius(this.get('outerR') - 15);
  108. this.get('svg').selectAll('.arc')
  109. .append('text')
  110. .attr('transform', function(d) { return "translate(" + labelArc.centroid(d) + ")"; })
  111. .attr('stroke', '#000')
  112. .attr('stroke-width', 0)
  113. .attr('font-size', '12px')
  114. .attr('dy', '.50em')
  115. .text(function(d) { return d.data.get('counter'); });
  116. },
  117. colorizeArcs: function() {
  118. var self = this;
  119. this.get('svg').selectAll('.arc path')
  120. .attr('fill', function(d) {
  121. return self.get('levelColors')[d.data.get('level').toUpperCase()];
  122. });
  123. }
  124. }),
  125. transitionByService: function(e) {
  126. var service = e.context;
  127. App.router.transitionTo('logs', {query: '?service_name=' + service.get('service.serviceName')});
  128. }
  129. });