-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathreport.js
120 lines (97 loc) · 3.96 KB
/
report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let dbInstance = null;
// Function to get query parameter by name
function getQueryParam(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
async function initDB() {
const SQL = await initSqlJs({
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/${file}`
});
const response = await fetch('Sample4GRM.mmb');
const dbBuffer = await response.arrayBuffer();
dbInstance = new SQL.Database(new Uint8Array(dbBuffer));
}
function renderReports(data) {
const container = document.getElementById('report-nav');
data.reportGroups.forEach(group => {
const groupDiv = document.createElement('div');
groupDiv.className = 'report-group';
groupDiv.innerHTML = `<h3>${group.name}</h3>`;
group.reports.forEach(report => {
const reportElem = document.createElement('div');
reportElem.className = 'report-item';
reportElem.textContent = report.name;
reportElem.onclick = () => loadReport(report);
groupDiv.appendChild(reportElem);
});
container.appendChild(groupDiv);
});
}
async function loadReport(report) {
try {
const sqlPath = `packages/${report.path}`;
const response = await fetch(sqlPath);
const sql = await response.text();
// Display SQL content in the UI
renderSQLContent(report.name, sql);
const stmt = dbInstance.prepare(sql);
const results = stmt.getAsObject({});
const columns = stmt.getColumnNames();
renderResults(report.name, columns, results);
// Update URL with the current report's name
window.history.pushState(
{ reportName: report.name }, // State object (can store data for the history entry)
'', // Title (optional)
`?report=${encodeURIComponent(report.name)}` // Update the URL with the report name
);
} catch (error) {
console.error('Error executing report:', error);
alert('Error loading report: ' + error.message);
} finally {
console.log(`Finished processing report: ${report.name}`);
// Optionally, update UI to reflect the loading status
}
}
function renderResults(title, columns, rows) {
const resultDiv = document.getElementById('query-result');
document.getElementById('report-title').textContent = title;
let html = '<table><tr>';
columns.forEach(col => html += `<th>${col}</th>`);
html += '</tr><tr>';
columns.forEach(col => {
html += `<td>${rows[col] || ''}</td>`;
});
html += '</tr></table>';
resultDiv.innerHTML = html;
}
// Function to render the SQL content and title in the UI
function renderSQLContent(title, sql) {
const sqlContainer = document.getElementById('sql-container');
// Set the report title for the SQL section
const reportTitleElem = sqlContainer.querySelector('#sql-title');
reportTitleElem.textContent = `SQL for: ${title}`; // Dynamic title based on report
// Set the SQL content in the pre tag
const sqlContentElem = sqlContainer.querySelector('#sql-content');
sqlContentElem.textContent = sql; // Display the SQL query in pre-formatted text
}
// 初始化
(async () => {
await initDB();
const response = await fetch('reports.json');
const data = await response.json();
renderReports(data);
// Check if there's a report query parameter in the URL
const reportParam = getQueryParam('report');
if (reportParam) {
// Find the report corresponding to the query parameter
const report = data.reportGroups.flatMap(group => group.reports)
.find(report => report.name === reportParam);
// If the report exists, load it
if (report) {
loadReport(report);
} else {
alert(`Report ${reportParam} not found!`);
}
}
})();