KatInterview

Access Log Health Report with Strict Line Validation

StringsAuto-graded · 25 test cases
EasyAuto-gradedStringsstringsparsingvalidation

Last updated

Validate access-log lines field by field, then count client errors, server errors, invalid lines and the failure percentage over the valid lines only.

Access Log Health Report

You are given a list of raw web-server log lines. Check each line, then build a summary report from the lines that pass.

Line format

<ip_address> <timestamp> <path> <status_code>

When is a line valid?

Apply the checks below to each line in order. A line is valid only if it passes all of them. Otherwise it is invalid. Never guess or repair missing fields.

  1. Field count. Split the line on runs of whitespace, exactly like Python's line.split() with no arguments. Leading and trailing whitespace is ignored, and any whitespace character (spaces, tabs, and so on) separates fields. The line must produce exactly four fields, in the order ip_address, timestamp, path, status_code.
  2. IP address. The ip_address field must be accepted by Python's ipaddress.ip_address(...), which covers IPv4 and IPv6. If it raises ValueError, the line is invalid.
  3. Timestamp. Take the timestamp field and replace every Z character with +00:00. The result must be accepted by Python's datetime.fromisoformat(...), which means it must not raise ValueError. This is the only timestamp rule. Do not apply any extra or stricter RFC 3339 rule. Anything fromisoformat accepts is valid, even a timestamp without a UTC offset. Anything it rejects is invalid, such as an impossible date, a lowercase z suffix, or a seconds value of 60.
  4. Path. The path field must start with the character /.
  5. Status code. The status_code field must be exactly 3 characters long, and str.isdigit() must be true for it. Its integer value, as converted by int(...), must lie between 100 and 599 inclusive. If the conversion fails, the line is invalid. A status with a numeric prefix followed by other characters (for example 200abc) is invalid because of the length and digit rules.

Metrics

Compute everything from valid lines only. Invalid lines never enter any count or denominator, except invalid_lines.

  • total_requests: number of valid lines.
  • client_errors: valid lines whose status is in 400 to 499.
  • server_errors: valid lines whose status is in 500 to 599.
  • failed_requests: client_errors + server_errors.
  • failure_percent: 100 * failed_requests / total_requests using ordinary true division, with no rounding. If total_requests is 0, it is the integer 0.
  • invalid_lines: number of lines that are not valid.

Every line is either valid or invalid, so total_requests + invalid_lines equals the number of input lines.

Output

Return a dictionary with exactly these keys: total_requests, client_errors, server_errors, failed_requests, failure_percent, invalid_lines.

Constraints

  • 0 <= len(entries) <= 10^5
  • Each line is a string of at most a few hundred characters.
  • Statuses 100 to 399 are valid and count as successful (neither client nor server error).
  • Boundary values 399, 400, 499, 500, 599 must be classified exactly as described above.
  • Valid lines may mix IPv4 and IPv6 addresses.
  • An empty input list gives all counters 0 and failure_percent 0.

Examples

Example 1

Input: build_log_report(['192.168.1.10 2024-01-01T12:00:00Z /home 200', '2001:db8::1 2024-01-01T12:00:01+00:00 /missing 404', '192.168.1.11 2024-01-01T12:00:02Z /error 500'])

Output: {'total_requests': 3, 'client_errors': 1, 'server_errors': 1, 'failed_requests': 2, 'failure_percent': 66.66666666666667, 'invalid_lines': 0}

Example 2

Input: build_log_report(['127.0.0.1 2023-03-12T09:30:00.123Z /ok 399', '::1 2023-03-12T09:30:00-05:00 /bad 400', '127.0.0.1 2023-03-12T09:30:00Z /gone 499', '127.0.0.1 2023-03-12T09:30:00Z /oops 500', '127.0.0.1 2023-03-12T09:30:00Z /fatal 599'])

Output: {'total_requests': 5, 'client_errors': 2, 'server_errors': 2, 'failed_requests': 4, 'failure_percent': 80, 'invalid_lines': 0}

Example 3

Input: build_log_report([])

Output: {'total_requests': 0, 'client_errors': 0, 'server_errors': 0, 'failed_requests': 0, 'failure_percent': 0, 'invalid_lines': 0}

Hints

Hint 1

Handle each line independently: call split() with no arguments, reject the line unless there are exactly four fields, and only then unpack them.

Hint 2

Let the standard library do the hard checks: ipaddress.ip_address for the IP, and datetime.fromisoformat on the timestamp after replacing every 'Z' with '+00:00'. Treat ValueError as 'invalid line' and add no extra timestamp rules.

Hint 3

Check the status as a string first (length 3 and isdigit()), then convert and check the 100 to 599 range. Only after every check passes should you count the line as valid. Compute the percentage at the end with a zero guard.