Skip to main content

Rubrik API

const appNames = ["vmware", "kafka", "zookeeper"];
const dataCenters = [
{ name: "london", url: "ldn.api.com" },
{ name: "hongkong", url: "hk,api.com" },
{ name: "bangalore", url: "blr.api.com" },
];

const getRandomNumber = (min, max) => {
return min + Math.floor(Math.random() * (max - min));
};

const db = [];
const testData = {};
/**
* Create db and test data
*/
for (const { name, url } of dataCenters) {
const entry = {
name,
url,
};
let [totalProtectedCount, totalUnprotectedCount] = [0, 0];
for (const appName of appNames) {
const protectedCount = getRandomNumber(100, 300);
const unProtectedCount = getRandomNumber(100, 300);
entry[appName] = {
protectedCount,
unProtectedCount,
};
totalProtectedCount += protectedCount;
totalUnprotectedCount += unProtectedCount;
}
testData[name] = {
totalProtectedCount,
totalUnprotectedCount,
};
db.push(entry);
}

/**
* Simulate api call
*/
const apiCall = (url, appName) => {
return new Promise((resolve) => {
const data = db.filter((entry) => entry.url === url)[0][appName];
setTimeout(() => {
resolve(data);
}, getRandomNumber(1000, 3000));
});
};

/**
* Test
*/
const test = (testData, res) => {
for (const { name } of dataCenters) {
console.log(
`Passing test for ${name} [protected]: ${
testData[name].totalProtectedCount ===
res[name].totalProtectedCount
}`
);
console.log(
`Passing test for ${name} [un-protected]: ${
testData[name].totalUnprotectedCount ===
res[name].totalUnprotectedCount
}`
);
}
};

const promises = [];
for (const { name, url } of dataCenters) {
for (const appName of appNames) {
const promise = apiCall(url, appName).then((data) => ({
name,
data,
}));
promises.push(promise);
}
}

const res = {};
Promise.all(promises).then((values) => {
values.forEach(({ name: dataCenterName, data }) => {
if (!res.hasOwnProperty(dataCenterName)) {
res[dataCenterName] = {
totalProtectedCount: 0,
totalUnprotectedCount: 0,
};
}

res[dataCenterName].totalProtectedCount += data.protectedCount;
res[dataCenterName].totalUnprotectedCount += data.unProtectedCount;
});
test(testData, res);
});