|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2020 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +import React, { useEffect } from "react"; |
| 17 | +import { IMessageEvent, w3cwebsocket as W3CWebSocket } from "websocket"; |
| 18 | +import storage from "local-storage-fallback"; |
| 19 | +import { AppState } from "../../../store"; |
| 20 | +import { connect } from "react-redux"; |
| 21 | +import { traceMessageReceived, traceResetMessages } from "./actions"; |
| 22 | +import { TraceMessage } from "./types"; |
| 23 | +import { createStyles, Theme, withStyles } from "@material-ui/core/styles"; |
| 24 | + |
| 25 | +const styles = (theme: Theme) => |
| 26 | + createStyles({ |
| 27 | + logList: { |
| 28 | + background: "white", |
| 29 | + maxHeight: "400px", |
| 30 | + overflow: "auto", |
| 31 | + "& ul": { |
| 32 | + margin: "4px", |
| 33 | + padding: "0px" |
| 34 | + }, |
| 35 | + "& ul li": { |
| 36 | + listStyle: "none", |
| 37 | + margin: "0px", |
| 38 | + padding: "0px", |
| 39 | + borderBottom: "1px solid #dedede" |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | +interface ITrace { |
| 45 | + classes: any; |
| 46 | + traceMessageReceived: typeof traceMessageReceived; |
| 47 | + traceResetMessages: typeof traceResetMessages; |
| 48 | + messages: TraceMessage[]; |
| 49 | +} |
| 50 | + |
| 51 | +const Trace = ({ |
| 52 | + classes, |
| 53 | + traceMessageReceived, |
| 54 | + traceResetMessages, |
| 55 | + messages |
| 56 | +}: ITrace) => { |
| 57 | + useEffect(() => { |
| 58 | + traceResetMessages(); |
| 59 | + const token: string = storage.getItem("token")!; |
| 60 | + const url = new URL(window.location.toString()); |
| 61 | + const isDev = process.env.NODE_ENV === "development"; |
| 62 | + const port = isDev ? "9090" : url.port; |
| 63 | + |
| 64 | + const setCookie = (name: string, val: string) => { |
| 65 | + const date = new Date(); |
| 66 | + const value = val; |
| 67 | + |
| 68 | + // Set it expire in 45 minutes |
| 69 | + date.setTime(date.getTime() + 45 * 60 * 1000); |
| 70 | + |
| 71 | + // Set it |
| 72 | + document.cookie = |
| 73 | + name + "=" + value + "; expires=" + date.toUTCString() + "; path=/"; |
| 74 | + }; |
| 75 | + |
| 76 | + setCookie("token", token); |
| 77 | + |
| 78 | + const c = new W3CWebSocket(`ws://${url.hostname}:${port}/ws/trace`); |
| 79 | + |
| 80 | + let interval: any | null = null; |
| 81 | + if (c !== null) { |
| 82 | + c.onopen = () => { |
| 83 | + console.log("WebSocket Client Connected"); |
| 84 | + c.send("ok"); |
| 85 | + interval = setInterval(() => { |
| 86 | + c.send("ok"); |
| 87 | + }, 10 * 1000); |
| 88 | + }; |
| 89 | + c.onmessage = (message: IMessageEvent) => { |
| 90 | + let m: TraceMessage = JSON.parse(message.data.toString()); |
| 91 | + m.time = new Date(m.time.toString()); |
| 92 | + m.key = Math.random(); |
| 93 | + traceMessageReceived(m); |
| 94 | + }; |
| 95 | + c.onclose = () => { |
| 96 | + clearInterval(interval); |
| 97 | + console.log("connection closed by server"); |
| 98 | + }; |
| 99 | + return () => { |
| 100 | + c.close(1000); |
| 101 | + clearInterval(interval); |
| 102 | + console.log("closing websockets"); |
| 103 | + }; |
| 104 | + } |
| 105 | + }, [traceMessageReceived]); |
| 106 | + |
| 107 | + const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; |
| 108 | + const niceBytes = (x: string) => { |
| 109 | + let l = 0, |
| 110 | + n = parseInt(x, 10) || 0; |
| 111 | + |
| 112 | + while (n >= 1024 && ++l) { |
| 113 | + n = n / 1024; |
| 114 | + } |
| 115 | + //include a decimal point and a tenths-place digit if presenting |
| 116 | + //less than ten of KB or greater units |
| 117 | + return n.toFixed(n < 10 && l > 0 ? 1 : 0) + " " + units[l]; |
| 118 | + }; |
| 119 | + const timeFromdate = (d: Date) => { |
| 120 | + let h = d.getHours() < 10 ? `0${d.getHours()}` : `${d.getHours()}`; |
| 121 | + let m = d.getMinutes() < 10 ? `0${d.getMinutes()}` : `${d.getMinutes()}`; |
| 122 | + let s = d.getSeconds() < 10 ? `0${d.getSeconds()}` : `${d.getSeconds()}`; |
| 123 | + |
| 124 | + return `${h}:${m}:${s}:${d.getMilliseconds()}`; |
| 125 | + }; |
| 126 | + |
| 127 | + return ( |
| 128 | + <div> |
| 129 | + <h1>Trace</h1> |
| 130 | + <div className={classes.logList}> |
| 131 | + <ul> |
| 132 | + {messages.map(m => { |
| 133 | + return ( |
| 134 | + <li key={m.key}> |
| 135 | + {timeFromdate(m.time)} - {m.api}[{m.statusCode} {m.statusMsg}]{" "} |
| 136 | + {m.api} {m.host} {m.client} {m.callStats.duration} ↑{" "} |
| 137 | + {niceBytes(m.callStats.rx + "")} ↓{" "} |
| 138 | + {niceBytes(m.callStats.tx + "")} |
| 139 | + </li> |
| 140 | + ); |
| 141 | + })} |
| 142 | + </ul> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
| 148 | +const mapState = (state: AppState) => ({ |
| 149 | + messages: state.trace.messages |
| 150 | +}); |
| 151 | + |
| 152 | +const connector = connect(mapState, { |
| 153 | + traceMessageReceived: traceMessageReceived, |
| 154 | + traceResetMessages: traceResetMessages |
| 155 | +}); |
| 156 | + |
| 157 | +export default connector(withStyles(styles)(Trace)); |
0 commit comments