algairim commented on a change in pull request #235: URL: https://github.com/apache/brooklyn-ui/pull/235#discussion_r662790656
########## File path: ui-modules/utils/logbook/logbook.js ########## @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import angular from 'angular'; +import template from './logbook.template.html'; +import {HIDE_INTERSTITIAL_SPINNER_EVENT} from '../interstitial-spinner/interstitial-spinner'; + +const MODULE_NAME = 'brooklyn.component.logbook'; + +angular.module(MODULE_NAME, []) + .directive('brLogbook', logbook); + +export default MODULE_NAME; + +export function logbook() { + + return { + template: template, + controller: ['$scope', '$element', 'brBrandInfo', 'logbookApi', controller], + controllerAs: 'vm' + }; + + function controller($scope, $element, brBrandInfo, logbookApi) { + $scope.$emit(HIDE_INTERSTITIAL_SPINNER_EVENT); + $scope.getBrandedText = brBrandInfo.getBrandedText; + + let vm = this; + let scrollableElements = Array.from($element.find('textarea')); + + $scope.$on('logbook.query', () => { + vm.doQuery(); + }); + + vm.getChecked = function (group) { + let levels = []; + group.forEach(function (item) { + if (item.selected) levels.push(item.value); + }); + return levels; + } + vm.doQuery = function () { + $scope.waitingResponse = true; + $scope.results = "Loading..." + + const levels = $scope.allLevels ? ['ALL'] : vm.getChecked($scope.logLevels); + + const params = { + reverseOrder: $scope.reverseOrder, + numberOfItems: $scope.numberOfItems, + levels: levels, + initTime: $scope.initTime, + finalTime: $scope.finalTime, + } + + logbookApi.logbookQuery(params, true).then(function (success) { + // TODO implement logic for make output as table + $scope.logEntries = success; + $scope.results = vm.createLogOutputAsText($scope.logEntries); + scrollToMostRecentRecords(); + }, function (error) { + $scope.results = "Error getting the logs: \n" + error.error.message; + console.log(JSON.stringify(error)); + }).finally(() => { + $scope.waitingResponse = false; + }); + }; + + vm.createLogOutputAsText = function (success) { + let output = []; + const fieldsToShow = vm.getChecked($scope.logFields); + success.forEach(entry => { + let outputLine = []; + if (fieldsToShow.includes("datetime") && entry.timestamp) + outputLine.push(entry.timestamp); + if (fieldsToShow.includes("taskId") && entry.taskId) + outputLine.push(entry.taskId); + if (fieldsToShow.includes("entityIds") && entry.entityIds) + outputLine.push(entry.entityIds); + if (fieldsToShow.includes("level") && entry.level) + outputLine.push(entry.level); + if (fieldsToShow.includes("bundleId") && entry.bundleId) + outputLine.push(entry.bundleId); + if (fieldsToShow.includes("class") && entry.class) + outputLine.push(entry.class); + if (fieldsToShow.includes("threadName") && entry.threadName) + outputLine.push(entry.threadName); + if (fieldsToShow.includes("message") && entry.message) + outputLine.push(entry.message); + + output.push(outputLine.join(" ")); + }) + return output.length > 0 ? output.join("\n") : "No results"; + } + + vm.resetForm = function () { + $scope.numberOfItems = 1000; + $scope.allLevels = true + $scope.logLevels = [ + {"name": "Debug", "value": "DEBUG", "selected": false}, + {"name": "Info", "value": "INFO ", "selected": false}, + {"name": "Warn", "value": "WARN ", "selected": false}, + {"name": "Error", "value": "ERROR", "selected": false}, + {"name": "Fatal", "value": "FATAL", "selected": false}, + ]; + $scope.fieldsToShow = ['datetime', 'class', 'message'] + $scope.logFields = [ + {"name": "Timestamp", "value": "datetime", "selected": true}, + {"name": "Task ID", "value": "taskId", "selected": false}, + {"name": "Entity IDs", "value": "entityIds", "selected": false}, + {"name": "Log level", "value": "level", "selected": true}, + {"name": "Bundle ID", "value": "bundleId", "selected": false}, + {"name": "Class", "value": "class", "selected": true}, + {"name": "Thread name", "value": "threadName", "selected": false}, + {"name": "Message", "value": "message", "selected": true}, + ]; + $scope.reverseOrder = false; + $scope.initTime = ""; + $scope.finalTime = ""; + } + + $scope.$watch('allLevels', function (v) { + if (!v) { + if (vm.getChecked($scope.logLevels).length === 0) { + $scope.allLevels = true; + } else { + return; + } + } + for (let i = 0; i < $scope.logLevels.length; ++i) { + $scope.logLevels[i].selected = false; + } + }); + + $scope.$watch('logLevels', function (newVal, oldVal) { + let selected = newVal.reduce(function (s, c) { + return s + (c.selected ? 1 : 0); + }, 0); + if (selected === newVal.length || selected === 0) { + $scope.allLevels = true; + } else if (selected > 0) { + $scope.allLevels = false; + } + }, true); + + $scope.$watch('logFields', function (newVal, oldVal) { + if ($scope.logEntries !== "") { + $scope.results = vm.createLogOutputAsText($scope.logEntries); + } + }, true); + + /** + * Scrolls down to the most recent records if order is not set to reverse. + */ + function scrollToMostRecentRecords() { + $scope.$applyAsync(() => { + if ($scope.reverseOrder) { + // NOOP: no need to scroll down. Reverse order displays the most recent records at the beginning. + } else { + // Scroll down to the most recent records. + scrollableElements.forEach(item => item.scrollTop = item.scrollHeight); + } + }); + } Review comment: Added auto-scroll, depending on reverse order, to address requirement to display last 1000 lines. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@brooklyn.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org