Skip to content

Commit

Permalink
Merge pull request #10 from viivue/time
Browse files Browse the repository at this point in the history
Enhance filter domain function
  • Loading branch information
phucbm authored Oct 18, 2022
2 parents 1a2be1e + 43f3b80 commit fc314d7
Showing 1 changed file with 185 additions and 14 deletions.
199 changes: 185 additions & 14 deletions src/upTime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {getDataFromSessionStorage, saveToSessionStorage} from "./utils";

let currentTime = Date.now();
let urlObj;
let urlObj = {},
urlList = [],
urlStorageObj = {},
index = 0,
lastAccess = false;

// Real time on particular page
const realTime = Date.now();
Expand All @@ -12,10 +16,41 @@ const realTime = Date.now();
* @returns boolean
*/
const isLastAccessPage = () => {
const urlObjStorage = getDataFromSessionStorage('FEDebugData');
if(!urlObjStorage) return false;
urlStorageObj = getDataFromSessionStorage('FEDebugData');
if(!urlStorageObj) return false;

urlObj = urlObjStorage;
// Destructuring object from SessionStorage
urlStorageObj.url = getUrl();
urlList = urlStorageObj.urlList;

// Does the page exist by checking includes
urlObj = urlList.find(urlObj => !!(urlObj.url.includes(getUrl()) || getUrl().includes(urlObj.url)));

// Has existed in Session Storage
if(urlObj){
// Absolutely Equivalent
if(urlObj.urlRefs.find(url => url === getUrl()) === undefined){
urlObj.urlRefs.push(getUrl());
}

// Get new domain (comparing with refs)
urlObj.url = getDomain(urlObj.url, urlObj.urlRefs);

// Replace new URL (with the exact URL)
urlStorageObj.urlList = replaceUrlWithNewUrl(urlObj.url, urlStorageObj.urlList);

// find Index to re-assign for total time before exiting
index = urlList.findIndex(urlObj => !!(urlObj.url.includes(getUrl()) || getUrl().includes(urlObj.url)));
}else{
// create the new one
index = urlList.length;
urlObj = {
url: getUrl(),
urlRefs: [getUrl()],
totalTime: 0,
};
urlStorageObj.urlList.push(urlObj);
}
return true;
};

Expand All @@ -26,27 +61,56 @@ const isLastAccessPage = () => {
const checkLastAccessPage = () => {
if(isLastAccessPage()){
currentTime = Date.now() - urlObj.totalTime;
lastAccess = true;
return;
}
const urlInstance = new URL(location.href);
urlObj = {
host: urlInstance.host,
hostname: urlInstance.hostname,
href: urlInstance.href,
origin: urlInstance.origin,
pathname: urlInstance.pathname
url: getUrl(),
urlRefs: [getUrl()],
totalTime: 0
};
urlStorageObj = {
url: getUrl(),
urlList: [urlObj],
};
};


/**
* Replace URL Array
* @returns array
*/
const replaceUrlWithNewUrl = (url, urlList) => {
const results = [{
url,
urlRefs: []
}];
let isReplace = false;
let maxTime = 0;

for(let i = 0; i < urlList.length; i++){
const currentObj = urlList[i];
if(!currentObj.url.includes(url)){
results.push(currentObj);
continue;
}
results[0].urlRefs = [...results[0].urlRefs, ...currentObj.urlRefs];
maxTime = currentObj.totalTime > maxTime ? currentObj.totalTime : maxTime;
isReplace = true;
}

if(!isReplace) return results.splice(1);
results[0].totalTime = maxTime;
return results;
};


/**
* Check if the page had accessed before and re-assigned currentTime variable
*/
const handlePageExit = () => {
saveToSessionStorage('FEDebugData', {
...urlObj,
totalTime: Date.now() - currentTime
});
urlStorageObj.urlList[index].totalTime = Date.now() - currentTime;
saveToSessionStorage('FEDebugData', urlStorageObj);
};
window.addEventListener('load', checkLastAccessPage);
window.addEventListener('beforeunload', handlePageExit);
Expand All @@ -73,4 +137,111 @@ export const getDiffTime = (currentDate, previousDate = currentTime) => {
*/
export const getRealTime = (currentDate, previousDate = realTime) => {
return getDiffTime(currentDate, previousDate);
};

/**
* Get URL string
* @returns string
*/
const getUrl = () => {
const url = new URL(location.href);
if(url.pathname === '/') return url.origin;
return url.origin + url.pathname + '/';
};


/**
* Add slash to the last character
* @param arr
* @returns array
*/
const addSlashToURL = (arr) => {
return arr.map(str => {
if(str.slice(-1) === '/') return str;
return str + '/';
});
};


/**
* Get the same string from 2 strings
* @param firstString
* @param secondString
* @returns string
*/
const getSameString = (firstString, secondString) => {
const fStrArray = firstString.split('/');
const sStrArray = secondString.split('/');

let newStr = '';

for(let i = 0; i < Math.min(fStrArray.length, sStrArray.length); i++){
const word = fStrArray[i];
if(word !== sStrArray[i]) return newStr;
newStr += word + '/';
}
return newStr.slice(0, -1);
};


/**
* Get different from domain list
* @param urlArray
* @returns {*}
*/
const getDiffDomainFromURL = (urlArray) => {
if(!urlArray) return;
if(urlArray.length === 1) return urlArray;

const newUrlArray = addSlashToURL(urlArray);
const results = [];

let i = 0, j = 0;
while(i < newUrlArray.length - 1){
const str1 = newUrlArray[i];
j = i + 1;
while(j < newUrlArray.length){
const str2 = newUrlArray[j++];
const newStr = getSameString(str1, str2);

// not contains the result of compare string and not a valid URL
if(!newStr || newUrlArray.find(str => str === newStr) === undefined)
continue;

const resultUrl = results.find(str => str === newStr);
if(!resultUrl){
const idx = results.findIndex(str => str.includes(newStr) || newStr.includes(str));

// not exist yet
if(idx === -1){
results.push(newStr);
continue;
}

// already exist
if(results[idx].includes(newStr)){
results[idx] = newStr;
}
}
}
i++;
}
return results;
};


/**
* Get home-domain from domain list
* @param url
* @param referenceUrls
* @returns string
*/
const getDomain = (url, referenceUrls = []) => {
if(!url) return '';
if(referenceUrls.length === 0) return url;

const hasExist = referenceUrls.find(urlArr => urlArr.includes(url));
if(hasExist === undefined) return url;

return getDiffDomainFromURL([url, ...referenceUrls])[0];
};

0 comments on commit fc314d7

Please sign in to comment.