/*
* Copyright (C) 2021 Red Hat, Inc.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
import '../lib/patternfly/patternfly-6-cockpit.scss';
import 'cockpit-dark-theme'; // once per page
import cockpit from "cockpit";
import React, { useState, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
import { ClipboardCopy } from "@patternfly/react-core/dist/esm/components/ClipboardCopy/index.js";
import { Page, PageSection, } from "@patternfly/react-core/dist/esm/components/Page/index.js";
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js";
import { SearchInput } from "@patternfly/react-core/dist/esm/components/SearchInput/index.js";
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js";
import { Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem, ToolbarToggleGroup } from "@patternfly/react-core/dist/esm/components/Toolbar/index.js";
import {
ExternalLinkSquareAltIcon,
FilterIcon,
HelpIcon,
} from '@patternfly/react-icons';
import { SimpleSelect } from "cockpit-components-simple-select";
import { TypeaheadSelect } from "cockpit-components-typeahead-select";
import {
checkJournalctlGrep,
getGrepFiltersFromOptions,
getOptionsFromTextInput,
} from "./logsHelpers.js";
import { JournalBox } from "./logsJournal.jsx";
import { LogEntry } from "./logDetails.jsx";
import { usePageLocation } from "hooks";
import "./logs.scss";
const _ = cockpit.gettext;
const timeFilterOptions = [
{ key: 1, value: { key: "boot", value: 0 }, content: _("Current boot") },
{ key: 2, value: { key: "boot", value: "-1" }, content: _("Previous boot") },
{ key: 3, value: { key: "since", value: "-24hours", default: true }, content: _("Last 24 hours") },
{ key: 4, value: { key: "since", value: "-7days" }, content: _("Last 7 days") },
];
const journalPrioOptions = [
{ value: "emerg", content: _("Only emergency") },
{ value: "alert", content: _("Alert and above") },
{ value: "crit", content: _("Critical and above") },
{ value: "err", content: _("Error and above") },
{ value: "warning", content: _("Warning and above") },
{ value: "notice", content: _("Notice and above") },
{ value: "info", content: _("Info and above") },
{ value: "debug", content: _("Debug and above") },
];
const getPrioFilterOption = options => {
// `prio` is a legacy name. Accept it, but don't generate it
return options.priority || options.prio;
};
const getTimeFilterOption = options => {
function find_key(key, value) {
return timeFilterOptions.find(o => o.value.key == key && o.value.value == value)?.value;
}
if (options.boot)
return find_key('boot', options.boot);
else if (options.since)
return find_key('since', options.since);
return timeFilterOptions.find(option => 'default' in option.value)?.value; // Use the default key
};
export const LogsPage = () => {
const { path, options } = usePageLocation();
let follow = !(options.follow && options.follow === "false");
if (options.boot && options.boot !== "0") // Don't follow if specific boot is picked
follow = false;
// If priority not specified use err
if (!options.priority && !options.prio)
options.priority = 'err';
const full_grep = getGrepFiltersFromOptions({ options })[0];
/* Initial state */
const [currentIdentifiers, setCurrentIdentifiers] = useState([]);
const [dataFollowing, setDataFollowing] = useState(follow);
const [filteredQuery, setFilteredQuery] = useState(undefined);
const [journalPrio, setJournalPrio] = useState(getPrioFilterOption(options));
const [identifiersFilter, setIdentifiersFilter] = useState(options.tag || "all");
const [showTextSearch, setShowTextSearch] = useState(false);
const [textFilter, setTextFilter] = useState(full_grep);
const [timeFilter, setTimeFilter] = useState(getTimeFilterOption(options));
const [updateIdentifiersList, setUpdateIdentifiersList] = useState(true);
useEffect(() => {
checkJournalctlGrep(setShowTextSearch);
function onNavigate() {
const { options, path } = cockpit.location;
const full_grep = getGrepFiltersFromOptions({ options })[0];
if (path.length == 1) return;
setJournalPrio(getPrioFilterOption(options));
setIdentifiersFilter(options.tag || "all");
setTextFilter(full_grep);
setTimeFilter(getTimeFilterOption(options));
}
cockpit.addEventListener("locationchanged", onNavigate);
return () => cockpit.removeEventListener("locationchanged", onNavigate);
}, []);
if (path.length == 1) {
return ;
} else if (path.length > 1) { /* redirect */
console.warn("not a journal location: " + path);
cockpit.location = '';
}
const updateUrl = (options) => {
cockpit.location.go([], options);
};
const onJournalPrioChange = (value) => {
setUpdateIdentifiersList(true);
updateUrl(Object.assign(options, { priority: value }));
};
const onIdentifiersFilterChange = (value) => {
setUpdateIdentifiersList(false);
if (value == "all") {
delete options.tag;
updateUrl(Object.assign(options));
} else {
updateUrl(Object.assign(options, { tag: value }));
}
};
const onTextFilterChange = (value) => {
setUpdateIdentifiersList(true);
updateUrl(Object.assign(getOptionsFromTextInput(value)));
};
const onTimeFilterChange = (newTimeFilter) => {
setUpdateIdentifiersList(true);
if (newTimeFilter.key == 'boot' && newTimeFilter.value !== "0") // Don't follow if specific boot is picked
setDataFollowing(false);
else if (options.boot && options.boot !== "0" && newTimeFilter.key !== "boot") // Start following is specific boot is removed
setDataFollowing(true);
// Remove all parameters which can be set up using filters
delete options.boot;
delete options.since;
cockpit.location.go([], Object.assign(options, { [newTimeFilter.key]: newTimeFilter.value }));
};
return (
{_("Toggle filters")}>} breakpoint="lg">
{_("Priority")}
{_("Identifier")}
{showTextSearch &&
<>
{_("Filters")}
>}
);
};
const IdentifiersFilter = ({ identifiersFilter, onIdentifiersFilterChange, currentIdentifiers }) => {
let identifiersArray;
if (currentIdentifiers !== undefined) {
identifiersArray = [
{ value: "all", content: _("All") }
];
if (currentIdentifiers.length > 0) {
identifiersArray.push({ decorator: "divider", key: "divider" });
}
identifiersArray = identifiersArray.concat(
currentIdentifiers
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.map(unit => ({ value: unit, content: unit }))
);
} else {
identifiersArray = [
{ value: identifiersFilter, content: identifiersFilter, isDisabled: true }
];
}
return (
{ onIdentifiersFilterChange(selection) }}
onClearSelection={identifiersFilter != "all" && (() => { onIdentifiersFilterChange("all") })}
/>
);
};
const TextFilter = ({ textFilter, onTextFilterChange, filteredQuery }) => {
const [unsubmittedTextFilter, setUnsubmittedTextFilter] = useState(textFilter);
const sinceUntilBody = _("Date specifications should be of the format YYYY-MM-DD hh:mm:ss. Alternatively the strings 'yesterday', 'today', 'tomorrow' are understood. 'now' refers to the current time. Finally, relative times may be specified, prefixed with '-' or '+'");
const sinceLabel = (
<>
{_("Since")}
} className="log-text-filter-popover-button" variant="plain" />
>
);
const untilLabel = (
<>
{_("Until")}
} className="log-text-filter-popover-button" variant="plain" />
>
);
const bootLabel = (
<>
{_("Boot")}
} className="log-text-filter-popover-button" variant="plain" />
>
);
const serviceLabel = (
<>
{_("Unit")}
} className="log-text-filter-popover-button" variant="plain" />
>
);
const freeTextLabel = (
<>
{_("Free-form search")}
} className="log-text-filter-popover-button" variant="plain" />
>
);
const searchInputAttributes = [
{ attr: "since", display: sinceLabel },
{ attr: "until", display: untilLabel },
{ attr: "boot", display: bootLabel },
{ attr: "unit", display: serviceLabel },
{ attr: "priority" }, // Hide this with CSS
{ attr: "tag" }, // Hide this with CSS
];
return (
{ onTextFilterChange(""); setUnsubmittedTextFilter("") }}
placeholder={_("Type to filter")}
value={unsubmittedTextFilter}
onChange={(_, val) => setUnsubmittedTextFilter(val)}
resetButtonLabel={_("Reset")}
submitSearchButtonLabel={_("Search")}
formAdditionalItems={
} iconPosition="right"
target="blank" rel="noopener noreferrer">
{_("journalctl manpage")}
{filteredQuery}
}
onSearch={() => onTextFilterChange(unsubmittedTextFilter)} />
);
};
function init() {
const root = createRoot(document.getElementById('logs'));
root.render();
}
document.addEventListener("DOMContentLoaded", init);