Skip to content

Commit

Permalink
Merge pull request #1796 from Inist-CNRS/feat/add-time-precomputed
Browse files Browse the repository at this point in the history
Feat(precomputed): Display time since precomputed start
  • Loading branch information
JulienMattiussi authored Nov 24, 2023
2 parents 2065aed + 65b2d40 commit 20f4697
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/api/models/precomputed.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export default async db => {
);
};

collection.updateStartedAt = async (id, startedAt) => {
collection.updateOne(
{
$or: [{ _id: new ObjectId(id) }, { _id: id }],
},
{ $set: { startedAt } },
);
};

collection.castIds = castIdsFactory(collection);

return collection;
Expand Down
4 changes: 4 additions & 0 deletions src/api/services/precomputed/precomputed.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ export const getComputedFromWebservice = async (
await ctx.precomputed.updateStatus(precomputedId, FINISHED, {
data,
});
await ctx.precomputed.updateStartedAt(precomputedId, null);

job.progress(100);
const isFailed = await job.isFailed();
Expand Down Expand Up @@ -447,6 +448,7 @@ export const getFailureFromWebservice = async (
await ctx.precomputed.updateStatus(precomputedId, ERROR, {
message: errorMessage,
});
await ctx.precomputed.updateStartedAt(precomputedId, null);

job.progress(100);
progress.finish(tenant);
Expand All @@ -467,6 +469,7 @@ export const getFailureFromWebservice = async (
export const processPrecomputed = async (precomputed, ctx) => {
let logData = {};
await ctx.precomputed.updateStatus(precomputed._id, IN_PROGRESS);
await ctx.precomputed.updateStartedAt(precomputed._id, new Date());

const room = `${ctx.tenant}-precomputed-job-${ctx.job.id}`;

Expand Down Expand Up @@ -568,6 +571,7 @@ export const setPrecomputedError = async (ctx, err) => {
message: err?.message,
},
);
await ctx.precomputed.updateStartedAt(id, null);

const room = `precomputed-job-${ctx.job.id}`;
const logData = JSON.stringify({
Expand Down
37 changes: 34 additions & 3 deletions src/app/js/admin/precomputed/PrecomputedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
import { io } from 'socket.io-client';
import CancelButton from '../../lib/components/CancelButton';
import { DEFAULT_TENANT } from '../../../../common/tools/tenantTools';
import getLocale from '../../../../common/getLocale';

// UTILITARY PART
const PRECOMPUTED_FORM = 'PRECOMPUTED_FORM';
Expand All @@ -71,7 +72,31 @@ const renderTextField = ({ input, label, meta: { touched, error } }) => {
);
};

export const renderStatus = (status, polyglot) => {
function getDisplayTimeStartedAt(startedAt) {
const now = new Date();
const startedAtDate = new Date(startedAt);
const diff = now - startedAtDate;

const diffInMinutes = Math.floor(diff / 60000);
const diffInHours = Math.floor(diffInMinutes / 60);
const diffInDays = Math.floor(diffInHours / 24);

const relativeTime = new Intl.RelativeTimeFormat(getLocale(), {
numeric: 'auto',
});
let timeSinceStarted = '';

if (diffInHours < 1) {
timeSinceStarted = relativeTime.format(-diffInMinutes, 'minute');
} else if (diffInDays < 1) {
timeSinceStarted = relativeTime.format(-diffInHours, 'hour');
} else {
timeSinceStarted = relativeTime.format(-diffInDays, 'day');
}
return timeSinceStarted;
}

export const renderStatus = (status, polyglot, startedAt = null) => {
if (status === PENDING) {
return (
<Chip
Expand All @@ -85,7 +110,9 @@ export const renderStatus = (status, polyglot) => {
return (
<Chip
component="span"
label={polyglot.t('precomputed_status_running')}
label={`${polyglot.t(
'precomputed_status_running',
)} (${getDisplayTimeStartedAt(startedAt)})`}
color="info"
/>
);
Expand Down Expand Up @@ -386,7 +413,11 @@ export const PrecomputedForm = ({
>
<Typography>
{polyglot.t('precomputed_status')} : &nbsp;
{renderStatus(precomputedStatus, polyglot)}
{renderStatus(
precomputedStatus,
polyglot,
initialValues?.startedAt,
)}
</Typography>
<Box>
<Button
Expand Down
8 changes: 6 additions & 2 deletions src/app/js/admin/precomputed/PrecomputedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ export const PrecomputedList = ({
{
field: 'status',
headerName: polyglot.t('precomputed_status'),
flex: 1,
flex: 2,
renderCell: params =>
renderStatus(params.value, polyglot),
renderStatus(
params.row.status,
polyglot,
params.row.startedAt,
),
},
{
field: 'run',
Expand Down

0 comments on commit 20f4697

Please sign in to comment.