Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Last active February 25, 2022 15:49
Show Gist options
  • Save vvgomes/99723de576ba0f2dc1ee407b4d5b08b9 to your computer and use it in GitHub Desktop.
Save vvgomes/99723de576ba0f2dc1ee407b4d5b08b9 to your computer and use it in GitHub Desktop.
// definitions
const toLines = logs => logs.split("\n");
const notEmpty = line => line.trim();
const toParts = line => line.split(" ");
const toLogEntry = parts => ({
path: parts[parts.length - 2],
status: parseInt(parts[parts.length - 1])
});
const isError = entry => entry.status >= 400;
const incErrorCount = (errors, entry) =>
Object.assign(errors, { [entry.path]: (errors[entry.path] || 0) + 1 });
const collectErrors = (logs, errorCounts) =>
toLines(logs)
.filter(notEmpty)
.map(toParts)
.map(toLogEntry)
.filter(isError)
.reduce(incErrorCount, errorCounts);
// usage
let = errorCounts = {};
const getErrorCount = (path) => errorCounts[path] || 0;
const sample = `
8:00:00 192.1.1.2 GET /index 200
8:00:01 192.1.1.1 GET /index 201
8:00:01 192.1.1.2 GET /msgs/ 401
8:00:01 192.1.1.2 GET /msgs/ 404
8:00:02 127.1.1.1 GET /index 200
8:00:03 127.1.1.3 POST /msgs/send 200
8:00:03 127.1.1.2 POST /msgs/send 500
8:00:03 127.1.1.2 POST /msgs/send 501
8:00:03 127.1.1.2 POST /msgs/send 502
8:00:03 127.1.1.2 POST /auth/login 500
8:00:03 127.1.1.2 POST /auth/login 504
8:00:03 127.1.1.2 GET /msgs/receive 403
`
const sample2 = `
8:00:01 192.1.1.2 GET /msgs/ 401
`
errorCounts = collectErrors(sample, errorCounts);
errorCounts = collectErrors(sample2, errorCounts);
console.log(getErrorCount("/msgs/")); // expected: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment