Summarize Server Healthcheck Logs
Problem statement
You are given server healthcheck logs, one record per line. A well-formed line has exactly three key=value fields separated by single spaces, in any order:
timestamp:1to9digits. Compare timestamps as numbers, so007and7are the same time.server:1to10characters, all lowercase letters or digits.status: exactlyPASSorFAIL.
Any other line is malformed. For example, a line is malformed if it has a missing, extra, or repeated key, a token without exactly one =, an empty value, a leading, trailing, or double space, or a value that breaks the rules above.
Two well-formed lines with the same server and the same timestamp report the same check. Keep only the first one; each later copy is a duplicate, even if its status differs.
Build a report from the kept checks:
- For each server, one line
"<server> <status> <failures>". Here<status>is the status of the server's newest check (largest timestamp), which tells operations whether the server is healthy now.<failures>is the number of its kept checks with statusFAIL, which an SLA report would count. Sort these lines by server name in lexicographic order. - Then the line
"DUPLICATE <d>", where<d>is the number of duplicate lines. - Then the line
"INVALID <m>", where<m>is the number of malformed lines.
Return the report lines in this order.
Function
summarizeHealthchecks(logs: String[]) → String[]Examples
Example 1
logs = ["timestamp=1 server=web1 status=PASS","timestamp=2 server=db1 status=FAIL","timestamp=3 server=web1 status=FAIL","timestamp=4 server=web1 status=PASS","timestamp=5 server=db1 status=PASS"]return = ["db1 PASS 1","web1 PASS 1","DUPLICATE 0","INVALID 0"]db1failed at time 2 and passed at time 5, so its newest status isPASSwith 1 failure.web1passed, failed at time 3, then passed at time 4: newest statusPASS, 1 failure.- No line is a duplicate or malformed.
Example 2
logs = ["timestamp=10 server=api status=FAIL","server=api status=PASS timestamp=8","timestamp=10 server=api status=FAIL","timestamp=12 server=api status=PASS","timestamp=10 server=api status=PASS"]return = ["api PASS 1","DUPLICATE 2","INVALID 0"]- The second line lists its fields in a different order, which is allowed.
- The third and fifth lines repeat time 10 for
api, so they are 2 duplicates and are ignored, even though the fifth one saysPASS. - The kept checks are times 10 (
FAIL), 8 and 12 (PASS). The newest is time 12, so the status isPASSwith 1 failure.
Example 3
logs = ["timestamp=5 server=gpu0 status=PASS","timestamp=6 server=gpu0","timestamp=x server=gpu0 status=PASS","timestamp=7 server=gpu0 status=OK","timestamp=8 server=GPU0 status=FAIL","timestamp=9 server=gpu0 status=FAIL"]return = ["gpu0 PASS 0","DUPLICATE 0","INVALID 5"]Only the first line is well formed. The others are malformed: one is missing status, one has a non-digit timestamp, one has status OK, one has an uppercase server name, and one has a double space. So gpu0 has newest status PASS with 0 failures, and 5 lines are invalid.
Constraints
0 <= logs.length <= 20000 <= logs[i].length <= 100- Each
logs[i]contains only letters, digits, spaces, and the characters=,-,_, and:.