File size: 2,380 Bytes
b199558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Access log report

`logs/access.log` is a web server access log in a combined-ish format:

```
<ip> - - [<timestamp>] "<METHOD> <path> HTTP/1.1" <status> <bytes> "<referer>" "<user-agent>" <duration_ms>
```

Some lines are damaged. The user-agent field may contain escaped double quotes
(`\"`), so the line cannot be parsed by splitting on `"` -- quoted fields have to
be scanned properly. `bytes` is `-` when the server did not record a size.

Write **`tools/report.sh`** in the workspace root. Running

```
bash tools/report.sh
```

from the workspace root must read `logs/access.log` and (re)generate the
following under `out/`, exit 0, and print nothing that matters. It must be
idempotent: deleting `out/` and running it again must produce byte-identical
files.

## `out/report.json`

A JSON object with exactly these keys:

| key | type | meaning |
| --- | --- | --- |
| `total_requests` | int | number of **valid** lines |
| `unique_paths` | int | number of distinct normalised paths |
| `status_counts` | object | `{"<status>": <count>}`, keys are strings; only statuses that occur |
| `top_paths` | array | up to 3 objects `{"path": str, "count": int}`, highest count first, ties broken by path ascending |
| `bytes_total` | int | sum of `bytes` over valid lines, `-` counts as 0 |
| `p95_ms` | int | 95th percentile of `duration_ms`, nearest-rank method |

`p95_ms` is the nearest-rank percentile: sort the durations ascending, take
element number `ceil(0.95 * n)` counting from 1. With `n = 36` that is the 35th
smallest duration. If there are no valid lines, `p95_ms` is `0`.

Paths are normalised by dropping everything from the first `?` onwards, so
`/api/users?id=42` counts as `/api/users`.

## `out/malformed.txt`

The 1-based line numbers of the lines that do not match the grammar above, one
per line, ascending. A line is malformed if any of these hold: it is not the
nine-part shape above, `status` is not exactly three digits, `bytes` is neither
`-` nor digits, or the trailing `duration_ms` is missing or not digits.
Malformed lines are excluded from every aggregate.

## `out/access.log.gz`

A gzip stream whose decompressed content is byte-for-byte identical to
`logs/access.log`.

## Notes

- Only `logs/access.log` is input; do not edit it.
- `python` is on `PATH` (`python3` is also available). Standard library only --
  there is no network access.