This Gist was automatically created by Carbide, a free online programming environment.
Last active
September 30, 2022 21:35
-
-
Save carbide-public/9c899418a32bce052cbdeb217f80e815 to your computer and use it in GitHub Desktop.
untitled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zzz = [ | |
{ | |
"rr": 843, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Login-LoginForm" | |
}, | |
{ | |
"rr": 50, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 7, | |
"uri": "/on/demandware.store/Sites-sheplers_us-Site/default/Login-LoginForm" | |
}, | |
{ | |
"rr": 4, | |
"uri": "/on/demandware.store/Sites-country_outfitters_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 3, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Request-Validate" | |
}, | |
{ | |
"rr": 2, | |
"uri": "/on/demandware.store/Sites-country_outfitters_us-Site/default/Request-Validate" | |
}, | |
{ | |
"rr": 2, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-LoginCustomerForCheckout" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-SendValidationCode" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-GetRewardInfo" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-sheplers_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C1684482757" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C389625348" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C1826598352" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C129144717" | |
} | |
] | |
const yyy = zzz.map( (z) => {return {uriSplit:z.uri.split('/'), rr:z.rr}}); | |
const q = yyy.reduce((prev,current) => {return Math.max(prev,current.uriSplit.length)}, 0); | |
const xMaps = []; | |
for (var i = 1; i < q; i++) { | |
const xMap = {}; | |
yyy.forEach((y) => { | |
if (y.uriSplit[i]) { | |
const fromToExpr = y.uriSplit[i-1]+'-'+ y.uriSplit[i] | |
if (!xMap[fromToExpr]) { | |
xMap[fromToExpr] = {from:y.uriSplit[i-1],to:y.uriSplit[i],weight:0}; | |
} | |
xMap[fromToExpr].weight += y.rr | |
} | |
}); | |
xMaps.push(xMap); | |
} | |
xMaps | |
const str = "/foo/bar/baz/something/else" | |
function nthIndexOf(str, pattern, n) { | |
let i = -1; | |
while (n-- && i++ < str.length) { | |
i = str.indexOf(pattern, i); | |
if (i < 0) break; | |
} | |
return i; | |
} | |
const location = nthIndexOf(str,"/",4); | |
str.substring(0,location); | |
// for (var i=0; i < ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zzz = [ | |
{ | |
"rr": 843, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Login-LoginForm" | |
}, | |
{ | |
"rr": 50, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 7, | |
"uri": "/on/demandware.store/Sites-sheplers_us-Site/default/Login-LoginForm" | |
}, | |
{ | |
"rr": 4, | |
"uri": "/on/demandware.store/Sites-country_outfitters_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 3, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Request-Validate" | |
}, | |
{ | |
"rr": 2, | |
"uri": "/on/demandware.store/Sites-country_outfitters_us-Site/default/Request-Validate" | |
}, | |
{ | |
"rr": 2, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-LoginCustomerForCheckout" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-SendValidationCode" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_us-Site/default/Account-GetRewardInfo" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-sheplers_us-Site/default/Order-SingleOrder" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C1684482757" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C389625348" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C1826598352" | |
}, | |
{ | |
"rr": 1, | |
"uri": "/on/demandware.store/Sites-bootbarn_rsp_us-Site/default/RSP_Login-Form/C129144717" | |
} | |
] | |
function nthIndexOf(str, pattern, n) { | |
let i = -1; | |
while (n-- && i++ < str.length) { | |
i = str.indexOf(pattern, i); | |
if (i < 0) break; | |
} | |
return i; | |
} | |
function addEnoughSpaces(name, nt) { | |
let result = name; | |
while (Object.values(nt).includes(result)) { | |
result = '-' + result; | |
} | |
return result; | |
} | |
const nt = {"/foo/bar":"bar","/foo/bletch/bar":"-bar"} | |
const answer = addEnoughSpaces("bar",nt) | |
function assembleUpTo(urlSegmentVisit, number, nameTable) { | |
const fullUri = urlSegmentVisit.uri.substring(0,nthIndexOf(urlSegmentVisit.uri,"/",number+1)); | |
let encodedName = urlSegmentVisit.uriSplit[number]; | |
if (nameTable[fullUri]) { | |
encodedName = nameTable[fullUri]; | |
} else { | |
encodedName = addEnoughSpaces(encodedName, nameTable); | |
nameTable[fullUri] = encodedName; | |
} | |
return encodedName; | |
} | |
function extractSankeySegments(pathVisits) { | |
const nameTable = {}; | |
// split the urls up so that /foo/bar/baz -> foo | bar | baz | |
const splitPathVisits = pathVisits.map( (z) => {return {uri:z.uri,uriSplit:z.uri.split('/'), rr:z.rr}}); | |
// find the deepest url depth ( /foo/bar/baz depth = 3 ) | |
const maxDepth = splitPathVisits.reduce((prev,current) => {return Math.max(prev,current.uriSplit.length)}, 0); | |
const sankeySegments = []; | |
for (let i = 1; i < maxDepth; i++) { | |
const segmentMap = {}; | |
splitPathVisits.forEach((urlSegmentVisit) => { | |
if (urlSegmentVisit.uriSplit[i]) { | |
const fromToExpr = urlSegmentVisit.uriSplit[i-1]+'-'+ urlSegmentVisit.uriSplit[i]; // for example, foo-bar. We keep track of to and from as the key | |
if (!segmentMap[fromToExpr]) { | |
segmentMap[fromToExpr] = { | |
name:urlSegmentVisit.uriSplit[i], | |
from: assembleUpTo(urlSegmentVisit,i-1,nameTable), | |
to:assembleUpTo(urlSegmentVisit, i,nameTable), | |
weight:0 | |
}; | |
} | |
segmentMap[fromToExpr].weight += urlSegmentVisit.rr | |
} | |
}); | |
Object.keys(segmentMap).forEach( (key) => {sankeySegments.push(segmentMap[key])}) | |
} | |
return sankeySegments; | |
} | |
extractSankeySegments(zzz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment