log_file_search_view.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. var App = require('app');
  19. var filters = require('views/common/filter_view');
  20. /**
  21. * @augments App.InfiniteScrollMixin
  22. * @type {Em.View}
  23. */
  24. App.LogFileSearchView = Em.View.extend(App.InfiniteScrollMixin, {
  25. classNames: ['log-file-search'],
  26. templateName: require('templates/common/log_file_search'),
  27. logLevels: ['fatal', 'critical', 'error', 'warning', 'info', 'debug'],
  28. /**
  29. * @typedef {Em.Object} FilterKeyword
  30. * @property {Boolean} isIncluded determines include/exclude status of keyword
  31. * @property {String} id unique identifier
  32. * @property {String} value keyword value
  33. */
  34. /**
  35. * Stores all selected keywords.
  36. *
  37. * @type {FilterKeyword[]}
  38. */
  39. selectedKeywords: [],
  40. selectedKeywordsDidChange: function() {
  41. this.fetchContent();
  42. }.observes('selectedKeywords.length'),
  43. levelsContext: function() {
  44. var self = this;
  45. var levels = this.get('logLevels');
  46. return Em.A(levels.map(function(level) {
  47. return Em.Object.create({name: level.toUpperCase(), counter: 0, displayName: level.capitalize(), checked: false});
  48. }));
  49. }.property(),
  50. levelsContextDidChange: function(e) {
  51. this.fetchContent();
  52. }.observes('levelsContext.@each.checked'),
  53. /** mock data **/
  54. content: function() {
  55. var data = [{
  56. message: 'java.lang.NullPointerException',
  57. date: '05.12.2016, 10:10:20',
  58. level: 'INFO'
  59. },
  60. {
  61. message: 'java.lang.NullPointerException',
  62. date: '05.12.2016, 10:10:20',
  63. level: 'ERROR'
  64. }];
  65. var initialSize = 20;
  66. var ret = [];
  67. for (var i = 0; i < 20; i++) {
  68. ret.push(Em.Object.create(data[Math.ceil(Math.random()*2) - 1]));
  69. }
  70. return ret;
  71. }.property(),
  72. contentDidChange: function() {
  73. this.refreshLevelCounters();
  74. }.observes('content.length'),
  75. dateFromValue: null,
  76. dateToValue: null,
  77. keywordsFilterView: filters.createTextView({
  78. layout: Em.Handlebars.compile('{{yield}}')
  79. }),
  80. keywordsFilterValue: null,
  81. didInsertElement: function() {
  82. this._super();
  83. this.infiniteScrollInit(this.$().find('.log-file-search-content'), {
  84. callback: this.loadMore.bind(this)
  85. });
  86. this.$().find('.log-file-search-content').contextmenu({
  87. target: '#log-file-search-item-context-menu'
  88. });
  89. this.refreshLevelCounters();
  90. },
  91. /** mock data **/
  92. loadMore: function() {
  93. var dfd = $.Deferred();
  94. var self = this;
  95. setTimeout(function() {
  96. var data = self.get('content');
  97. self.get('content').pushObjects(data.slice(0, 10));
  98. dfd.resolve();
  99. }, Math.ceil(Math.random()*4000));
  100. return dfd.promise();
  101. },
  102. refreshLevelCounters: function() {
  103. var self = this;
  104. this.get('logLevels').forEach(function(level) {
  105. var levelContext = self.get('levelsContext').findProperty('name', level.toUpperCase());
  106. levelContext.set('counter', self.get('content').filterProperty('level', level.toUpperCase()).length);
  107. });
  108. },
  109. /**
  110. * Make request and get content with applied filters.
  111. */
  112. fetchContent: function() {
  113. console.debug('Make Request with params:', this.serializeFilters());
  114. },
  115. submitKeywordsValue: function() {
  116. this.fetchContent();
  117. },
  118. serializeFilters: function() {
  119. var levels = this.serializeLevelFilters();
  120. var keywords = this.serializeKeywordsFilter();
  121. var date = this.serializeDateFilter();
  122. var includedExcludedKeywords = this.serializeIncludedExcludedKeywordFilter();
  123. return [levels, keywords, date, includedExcludedKeywords].compact().join('&');
  124. },
  125. serializeKeywordsFilter: function() {
  126. return !!this.get('keywordsFilterValue') ? 'keywords=' + this.get('keywordsFilterValue'): null;
  127. },
  128. serializeDateFilter: function() {
  129. var dateFrom = !!this.get('dateFromValue') ? 'dateFrom=' + this.get('dateFromValue') : null;
  130. var dateTo = !!this.get('dateToValue') ? 'dateTo=' + this.get('dateFromValue') : null;
  131. var ret = [dateTo, dateFrom].compact();
  132. return ret.length ? ret.join('&') : null;
  133. },
  134. serializeLevelFilters: function() {
  135. var selectedLevels = this.get('levelsContext').filterProperty('checked').mapProperty('name');
  136. return selectedLevels.length ? 'levels=' + selectedLevels.join(',') : null;
  137. },
  138. serializeIncludedExcludedKeywordFilter: function() {
  139. var self = this;
  140. var getValues = function(included) {
  141. return self.get('selectedKeywords').filterProperty('isIncluded', included).mapProperty('value');
  142. };
  143. var included = getValues(true).join(',');
  144. var excluded = getValues(false).join(',');
  145. var ret = [];
  146. if (included.length) ret.push('include=' + included);
  147. if (excluded.length) ret.push('exclude=' + excluded);
  148. return ret.length ? ret.join('&') : null;
  149. },
  150. /** include/exclude keywords methods **/
  151. keywordToId: function(keyword) {
  152. return keyword.toLowerCase().split(' ').join('_');
  153. },
  154. /**
  155. * Create keyword object
  156. * @param {string} keyword keyword value
  157. * @param {object} [opts]
  158. * @return {Em.Object}
  159. */
  160. createSelectedKeyword: function(keyword, opts) {
  161. var defaultOpts = {
  162. isIncluded: false,
  163. id: this.keywordToId(keyword),
  164. value: keyword
  165. };
  166. return Em.Object.create($.extend({}, defaultOpts, opts));
  167. },
  168. /**
  169. * Adds keyword if not added.
  170. * @param {FilterKeyword} keywordObject
  171. */
  172. addKeywordToList: function(keywordObject) {
  173. if (!this.get('selectedKeywords').someProperty('id', keywordObject.get('id'))) {
  174. this.get('selectedKeywords').pushObject(keywordObject);
  175. }
  176. },
  177. /**
  178. * @param {FilterKeyword} keyword
  179. */
  180. includeSelectedKeyword: function(keyword) {
  181. this.addKeywordToList(this.createSelectedKeyword(keyword, { isIncluded: true }));
  182. },
  183. /**
  184. * @param {FilterKeyword} keyword
  185. */
  186. excludeSelectedKeyword: function(keyword) {
  187. this.addKeywordToList(this.createSelectedKeyword(keyword, { isIncluded: false }));
  188. },
  189. /** view actions **/
  190. /** toolbar context menu actions **/
  191. moveTableTop: function(e) {
  192. var $el = $('.log-file-search-content');
  193. $el.scrollTop(0);
  194. $el = null;
  195. },
  196. moveTableBottom: function(e) {
  197. var $el = $('.log-file-search-content');
  198. $el.scrollTop($el.get(0).scrollHeight);
  199. $el = null;
  200. },
  201. navigateToLogUI: function(e) {
  202. console.error('navigate to Log UI');
  203. },
  204. removeKeyword: function(e) {
  205. this.get('selectedKeywords').removeObject(e.context);
  206. },
  207. /** toolbar reset filter actions **/
  208. resetKeywordsDateFilter: function(e) {
  209. this.setProperties({
  210. keywordsFilterValue: '',
  211. dateFromValue: '',
  212. dateToValue: ''
  213. });
  214. },
  215. resetLevelsFilter: function(e) {
  216. this.get('levelsContext').invoke('set', 'checked', false);
  217. },
  218. resetKeywordsFilter: function(e) {
  219. this.get('selectedKeywords').clear();
  220. },
  221. /** log search item context menu actions **/
  222. includeSelected: function() {
  223. var selection = window.getSelection().toString();
  224. if (!!selection) this.includeSelectedKeyword(selection);
  225. },
  226. excludeSelected: function() {
  227. var selection = window.getSelection().toString();
  228. if (!!selection) this.excludeSelectedKeyword(selection);
  229. }
  230. });