diff --git a/infra/nightly-resources/index.js b/infra/nightly-resources/index.js index 53b850ccc..38d7a6ade 100644 --- a/infra/nightly-resources/index.js +++ b/infra/nightly-resources/index.js @@ -68,7 +68,9 @@ function addTableTo(element, data, title) { const copyMacrosButton = document.createElement("button"); copyMacrosButton.innerText = "Copy Latex Macros"; copyMacrosButton.onclick = () => { - const macros = jsonToLatexMacros(data, "runMethod"); + const macros = nestedJsonToLatexMacros(data, "name", "executions", "runMethod"); + console.log("macros"); + console.log(macros); navigator.clipboard.writeText(macros); }; diff --git a/infra/nightly-resources/latex.js b/infra/nightly-resources/latex.js index d103a044f..99b43bfb3 100644 --- a/infra/nightly-resources/latex.js +++ b/infra/nightly-resources/latex.js @@ -1,10 +1,35 @@ +// some hacky functions to convert json to latex tables and macros +// expects json in the same format as `table.js` +// when a string is valid html, gets the text context of the string to display it + // format a number to 2 decimal places as a string // but only if it is a number -function formatNumber(num) { +function formatLatexTableValue(num) { if (typeof num === "number") { return num.toFixed(2); + } else if (typeof num == "string") { + // check if the string is valid html- if so, get the text content + if (num.includes("<")) { + var div = document.createElement("div"); + div.innerHTML = num; + return div.textContent; + } + return num; + } else if (typeof num == "object") { + // expect it to have the form {class: ..., value: ...} + // treat it as just value.value + if ( + Object.keys(num).length === 2 && + num.hasOwnProperty("value") && + num.hasOwnProperty("class") + ) { + return num.value; + } else { + throw new Error("Invalid object format"); + } + } else { + throw new Error("Invalid type for value"); } - return num; } function jsonHeaders(json) { @@ -71,7 +96,7 @@ function jsonToLatexTable(json) { for (var i = 0; i < rows; i++) { for (var j = 0; j < cols; j++) { var value = json[i][headers[j]]; - res += formatNumber(value); + res += formatLatexTableValue(value); if (j < cols - 1) { res += " & "; } @@ -86,6 +111,12 @@ function jsonToLatexTable(json) { // change dashes to underscores function convertStringToValidLatexVar(str) { + // if the str is valid html, get the text content + if (str.includes("<")) { + var div = document.createElement("div"); + div.innerHTML = str; + str = div.textContent + } return str.replace(/-/g, "_"); } @@ -95,7 +126,8 @@ function convertStringToValidLatexVar(str) { // example element: { geoMeanNormalized : "0.619", meanEggccCompileTimeSecs : "0.001", meanLlvmCompileTimeSecs : "0.447", // runMethod : "llvm-O1-O0" } // rowIndex is the name of the header of the left column, in this case "runMethod" -function jsonToLatexMacros(json, rowIndex) { +function jsonToLatexMacros(json, rowIndex, prefix) { + var prefix = prefix || ""; var names = []; var res = ""; @@ -106,12 +138,12 @@ function jsonToLatexMacros(json, rowIndex) { // for each header, col pair create a macro for (var i = 0; i < cols.length; i++) { for (var j = 0; j < headers.length; j++) { - var name = `${convertStringToValidLatexVar(cols[i])}${convertStringToValidLatexVar(headers[j])}`; + var name = `${prefix}${convertStringToValidLatexVar(cols[i])}${convertStringToValidLatexVar(headers[j])}`; res += "\\newcommand{\\" + name + "}{" + - formatNumber(json[i][headers[j]]) + + formatLatexTableValue(json[i][headers[j]]) + "}\n"; names.push(name); @@ -126,3 +158,20 @@ function jsonToLatexMacros(json, rowIndex) { return res; } + + +// given a json like the following: +/// [{ "name": "raytrace", "executions": { data: other_table} }] ... +/// Call jsonToLatexMacros on the other_table for each row index with the prefix name +function nestedJsonToLatexMacros(json, rowIndex, tableIndex, nestedRowIndex) { + var res = ""; + + for (var i = 0; i < json.length; i++) { + var name = convertStringToValidLatexVar(json[i][rowIndex]); + var table = json[i][tableIndex].data; + + res += jsonToLatexMacros(table, nestedRowIndex, name); + } + + return res; +} \ No newline at end of file