Monty Taylor | 4a781a7 | 2017-07-25 07:28:04 -0400 | [diff] [blame] | 1 | /* global URL, WebSocket, BuiltinConfig */ |
| 2 | // Client script for Zuul Log Streaming |
| 3 | // |
| 4 | // @licstart The following is the entire license notice for the |
| 5 | // JavaScript code in this page. |
| 6 | // |
| 7 | // Copyright 2017 BMW Car IT GmbH |
| 8 | // |
| 9 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | // not use this file except in compliance with the License. You may obtain |
| 11 | // a copy of the License at |
| 12 | // |
| 13 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | // |
| 15 | // Unless required by applicable law or agreed to in writing, software |
| 16 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | // License for the specific language governing permissions and limitations |
| 19 | // under the License. |
| 20 | // |
| 21 | // @licend The above is the entire license notice for the JavaScript code in |
| 22 | // this page. |
| 23 | |
| 24 | import angular from 'angular' |
| 25 | import './styles/stream.css' |
| 26 | |
| 27 | function escapeLog (text) { |
| 28 | const pattern = /[<>&"']/g |
| 29 | |
| 30 | return text.replace(pattern, function (match) { |
| 31 | return '&#' + match.charCodeAt(0) + ';' |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | function zuulStartStream () { |
| 36 | let pageUpdateInMS = 250 |
| 37 | let receiveBuffer = '' |
| 38 | |
| 39 | setInterval(function () { |
| 40 | console.log('autoScroll') |
| 41 | if (receiveBuffer !== '') { |
| 42 | document.getElementById('zuulstreamcontent').innerHTML += receiveBuffer |
| 43 | receiveBuffer = '' |
| 44 | if (document.getElementById('autoscroll').checked) { |
| 45 | window.scrollTo(0, document.body.scrollHeight) |
| 46 | } |
| 47 | } |
| 48 | }, pageUpdateInMS) |
| 49 | |
| 50 | let url = new URL(window.location) |
| 51 | |
| 52 | let params = { |
| 53 | uuid: url.searchParams.get('uuid') |
| 54 | } |
| 55 | document.getElementById('pagetitle').innerHTML = params['uuid'] |
| 56 | if (url.searchParams.has('logfile')) { |
| 57 | params['logfile'] = url.searchParams.get('logfile') |
| 58 | let logfileSuffix = `(${params['logfile']})` |
| 59 | document.getElementById('pagetitle').innerHTML += logfileSuffix |
| 60 | } |
| 61 | if (typeof BuiltinConfig !== 'undefined') { |
| 62 | params['websocket_url'] = BuiltinConfig.websocket_url |
| 63 | } else if (url.searchParams.has('websocket_url')) { |
| 64 | params['websocket_url'] = url.searchParams.get('websocket_url') |
| 65 | } else { |
| 66 | // Websocket doesn't accept relative urls so construct an |
| 67 | // absolute one. |
| 68 | let protocol = '' |
| 69 | if (url['protocol'] === 'https:') { |
| 70 | protocol = 'wss://' |
| 71 | } else { |
| 72 | protocol = 'ws://' |
| 73 | } |
| 74 | let path = url['pathname'].replace(/stream.html.*$/g, '') + 'console-stream' |
| 75 | params['websocket_url'] = protocol + url['host'] + path |
| 76 | } |
| 77 | let ws = new WebSocket(params['websocket_url']) |
| 78 | |
| 79 | ws.onmessage = function (event) { |
| 80 | console.log('onmessage') |
| 81 | receiveBuffer = receiveBuffer + escapeLog(event.data) |
| 82 | } |
| 83 | |
| 84 | ws.onopen = function (event) { |
| 85 | console.log('onopen') |
| 86 | ws.send(JSON.stringify(params)) |
| 87 | } |
| 88 | |
| 89 | ws.onclose = function (event) { |
| 90 | console.log('onclose') |
| 91 | receiveBuffer = receiveBuffer + '\n--- END OF STREAM ---\n' |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | angular.module('zuulStream', []).controller( |
| 96 | 'mainController', function ($scope, $http) { |
| 97 | window.onload = zuulStartStream() |
| 98 | } |
| 99 | ) |