metric.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * use: {{view App.MetricFilteringWidget controllerBinding="App.router.mainChartsController"}}
  21. * set controller.metric field with metric value
  22. * widget assign itself to controller like metricWidget (controller.get('metricWidget'))
  23. * @type {*}
  24. */
  25. App.MetricFilteringWidget = Em.View.extend({
  26. classNames:['metric-filtering-widget'],
  27. /**
  28. * chosen metric value
  29. */
  30. chosenMetric:null,
  31. chosenMoreMetric:null,
  32. showMore:0, // toggle more metrics indicator
  33. /**
  34. * metrics
  35. */
  36. metrics:[
  37. Em.Object.create({ label:Em.I18n.t('metric.default'), value:null}),
  38. Em.Object.create({ label:Em.I18n.t('metric.cpu'), value:'cpu'}),
  39. Em.Object.create({ label:Em.I18n.t('metric.memory'), value:'memory'}),
  40. Em.Object.create({ label:Em.I18n.t('metric.network'), value:'network'}),
  41. Em.Object.create({ label:Em.I18n.t('metric.io'), value:'io'})
  42. ],
  43. moreMetrics:[
  44. Em.Object.create({ label:Em.I18n.t('metric.more.cpu'), code:'cpu', items:[
  45. Em.Object.create({value:"cpu_nice"}),
  46. Em.Object.create({value:"cpu_wio"}),
  47. Em.Object.create({value:"cpu_user"}),
  48. Em.Object.create({value:"cpu_idle"}),
  49. Em.Object.create({value:"cpu_system"}),
  50. Em.Object.create({value:"cpu_aidle"})
  51. ] }),
  52. Em.Object.create({ label:Em.I18n.t('metric.more.disk'), code:'disk',
  53. items:[
  54. Em.Object.create({value:'disk_free'}),
  55. Em.Object.create({value:'disk_total'}),
  56. Em.Object.create({value:'part_max_used'})
  57. ]
  58. }),
  59. Em.Object.create({ label:Em.I18n.t('metric.more.load'), code:'load',
  60. items:[
  61. Em.Object.create({value:'load_one'}),
  62. Em.Object.create({value:'load_five'}),
  63. Em.Object.create({value:'load_fifteen'})
  64. ]
  65. }),
  66. Em.Object.create({ label:Em.I18n.t('metric.more.memory'), code:'memory',
  67. items:[
  68. Em.Object.create({value:'swap_free'}),
  69. Em.Object.create({value:'cpu'})
  70. ]
  71. }),
  72. Em.Object.create({ label:Em.I18n.t('metric.more.network'), code:'network',
  73. items:[
  74. Em.Object.create({value:'bytes_out'}),
  75. Em.Object.create({value:'bytes_in'}),
  76. Em.Object.create({value:'pkts_in'}),
  77. Em.Object.create({value:'pkts_out'})
  78. ]
  79. }),
  80. Em.Object.create({ label:Em.I18n.t('metric.more.process'), code:'process',
  81. items:[
  82. Em.Object.create({value:'proc_run'}),
  83. Em.Object.create({value:'proc_total'})
  84. ]
  85. })
  86. ],
  87. /**
  88. * return array of chosen metrics
  89. */
  90. chosenMetrics:function () {
  91. return this.get('chosenMetric') ? [this.get('chosenMetric')] : this.get('defaultMetrics');
  92. }.property('chosenMetric'),
  93. /**
  94. * metric item view
  95. */
  96. itemView:Em.View.extend({
  97. tagName:'li',
  98. classNameBindings:['disabled'],
  99. disabled:function () {
  100. return this.get('isActive') ? "disabled" : false;
  101. }.property('isActive'),
  102. isActive:function () {
  103. return this.get('metric.value') == this.get('widget.chosenMetric');
  104. }.property('widget.chosenMetric'),
  105. label:function () {
  106. return this.get('metric.label');
  107. }.property('metric.label'),
  108. template:Em.Handlebars.compile('<a {{action activate view.metric.value target="view.widget" href="#" }}>{{unbound view.label}}</a>')
  109. }),
  110. moreItemView:function () {
  111. return this.get('itemView').extend({
  112. label:function () {
  113. return this.get('metric.value');
  114. }.property('metric.value')
  115. });
  116. }.property(),
  117. /**
  118. * return default selected metrics (currently - all)
  119. */
  120. defaultMetrics:function () {
  121. var values = [];
  122. $.each(this.get('metrics'), function () {
  123. if (this.value) {
  124. values.push(this.value);
  125. }
  126. });
  127. return values;
  128. }.property(),
  129. bindToController:function () {
  130. var thisW = this;
  131. var controller = this.get('controller');
  132. controller.set('metricWidget', thisW);
  133. },
  134. toggleMore:function () {
  135. this.set('showMore', 1 - this.get('showMore'));
  136. },
  137. /**
  138. * assign this widget to controller, prepare items by metricsConfig
  139. */
  140. init:function () {
  141. this._super();
  142. this.bindToController();
  143. },
  144. /**
  145. * write active metric to widget
  146. * @param event
  147. */
  148. activate:function (event) {
  149. this.set('chosenMetric', event.context);
  150. },
  151. templateName:require('templates/common/metric')
  152. });