Skip to content

Commit

Permalink
BACKLOG-23323 Improvements to the background jobs page
Browse files Browse the repository at this point in the history
  • Loading branch information
r3dm1ke committed Dec 17, 2024
1 parent 75453b8 commit d727ab0
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-code-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v4
- uses: Jahia/jahia-modules-action/static-analysis@v2
with:
node_version: 14
node_version: 18
auditci_level: high

build:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@jahia/moonstone": "^2.9.1",
"@jahia/ui-extender": "^1.0.6",
"css-loader": "^7.1.2",
"graphql-tag": "^2.12.6",
"i18next": "^19.1.0",
"prop-types": "^15.7.2",
Expand All @@ -47,7 +48,8 @@
"react-iframe": "^1.8.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-table": "^7.8.0"
"react-table": "^7.8.0",
"style-loader": "^4.0.0"
},
"devDependencies": {
"@babel/cli": "^7.6.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo} from 'react';
import React, {useImperativeHandle, useMemo, forwardRef} from 'react';
import {useTable, useSortBy} from 'react-table';
import {
Loader,
Expand Down Expand Up @@ -37,7 +37,7 @@ NothingToDisplay.propTypes = {
isError: PropTypes.bool
};

const BackgroundJobsTable = ({tableProps, paginationProps, loading, error, ...props}) => {
const BackgroundJobsTable = forwardRef(({tableProps, paginationProps, loading, error, refetch, ...props}, ref) => {
const {
limit,
setLimit,
Expand All @@ -54,6 +54,10 @@ const BackgroundJobsTable = ({tableProps, paginationProps, loading, error, ...pr
prepareRow
} = useTable(tableProps, useSortBy);

useImperativeHandle(ref, () => ({
refetch
}));

const content = useMemo(() => {
if (rows.length === 0) {
return <NothingToDisplay/>;
Expand Down Expand Up @@ -118,14 +122,14 @@ const BackgroundJobsTable = ({tableProps, paginationProps, loading, error, ...pr
<TablePagination rowsPerPageOptions={[5, 10, 20]} currentPage={currentPage} totalNumberOfRows={totalCount} rowsPerPage={limit} onRowsPerPageChange={setLimit} onPageChange={setPage}/>
</>
);
};
});

BackgroundJobsTable.propTypes = {
tableProps: PropTypes.object.isRequired,
paginationProps: PropTypes.object.isRequired,
// eslint-disable-next-line react/boolean-prop-naming
loading: PropTypes.bool,

loading: PropTypes.bool,

Check warning on line 131 in src/javascript/serverSettings/backgroundJobs/BackgroundJobsTable.jsx

View workflow job for this annotation

GitHub Actions / Static Analysis (linting, vulns)

Prop name (loading) doesn't match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)
refetch: PropTypes.func,
error: PropTypes.object,
'data-testid': PropTypes.string
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, {useMemo} from 'react';
import React, {useMemo, forwardRef} from 'react';
import {useHistoryBackgroundJobs} from './hooks';
const {useTranslation} = require('react-i18next');
import {parseUsername} from './utils';
import BackgroundJobsTable from './BackgroundJobsTable';
import JobStatusBadge from './JobStatusBadge';

const HistoryBackgroundJobsTable = () => {
const HistoryBackgroundJobsTable = forwardRef((_, ref) => {
const {t} = useTranslation('serverSettings');
const {
jobs,
Expand All @@ -14,6 +14,7 @@ const HistoryBackgroundJobsTable = () => {
totalCount,
currentPage,
setPage,
refetch,
loading,
error
} = useHistoryBackgroundJobs();
Expand Down Expand Up @@ -52,7 +53,7 @@ const HistoryBackgroundJobsTable = () => {
Header: t('backgroundJobs.columns.duration'),
accessor: 'duration',
Cell: ({value}) => `${value} ms`,
customWidth: 80
customWidth: 105
}
];
}, [t]);
Expand All @@ -65,13 +66,15 @@ const HistoryBackgroundJobsTable = () => {

return (
<BackgroundJobsTable
ref={ref}
tableProps={tableProps}
paginationProps={{limit, setLimit, totalCount, currentPage, setPage}}
refetch={refetch}
loading={loading}
error={error}
data-testid="history-background-jobs-table"
/>
);
};
});

export default HistoryBackgroundJobsTable;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, {useMemo} from 'react';
import React, {useMemo, forwardRef} from 'react';
import {useTranslation} from 'react-i18next';
import {useScheduledBackgroundJobs} from './hooks';
import BackgroundJobsTable from './BackgroundJobsTable';

const ScheduledBackgroundJobsTable = () => {
const ScheduledBackgroundJobsTable = forwardRef((_, ref) => {
const {t} = useTranslation('serverSettings');
const {
jobs,
Expand All @@ -12,6 +12,7 @@ const ScheduledBackgroundJobsTable = () => {
totalCount,
currentPage,
setPage,
refetch,
loading,
error
} = useScheduledBackgroundJobs();
Expand All @@ -37,13 +38,15 @@ const ScheduledBackgroundJobsTable = () => {

return (
<BackgroundJobsTable
ref={ref}
tableProps={tableProps}
paginationProps={{limit, setLimit, totalCount, currentPage, setPage}}
loading={loading}
refetch={refetch}
error={error}
data-testid="scheduled-background-jobs-table"
/>
);
};
});

export default ScheduledBackgroundJobsTable;
6 changes: 4 additions & 2 deletions src/javascript/serverSettings/backgroundJobs/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const useHistoryBackgroundJobs = () => {
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(10);
const offset = useMemo(() => (page - 1) * limit, [page, limit]);
const {data, loading, error} = useQuery(GET_BACKGROUND_JOBS, {
const {data, loading, error, refetch} = useQuery(GET_BACKGROUND_JOBS, {
variables: {
includeStatuses: null,
excludeStatuses: ['SCHEDULED'],
Expand All @@ -23,6 +23,7 @@ export const useHistoryBackgroundJobs = () => {
jobs,
totalCount,
currentPage: page,
refetch,
setPage,
limit,
setLimit,
Expand All @@ -35,7 +36,7 @@ export const useScheduledBackgroundJobs = () => {
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(10);
const offset = useMemo(() => (page - 1) * limit, [page, limit]);
const {data, loading, error} = useQuery(GET_BACKGROUND_JOBS, {
const {data, loading, error, refetch} = useQuery(GET_BACKGROUND_JOBS, {
variables: {
includeStatuses: ['SCHEDULED'],
excludeStatuses: null,
Expand All @@ -53,6 +54,7 @@ export const useScheduledBackgroundJobs = () => {
totalCount,
currentPage: page,
setPage,
refetch,
limit,
setLimit,
loading,
Expand Down
36 changes: 29 additions & 7 deletions src/javascript/serverSettings/backgroundJobs/index.jsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
import React, {useState} from 'react';
import {Header, LayoutContent, Paper, Tab, TabItem} from '@jahia/moonstone';
import React, {useMemo, useRef, useState} from 'react';
import {Button, Header, LayoutContent, Paper, Tab, TabItem} from '@jahia/moonstone';
import HistoryBackgroundJobsTable from './HistoryBackgroundJobsTable';
import ScheduledBackgroundJobsTable from './ScheduledBackgroundJobsTable';
import {useTranslation} from 'react-i18next';
import SvgReload from '@jahia/moonstone/dist/icons/components/Reload';
import classes from './styles.css';

const BackgroundJobsTabs = () => {
const {t} = useTranslation('serverSettings');
const [activeTab, setActiveTab] = useState('history');
const historyRef = useRef(null);
const historyTable = useMemo(() => <HistoryBackgroundJobsTable ref={historyRef}/>, []);

const content = activeTab === 'history' ? <HistoryBackgroundJobsTable/> : <ScheduledBackgroundJobsTable/>;
const scheduledRef = useRef(null);
const scheduledTable = useMemo(() => <ScheduledBackgroundJobsTable ref={scheduledRef}/>, []);

return (
<LayoutContent
header={<Header title={t('backgroundJobs.title')}/>}
content={(
<Paper>
<Tab>
<Tab className={classes.tabs}>
<TabItem
id="history"
label={t('backgroundJobs.tabs.history').toUpperCase()}
label={t('backgroundJobs.tabs.history')}
size="big"
isSelected={activeTab === 'history'}
data-testid="background-jobs-history-tab"
onClick={() => setActiveTab('history')}
/>
<TabItem
id="scheduled"
label={t('backgroundJobs.tabs.scheduled').toUpperCase()}
label={t('backgroundJobs.tabs.scheduled')}
size="big"
isSelected={activeTab === 'scheduled'}
data-testid="background-jobs-scheduled-tab"
onClick={() => setActiveTab('scheduled')}
/>
<div className={classes.spacer}/>
<Button
variant="ghost"
icon={<SvgReload/>}
onClick={() => {
if (activeTab === 'history') {
historyRef.current.refetch();
} else {
scheduledRef.current.refetch();
}
}}
/>
</Tab>
{content}
<div style={activeTab === 'history' ? {} : {display: 'none'}}>
{historyTable}
</div>
<div style={activeTab === 'scheduled' ? {} : {display: 'none'}}>
{scheduledTable}
</div>
</Paper>
)}
/>
Expand Down
7 changes: 7 additions & 0 deletions src/javascript/serverSettings/backgroundJobs/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.tabs {
width: 100%;
}

.spacer {
flex: 1;
}
10 changes: 7 additions & 3 deletions src/javascript/serverSettings/backgroundJobs/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export const renderSortIndicator = (isSorted, isSortedDesc) => {
};

export const parseUsername = username => {
if (username.indexOf('/users/') !== -1) {
return username.split('/users/')[1];
if (!username) {
return 'Unknown user';
}

return username;
if (username.indexOf('/') === -1) {
return username;
}

return username.split('/').pop();
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const registerRoutes = function () {

registry.add('adminRoute', 'backgroundJobs', {
targets: ['administration-server-systemHealth:4'],
requiredPermission: 'view-all-jobs',
icon: null,
label: 'serverSettings:systemHealth.backgroundJobs',
isSelectable: true,
Expand Down
18 changes: 16 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (env, argv) => {
},
resolve: {
mainFields: ['module', 'main'],
extensions: ['.mjs', '.js', '.jsx', 'json']
extensions: ['.mjs', '.js', '.jsx', '.json']
},
module: {
rules: [
Expand Down Expand Up @@ -58,7 +58,21 @@ module.exports = (env, argv) => {
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/resource',
dependency: { not: ['url'] }
}
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
namedExport: false,
},
},
},
],
},
]
},
plugins: [
Expand Down
Loading

0 comments on commit d727ab0

Please sign in to comment.