mirror of
https://github.com/stashapp/stash.git
synced 2025-12-09 01:44:52 +01:00
Fix duplicate log messages (#3116)
This commit is contained in:
parent
b1c00a64fc
commit
420c6fa9d7
2 changed files with 50 additions and 24 deletions
|
|
@ -1,10 +1,11 @@
|
||||||
import React, { useEffect, useReducer, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
import * as GQL from "src/core/generated-graphql";
|
import * as GQL from "src/core/generated-graphql";
|
||||||
import { useLogs, useLoggingSubscribe } from "src/core/StashService";
|
import { useLoggingSubscribe, queryLogs } from "src/core/StashService";
|
||||||
import { SelectSetting } from "./Inputs";
|
import { SelectSetting } from "./Inputs";
|
||||||
import { SettingSection } from "./SettingSection";
|
import { SettingSection } from "./SettingSection";
|
||||||
import { JobTable } from "./Tasks/JobTable";
|
import { JobTable } from "./Tasks/JobTable";
|
||||||
|
|
||||||
function convertTime(logEntry: GQL.LogEntryDataFragment) {
|
function convertTime(logEntry: GQL.LogEntryDataFragment) {
|
||||||
function pad(val: number) {
|
function pad(val: number) {
|
||||||
let ret = val.toString();
|
let ret = val.toString();
|
||||||
|
|
@ -65,38 +66,57 @@ class LogEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// maximum number of log entries to display. Subsequent entries will truncate
|
// maximum number of log entries to keep - entries are discarded oldest-first
|
||||||
// the list, dropping off the oldest entries first.
|
const MAX_LOG_ENTRIES = 50000;
|
||||||
const MAX_LOG_ENTRIES = 200;
|
// maximum number of log entries to display
|
||||||
|
const MAX_DISPLAY_LOG_ENTRIES = 1000;
|
||||||
const logLevels = ["Trace", "Debug", "Info", "Warning", "Error"];
|
const logLevels = ["Trace", "Debug", "Info", "Warning", "Error"];
|
||||||
|
|
||||||
const logReducer = (existingEntries: LogEntry[], newEntries: LogEntry[]) => [
|
|
||||||
...newEntries.reverse(),
|
|
||||||
...existingEntries,
|
|
||||||
];
|
|
||||||
|
|
||||||
export const SettingsLogsPanel: React.FC = () => {
|
export const SettingsLogsPanel: React.FC = () => {
|
||||||
|
const [entries, setEntries] = useState<LogEntry[]>([]);
|
||||||
const { data, error } = useLoggingSubscribe();
|
const { data, error } = useLoggingSubscribe();
|
||||||
const { data: existingData } = useLogs();
|
|
||||||
const [currentData, dispatchLogUpdate] = useReducer(logReducer, []);
|
|
||||||
const [logLevel, setLogLevel] = useState<string>("Info");
|
const [logLevel, setLogLevel] = useState<string>("Info");
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const newData = (data?.loggingSubscribe ?? []).map((e) => new LogEntry(e));
|
async function getInitialLogs() {
|
||||||
dispatchLogUpdate(newData);
|
const logQuery = await queryLogs();
|
||||||
|
if (logQuery.error) return;
|
||||||
|
|
||||||
|
const initEntries = logQuery.data.logs.map((e) => new LogEntry(e));
|
||||||
|
if (initEntries.length !== 0) {
|
||||||
|
setEntries((prev) => {
|
||||||
|
return [...prev, ...initEntries].slice(0, MAX_LOG_ENTRIES);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getInitialLogs();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!data) return;
|
||||||
|
|
||||||
|
const newEntries = data.loggingSubscribe.map((e) => new LogEntry(e));
|
||||||
|
newEntries.reverse();
|
||||||
|
setEntries((prev) => {
|
||||||
|
return [...newEntries, ...prev].slice(0, MAX_LOG_ENTRIES);
|
||||||
|
});
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
const oldData = (existingData?.logs ?? []).map((e) => new LogEntry(e));
|
const displayEntries = entries
|
||||||
const filteredLogEntries = [...currentData, ...oldData]
|
|
||||||
.filter(filterByLogLevel)
|
.filter(filterByLogLevel)
|
||||||
.slice(0, MAX_LOG_ENTRIES);
|
.slice(0, MAX_DISPLAY_LOG_ENTRIES);
|
||||||
|
|
||||||
const maybeRenderError = error ? (
|
function maybeRenderError() {
|
||||||
<div className="error">Error connecting to log server: {error.message}</div>
|
if (error) {
|
||||||
) : (
|
return (
|
||||||
""
|
<div className="error">
|
||||||
|
Error connecting to log server: {error.message}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function filterByLogLevel(logEntry: LogEntry) {
|
function filterByLogLevel(logEntry: LogEntry) {
|
||||||
if (logLevel === "Trace") return true;
|
if (logLevel === "Trace") return true;
|
||||||
|
|
@ -127,8 +147,8 @@ export const SettingsLogsPanel: React.FC = () => {
|
||||||
</SettingSection>
|
</SettingSection>
|
||||||
|
|
||||||
<div className="logs">
|
<div className="logs">
|
||||||
{maybeRenderError}
|
{maybeRenderError()}
|
||||||
{filteredLogEntries.map((logEntry) => (
|
{displayEntries.map((logEntry) => (
|
||||||
<LogElement logEntry={logEntry} key={logEntry.id} />
|
<LogElement logEntry={logEntry} key={logEntry.id} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -985,6 +985,12 @@ export const useLogs = () =>
|
||||||
fetchPolicy: "no-cache",
|
fetchPolicy: "no-cache",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const queryLogs = () =>
|
||||||
|
client.query<GQL.LogsQuery>({
|
||||||
|
query: GQL.LogsDocument,
|
||||||
|
fetchPolicy: "no-cache",
|
||||||
|
});
|
||||||
|
|
||||||
export const useJobQueue = () =>
|
export const useJobQueue = () =>
|
||||||
GQL.useJobQueueQuery({
|
GQL.useJobQueueQuery({
|
||||||
fetchPolicy: "no-cache",
|
fetchPolicy: "no-cache",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue