Skip to content

Instantly share code, notes, and snippets.

@gramcha
Last active April 8, 2022 05:15
Show Gist options
  • Save gramcha/eedf5a771d830d4655902b6b2d212872 to your computer and use it in GitHub Desktop.
Save gramcha/eedf5a771d830d4655902b6b2d212872 to your computer and use it in GitHub Desktop.
share of business
function getShareMetaData() {
return {
t1: {
percentage: 30,
},
t2: {
percentage: 50,
},
t3: {
percentage: 20,
},
};
}
let totalCountPerPeriod = 0;
let transporterMap = {};
function getTransporter(shareMetadata) {
// console.log({transporterMap});
Object.keys(shareMetadata).forEach((transporterName) => {
if (!transporterMap[transporterName]) {
// console.log('going in...');
transporterMap[transporterName] = {
maxRatio: shareMetadata[transporterName].percentage/100,
currentCount: 0
};
}
});
// console.log({transporterMap});
const newCount = totalCountPerPeriod + 1;
const projectedTransporterRatios = Object.keys(transporterMap).map((transporterName) => {
const transporter = transporterMap[transporterName];
console.log('transporter -', transporter);
console.log('newCount -', newCount);
console.log('transporter.currentCount / newCount -', transporter.currentCount / newCount);
return {
...{
transporter: transporterName
},
ratio: (transporter.currentCount>0?transporter.currentCount / newCount:0),
};
});
const selectedTransporter = projectedTransporterRatios.reduce((acc, projectedTransporterRatio)=>{
console.log({transporterRatio: projectedTransporterRatio});
console.log('transporterRatio<acc.ratio - ', projectedTransporterRatio<acc.ratio);
if(projectedTransporterRatio.ratio<acc.ratio) {
console.log('transporterMap[transporterRatio.transporter].maxRatio - ', transporterMap[projectedTransporterRatio.transporter].maxRatio);
if (!projectedTransporterRatio.transporter) {
console.log('@1');
acc = projectedTransporterRatio;
}
else if (projectedTransporterRatio.ratio < transporterMap[projectedTransporterRatio.transporter].maxRatio) {
console.log('@2');
acc = projectedTransporterRatio;
} else {
console.log('@3');
}
}
return acc;
},{transporter: '', ratio: 100.1});
// console.log(JSON.stringify({transporterRatios}, null, 2));
totalCountPerPeriod = newCount;
console.log({selectedTransporter});
transporterMap[selectedTransporter.transporter].currentCount += 1;
// transporterMap[selectedTransporter.transporter].ratio = transporterMap[selectedTransporter.transporter].currentCount / totalCountPerPeriod;
Object.keys(transporterMap).forEach((transporterName)=>{
transporterMap[transporterName].ratio = transporterMap[transporterName].currentCount / totalCountPerPeriod;
});
console.log({selectedTransporter});
console.log({transporterMap});
}
const shareMetadata = getShareMetaData();
for(let i=0; i<80;i+=1) {
console.log('incoming - ', i+1);
getTransporter(shareMetadata);
}
console.log({transporterMap});
console.log({totalIndents: totalCountPerPeriod});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment