Skip to content

Commit

Permalink
fix(export): ajout des messages d'erreur au niveau de l'UI de l'export (
Browse files Browse the repository at this point in the history
#1171)

* fix(export): done

* remise de lexport

* fix: erreur

* Update targets/frontend/src/components/export-es/Status.tsx

Co-authored-by: Martial Maillot <[email protected]>

* fix: dataset

---------

Co-authored-by: Martial Maillot <[email protected]>
  • Loading branch information
maxgfr and m-maillot authored Dec 18, 2023
1 parent ba7dc37 commit 2375126
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 154 deletions.
1 change: 1 addition & 0 deletions shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ export interface ExportEsStatus {
created_at: Date;
updated_at: Date;
user?: User;
error?: string;
}

export type Answer = {
Expand Down
26 changes: 3 additions & 23 deletions targets/export-elasticsearch/src/repositories/graphql/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ mutation createExportEsStatus($id: uuid!, $user_id: uuid!, $environment: String!
}`;

export const updateOneExportEsStatus = `
mutation updateOneExportEsStatus($id: uuid!, $status: String!, $updated_at: timestamptz) {
update_export_es_status_by_pk(pk_columns: {id: $id}, _set: {status: $status, updated_at: $updated_at}) {
mutation updateOneExportEsStatus($id: uuid!, $status: String!, $updated_at: timestamptz, $error: String) {
update_export_es_status_by_pk(pk_columns: {id: $id}, _set: {status: $status, updated_at: $updated_at, error: $error}) {
id
environment
status
Expand All @@ -29,26 +29,6 @@ mutation updateOneExportEsStatus($id: uuid!, $status: String!, $updated_at: time
}
created_at
updated_at
}
}`;

export const updateExportEsStatus = `
mutation updateExportEsStatus($old_status: String!, $new_status: String!, $updated_at: timestamptz) {
update_export_es_status(
where: {status: {_eq: $old_status }}
_set: {status: $new_status, updated_at: $updated_at}) {
returning {
id
environment
status
user_id
user {
name
email
created_at
}
created_at
updated_at
}
error
}
}`;
21 changes: 4 additions & 17 deletions targets/export-elasticsearch/src/repositories/graphql/queries.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
export const getExportEsStatusById = `
query getExportEsStatusById($id: uuid!) {
export_es_status_by_pk(id: $id) {
id
environment
status
user_id
user {
name
email
created_at
}
created_at
updated_at
}
}`;

export const getAllExport = `
query getAllExport {
export_es_status(order_by: {updated_at: desc}) {
Expand All @@ -29,6 +12,7 @@ query getAllExport {
}
created_at
updated_at
error
}
}`;

Expand All @@ -46,6 +30,7 @@ query getExportEsStatusByEnvironments($environment: String!) {
}
created_at
updated_at
error
}
}`;

Expand All @@ -63,6 +48,7 @@ query getLatestExportEsStatus($environment: String!) {
}
created_at
updated_at
error
}
}
`;
Expand All @@ -81,5 +67,6 @@ query getExportEsStatusByStatus($status: String!) {
}
created_at
updated_at
error
}
}`;
46 changes: 3 additions & 43 deletions targets/export-elasticsearch/src/repositories/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import {
createExportEsStatus,
getAllExport,
getExportEsStatusByEnvironments,
getExportEsStatusById,
getExportEsStatusByStatus,
getLatestExportEsStatus,
updateExportEsStatus,
updateOneExportEsStatus,
} from "./graphql";

Expand Down Expand Up @@ -52,7 +50,8 @@ export class ExportRepository {
public async updateOne(
id: string,
status: Status,
updatedAt: Date
updatedAt: Date,
error?: string
): Promise<ExportEsStatus> {
const res = await gqlClient()
.mutation<{ update_export_es_status_by_pk: ExportEsStatus }>(
Expand All @@ -61,6 +60,7 @@ export class ExportRepository {
id,
status,
updated_at: updatedAt,
error: error ?? null,
}
)
.toPromise();
Expand All @@ -73,46 +73,6 @@ export class ExportRepository {
return res.data.update_export_es_status_by_pk;
}

public async updateAll(
oldStatus: Status,
newStatus: Status,
updatedAt: Date
): Promise<ExportEsStatus[]> {
const res = await gqlClient()
.mutation<{ update_export_es_status_by_pk: ExportEsStatus[] }>(
updateExportEsStatus,
{
new_status: newStatus,
old_status: oldStatus,
updated_at: updatedAt,
}
)
.toPromise();
if (res.error) {
throw res.error;
}
if (!res.data?.update_export_es_status_by_pk) {
throw new Error("Failed to update, undefined object");
}
return res.data.update_export_es_status_by_pk;
}

public async getOneById(id: string): Promise<ExportEsStatus> {
const res = await gqlClient()
.query<{ export_es_status_by_pk: ExportEsStatus }>(
getExportEsStatusById,
{ id }
)
.toPromise();
if (res.error) {
throw res.error;
}
if (!res.data?.export_es_status_by_pk) {
throw new Error("Failed to get, undefined object");
}
return res.data.export_es_status_by_pk;
}

public async getLatestByEnv(
environment: Environment
): Promise<ExportEsStatus> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,7 @@ export class FakeExportRepository {
status,
updated_at: updatedAt,
user_id: "updated-id",
};
}

async updateAll(
_oldStatus: Status,
newStatus: Status,
updatedAt: Date
): Promise<ExportEsStatus[]> {
await wait(100);
return [
{
created_at: new Date(),
environment: Environment.preproduction,
id: "1",
status: newStatus,
updated_at: updatedAt,
user_id: "updatedAll-id",
},
];
}

async getOneById(id: string): Promise<ExportEsStatus> {
await wait(100);
return {
created_at: new Date(),
environment: Environment.preproduction,
id,
status: Status.completed,
updated_at: new Date(),
user_id: "getOne-id",
error: "error",
};
}

Expand Down
6 changes: 3 additions & 3 deletions targets/export-elasticsearch/src/services/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export class ExportService {
Status.completed,
new Date()
);
} catch (e) {
console.error(e);
} catch (e: any) {
return await this.exportRepository.updateOne(
id,
Status.failed,
new Date()
new Date(),
e.message
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const ingester = async (): Promise<string> => {
);
resolve("Export elasticsearch completed successfully");
} catch (error: unknown) {
console.error("Error during export : ", JSON.stringify(error, null, 2));
reject(error);
}
});
Expand Down
81 changes: 70 additions & 11 deletions targets/frontend/src/components/export-es/Status.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import { Status as StatusType } from "@shared/types";
import { MdTimelapse } from "react-icons/md";
import { Box, Typography } from "@mui/material";
import {
Box,
CircularProgress,
Popover,
Typography,
Button,
IconButton,
} from "@mui/material";
import { fr } from "@codegouvfr/react-dsfr";
import React from "react";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";

type StatusProps = {
status?: StatusType;
error?: string;
};

export function Status({ status }: StatusProps): JSX.Element {
export function Status({ status, error }: StatusProps): JSX.Element {
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(
null
);

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};

const open = Boolean(anchorEl);
const id = open ? "simple-popover" : undefined;
const errorDisplayed =
error ?? "Aucune information sur l'erreur n'a été enregistrée";

if (!status) {
return (
<Box sx={{ alignItems: "center", display: "flex" }}>
<CircularProgress size="25px" style={{ marginRight: "1rem" }} />
<Typography>En cours</Typography>{" "}
<MdTimelapse style={{ marginBottom: "-.3rem", marginLeft: ".4rem" }} />
</Box>
);
}
Expand All @@ -32,17 +58,50 @@ export function Status({ status }: StatusProps): JSX.Element {
case StatusType.running:
return (
<Box sx={{ alignItems: "center", display: "flex" }}>
<Typography>En cours</Typography>{" "}
<MdTimelapse
style={{ marginBottom: "-.2rem", marginLeft: ".3rem" }}
/>
<CircularProgress size="25px" style={{ marginRight: "1rem" }} />
<Typography>En cours</Typography>
</Box>
);
case StatusType.failed:
return (
<Typography color={fr.colors.decisions.text.default.error.default}>
Erreur
</Typography>
<>
<Button
aria-describedby={id}
variant="outlined"
onClick={handleClick}
color="error"
>
Erreur
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
sx={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
}}
>
<Typography sx={{ p: 2 }}>
{errorDisplayed}{" "}
<IconButton
aria-label="close"
onClick={() => {
navigator.clipboard.writeText(errorDisplayed);
handleClose();
}}
>
<ContentCopyIcon />
</IconButton>
</Typography>
</Popover>
</>
);
default:
return <Typography>{status}</Typography>;
Expand Down
13 changes: 11 additions & 2 deletions targets/frontend/src/hooks/exportEs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type ExportEsState = {
export function useExportEs(): [
ExportEsState,
() => void,
(environment: Environment, user: User) => void
(environment: Environment, user: User) => void,
(env: Environment) => Date
] {
const [state, setState] = useState<ExportEsState>({
error: null,
Expand Down Expand Up @@ -57,6 +58,14 @@ export function useExportEs(): [
});
};

const getLatestDeployDate = (env: Environment) => {
const lastestCompleted = state?.exportData.filter(
(data) => data.status === Status.completed && data.environment === env
)[0];

return lastestCompleted?.created_at;
};

const runExportEs = (environment: Environment, user: User) => {
const newExportEs: ExportEsStatus = {
created_at: new Date(),
Expand Down Expand Up @@ -131,5 +140,5 @@ export function useExportEs(): [
});
};

return [state, getExportEs, runExportEs];
return [state, getExportEs, runExportEs, getLatestDeployDate];
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function DocumentList({

{isLoadingDocs ? (
<Stack mt={2} justifyContent="center">
<Spinner></Spinner>
<Spinner />
</Stack>
) : (
<>
Expand Down
Loading

0 comments on commit 2375126

Please sign in to comment.