Creating a Security Dashboard from JSON Data
In the previous tutorial, you displayed individual security events in a table. In this tutorial, you will create a simple dashboard that summarises the data and highlights important security information.
The dashboard will display:
- Total Devices
- Total Events
- Critical Events
- High Severity Events
- Medium Severity Events
Using the sample JSON file, there are 2 devices and 3 events, including one critical event.
Create a Dashboard Section
Open:
script.js
Replace the existing code with:
fetch("smart_security_data.json")
.then(response => response.json())
.then(data => {
let totalDevices = data.devices.length;
let totalEvents = 0;
let criticalEvents = 0;
let highEvents = 0;
let mediumEvents = 0;
data.devices.forEach(device => {
device.events.forEach(event => {
totalEvents++;
if (event.severity === "critical") {
criticalEvents++;
}
if (event.severity === "high") {
highEvents++;
}
if (event.severity === "medium") {
mediumEvents++;
}
});
});
});
This code calculates summary statistics from the JSON data.
Create Dashboard Cards
Below the calculations, add:
let html = `
<h2>Security Dashboard</h2>
<div class="card">
<h3>Total Devices</h3>
<p>${totalDevices}</p>
</div>
<div class="card">
<h3>Total Events</h3>
<p>${totalEvents}</p>
</div>
<div class="card">
<h3>Critical Events</h3>
<p>${criticalEvents}</p>
</div>
<div class="card">
<h3>High Events</h3>
<p>${highEvents}</p>
</div>
<div class="card">
<h3>Medium Events</h3>
<p>${mediumEvents}</p>
</div>
`;
Display the Dashboard
Add:
document.getElementById("output").innerHTML = html;
The completed code should now display the dashboard cards.
Add Dashboard Styling
Open:
index.html
Inside the <style> section, add:
.card {
border: 1px solid #cccccc;
border-radius: 8px;
padding: 15px;
margin-bottom: 10px;
}
.card h3 {
margin-top: 0;
}
Save the file.
View the Dashboard
Open:
http://localhost/json-demo/
Using the sample JSON file, you should see something similar to:
Security Dashboard
Total Devices: 2
Total Events: 3
Critical Events: 1
High Events: 1
Medium Events: 1
Display the Event Table Below the Dashboard
The dashboard is useful, but users often want to see the detailed events as well.
Add the following code underneath the dashboard cards:
html += `
<h2>Security Events</h2>
<table border="1">
<tr>
<th>Device ID</th>
<th>Event Type</th>
<th>Severity</th>
</tr>
`;
Add Event Rows
Add:
data.devices.forEach(device => {
device.events.forEach(event => {
html += `
<tr>
<td>${device.deviceId}</td>
<td>${event.eventType}</td>
<td>${event.severity}</td>
</tr>
`;
});
});
Close the table:
html += "</table>";
Complete JavaScript File
fetch("smart_security_data.json")
.then(response => response.json())
.then(data => {
let totalDevices = data.devices.length;
let totalEvents = 0;
let criticalEvents = 0;
let highEvents = 0;
let mediumEvents = 0;
data.devices.forEach(device => {
device.events.forEach(event => {
totalEvents++;
if (event.severity === "critical") criticalEvents++;
if (event.severity === "high") highEvents++;
if (event.severity === "medium") mediumEvents++;
});
});
let html = `
<h2>Security Dashboard</h2>
<div class="card">
<h3>Total Devices</h3>
<p>${totalDevices}</p>
</div>
<div class="card">
<h3>Total Events</h3>
<p>${totalEvents}</p>
</div>
<div class="card">
<h3>Critical Events</h3>
<p>${criticalEvents}</p>
</div>
<div class="card">
<h3>High Events</h3>
<p>${highEvents}</p>
</div>
<div class="card">
<h3>Medium Events</h3>
<p>${mediumEvents}</p>
</div>
<h2>Security Events</h2>
<table border="1">
<tr>
<th>Device ID</th>
<th>Event Type</th>
<th>Severity</th>
</tr>
`;
data.devices.forEach(device => {
device.events.forEach(event => {
html += `
<tr>
<td>${device.deviceId}</td>
<td>${event.eventType}</td>
<td>${event.severity}</td>
</tr>
`;
});
});
html += "</table>";
document.getElementById("output").innerHTML = html;
});
You now have a dashboard that summarises JSON data and displays detailed event information underneath.
Next tutorial: Filtering and Highlighting Critical Security Events.