Statistics.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. define([ "require", "backbone", "hbs!tmpl/site/Statistics_tmpl", "hbs!tmpl/site/Statistics_Notification_table_tmpl", "hbs!tmpl/site/Statistics_Topic_Offset_table_tmpl", "hbs!tmpl/site/entity_tmpl", "modules/Modal", "models/VCommon", "utils/UrlLinks", "collection/VTagList", "utils/CommonViewFunction", "utils/Enums", "utils/MigrationEnums", "moment", "utils/Utils", "utils/Globals", "moment-timezone" ], function(require, Backbone, StatTmpl, StatsNotiTable, TopicOffsetTable, EntityTable, Modal, VCommon, UrlLinks, VTagList, CommonViewFunction, Enums, MigrationEnums, moment, Utils, Globals) {
  2. "use strict";
  3. var StatisticsView = Backbone.Marionette.LayoutView.extend({
  4. template: StatTmpl,
  5. regions: {},
  6. ui: {
  7. entity: "[data-id='entity']",
  8. classification: "[data-id='classification']",
  9. serverCard: "[data-id='server-card']",
  10. connectionCard: "[data-id='connection-card']",
  11. notificationCard: "[data-id='notification-card']",
  12. statsNotificationTable: "[data-id='stats-notification-table']",
  13. entityCard: "[data-id='entity-card']",
  14. classificationCard: "[data-id='classification-card']",
  15. offsetCard: "[data-id='offset-card']",
  16. osCard: "[data-id='os-card']",
  17. runtimeCard: "[data-id='runtime-card']",
  18. memoryCard: "[data-id='memory-card']",
  19. memoryPoolUsage: "[data-id='memory-pool-usage-card']",
  20. statisticsRefresh: "[data-id='statisticsRefresh']",
  21. notificationDetails: "[data-id='notificationDetails']",
  22. migrationProgressBar: "[data-id='migrationProgressBar']",
  23. migrationProgressBarValue: "[data-id='migrationProgressBarValue']"
  24. },
  25. events: function() {
  26. var events = {};
  27. return events["click " + this.ui.statisticsRefresh] = function(e) {
  28. this.showLoader(), this.fetchMetricData(), this.fetchStatusData();
  29. }, events;
  30. },
  31. initialize: function(options) {
  32. _.extend(this, options);
  33. var that = this;
  34. if (this.DATA_MAX_LENGTH = 25, this.loaderCount = 0, this.isMigrationView) this.migrationImportStatus = new VTagList(),
  35. this.migrationImportStatus.url = UrlLinks.migrationStatusApiUrl(); else {
  36. var modal = new Modal({
  37. title: "统计信息",
  38. content: this,
  39. okCloses: !0,
  40. okText: "Close",
  41. showFooter: !0,
  42. allowCancel: !1,
  43. width: "60%",
  44. headerButtons: [ {
  45. title: "Refresh Data",
  46. btnClass: "fa fa-refresh",
  47. onClick: function() {
  48. modal.$el.find(".header-button .fa-refresh").tooltip("hide").prop("disabled", !0).addClass("fa-spin"),
  49. that.fetchMetricData({
  50. update: !0
  51. });
  52. }
  53. } ]
  54. });
  55. modal.on("closeModal", function() {
  56. modal.trigger("cancel");
  57. }), this.modal = modal, modal.open();
  58. }
  59. },
  60. bindEvents: function() {
  61. var that = this;
  62. this.modal && this.$el.on("click", ".linkClicked", function() {
  63. that.modal.close();
  64. });
  65. },
  66. fetchStatusData: function() {
  67. var that = this;
  68. ++this.loaderCount, that.migrationImportStatus.fetch({
  69. success: function(data) {
  70. var data = _.first(data.toJSON()), migrationStatus = data.MigrationStatus || null, operationStatus = migrationStatus.operationStatus, showProgress = !0, totalProgress = 0, progressMessage = "";
  71. if (migrationStatus) {
  72. if ("DONE" === MigrationEnums.migrationStatus[operationStatus]) showProgress = !1; else if ("IN_PROGRESS" === MigrationEnums.migrationStatus[operationStatus] || "STARTED" === MigrationEnums.migrationStatus[operationStatus]) {
  73. migrationStatus.currentIndex || 0, migrationStatus.totalCount || 0;
  74. totalProgress = Math.ceil(migrationStatus.currentIndex / migrationStatus.totalCount * 100),
  75. progressMessage = totalProgress + "%", that.ui.migrationProgressBar.removeClass("progress-bar-danger"),
  76. that.ui.migrationProgressBar.addClass("progress-bar-success");
  77. } else "FAIL" === MigrationEnums.migrationStatus[operationStatus] && (totalProgress = "100",
  78. progressMessage = "Failed", that.ui.migrationProgressBar.addClass("progress-bar-danger"),
  79. that.ui.migrationProgressBar.removeClass("progress-bar-success"));
  80. showProgress ? (that.$el.find(".statistics-header>.progress").removeClass("hide"),
  81. that.$el.find(".statistics-header>.successStatus").addClass("hide"), that.ui.migrationProgressBar.css({
  82. width: totalProgress + "%"
  83. }), that.ui.migrationProgressBarValue.text(progressMessage)) : (that.$el.find(".statistics-header>.progress").addClass("hide"),
  84. that.$el.find(".statistics-header>.successStatus").removeClass("hide"));
  85. }
  86. },
  87. complete: function() {
  88. --that.loaderCount, that.hideLoader();
  89. }
  90. });
  91. },
  92. fetchMetricData: function(options) {
  93. var that = this;
  94. ++this.loaderCount, this.metricCollection.fetch({
  95. success: function(data) {
  96. var data = _.first(data.toJSON());
  97. that.renderStats({
  98. valueObject: data.general.stats,
  99. dataObject: data.general
  100. }), that.renderEntities({
  101. data: data
  102. }), that.renderSystemDeatils({
  103. data: data
  104. }), that.renderClassifications({
  105. data: data
  106. }), options && options.update && (that.modal && that.modal.$el.find(".header-button .fa-refresh").prop("disabled", !1).removeClass("fa-spin"),
  107. Utils.notifySuccess({
  108. content: "Metric data is refreshed"
  109. }));
  110. },
  111. complete: function() {
  112. --that.loaderCount, that.hideLoader();
  113. }
  114. });
  115. },
  116. hideLoader: function() {
  117. if (0 === this.loaderCount) {
  118. var className = ".statsContainer";
  119. this.isMigrationView && (className += ",.statistics-header"), this.$(className).removeClass("hide"),
  120. this.$(".statsLoader").removeClass("show");
  121. }
  122. },
  123. showLoader: function() {
  124. var className = ".statsContainer";
  125. this.isMigrationView && (className += ",.statistics-header"), this.$(className).addClass("hide"),
  126. this.$(".statsLoader").addClass("show");
  127. },
  128. onRender: function() {
  129. this.bindEvents(), this.isMigrationView && (this.showLoader(), this.fetchStatusData()),
  130. this.fetchMetricData();
  131. },
  132. closePanel: function(options) {
  133. var el = options.el;
  134. el.find(">.panel-heading").attr("aria-expanded", "false"), el.find(">.panel-collapse.collapse").removeClass("in");
  135. },
  136. genrateStatusData: function(stateObject) {
  137. var stats = {};
  138. return _.each(stateObject, function(val, key) {
  139. var keys = key.split(":"), key = keys[0], subKey = keys[1];
  140. stats[key] ? stats[key][subKey] = val : (stats[key] = {}, stats[key][subKey] = val);
  141. }), stats;
  142. },
  143. createTable: function(obj) {
  144. var that = this, tableBody = "", type = obj.type, data = obj.data;
  145. return _.each(data, function(value, key, list) {
  146. var newValue = that.getValue({
  147. value: value
  148. });
  149. "classification" === type && (newValue = "<a title=\"Search for entities associated with '" + key + '\'" class="linkClicked" href="#!/search/searchResult?searchType=basic&tag=' + key + '">' + newValue + "<a>"),
  150. tableBody += "<tr><td>" + key + '</td><td class="">' + newValue + "</td></tr>";
  151. }), tableBody;
  152. },
  153. renderClassifications: function(options) {
  154. var that = this, data = options.data, classificationData = data.tag || {}, tagEntitiesData = classificationData ? classificationData.tagEntities || {} : {}, tagsCount = 0, newTagEntitiesData = {}, tagEntitiesKeys = _.keys(tagEntitiesData);
  155. _.each(_.sortBy(tagEntitiesKeys, function(o) {
  156. return o.toLocaleLowerCase();
  157. }), function(key) {
  158. var val = tagEntitiesData[key];
  159. newTagEntitiesData[key] = val, tagsCount += val;
  160. }), tagEntitiesData = newTagEntitiesData, _.isEmpty(tagEntitiesData) || (this.ui.classificationCard.html(that.createTable({
  161. data: tagEntitiesData,
  162. type: "classification"
  163. })), this.ui.classification.find(".count").html("&nbsp;(" + _.numberFormatWithComma(tagsCount) + ")"),
  164. tagEntitiesKeys.length > this.DATA_MAX_LENGTH && this.closePanel({
  165. el: this.ui.classification
  166. }));
  167. },
  168. renderEntities: function(options) {
  169. var data = options.data, entityData = data.entity, activeEntities = entityData.entityActive || {}, deletedEntities = entityData.entityDeleted || {}, shellEntities = entityData.entityShell || {}, stats = {}, activeEntityCount = 0, deletedEntityCount = 0, shellEntityCount = 0, createEntityData = function(opt) {
  170. var entityData = opt.entityData, type = opt.type;
  171. _.each(entityData, function(val, key) {
  172. var intVal = _.isUndefined(val) ? 0 : val;
  173. "active" == type && (activeEntityCount += intVal), "deleted" == type && (deletedEntityCount += intVal),
  174. "shell" == type && (shellEntityCount += intVal), intVal = _.numberFormatWithComma(intVal),
  175. stats[key] ? stats[key][type] = intVal : (stats[key] = {}, stats[key][type] = intVal);
  176. });
  177. };
  178. if (createEntityData({
  179. entityData: activeEntities,
  180. type: "active"
  181. }), createEntityData({
  182. entityData: deletedEntities,
  183. type: "deleted"
  184. }), createEntityData({
  185. entityData: shellEntities,
  186. type: "shell"
  187. }), !_.isEmpty(stats)) {
  188. var statsKeys = _.keys(stats);
  189. this.ui.entityCard.html(EntityTable({
  190. data: _.pick(stats, _.sortBy(statsKeys, function(o) {
  191. return o.toLocaleLowerCase();
  192. }))
  193. })), this.$('[data-id="activeEntity"]').html("&nbsp;(" + _.numberFormatWithComma(activeEntityCount) + ")"),
  194. this.$('[data-id="deletedEntity"]').html("&nbsp;(" + _.numberFormatWithComma(deletedEntityCount) + ")"),
  195. this.$('[data-id="shellEntity"]').html("&nbsp;(" + _.numberFormatWithComma(shellEntityCount) + ")"),
  196. this.ui.entity.find(".count").html("&nbsp;(" + _.numberFormatWithComma(data.general.entityCount) + ")"),
  197. statsKeys.length > this.DATA_MAX_LENGTH && this.closePanel({
  198. el: this.ui.entity
  199. });
  200. }
  201. },
  202. renderStats: function(options) {
  203. var that = this, data = this.genrateStatusData(options.valueObject), generalData = options.dataObject, createTable = function(obj) {
  204. var tableBody = "", enums = obj.enums, data = obj.data;
  205. return _.each(data, function(value, key, list) {
  206. tableBody += "<tr><td>" + key + '</td><td class="">' + that.getValue({
  207. value: value,
  208. type: enums[key]
  209. }) + "</td></tr>";
  210. }), tableBody;
  211. };
  212. if (!that.isMigrationView && data.Notification) {
  213. var tableCol = [ {
  214. label: "Total <br> (from " + that.getValue({
  215. value: data.Server.startTimeStamp,
  216. type: Enums.stats.Server.startTimeStamp
  217. }) + ")",
  218. key: "total"
  219. }, {
  220. label: "Current Hour <br> (from " + that.getValue({
  221. value: data.Notification.currentHourStartTime,
  222. type: Enums.stats.Notification.currentHourStartTime
  223. }) + ")",
  224. key: "currentHour"
  225. }, {
  226. label: "Previous Hour",
  227. key: "previousHour"
  228. }, {
  229. label: "Current Day <br> (from " + that.getValue({
  230. value: data.Notification.currentDayStartTime,
  231. type: Enums.stats.Notification.currentDayStartTime
  232. }) + ")",
  233. key: "currentDay"
  234. }, {
  235. label: "Previous Day",
  236. key: "previousDay"
  237. } ], tableHeader = [ "count", "AvgTime", "EntityCreates", "EntityUpdates", "EntityDeletes", "Failed" ];
  238. that.ui.notificationCard.html(StatsNotiTable({
  239. enums: Enums.stats.Notification,
  240. data: data.Notification,
  241. tableHeader: tableHeader,
  242. tableCol: tableCol,
  243. getTmplValue: function(argument, args) {
  244. var pickValueFrom = argument.key.concat(args);
  245. "total" == argument.key && "EntityCreates" == args ? pickValueFrom = "totalCreates" : "total" == argument.key && "EntityUpdates" == args ? pickValueFrom = "totalUpdates" : "total" == argument.key && "EntityDeletes" == args ? pickValueFrom = "totalDeletes" : "count" == args && (pickValueFrom = argument.key);
  246. var returnVal = data.Notification[pickValueFrom];
  247. return returnVal ? _.numberFormatWithComma(returnVal) : 0;
  248. }
  249. }));
  250. var offsetTableColumn = function(obj) {
  251. var returnObj = [];
  252. return _.each(obj, function(value, key) {
  253. returnObj.push({
  254. label: key,
  255. dataValue: value
  256. });
  257. }), returnObj;
  258. };
  259. that.ui.offsetCard.html(TopicOffsetTable({
  260. data: data.Notification.topicDetails,
  261. tableHeader: [ "offsetStart", "offsetCurrent", "processedMessageCount", "failedMessageCount", "lastMessageProcessedTime" ],
  262. tableCol: offsetTableColumn(data.Notification.topicDetails),
  263. getTmplValue: function(argument, args) {
  264. var returnVal = data.Notification.topicDetails[argument.label][args];
  265. return returnVal ? that.getValue({
  266. value: returnVal,
  267. type: Enums.stats.Notification[args]
  268. }) : 0;
  269. }
  270. })), that.ui.notificationDetails.removeClass("hide");
  271. }
  272. data.Server && that.ui.serverCard.html(createTable({
  273. enums: _.extend(Enums.stats.Server, Enums.stats.ConnectionStatus, Enums.stats.generalData),
  274. data: _.extend(_.pick(data.Server, "startTimeStamp", "activeTimeStamp", "upTime", "statusBackendStore", "statusIndexStore"), _.pick(generalData, "collectionTime"))
  275. }));
  276. },
  277. renderSystemDeatils: function(options) {
  278. var that = this, data = options.data, systemData = data.system, systemOS = systemData.os || {}, systemRuntimeData = systemData.runtime || {}, systemMemoryData = systemData.memory || {};
  279. if (_.isEmpty(systemOS) || that.ui.osCard.html(that.createTable({
  280. data: systemOS
  281. })), _.isEmpty(systemRuntimeData) || (_.each(systemRuntimeData, function(val, key) {
  282. }), that.ui.runtimeCard.html(that.createTable({
  283. data: systemRuntimeData
  284. }))), !_.isEmpty(systemMemoryData)) {
  285. var memoryTable = CommonViewFunction.propertyTable({
  286. scope: this,
  287. formatStringVal: !0,
  288. valueObject: systemMemoryData,
  289. numberFormat: _.numberFormatWithBytes
  290. });
  291. that.ui.memoryCard.html(memoryTable);
  292. }
  293. },
  294. getValue: function(options) {
  295. var value = options.value, type = options.type;
  296. return "time" == type ? Utils.millisecondsToTime(value) : "day" == type ? Utils.formatDate({
  297. date: value
  298. }) : "number" == type ? _.numberFormatWithComma(value) : "millisecond" == type ? _.numberFormatWithComma(value) + " millisecond/s" : "status-html" == type ? '<span class="connection-status ' + value + '"></span>' : value;
  299. }
  300. });
  301. return StatisticsView;
  302. });