Skip to content

Commit

Permalink
revised code and added readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MMMikeM committed Apr 12, 2021
1 parent 69f79b0 commit 81d2378
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
linetags
linetags
coverage
test/fixtures
Binary file added cli
Binary file not shown.
Binary file added contention_ratio_calculator
Binary file not shown.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contention Ratio CLI Tool

An internal tool to expedite a process whereby the core infrastructure team ensured contention ratios were within spec
55 changes: 40 additions & 15 deletions src/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@ const calculate = (linetagFolder, soNumber, city, carrier) => {

let files = fs.readdirSync(linetagFolder);
let filteredFiles = files.filter((file) => file[0] !== ".");
let data = filteredFiles.map((file) =>

let test = filteredFiles.map((file) =>
filterFiles(`${linetagFolder}/${file}`)
);

let xcData = filteredFiles.map((file) =>
extractData(`${linetagFolder}/${file}`)
);

let mainEntry = data.find(
let mainEntry = xcData.find(
(entry) =>
entry.circuitType === "xc" &&
entry.carrier === carrier &&
entry.city === city
);
let xcSpeedString = mainEntry.speed;

let xcSpeedString = mainEntry.speed || "";
let xcSpeed = unitHandler(xcSpeedString, mainEntry.fileName);

let data = files
.filter((file) => file[0] !== ".")
.filter((file) => fs.readFileSync(file).contains("svsbackhaul"));

soldSpeed = data
.filter((entry) => entry.circuitType !== "xc")
.filter((entry) => entry.circuitNumber.includes(soNumber))
Expand All @@ -46,37 +56,52 @@ const unitHandler = (input, fileName) => {
let [_, speed, unit] = speedRegex.exec(input);
let speedInt = parseInt(speed);
if (unit === "g") {
speedInt = speedInt * 1024;
speedInt = speedInt * 1000;
}
if (unit === "t") {
speedInt = speedInt * 1024 * 1024;
speedInt = speedInt * 1000 * 1000;
}
return speedInt;
}
};

const filterFiles = (filePath) => {
const file = fs.readFileSync(filePath, "utf8");
const lines = file.split("\n");
let fileName = path.basename(filePath);
console.log({
filePath: filePath,
file: lines.find((item) => item.match(/speed: (.*)/)),
});
};

const extractData = (filePath) => {
const file = fs.readFileSync(filePath, "utf8");
const lines = file.split("\n");
let fileName = path.basename(filePath);

let speed, carrier, city, circuitType, circuitNumber;
let error = null;

try {
[speed, error] = regexer(/speed: (.*)/, lines);
[carrier, error] = regexer(/circt_carrier: (.*)/, lines);
[city, error] = regexer(/pswitch_host:\ssvs-za-(.*)-\w+-\w+-\d+/, lines);
[circuitType, error] = regexer(/circt_type: (.*)/, lines);
[circuitNumber, error] = regexer(/circt_num_l1: (.*)/, lines);
[speed, error] = regexer(/speed: (.*)/, lines, fileName);
[carrier, error] = regexer(/circt_carrier: (.*)/, lines, fileName);
[city, error] = regexer(
/pswitch_host:\ssvs-za-(.*)-\w+-\w+-\d+/,
lines,
fileName
);
[circuitType, error] = regexer(/circt_type: (.*)/, lines, fileName);
[circuitNumber, error] = regexer(/circt_num_l1: (.*)/, lines, fileName);
if (error !== null) {
// throw new Error(`Regex failed on file: ${filePath} - ${error}`);
console.log(error);
throw new Error(`Regex failed on file: ${filePath} - ${error}`);
// console.log(error);
}
} catch (error) {
console.log(error);
} finally {
return {
fileName: path.basename(filePath),
fileName: fileName,
speed: speed,
circuitType: circuitType,
circuitNumber: circuitNumber,
Expand All @@ -86,14 +111,14 @@ const extractData = (filePath) => {
}
};

const regexer = (pattern, lines) => {
const regexer = (pattern, lines, filePath) => {
let matchDirty = lines.find((line) => line.match(pattern));
let match = "";
if (matchDirty) {
match = pattern.exec(matchDirty);
return [match[1], null];
} else {
return ["", `No match found - ${pattern}`];
return ["", `No match found - ${pattern} in ${filePath}`];
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/calculator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("contention ratio calculator", () => {
}),
test("When given a bandwidth speed, it returns an integer of the speed in mbps", () => {
const input = "123tbps";
const output = 128974848;
const output = 123000000;
const res = unitHandler(input);
expect(res).toBe(output);
}),
Expand Down

0 comments on commit 81d2378

Please sign in to comment.