HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h3>Downtime clock</h3>
<div id="downtime"></div>
#downtime {
color: maroon;
}
/**
* Test class used to show how one could calculate takt time with various scenarios for blog post:
* TODO add blog link here
* 12/5/2020 KM
*/
class TaktCalculations {
/**
* Validates data for calculating takt downtime
*
* @param {Number} createdTime
* @param {Number} resolvedTime
* @param {Number} downtime
* @param {Number} index
* @param {Array} taktAlerts - must be in ascending order by creation time
* @return true if invalid data, false if valid
*/
hasInvalidCalculationData(createdTime, resolvedTime, downtime, index, taktAlerts) {
// verify we have data
if (!taktAlerts || !taktAlerts.length === 0) {
return true;
}
// Note: additionally we could check if its ascended here if we wanted to be extra cautious
// createdTime checks
if (isNaN(createdTime)) {
console.error('Created time was not a number or undefined when calculating downtime.');
return true;
}
// resolved checks
if (isNaN(resolvedTime)) {
console.error('Resolved time was not a number or undefined when calculating downtime.');
return true;
}
// downtime checks
if (isNaN(downtime)) {
console.error('Downtime was not a number or undefined when calculating downtime.');
return true;
}
// index checks
if (isNaN(index)) {
console.error('Index was not a number or undefined when calculating downtime.');
return true;
}
}
/**
* Recusively calculates downtime for raised downtime alerts with recursion!
*
* One could add more try/catch and data validations in this function. The original had more checking, but for
* brevity of the code in this blog post, I removed them.
*
* @param {Number} createdTime
* @param {Number} resolvedTime
* @param {Number} totalDowntime
* @param {Number} index
* @param {Array} taktAlerts - must be in ascending order by creation time
* @return {Number}
*/
calculateDowntime(
createdTime,
resolvedTime,
totalDowntime,
index,
taktAlerts
) {
// make sure there isn't any funny business in our data before trying to calculate downtime
if (this.hasInvalidCalculationData(createdTime, resolvedTime, totalDowntime, index, taktAlerts)) {
return 0;
}
// conventient local consts to ease referencing the times
// we could further validate that these have data before continuing, left it out for concise example
const currentTaktAlertCreationTime = taktAlerts[index].creationTime;
const currentTaktAlertResolvedTime = taktAlerts[index].alertResolved ?
taktAlerts[index].resolvedTime : new Date().getTime();
// ////////////////
// initialization
// ////////////////
if (index === 0 && taktAlerts.length === 1) {
// only 1 alert so let's just return downtime
return currentTaktAlertResolvedTime - currentTaktAlertCreationTime;
} else if (index === 0 && taktAlerts.length > 1) {
// start recursing with init data set
return this.calculateDowntime(
currentTaktAlertCreationTime, // createdTime
currentTaktAlertResolvedTime, // resolvedTime
currentTaktAlertResolvedTime - currentTaktAlertCreationTime, // downtime
index + 1, // index
taktAlerts
);
}
// ////////////////
// engulf case
// example: [
// { id: 1, creationTime: 10, resolvedTime: 20},
// { id: 2, creationTime: 11, resolvedTime: 19}
// ]
// ////////////////
if (currentTaktAlertCreationTime >= createdTime &&
currentTaktAlertResolvedTime <= resolvedTime) {
// check for next iteration
if (index + 1 < taktAlerts.length) {
return this.calculateDowntime(
createdTime,
resolvedTime,
totalDowntime,
index + 1,
taktAlerts
);
}
// return total downtime since theres not a next alert
return totalDowntime;
// ////////////////
// overlap case
// example: [
// { id: 1, creationTime: 10, resolvedTime: 20},
// { id: 2, creationTime: 11, resolvedTime: 25}
// ]
// ////////////////
} else if (
currentTaktAlertCreationTime >= createdTime &&
currentTaktAlertCreationTime <= resolvedTime &&
currentTaktAlertResolvedTime >= resolvedTime
) {
// set resolve to TA resolve
totalDowntime += currentTaktAlertResolvedTime - resolvedTime;
resolvedTime = currentTaktAlertResolvedTime;
// check for next iteration
if (index + 1 < taktAlerts.length) {
return this.calculateDowntime(
createdTime,
resolvedTime,
totalDowntime,
index + 1,
taktAlerts
);
}
// overlap base case -> return downtime
return totalDowntime;
// ////////////////
// next alert case
// example: [
// { id: 1, creationTime: 10, resolvedTime: 20},
// { id: 2, creationTime: 40, resolvedTime: 50}
// ]
// ////////////////
} else if (currentTaktAlertCreationTime >= resolvedTime) {
if (index + 1 < taktAlerts.length) {
return this.calculateDowntime(
currentTaktAlertCreationTime,
currentTaktAlertResolvedTime,
totalDowntime + (currentTaktAlertResolvedTime - currentTaktAlertCreationTime),
index + 1,
taktAlerts
);
}
// next alert base case -> return total downtime
return totalDowntime + (currentTaktAlertResolvedTime - currentTaktAlertCreationTime);
} else {
console.error('Unknown alert case when calculating downtime.');
return 0;
}
}
}
// ***********************
// Test Data and Inits
// ***********************
const calculator = new TaktCalculations();
const now = moment();
const fiveMinutesAgo = moment(now).subtract(5, 'm');
const tenMinutesAgo = moment(now).subtract(10, 'm');
const fifteenMinutesAgo = moment(now).subtract(15, 'm');
const twentyMinutesAgo = moment(now).subtract(20, 'm');
const twentyFiveMinutesAgo = moment(now).subtract(25, 'm');
const thirtyMinutesAgo = moment(now).subtract(30, 'm');
const thirtyFiveMinutesAgo = moment(now).subtract(35, 'm');
const fortyMinutesAgo = moment(now).subtract(40, 'm');
const fortyFiveMinutesAgo = moment(now).subtract(45, 'm');
const fiftyMinutesAgo = moment(now).subtract(50, 'm');
const fiftyFiveMinutesAgo = moment(now).subtract(55, 'm');
const oneMinuteInMs = 60000;
const oneSecondInMs = 1000;
const oneTaktAlert = [
{ id: 1, creationTime: twentyFiveMinutesAgo.toDate(), resolvedTime: twentyMinutesAgo.toDate(), alertResolved: true}, // 5
]
const taktAlertsNoOverlapOrEngulf = [
{ id: 1, creationTime: twentyFiveMinutesAgo.toDate(), resolvedTime: twentyMinutesAgo.toDate(), alertResolved: true}, // 5
{ id: 2, creationTime: tenMinutesAgo.toDate(), resolvedTime: fiveMinutesAgo.toDate(), alertResolved: true}, // 5
];
const taktAlertsWithEngulf = [
{ id: 1, creationTime: twentyFiveMinutesAgo.toDate(), resolvedTime: fiveMinutesAgo.toDate(), alertResolved: true}, // 20
{ id: 2, creationTime: twentyMinutesAgo.toDate(), resolvedTime: fifteenMinutesAgo.toDate(), alertResolved: true}, // eng
];
const taktAlertsWithOverlap = [
{ id: 1, creationTime: thirtyFiveMinutesAgo.toDate(), resolvedTime: fifteenMinutesAgo.toDate(), alertResolved: true}, // 15
{ id: 2, creationTime: twentyMinutesAgo.toDate(), resolvedTime: fiveMinutesAgo.toDate(), alertResolved: true}, //ext 15
];
const talkAlertsWithAllScenarios = [
{ id: 1, creationTime: fiftyFiveMinutesAgo.toDate(), resolvedTime: fiftyMinutesAgo.toDate(), alertResolved: true}, // 5
{ id: 2, creationTime: fortyFiveMinutesAgo.toDate(), resolvedTime: thirtyMinutesAgo.toDate(), alertResolved: true}, // 15
{ id: 3, creationTime: fortyMinutesAgo.toDate(), resolvedTime: thirtyFiveMinutesAgo.toDate(), alertResolved: true}, //en
{ id: 4, creationTime: thirtyMinutesAgo.toDate(), resolvedTime: twentyMinutesAgo.toDate(), alertResolved: true}, // 10
{ id: 5, creationTime: twentyFiveMinutesAgo.toDate(), resolvedTime: fifteenMinutesAgo.toDate(), alertResolved: true}, //ext5
{ id: 6, creationTime: tenMinutesAgo.toDate(), resolvedTime: fiveMinutesAgo.toDate(), alertResolved: true} // 5
]
console.log(`${calculator.calculateDowntime(0, 0, 0, 0, oneTaktAlert)/oneMinuteInMs} should be 5`);
console.log(`${calculator.calculateDowntime(0, 0, 0, 0, taktAlertsNoOverlapOrEngulf)/oneMinuteInMs} should be 10`);
console.log(`${calculator.calculateDowntime(0, 0, 0, 0, taktAlertsWithEngulf)/oneMinuteInMs} should be 20`);
console.log(`${calculator.calculateDowntime(0, 0, 0, 0, taktAlertsWithOverlap)/oneMinuteInMs} should be 30`);
console.log(`${calculator.calculateDowntime(0, 0, 0, 0, talkAlertsWithAllScenarios)/oneMinuteInMs} should be 40`);
// Counting Clock due to unresolved alert
const taktAlertsWithOneUnresolved = [
{ id: 1, creationTime: twentyFiveMinutesAgo.toDate(), resolvedTime: twentyMinutesAgo.toDate(), alertResolved: true}, // 5
{ id: 2, creationTime: fifteenMinutesAgo.toDate(), resolvedTime: tenMinutesAgo.toDate(), alertResolved: true}, // 5
{ id: 3, creationTime: fiveMinutesAgo.toDate(), resolvedTime: null, alertResolved: false},
];
setInterval(() => {
const downtime = calculator.calculateDowntime(0, 0, 0, 0, taktAlertsWithOneUnresolved);
const minutes = Math.floor(downtime / oneMinuteInMs);
const seconds = ((downtime % oneMinuteInMs) / oneSecondInMs).toFixed(0);
document.getElementById("downtime").innerHTML = `${minutes} minutes(s) and ${seconds} second(s)`;
}, 750);
Also see: Tab Triggers