/**
 * https://developer.mozilla.org/en-US/docs/Web/API/PerformancePaintTiming
 */
function calculatePaintingTimes(result) {
  if ('performance' in window) {
    let performance = window.performance;
    let performanceEntries = performance.getEntriesByType('paint');
    performanceEntries.forEach((performanceEntry, i, entries) => {
      result[performanceEntry.name] = Math.round(performanceEntry.startTime);
    });
  }
}

/**
 * https://developers.google.com/web/fundamentals/performance/critical-rendering-path/measure-crp
 */
function measureCRP(result) {
  if ('performance' in window) {
    let t = window.performance.timing;
    result['dom-interactive-time'] = t.domInteractive - t.navigationStart;
    result['dom-content-loaded'] = t.domContentLoadedEventEnd - t.navigationStart;
    result['page-loaded'] = t.loadEventEnd - t.navigationStart;
    result['request-start'] = t.requestStart - t.navigationStart;
    result['response-start'] = t.responseStart - t.navigationStart;
    result['response-end'] = t.responseEnd - t.navigationStart;
    return result;
  }
}

function readConnectionType(result) {
  var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
  if (connection) {
    result['connection'] = connection.effectiveType;
    result['downlink'] = connection.downlink;
  }
}

import ttiPolyfill from './tti-polyfill.js';

function sendResults(result) {
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/logs/", true);
  xhr.setRequestHeader("Content-type", "application/json");
  let data = {
    attributes: {
      context: result,
      url: window.location.href
    }
  };
  xhr.send(JSON.stringify(data));
}

function calculateMetrics() {
  let result = {};
  if(window.location.pathname.match('\/(dashboard|calendar|news|courses)\/')){
    ttiPolyfill.getFirstConsistentlyInteractive().then((tti) => {
      result['time-to-interactive'] = Math.round(tti);
      return result;
    }).then(result => {
      measureCRP(result);
      calculatePaintingTimes(result);
      readConnectionType(result);
      sendResults(result);
    });
  }
}

window.addEventListener('load', function () {
  function updateOnlineStatus(event) {
    if (navigator.onLine) {
      $("#offlineAlert").hide('slow');
    } else {
      $("#offlineAlert").show('slow');
    }
  }
  window.addEventListener('online', updateOnlineStatus);
  window.addEventListener('offline', updateOnlineStatus);
  updateOnlineStatus();
});

calculateMetrics();

@@ -1733,89 +1733,8 @@
 %7B%7D;%0A
-  if(window.location.pathname.match('%5C/(dashboard%7Ccalendar%7Cnews%7Ccourses)%5C/'))%7B%0A  
   tt
@@ -1789,26 +1789,24 @@
 ((tti) =%3E %7B%0A
-  
     result%5B'
@@ -1850,18 +1850,16 @@
 i);%0A    
-  
 return r
@@ -1865,18 +1865,16 @@
 result;%0A
-  
   %7D).the
@@ -1883,26 +1883,24 @@
 result =%3E %7B%0A
-  
     measureC
@@ -1915,18 +1915,16 @@
 t);%0A    
-  
 calculat
@@ -1951,18 +1951,16 @@
 t);%0A    
-  
 readConn
@@ -1979,18 +1979,16 @@
 esult);%0A
-  
     send
@@ -2004,28 +2004,22 @@
 esult);%0A
-  
   %7D);%0A
-  %7D%0A
 %7D%0A%0Awindo

enable logging on all pages