helper.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. String.prototype.contains = function(substring) {
  28. return this.indexOf(substring) != -1;
  29. };
  30. String.prototype.capitalize = function () {
  31. return this.charAt(0).toUpperCase() + this.slice(1);
  32. };
  33. /**
  34. * Replace {i} with argument. where i is number of argument to replace with
  35. * @return {String}
  36. */
  37. String.prototype.format = function () {
  38. var args = arguments;
  39. return this.replace(/{(\d+)}/g, function (match, number) {
  40. return typeof args[number] != 'undefined' ? args[number] : match;
  41. });
  42. };
  43. String.prototype.highlight = function (words, highlightTemplate) {
  44. var self = this;
  45. highlightTemplate = highlightTemplate ? highlightTemplate : "<b>{0}</b>";
  46. words.forEach(function (word) {
  47. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  48. self = self.replace(searchRegExp, function (found) {
  49. return highlightTemplate.format(found);
  50. });
  51. });
  52. return self;
  53. };
  54. Number.prototype.toDaysHoursMinutes = function () {
  55. var formatted = {},
  56. dateDiff = this,
  57. secK = 1000, //ms
  58. minK = 60 * secK, // sec
  59. hourK = 60 * minK, // sec
  60. dayK = 24 * hourK;
  61. dateDiff = parseInt(dateDiff);
  62. formatted.d = Math.floor(dateDiff / dayK);
  63. dateDiff -= formatted.d * dayK;
  64. formatted.h = Math.floor(dateDiff / hourK);
  65. dateDiff -= formatted.h * hourK;
  66. formatted.m = (dateDiff / minK).toFixed(2);
  67. return formatted;
  68. };
  69. /**
  70. Sort an array by the key specified in the argument.
  71. Handle only native js objects as element of array, not the Ember's object.
  72. Can be used as alternative to sortProperty method of Ember library
  73. in order to speed up executing on large data volumes
  74. @method sortBy
  75. @param {String} path name(s) to sort on
  76. @return {Array} The sorted array.
  77. */
  78. Array.prototype.sortPropertyLight = function (path) {
  79. var realPath = (typeof path === "string") ? path.split('.') : [];
  80. this.sort(function (a, b) {
  81. var aProperty = a;
  82. var bProperty = b;
  83. realPath.forEach(function (key) {
  84. aProperty = aProperty[key];
  85. bProperty = bProperty[key];
  86. });
  87. if (aProperty > bProperty) return 1;
  88. if (aProperty < bProperty) return -1;
  89. return 0;
  90. });
  91. return this;
  92. };
  93. Em.CoreObject.reopen({
  94. t:function (key, attrs) {
  95. return Em.I18n.t(key, attrs)
  96. }
  97. });
  98. Em.Handlebars.registerHelper('log', function (variable) {
  99. console.log(variable);
  100. });
  101. Em.Handlebars.registerHelper('warn', function (variable) {
  102. console.warn(variable);
  103. });
  104. Em.Handlebars.registerHelper('highlight', function (property, words, fn) {
  105. var context = (fn.contexts && fn.contexts[0]) || this;
  106. property = Em.Handlebars.getPath(context, property, fn);
  107. words = words.split(";");
  108. // if (highlightTemplate == undefined) {
  109. var highlightTemplate = "<b>{0}</b>";
  110. // }
  111. words.forEach(function (word) {
  112. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  113. property = property.replace(searchRegExp, function (found) {
  114. return highlightTemplate.format(found);
  115. });
  116. });
  117. return new Em.Handlebars.SafeString(property);
  118. });
  119. App = require('app');
  120. /**
  121. * Certain variables can have JSON in string
  122. * format, or in JSON format itself.
  123. */
  124. App.parseJSON = function (value) {
  125. if (typeof value == "string") {
  126. return jQuery.parseJSON(value);
  127. }
  128. return value;
  129. };
  130. /**
  131. * Check for empty <code>Object</code>, built in Em.isEmpty()
  132. * doesn't support <code>Object</code> type
  133. *
  134. * @params obj {Object}
  135. *
  136. * @return {Boolean}
  137. */
  138. App.isEmptyObject = function(obj) {
  139. var empty = true;
  140. for (var prop in obj) { if (obj.hasOwnProperty(prop)) {empty = false; break;} }
  141. return empty;
  142. }
  143. App.format = {
  144. /**
  145. * @type Object
  146. */
  147. components: {
  148. 'APP_TIMELINE_SERVER': 'App Timeline Server',
  149. 'DATANODE': 'DataNode',
  150. 'DECOMMISSION_DATANODE': 'Update Exclude File',
  151. 'DRPC_SERVER': 'DRPC Server',
  152. 'FALCON': 'Falcon',
  153. 'FALCON_CLIENT': 'Falcon Client',
  154. 'FALCON_SERVER': 'Falcon Server',
  155. 'FALCON_SERVICE_CHECK': 'Falcon Service Check',
  156. 'FLUME_SERVER': 'Flume Agent',
  157. 'GANGLIA_MONITOR': 'Ganglia Monitor',
  158. 'GANGLIA_SERVER': 'Ganglia Server',
  159. 'GLUSTERFS_CLIENT': 'GLUSTERFS Client',
  160. 'GLUSTERFS_SERVICE_CHECK': 'GLUSTERFS Service Check',
  161. 'GMETAD_SERVICE_CHECK': 'Gmetad Service Check',
  162. 'GMOND_SERVICE_CHECK': 'Gmond Service Check',
  163. 'HADOOP_CLIENT': 'Hadoop Client',
  164. 'HBASE_CLIENT': 'HBase Client',
  165. 'HBASE_MASTER': 'HBase Master',
  166. 'HBASE_REGIONSERVER': 'HBase RegionServer',
  167. 'HBASE_SERVICE_CHECK': 'HBase Service Check',
  168. 'HCAT': 'HCat',
  169. 'HCAT_SERVICE_CHECK': 'HCat Service Check',
  170. 'HDFS_CLIENT': 'HDFS Client',
  171. 'HDFS_SERVICE_CHECK': 'HDFS Service Check',
  172. 'HISTORYSERVER': 'History Server',
  173. 'HIVE_CLIENT': 'Hive Client',
  174. 'HIVE_METASTORE': 'Hive Metastore',
  175. 'HIVE_SERVER': 'HiveServer2',
  176. 'HIVE_SERVICE_CHECK': 'Hive Service Check',
  177. 'HUE_SERVER': 'Hue Server',
  178. 'JAVA_JCE': 'Java JCE',
  179. 'JOBTRACKER': 'JobTracker',
  180. 'JOBTRACKER_SERVICE_CHECK': 'JobTracker Service Check',
  181. 'JOURNALNODE': 'JournalNode',
  182. 'KERBEROS_ADMIN_CLIENT': 'Kerberos Admin Client',
  183. 'KERBEROS_CLIENT': 'Kerberos Client',
  184. 'KERBEROS_SERVER': 'Kerberos Server',
  185. 'LOGVIEWER_SERVER': 'Logviewer Server',
  186. 'MAPREDUCE2_CLIENT': 'MapReduce2 Client',
  187. 'MAPREDUCE2_SERVICE_CHECK': 'MapReduce2 Service Check',
  188. 'MAPREDUCE_CLIENT': 'MapReduce Client',
  189. 'MAPREDUCE_SERVICE_CHECK': 'MapReduce Service Check',
  190. 'MYSQL_SERVER': 'MySQL Server',
  191. 'NAGIOS_SERVER': 'Nagios Server',
  192. 'NAMENODE': 'NameNode',
  193. 'NAMENODE_SERVICE_CHECK': 'NameNode Service Check',
  194. 'NIMBUS': 'Nimbus',
  195. 'NODEMANAGER': 'NodeManager',
  196. 'OOZIE_CLIENT': 'Oozie Client',
  197. 'OOZIE_SERVER': 'Oozie Server',
  198. 'OOZIE_SERVICE_CHECK': 'Oozie Service Check',
  199. 'PIG': 'Pig',
  200. 'PIG_SERVICE_CHECK': 'Pig Service Check',
  201. 'RESOURCEMANAGER': 'ResourceManager',
  202. 'SECONDARY_NAMENODE': 'SNameNode',
  203. 'SQOOP': 'Sqoop',
  204. 'SQOOP_SERVICE_CHECK': 'Sqoop Service Check',
  205. 'STORM_REST_API': 'Storm REST API Server',
  206. 'STORM_SERVICE_CHECK': 'Storm Service Check',
  207. 'STORM_UI_SERVER': 'Storm UI Server',
  208. 'SUPERVISOR': 'Supervisor',
  209. 'TASKTRACKER': 'TaskTracker',
  210. 'TEZ_CLIENT': 'Tez Client',
  211. 'WEBHCAT_SERVER': 'WebHCat Server',
  212. 'WEBHCAT_SERVICE_CHECK': 'WebHCat Service Check',
  213. 'YARN_CLIENT': 'YARN Client',
  214. 'YARN_SERVICE_CHECK': 'YARN Service Check',
  215. 'ZKFC': 'ZKFailoverController',
  216. 'ZOOKEEPER_CLIENT': 'ZooKeeper Client',
  217. 'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
  218. 'ZOOKEEPER_SERVER': 'ZooKeeper Server',
  219. 'ZOOKEEPER_SERVICE_CHECK': 'ZooKeeper Service Check',
  220. 'CLIENT': 'client'
  221. },
  222. /**
  223. * @type Object
  224. */
  225. command: {
  226. 'INSTALL': 'Install',
  227. 'UNINSTALL': 'Uninstall',
  228. 'START': 'Start',
  229. 'STOP': 'Stop',
  230. 'EXECUTE': 'Execute',
  231. 'ABORT': 'Abort',
  232. 'UPGRADE': 'Upgrade',
  233. 'RESTART': 'Restart',
  234. 'SERVICE_CHECK': 'Check',
  235. 'Excluded:': 'Decommission:',
  236. 'Included:': 'Recommission:'
  237. },
  238. /**
  239. * convert role to readable string
  240. * @param role
  241. */
  242. role:function (role) {
  243. return this.components[role] ? this.components[role] : '';
  244. },
  245. /**
  246. * convert command_detail to readable string, show the string for all tasks name
  247. * @param command_detail
  248. */
  249. commandDetail: function (command_detail) {
  250. var detailArr = command_detail.split(' ');
  251. var self = this;
  252. var result = '';
  253. detailArr.forEach( function(item) {
  254. // if the item has the pattern SERVICE/COMPONENT, drop the SERVICE part
  255. if (item.contains('/')) {
  256. item = item.split('/')[1];
  257. }
  258. // ignore 'DECOMMISSION', command came from 'excluded/included'
  259. if (item == 'DECOMMISSION,') {
  260. item = '';
  261. }
  262. if (self.components[item]) {
  263. result = result + ' ' + self.components[item];
  264. } else if (self.command[item]) {
  265. result = result + ' ' + self.command[item];
  266. } else {
  267. result = result + ' ' + item;
  268. }
  269. });
  270. if (result === ' nagios_update_ignore ACTIONEXECUTE') {
  271. result = Em.I18n.t('common.maintenance.task');
  272. }
  273. return result;
  274. },
  275. /**
  276. * PENDING - Not queued yet for a host
  277. * QUEUED - Queued for a host
  278. * IN_PROGRESS - Host reported it is working
  279. * COMPLETED - Host reported success
  280. * FAILED - Failed
  281. * TIMEDOUT - Host did not respond in time
  282. * ABORTED - Operation was abandoned
  283. */
  284. taskStatus:function (_taskStatus) {
  285. return _taskStatus.toLowerCase();
  286. }
  287. };
  288. /**
  289. * wrapper to bootstrap popover
  290. * fix issue when popover stuck on view routing
  291. * @param self
  292. * @param options
  293. */
  294. App.popover = function(self, options) {
  295. self.popover(options);
  296. self.on("remove", function () {
  297. $(this).trigger('mouseleave');
  298. });
  299. };
  300. /**
  301. * wrapper to bootstrap tooltip
  302. * fix issue when tooltip stuck on view routing
  303. * @param self - DOM element
  304. * @param options
  305. */
  306. App.tooltip = function(self, options) {
  307. self.tooltip(options);
  308. self.on("remove", function () {
  309. $(this).trigger('mouseleave');
  310. });
  311. };
  312. /**
  313. * wrapper to Date().getTime()
  314. * fix issue when client clock and server clock not sync
  315. * @return timeStamp of current server clock
  316. */
  317. App.dateTime = function() {
  318. return new Date().getTime() + App.clockDistance;
  319. };
  320. /*
  321. * Helper function for bound property helper registration
  322. * @params name {String} - name of helper
  323. * @params view {Em.View} - view
  324. */
  325. App.registerBoundHelper = function(name, view) {
  326. Em.Handlebars.registerHelper(name, function(property, options) {
  327. options.hash.contentBinding = property;
  328. return Em.Handlebars.helpers.view.call(this, view, options);
  329. });
  330. };
  331. /*
  332. * Return singular or plural word based on Em.I18n property key.
  333. *
  334. * Example: {{pluralize hostsCount singular="t:host" plural="t:hosts"}}
  335. */
  336. App.registerBoundHelper('pluralize', Em.View.extend({
  337. tagName: 'span',
  338. template: Em.Handlebars.compile('{{view.wordOut}}'),
  339. wordOut: function() {
  340. var count, singular, plural;
  341. count = this.get('content');
  342. singular = this.get('singular');
  343. plural = this.get('plural');
  344. return this.getWord(count, singular, plural);
  345. }.property('content'),
  346. getWord: function(count, singular, plural) {
  347. singular = this.tDetect(singular);
  348. plural = this.tDetect(plural);
  349. if (singular && plural) {
  350. if (count > 1) {
  351. return plural;
  352. } else {
  353. return singular;
  354. }
  355. }
  356. return '';
  357. },
  358. /*
  359. * Detect for Em.I18n.t reference call
  360. * @params word {String}
  361. * return {String}
  362. */
  363. tDetect: function(word) {
  364. var splitted = word.split(':');
  365. if (splitted.length > 1 && splitted[0] == 't') {
  366. return Em.I18n.t(splitted[1]);
  367. } else {
  368. return splitted[0];
  369. }
  370. }
  371. })
  372. );
  373. /**
  374. * Return defined string instead of empty if value is null/undefined
  375. * by default is `n/a`.
  376. *
  377. * @param empty {String} - value instead of empty string (not required)
  378. * can be used with Em.I18n pass value started with't:'
  379. *
  380. * Examples:
  381. *
  382. * default value will be returned
  383. * {{formatNull service.someValue}}
  384. *
  385. * <code>empty<code> will be returned
  386. * {{formatNull service.someValue empty="I'm empty"}}
  387. *
  388. * Em.I18n translation will be returned
  389. * {{formatNull service.someValue empty="t:my.key.to.translate"
  390. */
  391. App.registerBoundHelper('formatNull', Em.View.extend({
  392. tagName: 'span',
  393. template: Em.Handlebars.compile('{{view.result}}'),
  394. result: function() {
  395. var emptyValue = this.get('empty') ? this.get('empty') : Em.I18n.t('services.service.summary.notAvailable');
  396. emptyValue = emptyValue.startsWith('t:') ? Em.I18n.t(emptyValue.substr(2, emptyValue.length)) : emptyValue;
  397. return (this.get('content') || this.get('content') == 0) ? this.get('content') : emptyValue;
  398. }.property('content')
  399. }));
  400. /**
  401. * Return formatted string with inserted <code>wbr</code>-tag after each dot
  402. *
  403. * @param {String} content
  404. *
  405. * Examples:
  406. *
  407. * returns 'apple'
  408. * {{formatWordBreak 'apple'}}
  409. *
  410. * returns 'apple.<wbr />banana'
  411. * {{formatWordBreak 'apple.banana'}}
  412. *
  413. * returns 'apple.<wbr />banana.<wbr />uranium'
  414. * {{formatWordBreak 'apple.banana.uranium'}}
  415. */
  416. App.registerBoundHelper('formatWordBreak', Em.View.extend({
  417. tagName: 'span',
  418. template: Em.Handlebars.compile('{{{view.result}}}'),
  419. /**
  420. * @type {string}
  421. */
  422. result: function() {
  423. return this.get('content').replace(/\./g, '.<wbr />');
  424. }.property('content')
  425. }));
  426. /**
  427. * Ambari overrides the default date transformer.
  428. * This is done because of the non-standard data
  429. * sent. For example Nagios sends date as "12345678".
  430. * The problem is that it is a String and is represented
  431. * only in seconds whereas Javascript's Date needs
  432. * milliseconds representation.
  433. */
  434. DS.attr.transforms.date = {
  435. from: function (serialized) {
  436. var type = typeof serialized;
  437. if (type === Em.I18n.t('common.type.string')) {
  438. serialized = parseInt(serialized);
  439. type = typeof serialized;
  440. }
  441. if (type === Em.I18n.t('common.type.number')) {
  442. if (!serialized ){ //serialized timestamp = 0;
  443. return 0;
  444. }
  445. // The number could be seconds or milliseconds.
  446. // If seconds, then the length is 10
  447. // If milliseconds, the length is 13
  448. if (serialized.toString().length < 13) {
  449. serialized = serialized * 1000;
  450. }
  451. return new Date(serialized);
  452. } else if (serialized === null || serialized === undefined) {
  453. // if the value is not present in the data,
  454. // return undefined, not null.
  455. return serialized;
  456. } else {
  457. return null;
  458. }
  459. },
  460. to: function (deserialized) {
  461. if (deserialized instanceof Date) {
  462. return deserialized.getTime();
  463. } else if (deserialized === undefined) {
  464. return undefined;
  465. } else {
  466. return null;
  467. }
  468. }
  469. };
  470. DS.attr.transforms.object = {
  471. from: function(serialized) {
  472. return Ember.none(serialized) ? null : Object(serialized);
  473. },
  474. to: function(deserialized) {
  475. return Ember.none(deserialized) ? null : Object(deserialized);
  476. }
  477. };
  478. /**
  479. * Allows EmberData models to have array properties.
  480. *
  481. * Declare the property as <code>
  482. * operations: DS.attr('array'),
  483. * </code> and
  484. * during load provide a JSON array for value.
  485. *
  486. * This transform simply assigns the same array in both directions.
  487. */
  488. DS.attr.transforms.array = {
  489. from : function(serialized) {
  490. return serialized;
  491. },
  492. to : function(deserialized) {
  493. return deserialized;
  494. }
  495. };