Skip to content
RED SAGSkills

Endpoint Report from Access Logs

A log shipper gives you raw access-log lines like 2024-05-01T10:00:00Z GET /api/users?page=2 200 123ms: timestamp, method, path, status and duration, separated by whitespace.

Skip a line (after trimming) unless it has exactly 5 whitespace-separated parts, the status is exactly three digits and the duration matches digits followed by ms. Strip any query string from the path (everything from the first ?). The endpoint key is method + " " + path.

For each endpoint return { endpoint, requests, errorRate, avgMs }: errorRate is the share of requests with status >= 500, rounded to 4 decimals, and avgMs the mean duration rounded to 1 decimal (Math.round(x * 10) / 10). Sort by requests descending, then endpoint ascending in plain string order.

Example 1

Input: lines = ["2024-05-01T10:00:00Z GET /api/users 200 120ms","2024-05-01T10:00:01Z GET /api/users?page=2 500 80ms","2024-05-01T10:00:02Z POST /api/orders 201 300ms","garbage line","2024-05-01T10:00:03Z GET /api/users 404 40ms"]
Output: [{"endpoint":"GET /api/users","requests":3,"errorRate":0.3333,"avgMs":80},{"endpoint":"POST /api/orders","requests":1,"errorRate":0,"avgMs":300}]
Explanation: Three valid GET /api/users lines (one is a 500); the garbage line is skipped.

Example 2

Input: lines = []
Output: []

Constraints

  • 0 <= lines.length <= 10^5
Hint 1

line.trim().split(/\s+/) gives the parts.

Hint 2

Accumulate counts and totals per key in a Map, round only at the end.

Good practice for: Site Reliability Engineer, DevOps Engineer, Cloud Engineer, Backend Developer, Data Engineer, Full Stack Developer

Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.