added sse

main
alex 2023-09-11 23:04:05 +02:00
parent 92005ef616
commit 3576ecb258
8 changed files with 94 additions and 41 deletions

13
package-lock.json generated
View File

@ -25,6 +25,7 @@
"react-router-dom": "^6.10.0", "react-router-dom": "^6.10.0",
"react-scripts": "5.0.1", "react-scripts": "5.0.1",
"react-stl-viewer": "^2.2.5", "react-stl-viewer": "^2.2.5",
"react-virtuoso": "^4.5.1",
"react-webcam": "^7.1.1", "react-webcam": "^7.1.1",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"web-vitals": "^2.1.4" "web-vitals": "^2.1.4"
@ -15806,6 +15807,18 @@
"react-dom": ">=16.13" "react-dom": ">=16.13"
} }
}, },
"node_modules/react-virtuoso": {
"version": "4.5.1",
"resolved": "https://registry.npmjs.org/react-virtuoso/-/react-virtuoso-4.5.1.tgz",
"integrity": "sha512-Jdo9M/T5PcDAczvmXAKwvb/BW0MCMr/cb+3j2m9192zlQgQ+syMdJR42i+Sk80ln5aSNaL1fnxleJzdKsCc4lw==",
"engines": {
"node": ">=10"
},
"peerDependencies": {
"react": ">=16 || >=17 || >= 18",
"react-dom": ">=16 || >=17 || >= 18"
}
},
"node_modules/react-webcam": { "node_modules/react-webcam": {
"version": "7.1.1", "version": "7.1.1",
"resolved": "https://registry.npmjs.org/react-webcam/-/react-webcam-7.1.1.tgz", "resolved": "https://registry.npmjs.org/react-webcam/-/react-webcam-7.1.1.tgz",

View File

@ -20,6 +20,7 @@
"react-router-dom": "^6.10.0", "react-router-dom": "^6.10.0",
"react-scripts": "5.0.1", "react-scripts": "5.0.1",
"react-stl-viewer": "^2.2.5", "react-stl-viewer": "^2.2.5",
"react-virtuoso": "^4.5.1",
"react-webcam": "^7.1.1", "react-webcam": "^7.1.1",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"web-vitals": "^2.1.4" "web-vitals": "^2.1.4"

View File

@ -423,5 +423,10 @@
"minute_plural": "Vor {{count}} Minuten", "minute_plural": "Vor {{count}} Minuten",
"second": "Vor {{count}} Sekunde", "second": "Vor {{count}} Sekunde",
"second_plural": "Vor {{count}} Sekunden" "second_plural": "Vor {{count}} Sekunden"
},
"consoles": {
"noLogTypesFound": "Keine Log-Typen gefunden",
"noLogManagerServerSpecified": "Kein Log-Manager-Server angegeben",
"couldntReachlogManagerServer": "Verbindung zum Log-Manager-Server konnte nicht hergestellt werden"
} }
} }

View File

@ -422,5 +422,10 @@
"minute_plural": "{{count}} minutes ago", "minute_plural": "{{count}} minutes ago",
"second": "{{count}} second ago", "second": "{{count}} second ago",
"second_plural": "{{count}} seconds ago" "second_plural": "{{count}} seconds ago"
},
"consoles": {
"noLogTypesFound": "No log types found",
"noLogManagerServerSpecified": "No log manager specified",
"couldntReachlogManagerServer": "Connection to log manager server failed"
} }
} }

View File

@ -1,10 +1,16 @@
import { ReloadOutlined } from "@ant-design/icons"; import { ReloadOutlined } from "@ant-design/icons";
import { Card, Checkbox, Select, Space, Spin, Tooltip } from "antd"; import { Card, Checkbox, Select, Space, Spin, Tooltip } from "antd";
import { useEffect, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { Constants, myFetch } from "../../utils"; import { Constants, myFetch } from "../../utils";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Virtuoso } from "react-virtuoso";
export default function LogCard({ type, title, customResult }) { export default function LogCard({
type,
title,
customResult,
logApiAddress = Constants.LOG_API_ADDRESS,
}) {
const { t } = useTranslation(); const { t } = useTranslation();
const [checkboxInfoChecked, setCheckboxInfoChecked] = useState(true); const [checkboxInfoChecked, setCheckboxInfoChecked] = useState(true);
const [checkboxErrorChecked, setCheckboxErrorChecked] = useState(true); const [checkboxErrorChecked, setCheckboxErrorChecked] = useState(true);
@ -14,18 +20,16 @@ export default function LogCard({ type, title, customResult }) {
const [selectedDate, setSelectedDate] = useState(""); const [selectedDate, setSelectedDate] = useState("");
const [logs, setLogs] = useState([]); const [logs, setLogs] = useState([]);
useEffect(() => { const sseEventSource = useRef(null);
console.log("useEffect logCard", type);
useEffect(() => {
if (type === "") return; if (type === "") return;
// init request to get all available dates // init request to get all available dates
myFetch(`${type}`, "GET", null, {}, 0, Constants.LOG_API_ADDRESS) myFetch(`s/${type}`, "GET", null, {}, 0, logApiAddress)
.then((data) => { .then((data) => {
let dates = []; let dates = [];
console.log("data", data);
if (data.length === 0) return; if (data.length === 0) return;
dates = data.map((date) => { dates = data.map((date) => {
@ -48,12 +52,10 @@ export default function LogCard({ type, title, customResult }) {
const loadLogs = (date) => { const loadLogs = (date) => {
setLoadingSpinner(true); setLoadingSpinner(true);
console.log("loadLogs", date);
if (date === "") return; if (date === "") return;
// fetch logs // fetch logs
myFetch(`${type}?d=${date}`, "GET", null, {}, 0, Constants.LOG_API_ADDRESS) myFetch(`s/${type}?d=${date}`, "GET", null, {}, 0, logApiAddress)
.then((data) => { .then((data) => {
setLoadingSpinner(false); setLoadingSpinner(false);
@ -68,9 +70,47 @@ export default function LogCard({ type, title, customResult }) {
.catch((errStatus) => console.log("error", errStatus)); .catch((errStatus) => console.log("error", errStatus));
}; };
useEffect(() => loadLogs(selectedDate), [selectedDate]);
useEffect(() => { useEffect(() => {
loadLogs(selectedDate); if (type === "" || selectedDate === "") return;
}, [selectedDate]);
const currentDate = new Date();
const today = `${currentDate.getDate()}-${
currentDate.getMonth() + 1
}-${currentDate.getFullYear()}`;
if (selectedDate === today) {
sseEventSource.current = new EventSource(
`${logApiAddress}/sse/${type}/${selectedDate}`
);
sseEventSource.current.onmessage = (event) => {
const data = JSON.parse(event.data);
setLogs((prevLogs) => {
const newLogs = [...prevLogs];
data.forEach((log) => {
newLogs.push(log);
});
return newLogs;
});
};
sseEventSource.current.onerror = (event) =>
console.log("sse error", event);
sseEventSource.current.onopen = (event) => console.log("sse open", event);
sseEventSource.current.onclose = (event) =>
console.log("sse close", event);
return () => sseEventSource.current.close();
}
}, [type, selectedDate]);
return ( return (
<Card <Card
@ -112,14 +152,13 @@ export default function LogCard({ type, title, customResult }) {
{customResult === undefined ? ( {customResult === undefined ? (
<> <>
{!loadingSpinner && type !== "" ? ( {!loadingSpinner && type !== "" ? (
<div <div style={{ height: "80vh", paddingRight: 6 }}>
style={{ height: "80vh", overflowY: "scroll", paddingRight: 6 }} <Virtuoso
> data={logs}
{logs.map((log, i) => ( itemContent={(_, log) => (
<p key={i} style={{ margin: 0 }}> <div style={{ padding: "2px" }}>{log}</div>
{log} )}
</p> />
))}
</div> </div>
) : ( ) : (
<div <div

View File

@ -15,10 +15,6 @@ export default function ConsolesProvider({ children }) {
label: "Stable Server", label: "Stable Server",
value: "http://127.0.0.1:50120/v1/log", value: "http://127.0.0.1:50120/v1/log",
}, },
{
label: "Test123",
value: "test",
},
]); ]);
return ( return (

View File

@ -3,8 +3,10 @@ import LogCard from "../../Components/LogCard";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { myFetch } from "../../utils"; import { myFetch } from "../../utils";
import { useConsolesContext } from "../../Contexts/ConsolesContext"; import { useConsolesContext } from "../../Contexts/ConsolesContext";
import { useTranslation } from "react-i18next";
export default function Consoles() { export default function Consoles() {
const { t } = useTranslation();
const consolesContext = useConsolesContext(); const consolesContext = useConsolesContext();
const [selectedLogServer, setSelectedLogServer] = useState( const [selectedLogServer, setSelectedLogServer] = useState(
@ -17,14 +19,10 @@ export default function Consoles() {
const [result, setResult] = useState(); const [result, setResult] = useState();
useEffect(() => { useEffect(() => {
console.log("selectedLogServer", selectedLogServer);
if (selectedLogServer === "") return; if (selectedLogServer === "") return;
myFetch("/types", "GET", null, {}, 0, selectedLogServer) myFetch("/types", "GET", null, {}, 0, selectedLogServer)
.then((data) => { .then((data) => {
console.log("data", data);
let newTypes = []; let newTypes = [];
if (data.length > 0) { if (data.length > 0) {
@ -37,24 +35,21 @@ export default function Consoles() {
setResult( setResult(
<Result <Result
status="404" status="404"
title="No log types found" title={t("consoles.noLogTypesFound")}
subTitle="Contact admin" subTitle={t("common.contactAdmin")}
/> />
); );
} }
setAvailableLogTypes(newTypes); setAvailableLogTypes(newTypes);
setSelectedLogType(newTypes.length > 0 ? newTypes[0].value : ""); setSelectedLogType(newTypes.length > 0 ? newTypes[0].value : "");
}) })
.catch((err) => { .catch(() => {
console.log("err", err);
setResult( setResult(
<Result <Result
status="500" status="500"
title="Couldn't reach log manager server" title={t("consoles.couldntReachlogManagerServer")}
subTitle="Contact admin" subTitle={t("common.contactAdmin")}
/> />
); );
}); });
@ -63,14 +58,15 @@ export default function Consoles() {
return ( return (
<LogCard <LogCard
type={selectedLogType} type={selectedLogType}
logApiAddress={selectedLogServer}
customResult={ customResult={
consolesContext.logManagerServers.length > 0 ? ( consolesContext.logManagerServers.length > 0 ? (
result result
) : ( ) : (
<Result <Result
status="500" status="500"
title="No log manager specified" title={t("common.noLogManagerServerSpecified")}
subTitle="Contact admin" subTitle={t("common.contactAdmin")}
/> />
) )
} }
@ -85,8 +81,6 @@ export default function Consoles() {
placeholder="Select server" placeholder="Select server"
/> />
{console.warn("def", consolesContext.logManagerServers[0])}
<Select <Select
disabled={ disabled={
result !== undefined || result !== undefined ||

View File

@ -17,7 +17,7 @@ if (window.location.hostname === "localhost") {
apiAddress = "http://localhost:50050/v1"; apiAddress = "http://localhost:50050/v1";
staticContentAddress = "http://localhost:50050/"; staticContentAddress = "http://localhost:50050/";
wsAddress = "ws://localhost:50050/ws"; wsAddress = "ws://localhost:50050/ws";
logApiAddress = "http://127.0.0.1:50110/v1/logs/"; logApiAddress = "http://127.0.0.1:50110/v1/log";
} else if (window.location.hostname === "192.168.178.93") { } else if (window.location.hostname === "192.168.178.93") {
apiAddress = "http://192.168.178.93:50050/v1"; apiAddress = "http://192.168.178.93:50050/v1";
staticContentAddress = "http://192.168.178.93:50050/"; staticContentAddress = "http://192.168.178.93:50050/";