helper.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. Em.CoreObject.reopen({
  70. t:function (key, attrs) {
  71. return Em.I18n.t(key, attrs)
  72. }
  73. });
  74. Em.Handlebars.registerHelper('log', function (variable) {
  75. console.log(variable);
  76. });
  77. Em.Handlebars.registerHelper('warn', function (variable) {
  78. console.warn(variable);
  79. });
  80. Em.Handlebars.registerHelper('highlight', function (property, words, fn) {
  81. var context = (fn.contexts && fn.contexts[0]) || this;
  82. property = Em.Handlebars.getPath(context, property, fn);
  83. words = words.split(";");
  84. // if (highlightTemplate == undefined) {
  85. var highlightTemplate = "<b>{0}</b>";
  86. // }
  87. words.forEach(function (word) {
  88. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  89. property = property.replace(searchRegExp, function (found) {
  90. return highlightTemplate.format(found);
  91. });
  92. });
  93. return new Em.Handlebars.SafeString(property);
  94. });
  95. App = require('app');
  96. /**
  97. * Certain variables can have JSON in string
  98. * format, or in JSON format itself.
  99. */
  100. App.parseJSON = function (value) {
  101. if (typeof value == "string") {
  102. return jQuery.parseJSON(value);
  103. }
  104. return value;
  105. };
  106. App.format = {
  107. /**
  108. * @type Object
  109. */
  110. components: {
  111. 'APP_TIMELINE_SERVER': 'App Timeline Server',
  112. 'DATANODE': 'DataNode',
  113. 'DECOMMISSION_DATANODE': 'Update Exclude File',
  114. 'DRPC_SERVER': 'DRPC Server',
  115. 'FALCON': 'Falcon',
  116. 'FALCON_CLIENT': 'Falcon Client',
  117. 'FALCON_SERVER': 'Falcon Server',
  118. 'FALCON_SERVICE_CHECK': 'Falcon Service Check',
  119. 'FLUME_SERVER': 'Flume Agent',
  120. 'GANGLIA_MONITOR': 'Ganglia Monitor',
  121. 'GANGLIA_SERVER': 'Ganglia Server',
  122. 'GLUSTERFS_CLIENT': 'GLUSTERFS Client',
  123. 'GLUSTERFS_SERVICE_CHECK': 'GLUSTERFS Service Check',
  124. 'GMETAD_SERVICE_CHECK': 'Gmetad Service Check',
  125. 'GMOND_SERVICE_CHECK': 'Gmond Service Check',
  126. 'HADOOP_CLIENT': 'Hadoop Client',
  127. 'HBASE_CLIENT': 'HBase Client',
  128. 'HBASE_MASTER': 'HBase Master',
  129. 'HBASE_REGIONSERVER': 'HBase RegionServer',
  130. 'HBASE_SERVICE_CHECK': 'HBase Service Check',
  131. 'HCAT': 'HCat',
  132. 'HCAT_SERVICE_CHECK': 'HCat Service Check',
  133. 'HDFS_CLIENT': 'HDFS Client',
  134. 'HDFS_SERVICE_CHECK': 'HDFS Service Check',
  135. 'HISTORYSERVER': 'History Server',
  136. 'HIVE_CLIENT': 'Hive Client',
  137. 'HIVE_METASTORE': 'Hive Metastore',
  138. 'HIVE_SERVER': 'HiveServer2',
  139. 'HIVE_SERVICE_CHECK': 'Hive Service Check',
  140. 'HUE_SERVER': 'Hue Server',
  141. 'JAVA_JCE': 'Java JCE',
  142. 'JOBTRACKER': 'JobTracker',
  143. 'JOBTRACKER_SERVICE_CHECK': 'JobTracker Service Check',
  144. 'JOURNALNODE': 'JournalNode',
  145. 'KERBEROS_ADMIN_CLIENT': 'Kerberos Admin Client',
  146. 'KERBEROS_CLIENT': 'Kerberos Client',
  147. 'KERBEROS_SERVER': 'Kerberos Server',
  148. 'LOGVIEWER_SERVER': 'Logviewer Server',
  149. 'MAPREDUCE2_CLIENT': 'MapReduce2 Client',
  150. 'MAPREDUCE2_SERVICE_CHECK': 'MapReduce2 Service Check',
  151. 'MAPREDUCE_CLIENT': 'MapReduce Client',
  152. 'MAPREDUCE_SERVICE_CHECK': 'MapReduce Service Check',
  153. 'MYSQL_SERVER': 'MySQL Server',
  154. 'NAGIOS_SERVER': 'Nagios Server',
  155. 'NAMENODE': 'NameNode',
  156. 'NAMENODE_SERVICE_CHECK': 'NameNode Service Check',
  157. 'NIMBUS': 'Nimbus',
  158. 'NODEMANAGER': 'NodeManager',
  159. 'OOZIE_CLIENT': 'Oozie Client',
  160. 'OOZIE_SERVER': 'Oozie Server',
  161. 'OOZIE_SERVICE_CHECK': 'Oozie Service Check',
  162. 'PIG': 'Pig',
  163. 'PIG_SERVICE_CHECK': 'Pig Service Check',
  164. 'RESOURCEMANAGER': 'ResourceManager',
  165. 'SECONDARY_NAMENODE': 'SNameNode',
  166. 'SQOOP': 'Sqoop',
  167. 'SQOOP_SERVICE_CHECK': 'Sqoop Service Check',
  168. 'STORM_SERVICE_CHECK': 'Storm Service Check',
  169. 'STORM_UI_SERVER': 'Storm UI Server',
  170. 'SUPERVISOR': 'Supervisor',
  171. 'TASKTRACKER': 'TaskTracker',
  172. 'TEZ_CLIENT': 'Tez Client',
  173. 'WEBHCAT_SERVER': 'WebHCat Server',
  174. 'WEBHCAT_SERVICE_CHECK': 'WebHCat Service Check',
  175. 'YARN_CLIENT': 'YARN Client',
  176. 'YARN_SERVICE_CHECK': 'YARN Service Check',
  177. 'ZKFC': 'ZKFailoverController',
  178. 'ZOOKEEPER_CLIENT': 'ZooKeeper Client',
  179. 'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
  180. 'ZOOKEEPER_SERVER': 'ZooKeeper Server',
  181. 'ZOOKEEPER_SERVICE_CHECK': 'ZooKeeper Service Check'
  182. },
  183. /**
  184. * @type Object
  185. */
  186. command: {
  187. 'INSTALL': 'Install',
  188. 'UNINSTALL': 'Uninstall',
  189. 'START': 'Start',
  190. 'STOP': 'Stop',
  191. 'EXECUTE': 'Execute',
  192. 'ABORT': 'Abort',
  193. 'UPGRADE': 'Upgrade',
  194. 'RESTART': 'Restart',
  195. 'SERVICE_CHECK': 'Check',
  196. 'DECOMMISSION,': 'Decommission,'
  197. },
  198. /**
  199. * convert role to readable string
  200. * @param role
  201. */
  202. role:function (role) {
  203. return this.components[role] ? this.components[role] : '';
  204. },
  205. /**
  206. * convert command_detail to readable string, show the string for all tasks name
  207. * @param command_detail
  208. */
  209. commandDetail: function (command_detail) {
  210. var detailArr = command_detail.split(' ');
  211. var self = this;
  212. var result = '';
  213. detailArr.forEach( function(item) {
  214. // if the item has the pattern SERVICE/COMPONENT, drop the SERVICE part
  215. if (item.contains('/')) {
  216. item = item.split('/')[1];
  217. }
  218. if (self.components[item]) {
  219. result = result + ' ' + self.components[item];
  220. } else if (self.command[item]) {
  221. result = result + ' ' + self.command[item];
  222. } else {
  223. result = result + ' ' + item;
  224. }
  225. });
  226. if (result === ' nagios_update_ignore ACTIONEXECUTE') {
  227. result = Em.I18n.t('common.maintenance.task');
  228. }
  229. return result;
  230. },
  231. /**
  232. * PENDING - Not queued yet for a host
  233. * QUEUED - Queued for a host
  234. * IN_PROGRESS - Host reported it is working
  235. * COMPLETED - Host reported success
  236. * FAILED - Failed
  237. * TIMEDOUT - Host did not respond in time
  238. * ABORTED - Operation was abandoned
  239. */
  240. taskStatus:function (_taskStatus) {
  241. return _taskStatus.toLowerCase();
  242. }
  243. };
  244. /**
  245. * wrapper to bootstrap popover
  246. * fix issue when popover stuck on view routing
  247. * @param self
  248. * @param options
  249. */
  250. App.popover = function(self, options) {
  251. self.popover(options);
  252. self.on("remove", function () {
  253. $(this).trigger('mouseleave');
  254. });
  255. };
  256. /**
  257. * wrapper to bootstrap tooltip
  258. * fix issue when tooltip stuck on view routing
  259. * @param self - DOM element
  260. * @param options
  261. */
  262. App.tooltip = function(self, options) {
  263. self.tooltip(options);
  264. self.on("remove", function () {
  265. $(this).trigger('mouseleave');
  266. });
  267. };
  268. /**
  269. * wrapper to Date().getTime()
  270. * fix issue when client clock and server clock not sync
  271. * @return timeStamp of current server clock
  272. */
  273. App.dateTime = function() {
  274. return new Date().getTime() + App.clockDistance;
  275. };
  276. /*
  277. * Helper function for bound property helper registration
  278. * @params name {String} - name of helper
  279. * @params view {Em.View} - view
  280. */
  281. App.registerBoundHelper = function(name, view) {
  282. Em.Handlebars.registerHelper(name, function(property, options) {
  283. options.hash.contentBinding = property;
  284. return Em.Handlebars.helpers.view.call(this, view, options);
  285. });
  286. };
  287. /*
  288. * Return singular or plural word based on Em.I18n property key.
  289. *
  290. * Example: {{pluralize hostsCount singular="t:host" plural="t:hosts"}}
  291. */
  292. App.registerBoundHelper('pluralize', Em.View.extend({
  293. tagName: 'span',
  294. template: Em.Handlebars.compile('{{view.wordOut}}'),
  295. wordOut: function() {
  296. var count, singular, plural;
  297. count = this.get('content');
  298. singular = this.get('singular');
  299. plural = this.get('plural');
  300. return this.getWord(count, singular, plural);
  301. }.property('content'),
  302. getWord: function(count, singular, plural) {
  303. singular = this.tDetect(singular);
  304. plural = this.tDetect(plural);
  305. if (singular && plural) {
  306. if (count > 1) {
  307. return plural;
  308. } else {
  309. return singular;
  310. }
  311. }
  312. return '';
  313. },
  314. /*
  315. * Detect for Em.I18n.t reference call
  316. * @params word {String}
  317. * return {String}
  318. */
  319. tDetect: function(word) {
  320. var splitted = word.split(':');
  321. if (splitted.length > 1 && splitted[0] == 't') {
  322. return Em.I18n.t(splitted[1]);
  323. } else {
  324. return splitted[0];
  325. }
  326. }
  327. })
  328. );