helper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.trim = function () {
  19. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  20. };
  21. String.prototype.endsWith = function(suffix) {
  22. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  23. };
  24. /**
  25. * convert ip address string to long int
  26. * @return {*}
  27. */
  28. String.prototype.ip2long = function () {
  29. // * example 1: ip2long('192.0.34.166');
  30. // * returns 1: 3221234342
  31. // * example 2: ip2long('0.0xABCDEF');
  32. // * returns 2: 11259375
  33. // * example 3: ip2long('255.255.255.256');
  34. // * returns 3: false
  35. var i = 0;
  36. // PHP allows decimal, octal, and hexadecimal IP components.
  37. // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
  38. var IP = this.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); // Verify IP format.
  39. if (!IP) {
  40. return false; // Invalid format.
  41. }
  42. // Reuse IP variable for component counter.
  43. IP[0] = 0;
  44. for (i = 1; i < 5; i += 1) {
  45. IP[0] += !!((IP[i] || '').length);
  46. IP[i] = parseInt(IP[i]) || 0;
  47. }
  48. // Continue to use IP for overflow values.
  49. // PHP does not allow any component to overflow.
  50. IP.push(256, 256, 256, 256);
  51. // Recalculate overflow of last component supplied to make up for missing components.
  52. IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
  53. if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
  54. return false;
  55. }
  56. return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
  57. };
  58. String.prototype.capitalize = function () {
  59. return this.charAt(0).toUpperCase() + this.slice(1);
  60. }
  61. Em.CoreObject.reopen({
  62. t:function (key, attrs) {
  63. return Em.I18n.t(key, attrs)
  64. }
  65. });
  66. Em.Handlebars.registerHelper('log', function (variable) {
  67. console.log(variable);
  68. });
  69. Em.Handlebars.registerHelper('warn', function (variable) {
  70. console.warn(variable);
  71. });
  72. Em.Handlebars.registerHelper('highlight', function (property, words, fn) {
  73. var context = (fn.contexts && fn.contexts[0]) || this;
  74. property = Em.Handlebars.getPath(context, property, fn);
  75. words = words.split(";");
  76. // if (highlightTemplate == undefined) {
  77. var highlightTemplate = "<b>{0}</b>";
  78. // }
  79. words.forEach(function (word) {
  80. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  81. property = property.replace(searchRegExp, function (found) {
  82. return highlightTemplate.format(found);
  83. });
  84. });
  85. return new Em.Handlebars.SafeString(property);
  86. })
  87. /**
  88. * Replace {i} with argument. where i is number of argument to replace with
  89. * @return {String}
  90. */
  91. String.prototype.format = function () {
  92. var args = arguments;
  93. return this.replace(/{(\d+)}/g, function (match, number) {
  94. return typeof args[number] != 'undefined' ? args[number] : match;
  95. });
  96. };
  97. String.prototype.highlight = function (words, highlightTemplate) {
  98. var self = this;
  99. if (highlightTemplate == undefined) {
  100. var highlightTemplate = "<b>{0}</b>";
  101. }
  102. words.forEach(function (word) {
  103. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  104. self = self.replace(searchRegExp, function (found) {
  105. return highlightTemplate.format(found);
  106. });
  107. });
  108. return self;
  109. };
  110. Number.prototype.toDaysHoursMinutes = function () {
  111. var formatted = {},
  112. dateDiff = this,
  113. secK = 1000, //ms
  114. minK = 60 * secK, // sec
  115. hourK = 60 * minK, // sec
  116. dayK = 24 * hourK;
  117. dateDiff = parseInt(dateDiff);
  118. formatted.d = Math.floor(dateDiff / dayK);
  119. dateDiff -= formatted.d * dayK;
  120. formatted.h = Math.floor(dateDiff / hourK);
  121. dateDiff -= formatted.h * hourK;
  122. formatted.m = (dateDiff / minK).toFixed(2);
  123. return formatted;
  124. }
  125. Number.prototype.countPercentageRatio = function (maxValue) {
  126. var usedValue = this;
  127. return Math.round((usedValue / maxValue) * 100) + "%";
  128. }
  129. Number.prototype.long2ip = function () {
  130. // http://kevin.vanzonneveld.net
  131. // + original by: Waldo Malqui Silva
  132. // * example 1: long2ip( 3221234342 );
  133. // * returns 1: '192.0.34.166'
  134. if (!isFinite(this))
  135. return false;
  136. return [this >>> 24, this >>> 16 & 0xFF, this >>> 8 & 0xFF, this & 0xFF].join('.');
  137. }
  138. /**
  139. * Formats the given URL template by replacing keys in 'substitutes'
  140. * with their values. If not in App.testMode, the testUrl is used.
  141. *
  142. * The substitution points in urlTemplate should be of format "...{key}..."
  143. * For example "http://apache.org/{projectName}".
  144. * The substitutes can then be{projectName: "Ambari"}.
  145. *
  146. * Keys which will be automatically taken care of are:
  147. * {
  148. * hostName: App.test_hostname,
  149. * fromSeconds: ..., // 1 hour back from now
  150. * toSeconds: ..., // now
  151. * stepSeconds: ..., // 15 seconds by default
  152. * }
  153. *
  154. * @param {String} urlTemplate URL template on which substitutions are to be made
  155. * @param substitutes Object containing keys to be replaced with respective values
  156. * @param {String} testUrl URL to be used if app is not in test mode (!App.testMode)
  157. * @return {String} Formatted URL
  158. */
  159. App = require('app');
  160. App.formatUrl = function (urlTemplate, substitutes, testUrl) {
  161. var formatted = urlTemplate;
  162. if (urlTemplate) {
  163. if (!App.testMode) {
  164. var toSeconds = Math.round(new Date().getTime() / 1000);
  165. var allSubstitutes = {
  166. toSeconds:toSeconds,
  167. fromSeconds:toSeconds - 3600, // 1 hour back
  168. stepSeconds:15, // 15 seconds
  169. hostName:App.test_hostname
  170. };
  171. jQuery.extend(allSubstitutes, substitutes);
  172. for (key in allSubstitutes) {
  173. var useKey = '{' + key + '}';
  174. formatted = formatted.replace(new RegExp(useKey, 'g'), allSubstitutes[key]);
  175. }
  176. } else {
  177. formatted = testUrl;
  178. }
  179. }
  180. return formatted;
  181. }
  182. /**
  183. * Certain variables can have JSON in string
  184. * format, or in JSON format itself.
  185. */
  186. App.parseJSON = function (value) {
  187. if (typeof value == "string") {
  188. return jQuery.parseJSON(value);
  189. }
  190. return value;
  191. };
  192. App.format = {
  193. role:function (role) {
  194. switch (role) {
  195. case 'ZOOKEEPER_SERVER':
  196. return 'ZooKeeper Server';
  197. case 'ZOOKEEPER_CLIENT':
  198. return 'ZooKeeper Client';
  199. case 'NAMENODE':
  200. return 'NameNode';
  201. case 'NAMENODE_SERVICE_CHECK':
  202. return 'NameNode Check';
  203. case 'DATANODE':
  204. return 'DataNode';
  205. case 'HDFS_SERVICE_CHECK':
  206. return 'HDFS Check';
  207. case 'SECONDARY_NAMENODE':
  208. return 'SNameNode';
  209. case 'HDFS_CLIENT':
  210. return 'HDFS Client';
  211. case 'HBASE_MASTER':
  212. return 'HBase Master';
  213. case 'HBASE_REGIONSERVER':
  214. return 'HBase RegionServer';
  215. case 'HBASE_CLIENT':
  216. return 'HBase Client';
  217. case 'JOBTRACKER':
  218. return 'JobTracker';
  219. case 'TASKTRACKER':
  220. return 'TaskTracker';
  221. case 'MAPREDUCE_CLIENT':
  222. return 'MapReduce Client';
  223. case 'HISTORYSERVER':
  224. return 'History Server';
  225. case 'NODEMANAGER':
  226. return 'Node Manager';
  227. case 'RESOURCEMANAGER':
  228. return 'Resource Manager';
  229. case 'TEZ_CLIENT':
  230. return 'Tez Client';
  231. case 'MAPREDUCE2_CLIENT':
  232. return 'MapReduce2 Client';
  233. case 'YARN_CLIENT':
  234. return 'YARN Client';
  235. case 'JAVA_JCE':
  236. return 'Java JCE';
  237. case 'KERBEROS_SERVER':
  238. return 'Kerberos Server';
  239. case 'KERBEROS_CLIENT':
  240. return 'Kerberos Client';
  241. case 'KERBEROS_ADMIN_CLIENT':
  242. return 'Kerberos Admin Client';
  243. case 'HADOOP_CLIENT':
  244. return 'Hadoop Client';
  245. case 'JOBTRACKER_SERVICE_CHECK':
  246. return 'JobTracker Check';
  247. case 'MAPREDUCE_SERVICE_CHECK':
  248. return 'MapReduce Check';
  249. case 'ZOOKEEPER_SERVICE_CHECK':
  250. return 'ZooKeeper Check';
  251. case 'ZOOKEEPER_QUORUM_SERVICE_CHECK':
  252. return 'ZK Quorum Check';
  253. case 'HBASE_SERVICE_CHECK':
  254. return 'HBase Check';
  255. case 'MYSQL_SERVER':
  256. return 'MySQL Server';
  257. case 'HIVE_SERVER':
  258. return 'HiveServer2';
  259. case 'HIVE_METASTORE':
  260. return 'Hive Metastore';
  261. case 'HIVE_CLIENT':
  262. return 'Hive Client';
  263. case 'HIVE_SERVICE_CHECK':
  264. return 'Hive Check';
  265. case 'HCAT':
  266. return 'HCat';
  267. case 'HCAT_SERVICE_CHECK':
  268. return 'HCat Check';
  269. case 'OOZIE_CLIENT':
  270. return 'Oozie Client';
  271. case 'OOZIE_SERVER':
  272. return 'Oozie Server';
  273. case 'OOZIE_SERVICE_CHECK':
  274. return 'Oozie Check';
  275. case 'PIG':
  276. return 'Pig';
  277. case 'PIG_SERVICE_CHECK':
  278. return 'Pig Check';
  279. case 'MAPREDUCE2_SERVICE_CHECK':
  280. return 'MapReduce2 Check';
  281. case 'YARN_SERVICE_CHECK':
  282. return 'YARN Check';
  283. case 'SQOOP':
  284. return 'Sqoop';
  285. case 'SQOOP_SERVICE_CHECK':
  286. return 'Sqoop Check';
  287. case 'WEBHCAT_SERVER':
  288. return 'WebHCat Server';
  289. case 'WEBHCAT_SERVICE_CHECK':
  290. return 'WebHCat Check';
  291. case 'NAGIOS_SERVER':
  292. return 'Nagios Server';
  293. case 'GANGLIA_SERVER':
  294. return 'Ganglia Server';
  295. case 'GANGLIA_MONITOR':
  296. return 'Ganglia Monitor';
  297. case 'GMOND_SERVICE_CHECK':
  298. return 'Gmond Check';
  299. case 'GMETAD_SERVICE_CHECK':
  300. return 'Gmetad Check';
  301. case 'DECOMMISSION_DATANODE':
  302. return 'Update Exclude File';
  303. case 'HUE_SERVER':
  304. return 'Hue Server';
  305. case 'HCFS_CLIENT':
  306. return 'HCFS Client';
  307. case 'HCFS_SERVICE_CHECK':
  308. return 'HCFS Service Check';
  309. case 'FLUME_SERVER':
  310. return 'Flume Agent';
  311. }
  312. },
  313. /**
  314. * PENDING - Not queued yet for a host
  315. * QUEUED - Queued for a host
  316. * IN_PROGRESS - Host reported it is working
  317. * COMPLETED - Host reported success
  318. * FAILED - Failed
  319. * TIMEDOUT - Host did not respond in time
  320. * ABORTED - Operation was abandoned
  321. */
  322. taskStatus:function (_taskStatus) {
  323. return _taskStatus.toLowerCase();
  324. }
  325. };