Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h3>Downtime clock</h3>
<div id="downtime"></div>
              
            
!

CSS

              
                #downtime {
  color: maroon;
}
              
            
!

JS

              
                
/**
 * 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);
              
            
!
999px

Console