SessionLoggingPane.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2018
  6. * US Government Users Restricted Rights -
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'q', 'bi/commons/ui/properties/PropertyPageView', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/nls/StringResource'], function (_, Q, PropertyPageView, PropertyUIControl, StringResource) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var SessionLoggingPane = PropertyPageView.extend({
  12. sessionLoggingUrl: 'v1/glug/sessions/current',
  13. sessionConfigUrl: 'v1/glug/sessions',
  14. init: function init(options) {
  15. SessionLoggingPane.inherited('init', this, arguments);
  16. $.extend(this, options);
  17. },
  18. render: function render() {
  19. SessionLoggingPane.inherited('render', this, arguments); //get current session logging setting
  20. return this._getCurrentSession().then(function (defaults) {
  21. //check if this session is expired
  22. var defaultSession = this._renderCurrentSession(defaults);
  23. this.currentSession = defaultSession;
  24. this.enableSessionButton = {
  25. type: 'SingleLineLinks',
  26. multiline: false,
  27. name: 'enableSessionComb',
  28. id: 'enableSessionComb',
  29. items: [{
  30. align: 'left',
  31. items: [{
  32. type: 'text',
  33. value: StringResource.get('sessionLoggingBtn')
  34. }]
  35. }, {
  36. align: 'right',
  37. items: [{
  38. 'type': 'text',
  39. 'value': this.currentSession.enable === true ? StringResource.get('on') : StringResource.get('off'),
  40. 'class': 'enableSessionText'
  41. }, {
  42. 'type': 'ToggleButton',
  43. 'name': 'enableSession',
  44. 'id': 'enableSession',
  45. 'checked': this.currentSession.enable,
  46. 'onChange': function () {
  47. this.updateSessionLoggingConfiguration().then(function () {
  48. this.refresh();
  49. }.bind(this));
  50. }.bind(this)
  51. }]
  52. }]
  53. };
  54. this.logIdentifiers = {
  55. 'type': 'SingleLineValue',
  56. 'label': StringResource.get("logIdentifier"),
  57. 'editable': false,
  58. 'value': this.currentSession.logIdentifier,
  59. 'multiline': true,
  60. 'name': 'logIdentifiers',
  61. 'ellipses': false
  62. };
  63. var aControls = []; //if not enabled all turn grey
  64. if (!this.currentSession.enable) {
  65. this.logIdentifiers['disabled'] = true;
  66. this.logIdentifiers['multiline'] = false;
  67. this.enableSessionButton['checked'] = false;
  68. }
  69. aControls.push({
  70. 'value': StringResource.get('sessionLoggingTitle'),
  71. 'type': 'Banner'
  72. });
  73. aControls.push(this.enableSessionButton);
  74. aControls.push(this.logIdentifiers);
  75. aControls.push({
  76. 'type': 'SectionLabel',
  77. 'label': ''
  78. }, {
  79. 'type': 'TextArea',
  80. 'label': StringResource.get("sessionLoggingDescription")
  81. }, {
  82. 'type': 'TextArea',
  83. 'label': StringResource.get("sessionLoggingNoification")
  84. });
  85. this._oPropertyUIControl = this._getNewPropertyUIControl(aControls);
  86. return this._oPropertyUIControl.render();
  87. }.bind(this));
  88. },
  89. _getNewPropertyUIControl: function _getNewPropertyUIControl(items) {
  90. return new PropertyUIControl({
  91. 'el': this.$el,
  92. 'glassContext': this.glassContext,
  93. 'items': items
  94. });
  95. },
  96. _getCurrentSession: function _getCurrentSession() {
  97. var ajaxService = this.glassContext.services.ajax;
  98. var promise = ajaxService.ajax({
  99. 'dataType': 'json',
  100. 'type': 'GET',
  101. 'data': {},
  102. 'url': this.sessionLoggingUrl
  103. });
  104. return Q.when(promise);
  105. },
  106. _renderCurrentSession: function _renderCurrentSession(sessionJson) {
  107. var renderedObj;
  108. if (_.isUndefined(sessionJson.error)) {
  109. var renderedTime = this._doCalculation(sessionJson['expiry'], sessionJson['durationSec']);
  110. renderedObj = {
  111. 'enable': true,
  112. 'duration': renderedTime['duration'],
  113. 'startTime': renderedTime['startTime'],
  114. 'endTime': renderedTime['endTime'],
  115. 'logIdentifier': sessionJson['sessionID']
  116. };
  117. } else {
  118. renderedObj = {
  119. 'enable': false,
  120. 'duration': '1',
  121. 'startTime': '--',
  122. 'endTime': '--',
  123. 'logIdentifier': '--'
  124. };
  125. }
  126. return renderedObj;
  127. },
  128. _doCalculation: function _doCalculation(expiry, durationSec) {
  129. var renderedTime;
  130. var duration = durationSec / 3600;
  131. var endDate = new Date(expiry);
  132. var secToMilli = durationSec * 1000;
  133. var startDate = new Date(expiry - secToMilli);
  134. var startTime = this._formatTimeString(startDate);
  135. var endTime = this._formatTimeString(endDate);
  136. renderedTime = {
  137. 'duration': duration,
  138. 'startTime': startTime,
  139. 'endTime': endTime
  140. };
  141. return renderedTime;
  142. },
  143. _formatTimeString: function _formatTimeString(date) {
  144. var hours = date.getHours();
  145. var minutes = date.getMinutes();
  146. var ampm = hours >= 12 ? 'PM' : 'AM';
  147. hours = hours % 12; // the hour '0' should be '12'
  148. hours = hours ? hours : 12;
  149. minutes = minutes < 10 ? '0' + minutes : minutes;
  150. var year = date.getFullYear();
  151. var month = date.getMonth() + 1;
  152. var day = date.getDate();
  153. var formatTimeStr = hours + ':' + minutes + ' ' + ampm + ' ' + month + '/' + day + '/' + year;
  154. return formatTimeStr;
  155. },
  156. updateSessionLoggingConfiguration: function updateSessionLoggingConfiguration() {
  157. var uiControl = this.getPropertyUIControl();
  158. var modifiedProps = uiControl.getModifiedProperties();
  159. var duration = modifiedProps.sessionExpirationLevel;
  160. if (_.isUndefined(duration)) {
  161. duration = 1;
  162. }
  163. var active = modifiedProps.enableSession;
  164. var durationMin = duration * 60;
  165. if (active) {
  166. return this._enbaleSessionLogging(durationMin).then(function () {
  167. return this.glassContext.appController.showToast(StringResource.get('sessionSuccessMsg'), {
  168. 'type': 'success'
  169. });
  170. }.bind(this));
  171. } else {
  172. return this._disableSessionLogging().then(function () {
  173. return this.glassContext.appController.showToast(StringResource.get('sessionSuccessDisableMsg'), {
  174. 'type': 'success'
  175. });
  176. }.bind(this));
  177. } //be defaultly period n hours is 1
  178. //check if enabling or disabling
  179. //update and update the value
  180. },
  181. _enbaleSessionLogging: function _enbaleSessionLogging(durationMin) {
  182. var loggingSettingsDeferred;
  183. var loggingSettings = {
  184. "durationMin": durationMin
  185. };
  186. var ajaxService = this.glassContext.services.ajax;
  187. loggingSettingsDeferred = ajaxService.ajax({
  188. 'type': 'POST',
  189. 'url': this.sessionConfigUrl,
  190. 'contentType': 'application/json',
  191. 'data': JSON.stringify(loggingSettings)
  192. });
  193. return Q.when(loggingSettingsDeferred);
  194. },
  195. _disableSessionLogging: function _disableSessionLogging() {
  196. var loggingSettingsDeferred;
  197. var ajaxService = this.glassContext.services.ajax;
  198. loggingSettingsDeferred = ajaxService.ajax({
  199. 'dataType': 'json',
  200. 'type': 'DELETE',
  201. 'data': {},
  202. 'url': this.sessionLoggingUrl
  203. });
  204. return Q.when(loggingSettingsDeferred);
  205. },
  206. refresh: function refresh() {
  207. //remove all the items then render
  208. this._oPropertyUIControl.remove();
  209. this.render();
  210. }
  211. });
  212. return SessionLoggingPane;
  213. });