Skip to content

Commit

Permalink
Merge pull request #17 from vicriveros/alert_template
Browse files Browse the repository at this point in the history
ils-status - Alert template
  • Loading branch information
bryanjonker-illinois authored Oct 28, 2023
2 parents 1b4ad4a + e8fe01d commit 5fdb12e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 46 deletions.
9 changes: 9 additions & 0 deletions ils-status/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ Required code to use in the body:

<div class="ils-status"></div>

To display alert cards for maintenance, affected, and down services, add the following parameters:
- **data-ils-status-serviceids**: A list of the service IDs you want to display separated by commas.
- **data-ils-status-template**: The value options are maintenance, affected, and down. If the parameter is missing, the down services will be displayed by default.
- **data-ils-status-daysahead**: Use an integer to determine how much to look ahead (so "10" has everything from today to 10 days ahead). If the parameter is missing, it will include everything (including past events).

Example

<div class="ils-status" data-ils-status-serviceids="65281,65280,64985" data-ils-status-template="maintenance" data-ils-status-daysahead="12"></div>

The passthrough query is at https://statushub.itpartners.illinois.edu/api/GetSimpleStatus?name=illinois

The github for this middleware project is at https://github.com/itpartnersillinois/StatusSupport.
Expand Down
27 changes: 27 additions & 0 deletions ils-status/example_alert_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="dns-prefetch" href="https://cdn.toolkit.illinois.edu">
<link rel="icon" href="https://cdn.brand.illinois.edu/favicon.ico">
<link rel="stylesheet" href="https://cdn.toolkit.illinois.edu/2.14/toolkit.css" media="all">
<script src="https://cdn.toolkit.illinois.edu/2.14/toolkit.js"></script>
<script src="status.js"></script>
<link rel="stylesheet" href="status.css" media="all">
<title>Testing status widget</title>
</head>
<body>
<div class="il-formatted">
<h2>Testing status alert widget</h2>

<div class="ils-status" data-ils-status-serviceids="65281,65280,64985,64974" data-ils-status-template="down" data-ils-status-daysahead="12"></div>

<div class="ils-status" data-ils-status-serviceids="65281,65280,64985,64974" data-ils-status-template="affected" data-ils-status-daysahead="12"></div>

<div class="ils-status" data-ils-status-serviceids="65281,65280,64985,64974" data-ils-status-template="maintenance" data-ils-status-daysahead="12"></div>

</div>


</body>
</html>
29 changes: 29 additions & 0 deletions ils-status/status.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,32 @@
font-size: 2.5rem;
}

.ils-status-alert {
position: relative;
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border: 1px solid transparent;
border-radius: 0.25rem;
}

.ils-status-alert-warning {
color: #856404;
background-color: #fff3cd;
border-color: #ffeeba;
}

.ils-status-alert-danger {
color: #842029;
background-color: #f8d7da;
border-color: #f5c2c7;
}

.ils-status-sr-only:not(:focus):not(:active) {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
129 changes: 83 additions & 46 deletions ils-status/status.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,89 @@
window.addEventListener('load', (event) => {
var target = document.querySelector('div.ils-status');
if (target && target != null) {
checkStatus();
setInterval(function () { checkStatus(); }, 12000);
}
const targets = Array.from(document.querySelectorAll('div.ils-status'));
targets.forEach(target => {
checkStatus(target);
setInterval(function () { checkStatus(target); }, 12000);
});
});

function checkStatus() {
var target = document.querySelector('div.ils-status');
fetch('https://statushub.itpartners.illinois.edu/api/GetSimpleStatus?name=illinois', { method: 'GET', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" } })
.then(res => res.json())
.then(data => {
target.innerHTML = `<a id='ils-status-link' href='https://illinois.statushub.io' aria-label='Go to the Status at Illinois page'><ul class='ils-status-row'>
<li class='ils-status-bubble'><span class='ils-status-green'>${data.NumberUp}</span><span>up</span></li>
<li class='ils-status-bubble'><span class='ils-status-yellow'>${data.NumberAffected}</span><span>affected</span></li>
<li class='ils-status-bubble'><span class='ils-status-red'>${data.NumberDown}</span><span>down</span></li>
</ul></a>`;
if (data.DownServices.length > 0) {
target.innerHTML += '<p>The following services are down: </p><ul>';
data.DownServices.forEach(d => {
target.innerHTML += `<li><a href='${d.Url}'>${d.Name}</a><br>${d.Incident}</li>`;
});
target.innerHTML += '</ul>';
const dataSourceSimple = 'https://statushub.itpartners.illinois.edu/api/GetSimpleStatus?name=illinois';
const dataSourceDetailed = 'https://statushub.itpartners.illinois.edu/api/GetDetailedStatus?name=illinois';

async function fetchData(dataSource){
const response = await fetch(dataSource, { method: 'GET', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" } });
const data = await response.json();
return data;
}

async function checkStatus(target) {
let serviceids = target.getAttribute('data-ils-status-serviceids');
if (serviceids && serviceids != null) {
let daysahead = target.getAttribute('data-ils-status-daysahead');
let daysaheadString = daysahead == null ? '' : '&daysAhead='+daysahead;
let template = target.getAttribute('data-ils-status-template');
let url = dataSourceDetailed + '&serviceIds='+serviceids+daysaheadString;
let data = await fetchData(url);
alertTemplate(target, data, template);
}else{
let data = await fetchData(dataSourceSimple);
simpleStatusTemplate(target, data);
}
}

function simpleStatusTemplate(target, data) {
target.innerHTML = `<a id='ils-status-link' href='https://illinois.statushub.io' aria-label='Go to the Status at Illinois page'><ul class='ils-status-row'>
<li class='ils-status-bubble'><span class='ils-status-green'>${data.NumberUp}</span><span>up</span></li>
<li class='ils-status-bubble'><span class='ils-status-yellow'>${data.NumberAffected}</span><span>affected</span></li>
<li class='ils-status-bubble'><span class='ils-status-red'>${data.NumberDown}</span><span>down</span></li>
</ul></a>`;
if (data.DownServices.length > 0) {
target.innerHTML += '<p>The following services are down: </p><ul>';
data.DownServices.forEach(d => {
target.innerHTML += `<li><a href='${d.Url}'>${d.Name}</a><br>${d.Incident}</li>`;
});
target.innerHTML += '</ul>';

let downHeader = document.createElement('p');
downHeader.innerHTML = 'Down services:';
let down = document.createElement('ul');
data.AffectedServices.forEach(d => {
let li = document.createElement('li');
li.innerHTML = `<a href='${d.Url}'>${d.Name}</a><br>${d.Incident}`;
down.appendChild(li);
});
target.appendChild(downHeader);
target.appendChild(down);
let downHeader = document.createElement('p');
downHeader.innerHTML = 'Down services:';
let down = document.createElement('ul');
data.AffectedServices.forEach(d => {
let li = document.createElement('li');
li.innerHTML = `<a href='${d.Url}'>${d.Name}</a><br>${d.Incident}`;
down.appendChild(li);
});
target.appendChild(downHeader);
target.appendChild(down);

}
if (data.AffectedServices.length > 0) {
let affectedHeader = document.createElement('p');
affectedHeader.innerHTML = 'Affected services:';
let affected = document.createElement('ul');
data.AffectedServices.forEach(a => {
let li = document.createElement('li');
li.innerHTML = `<a href='${a.Url}'>${a.Name}</a><br>${a.Incident}`;
affected.appendChild(li);
});
target.appendChild(affectedHeader);
target.appendChild(affected);
}
console.log(target.innerHTML);
});
}
if (data.AffectedServices.length > 0) {
let affectedHeader = document.createElement('p');
affectedHeader.innerHTML = 'Affected services:';
let affected = document.createElement('ul');
data.AffectedServices.forEach(a => {
let li = document.createElement('li');
li.innerHTML = `<a href='${a.Url}'>${a.Name}</a><br>${a.Incident}`;
affected.appendChild(li);
});
target.appendChild(affectedHeader);
target.appendChild(affected);
}
}

function alertTemplate(target, data, templateName){
let template = data.DownServices;
let label = 'Down - ';
let style = 'danger';
let alert ='', date='';
if(templateName == 'maintenance'){template = data.MaintenanceServices; label = 'Maintenance - '; style = 'warning';}
if(templateName == 'affected'){template = data.AffectedServices; label = 'Affected - '; style = 'warning';}
template.forEach(d => {
if(templateName == 'maintenance'){date = d.StartDate.substr(0, 5); label = 'Maintenance ('+ date +') - ';}
alert += `
<div class="ils-status-alert ils-status-alert-${style}">
<h2 class="ils-status-sr-only">Status alert announcement</h2>
<span>${label}</span> <a title="Alert information" href="${d.Url}"> ${d.Incident} </a>
</div>`;
})

target.innerHTML = alert;
}

0 comments on commit 5fdb12e

Please sign in to comment.