Skip to content

Commit

Permalink
Merge pull request #1 from CLOSER-Cohorts/profileValidator
Browse files Browse the repository at this point in the history
Profile validator
  • Loading branch information
ollylucl authored Jun 13, 2024
2 parents bda8a2f + 3130f1a commit 3b40923
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 6 deletions.
200 changes: 194 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,210 @@
<html>
<body>

<script src="https://cdn.sheetjs.com/xlsx-0.20.2/package/dist/xlsx.full.min.js"></script>
<div style="font-size:20px; padding:5px"><h1>Generate text mappings files</h1>
<input id="input" type="file"/>
<div style="padding:5px"></div>
<div style="padding:5px"></div>
<div style="padding:5px"></div>
<div style="padding:5px"></div>
<div style="padding:5px" id="qv"></div>
<div style="padding:5px" id="tv"></div>
<div style="padding:5px" id="tq"></div>
<div style="padding:5px" id="dv"></div>
</div>

<script>

function createColumn(indexOffset,dataCellField,suffix = "") {
var questionLabels=[]
const indexes2 = [...Array(numberOfVariables).keys().map(x => (x*8)+4)]

indexes2.forEach(x => {
const qvTxtColumn2 = worksheetCells[dataCells[x]]['h']
questionLabels.push(qvTxtColumn2)
}
)

}

function addLinkToPage(linkURL, linkText, downloadedFilename, pageDivId) {

const link = document.createElement("a");

link.href=linkURL

link.text=linkText

link.download=downloadedFilename

document.getElementById(pageDivId).appendChild(link)

}

function createBlobURL(columns) {

var file2 = ""
for (let i = 0; i < columns[0].length; i++) {

const fileEntry = columns.map(column => column[i])

console.log(fileEntry)

if (!(fileEntry.includes("NA") || fileEntry.includes("Derived")))
file2 = file2 +fileEntry.join("\t") +"\n"

}

var formBlob = new Blob([file2], { type: 'text/plain' });

const link2 = window.URL.createObjectURL(formBlob);

return link2

}

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
const fileList = this.files; /* now you can work with the file list */
console.log("HEY two")
console.log(fileList)
const reader = new FileReader()

reader.onload = (function (event) {
const wb = XLSX.read(reader.result);

const cellNames = Object.keys(wb['Sheets']['QV and TV Mappings'])
const worksheetCells = wb['Sheets']['QV and TV Mappings']
console.log(worksheetCells)
const dataCells=cellNames.slice(9,cellNames.length-1)
const numberOfVariables = (dataCells.length)/8
console.log(dataCells)

const cellNames2 = Object.keys(wb['Sheets']['DV'])
const worksheetCells2 = wb['Sheets']['DV']
console.log(worksheetCells2)
const dataCells2=cellNames2.slice(7,cellNames2.length-2)
const numberOfVariables2 = (dataCells2.length)/6
console.log(dataCells2)

// I NEED TO WRITE CODE THAT GENERALISES THE OPERATIONS BELOW

var questionnairePrefix=[]
const indexes = [...Array(numberOfVariables).keys().map(x => (x*8)+3)]
indexes.forEach(x => {
const qvTxtColumn1 = worksheetCells[dataCells[x]]['v']+"_ccs01"
questionnairePrefix.push(qvTxtColumn1)
}
)

var questionLabels=[]
const indexes2 = [...Array(numberOfVariables).keys().map(x => (x*8)+4)]

indexes2.forEach(x => {
const qvTxtColumn2 = worksheetCells[dataCells[x]]['h']
questionLabels.push(qvTxtColumn2)
}
)

var datasetPrefixes=[]
const indexes3 = [...Array(numberOfVariables).keys().map(x => (x*8))]
indexes3.forEach(x => {
const qvTxtColumn3 = worksheetCells[dataCells[x]]['v']
datasetPrefixes.push(qvTxtColumn3)
}
)

var variableNames=[]
const indexes4 = [...Array(numberOfVariables).keys().map(x => (x*8)+1)]

indexes4.forEach(x => {
const qvTxtColumn4 = worksheetCells[dataCells[x]]['v']
variableNames.push(qvTxtColumn4)
}
)

var topicIds=[]
const indexes5 = [...Array(numberOfVariables).keys().map(x => (x*8)+6)]

indexes5.forEach(x => {

const qvTxtColumn5 = worksheetCells[dataCells[x]]['v']
topicIds.push(qvTxtColumn5)

}
)

var derivedVariableNames=[]
const indexes6 = [...Array(numberOfVariables2).keys().map(x => (x*6)+1)]

indexes6.forEach(x => {

const qvTxtColumn6 = worksheetCells2[dataCells2[x]]['v']
derivedVariableNames.push(qvTxtColumn6)

}
)

var sourceVariableNames=[]
const indexes7 = [...Array(numberOfVariables2).keys().map(x => (x*6)+4)]

indexes7.forEach(x => {

const qvTxtColumn7 = worksheetCells2[dataCells2[x]]['v']
sourceVariableNames.push(qvTxtColumn7)

}
)

var variableDatasetPrefixes=[]
const indexes8 = [...Array(numberOfVariables2).keys().map(x => (x*6))]

indexes8.forEach(x => {

const qvTxtColumn8 = worksheetCells2[dataCells2[x]]['v']
variableDatasetPrefixes.push(qvTxtColumn8)

}
)

console.log(questionnairePrefix.length)
console.log(questionLabels.length)
console.log(datasetPrefixes.length)
console.log(variableNames.length)
console.log(topicIds.length)
console.log(derivedVariableNames.length)
console.log(sourceVariableNames.length)
console.log(variableDatasetPrefixes.length)

var qvLinkURL=createBlobURL([questionnairePrefix,
questionLabels,
datasetPrefixes,
variableNames])

addLinkToPage(qvLinkURL, "qv.txt", "qv.txt", "qv")

var tvLinkURL=createBlobURL([datasetPrefixes,
variableNames,
topicIds])

addLinkToPage(tvLinkURL, "tv.txt", "tv.txt", "tv")

var tqLinkURL=createBlobURL([questionnairePrefix,
questionLabels,
topicIds])

addLinkToPage(tqLinkURL, "tq.txt", "tq.txt", "tq")

var dvLinkURL=createBlobURL([variableDatasetPrefixes,
derivedVariableNames,
variableDatasetPrefixes,
sourceVariableNames
])

addLinkToPage(dvLinkURL, "dv.txt", "dv.txt", "dv")

})

reader.readAsArrayBuffer(fileList[0])


}
function generateFiles() {

Expand All @@ -27,4 +215,4 @@


</body>
</html>
</html>
93 changes: 93 additions & 0 deletions profileValidator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<html>
<bvody>
<script src="https://cdn.sheetjs.com/xlsx-0.20.2/package/dist/xlsx.full.min.js"></script>
<div style="font-size:20px; padding:5px"><h1>DDI Profile Validator</h1>
<input id="input" type="file"/>
<div style="padding:5px" id="qv"></div>
<div style="padding:5px" id="tv"></div>
<div style="padding:5px" id="tq"></div>
<div style="padding:5px" id="dv"></div>
</div>
<div id="validatorOutput"></div>

<script>

async function executePostRequestWithoutToken(url, requestBody) {

const options = {
method: 'POST',
body: JSON.stringify(requestBody),
headers: { 'Content-Type': 'application/json' }

}

try {
const res = await fetch(url, options);
return await res;
} catch (error) {
console.log(`Error: API call to ${url} failed! ${error.message}, ${error.cause}, ${error.cause.code}`)
}

}

function createBlobURL(columns) {

var file2 = ""
for (let i = 0; i < columns[0].length; i++) {

const fileEntry = columns.map(column => column[i])

console.log(fileEntry)

if (!(fileEntry.includes("NA") || fileEntry.includes("Derived")))
file2 = file2 +fileEntry.join("\t") +"\n"

}

var formBlob = new Blob([file2], { type: 'text/plain' });

const link2 = window.URL.createObjectURL(formBlob);

return link2

}

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);

function handleFiles() {
const fileList = this.files;
console.log("UPLOADED")
console.log(fileList[0])
const reader = new FileReader()
reader.readAsArrayBuffer(fileList[0])
reader.onload = (function (event) {
parsedJson = (JSON.parse(new TextDecoder().decode(reader.result)));
console.log(parsedJson)
var jsonBody = parsedJson
executePostRequestWithoutToken("https://cmv.cessda.eu/api/V0/Validation",
jsonBody
).then(data => {
return data.json()
}
).then(data2 =>
{
var output = "<div>"
output = output + data2.ConstraintViolation.map(x => "<div>" + x.message + "</div>").join("")
output = output + "</div>"
document.getElementById("validatorOutput").innerHTML = output
}

)

})


}


</script>


</body>
</html>

0 comments on commit 3b40923

Please sign in to comment.