helper.js 11 KB

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