mysqlExporterDeno/static/index.html
2020-04-14 12:14:07 +02:00

179 lines
No EOL
5.7 KiB
HTML

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MySQL exporter</title>
</head>
<body>
<div id="app">Loading...</div>
<style>
body {
margin: 30px auto;
max-width: 900px;
line-height: 1.6;
font-size: 18px;
color: #444;
padding: 0 10px
}
h1,
h2,
h3 {
font-family: Simplifica;
line-height: 1.2
}
a {
text-decoration: none;
color: rgb(0, 140, 255);
}
a:visited {
color: rgb(153, 0, 255);
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
word-break: normal;
text-align: center;
}
table {
width: min(90vw, 900px);
table-layout: fixed;
}
th {
padding: 10px;
}
td {
padding: 5px;
}
tr:hover {
background-color: #444;
color: white;
}
.collation, .extra, .default {
word-break: break-all;
}
.null,
.key {
width: 40px;
}
</style>
<script>
const app = document.getElementById("app");
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
(async () => {
if (window.location.hostname !== "localhost") {
console.warn("Fetching info supported only from localhost!");
return;
}
var dbRes = await fetch("/database");
var db = await dbRes.json();
if (!dbRes.ok) return console.warn("Couldn't fetch data");
app.innerText = "";
const appTitle = document.createElement("h1");
appTitle.innerText = "Data dictionary of " + db.name;
app.appendChild(appTitle);
const date = document.createElement("p");
date.innerText = "Loading table information...";
app.appendChild(date);
var res = await fetch("/tables");
var tables = await res.json();
var re = await fetch("/fieldsIgnored");
var fieldsIgnored = await re.json();
fieldsIgnored = fieldsIgnored.map(el => el.toLowerCase());
date.innerText = "Generating data dictionary...";
for (var table in tables) {
var data = tables[table];
var tableEl = document.createElement("table");
var title = document.createElement("h2");
title.innerText = table;
app.appendChild(title);
if (!data.columns) {
console.error(data);
app.appendChild(htmlToElement("<p>" + data + "<p>"));
continue;
}
try {
var comment = document.createElement("p");
comment.innerText = data.comment[0].table_comment;
app.appendChild(comment);
} catch (e) {}
var head = htmlToElement(`
<tr>
${fieldsIgnored.includes("field") ? "" : "<th class='field'>Field</th>" }
${fieldsIgnored.includes("type") ? "" : "<th class='type'>Type</th>" }
${fieldsIgnored.includes("collation") ? "" : "<th class='collation'>Collation</th>" }
${fieldsIgnored.includes("null") ? "" : "<th class='null'>Null?</th>" }
${fieldsIgnored.includes("key") ? "" : "<th class='key'>Key?</th>" }
${fieldsIgnored.includes("default") ? "" : "<th class='default'>Default</th>" }
${fieldsIgnored.includes("extra") ? "" : "<th class='extra'>Extra?</th>" }
${fieldsIgnored.includes("privileges") ? "" : "<th class='privileges'>Privileges</th>" }
${fieldsIgnored.includes("comment") ? "" : "<th class='comment'>Comment</th>" }
</tr>
`);
tableEl.appendChild(head);
for (var column of data.columns) {
var el = "";
for (var field in column) {
if (fieldsIgnored.includes(field.toLowerCase())) continue;
if (field.toLowerCase() === "privileges") {
column[field] = column[field].split(",").join(", ");
}
if (column[field]) {
el += "<td class='" + field.toLowerCase() + "'>" + column[field] + "</td>";
} else if (field == "Default") {
el += "<td class='" + field.toLowerCase() + "'>null</td>";
} else {
el += "<td class='" + field.toLowerCase() + "'></td>";
}
}
tableEl.appendChild(htmlToElement(`
<tr>
${el}
</tr>
`))
}
app.appendChild(tableEl);
}
var d = new Date();
date.innerHTML = "Generated " + d.toString() + ".<br>Number of tables: " + Object.keys(tables)
.length + "<br>Host: " + db.host;
})()
</script>
</body>
</html>