Skip to content

Commit

Permalink
Refactoring code to generate reproducibility checklist, and restructu…
Browse files Browse the repository at this point in the history
…ring report layout.
  • Loading branch information
lrasmus committed Jan 7, 2025
1 parent baf3ec8 commit a8a8c6d
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function ChecklistItem(props) {
<li key={index}>{answer}</li>
))
) : (
<li>No answers available</li>
<li>No results</li>
)}
</ul>
</div>
Expand Down
239 changes: 4 additions & 235 deletions app/components/ReproChecklist/ReproChecklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ import {
DialogTitle,
} from '@mui/material';
import { SaveAlt } from '@mui/icons-material';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
import ChecklistService from '../../services/checklist';
import GeneralUtil from '../../utils/general';
import ChecklistUtil from '../../utils/checklist';
import AssetUtil from '../../utils/asset';
import Constants from '../../constants/constants';

pdfMake.vfs = pdfFonts.pdfMake.vfs;

const path = require('path');

// These functions are mapped using the statement type to the corresponding scan function
const scanFunctions = {
Dependency: ChecklistUtil.findAssetLanguagesAndDependencies,
Dependency: ChecklistUtil.findProjectLanguagesAndDependencies,
Data: ChecklistUtil.findDataFiles,
Entrypoint: ChecklistUtil.findEntryPointFiles,
Documentation: ChecklistUtil.findDocumentationFiles,
Expand Down Expand Up @@ -71,234 +66,8 @@ function ReproChecklist(props) {

// Handles the generation of the reproducibility checklist report in PDF format
const handleReportGeneration = (exportNotes) => {
// pdfMake requires base64 encoded images
const checkedIcon = GeneralUtil.convertImageToBase64(path.join(__dirname, 'images/yes.png'));
const statWrapLogo = GeneralUtil.convertImageToBase64(
path.join(__dirname, 'images/banner.png'),
);

const documentDefinition = {
content: [
{
image: statWrapLogo,
width: 150,
alignment: 'center',
},
{
text: 'Reproducibility Checklist',
style: 'mainHeader',
alignment: 'center',
margin: [0, 20],
},
{
text: 'Project Overview',
style: 'sectionHeader',
margin: [0, 10],
},
{
text: `Project Name: ${project.name}`,
margin: [0, 5],
},
{
text: `Date: ${new Date().toLocaleDateString()}`,
margin: [0, 5],
},
{
columns: [
{
text: `Checklist Summary`,
style: 'sectionHeader',
margin: [0, 10],
},
{
text: 'Yes',
margin: [0, 12, 5, 0],
width: 30,
alignment: 'right',
fontSize: 16,
bold: true,
noWrap: true,
},
{
text: 'No',
margin: [0, 12],
width: 40,
alignment: 'right',
fontSize: 16,
bold: true,
noWrap: true,
},
],
},
...checklist
.map((item, index) => {
const maxWidth = 450;
let subChecklist = [];
if (item.subChecklist && item.subChecklist.length > 0) {
subChecklist = item.subChecklist.map((subItem, subIndex) => ({
columns: [
{
text: `${index + 1}.${subIndex + 1} ${subItem.statement}`,
margin: [15, 5],
width: '*',
alignment: 'left',
},
subItem.answer
? {
image: checkedIcon,
width: 15,
alignment: 'right',
margin: [0, 5, 25, 0],
}
: {
image: checkedIcon,
width: 15,
alignment: 'right',
margin: [0, 5, 1, 0],
},
],
columnGap: 0,
}));
}

let notes = [];
if (exportNotes && item.notes && item.notes.length > 0) {
notes = item.notes.map((note, noteIndex) => ({
text: `${noteIndex + 1}. ${note.content}`,
margin: [20, 2],
width: maxWidth,
}));
}

let images = [];
const imageWidth = 135;
if (item.images && item.images.length > 0) {
images = item.images.map((image) => {
const base64Image = GeneralUtil.convertImageToBase64(image.uri);
if (base64Image) {
return {
image: base64Image,
width: imageWidth,
margin: [0, 5],
alignment: 'center',
};
}
return { text: `Failed to load image: ${image.uri}`, color: 'red' };
});
}

// Rendering images by rows, as rendering in columns overflows the page and we can't wrap columns under each other,
// math for 3 images per row is as follows:
// imageWidth*n + calumnGap*(n-1) <= maxWidth - leftMargin - rightMargin
// 135*n + 10*(n-1) <= 450 - 20 - 0;
// n <= 440/145 --> n = 3
const imagesPerRow = Math.floor((maxWidth - 20 + 10) / (imageWidth + 10));

let imageRows = [];
for (let i = 0; i < images.length; i += imagesPerRow) {
imageRows.push({
columns: images.slice(i, i + imagesPerRow),
columnGap: 10,
margin: [20, 5],
});
}

let urls = [];
if (item.urls && item.urls.length > 0) {
urls = item.urls.map((url, urlIndex) => {
return {
unbreakable: true,
columns: [
{
text: `${urlIndex + 1}. `,
width: 25,
margin: [20, 1, 0, 0],
alignment: 'left',
noWrap: true,
},
{
stack: [
{
text: url.title,
margin: [7, 1],
alignment: 'left',
style: 'hyperlink',
link: url.hyperlink,
},
{
text: url.description,
margin: [7, 3],
alignment: 'left',
},
],
width: maxWidth,
},
],
};
});
}

return [
{
columns: [
{
text: `${index + 1}. `,
width: 10,
margin: [0, 10],
alignment: 'left',
},
{
text: `${item.statement}`,
margin: [0, 10, 25, 0],
width: maxWidth,
alignment: 'left',
bold: true,
},
item.answer
? {
image: checkedIcon,
width: 15,
alignment: 'right',
marginRight: 10,
marginTop: 12,
}
: {
image: checkedIcon,
width: 15,
marginLeft: 28,
marginTop: 12,
},
],
columnGap: 5,
},
...subChecklist,
notes.length > 0 ? { text: 'Notes:', margin: [15, 5] } : '',
...notes,
assets.length > 0 ? { text: 'Related Assets:', margin: [15, 10] } : '',
...imageRows,
];
})
.flat(),
],
styles: {
mainHeader: { fontSize: 22, bold: true, color: '#663399' },
sectionHeader: { fontSize: 18, bold: true, color: '#8b6fb3', margin: [0, 20] },
hyperlink: { color: '#0000EE' },
},
defaultStyle: {
fontSize: 12,
},
pageMargins: [40, 25, 40, 60],
footer: function (currentPage, pageCount) {
return {
text: `Page ${currentPage} of ${pageCount}`,
alignment: 'center',
margin: [0, 30],
};
},
};

pdfMake.createPdf(documentDefinition).download('Reproducibility_Checklist.pdf');
const service = new ChecklistService();
service.generateReport(checklist, 'Reproducibility_Checklist.pdf', exportNotes, project);
setOpenExportDialog(false);
};

Expand Down
Loading

0 comments on commit a8a8c6d

Please sign in to comment.