helper.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. String.prototype.capitalize = function () {
  19. return this.charAt(0).toUpperCase() + this.slice(1);
  20. }
  21. Em.CoreObject.reopen({
  22. t:function (key, attrs) {
  23. return Em.I18n.t(key, attrs)
  24. }
  25. });
  26. Handlebars.registerHelper('log', function (variable) {
  27. console.log(variable);
  28. });
  29. Handlebars.registerHelper('warn', function (variable) {
  30. console.warn(variable);
  31. });
  32. String.prototype.format = function () {
  33. var args = arguments;
  34. return this.replace(/{(\d+)}/g, function (match, number) {
  35. return typeof args[number] != 'undefined' ? args[number] : match;
  36. });
  37. };
  38. /**
  39. * Convert byte size to other metrics.
  40. * @param {Number} precision Number to adjust precision of return value. Default is 0.
  41. * @param {String} parseType JS method name for parse string to number. Default is "parseInt".
  42. * @remarks The parseType argument can be "parseInt" or "parseFloat".
  43. * @return {String) Returns converted value with abbreviation.
  44. */
  45. Number.prototype.bytesToSize = function (precision, parseType/* = 'parseInt' */) {
  46. if (arguments[1] === undefined) {
  47. parseType = 'parseInt';
  48. }
  49. var value = this;
  50. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  51. var posttxt = 0;
  52. if (this == 0) return 'n/a';
  53. while (value >= 1024) {
  54. posttxt++;
  55. value = value / 1024;
  56. }
  57. var parsedValue = window[parseType](value);
  58. return parsedValue.toFixed(precision) + " " + sizes[posttxt];
  59. }
  60. Number.prototype.toDaysHoursMinutes = function () {
  61. var formatted = {},
  62. dateDiff = this,
  63. minK = 60, // sec
  64. hourK = 60 * minK, // sec
  65. dayK = 24 * hourK;
  66. dateDiff = parseInt(dateDiff / 1000);
  67. formatted.d = Math.floor(dateDiff / dayK);
  68. dateDiff -= formatted.d * dayK;
  69. formatted.h = Math.floor(dateDiff / hourK);
  70. dateDiff -= formatted.h * hourK;
  71. formatted.m = Math.floor(dateDiff / minK);
  72. dateDiff -= formatted.m * minK;
  73. return formatted;
  74. }
  75. Number.prototype.countPercentageRatio = function (maxValue) {
  76. var usedValue = this;
  77. return Math.round((usedValue / maxValue) * 100) + "%";
  78. }
  79. /**
  80. * Formats the given URL template by replacing keys in 'substitutes'
  81. * with their values. If not in App.testMode, the testUrl is used.
  82. *
  83. * The substitution points in urlTemplate should be of format "...{key}..."
  84. * For example "http://apache.org/{projectName}".
  85. * The substitutes can then be{projectName: "Ambari"}.
  86. *
  87. * Keys which will be automatically taken care of are:
  88. * {
  89. * hostName: App.test_hostname,
  90. * fromSeconds: ..., // 1 hour back from now
  91. * toSeconds: ..., // now
  92. * stepSeconds: ..., // 15 seconds by default
  93. * }
  94. *
  95. * @param {String} urlTemplate URL template on which substitutions are to be made
  96. * @param substitutes Object containing keys to be replaced with respective values
  97. * @param {String} testUrl URL to be used if app is not in test mode (!App.testMode)
  98. * @return {String} Formatted URL
  99. */
  100. App.formatUrl = function (urlTemplate, substitutes, testUrl) {
  101. var formatted = urlTemplate;
  102. if (urlTemplate) {
  103. if (App.testMode) {
  104. var toSeconds = Math.round(new Date().getTime() / 1000);
  105. var allSubstitutes = {
  106. toSeconds: toSeconds,
  107. fromSeconds: toSeconds - 3600, // 1 hour back
  108. stepSeconds: 15, // 15 seconds
  109. hostName: App.test_hostname
  110. };
  111. jQuery.extend(allSubstitutes, substitutes);
  112. for (key in allSubstitutes) {
  113. var useKey = '{' + key + '}';
  114. formatted = formatted.replace(new RegExp(useKey, 'g'), allSubstitutes[key]);
  115. }
  116. } else {
  117. formatted = testUrl;
  118. }
  119. }
  120. return formatted;
  121. }
  122. App.format = {
  123. role: function(role) {
  124. switch (role) {
  125. case 'ZOOKEEPER_SERVER':
  126. return 'ZooKeeper Server';
  127. case 'ZOOKEEPER_CLIENT':
  128. return 'ZooKeeper Client';
  129. case 'NAMENODE':
  130. return 'NameNode';
  131. case 'NAMENODE_SERVICE_CHECK':
  132. return 'NameNode Service Check';
  133. case 'DATANODE':
  134. return 'DataNode';
  135. case 'HDFS_SERVICE_CHECK':
  136. return 'HDFS Service Check';
  137. case 'SECONDARY_NAMENODE':
  138. return 'SNameNode';
  139. case 'HDFS_CLIENT':
  140. return 'HDFS Client';
  141. case 'HBASE_MASTER':
  142. return 'HBase Master';
  143. case 'HBASE_REGIONSERVER':
  144. return 'HBase RegionServer';
  145. case 'HBASE_CLIENT':
  146. return 'HBase Client';
  147. case 'JOBTRACKER':
  148. return 'JobTracker';
  149. case 'TASKTRACKER':
  150. return 'TaskTracker';
  151. case 'MAPREDUCE_CLIENT':
  152. return 'MapReduce Client';
  153. case 'JAVA_JCE':
  154. return 'Java JCE';
  155. case 'KERBEROS_SERVER':
  156. return 'Kerberos Server';
  157. case 'KERBEROS_CLIENT':
  158. return 'Kerberos Client';
  159. case 'KERBEROS_ADMIN_CLIENT':
  160. return 'Kerberos Admin Client';
  161. case 'HADOOP_CLIENT':
  162. return 'Hadoop Client';
  163. case 'JOBTRACKER_SERVICE_CHECK':
  164. return 'JobTracker Service Check';
  165. case 'MAPREDUCE_SERVICE_CHECK':
  166. return 'MapReduce Service Check';
  167. case 'ZOOKEEPER_SERVICE_CHECK':
  168. return 'ZooKeeper Service Check';
  169. case 'ZOOKEEPER_QUORUM_SERVICE_CHECK':
  170. return 'ZooKeeper Quorum Service Check';
  171. case 'HBASE_SERVICE_CHECK':
  172. return 'HBase Service Check';
  173. case 'MYSQL_SERVER':
  174. return 'MySQL Server';
  175. case 'HIVE_SERVER':
  176. return 'Hive Server';
  177. case 'HIVE_CLIENT':
  178. return 'Hive Client';
  179. case 'HIVE_SERVICE_CHECK':
  180. return 'Hive Service Check';
  181. case 'HCAT':
  182. return 'HCat';
  183. case 'HCAT_SERVICE_CHECK':
  184. return 'HCat Service Check';
  185. case 'OOZIE_CLIENT':
  186. return 'Oozie Client';
  187. case 'OOZIE_SERVER':
  188. return 'Oozie Server';
  189. case 'OOZIE_SERVICE_CHECK':
  190. return 'Oozie Service Check';
  191. case 'PIG':
  192. return 'Pig';
  193. case 'PIG_SERVICE_CHECK':
  194. return 'Pig Service Check';
  195. case 'SQOOP':
  196. return 'Sqoop';
  197. case 'SQOOP_SERVICE_CHECK':
  198. return 'Sqoop Service Check';
  199. case 'TEMPLETON_CLIENT':
  200. return 'Templeton Client';
  201. case 'TEMPLETON_SERVER':
  202. return 'Templeton Server';
  203. case 'TEMPLETON_SERVICE_CHECK':
  204. return 'Templeton Service Check';
  205. case 'DASHBOARD':
  206. return 'Dashboard';
  207. case 'DASHBOARD_SERVICE_CHECK':
  208. return 'Dashboard Service Check';
  209. case 'NAGIOS_SERVER':
  210. return 'Nagios Server';
  211. case 'GANGLIA_SERVER':
  212. return 'Ganglia Server';
  213. case 'GANGLIA_MONITOR':
  214. return 'Ganglia Monitor';
  215. case 'GMOND_SERVICE_CHECK':
  216. return 'Gmond Service Check'
  217. case 'GMETAD_SERVICE_CHECK':
  218. return 'Gmetad Service Check';
  219. case 'MONTOR_WEBSERVER':
  220. return 'Monitor Webserver'
  221. case 'DECOMMISSION_DATANODE':
  222. return 'Decommission DataNode';
  223. }
  224. },
  225. /**
  226. * PENDING - Not queued yet for a host
  227. * QUEUED - Queued for a host
  228. * IN_PROGRESS - Host reported it is working
  229. * COMPLETED - Host reported success
  230. * FAILED - Failed
  231. * TIMEDOUT - Host did not respond in time
  232. * ABORTED - Operation was abandoned
  233. */
  234. taskStatus: function(_taskStatus) {
  235. return _taskStatus.replace('_', ' ').toLowerCase();
  236. }
  237. };