SimpleJobAnalyzer
A Very Basic Job Analyzer.
Reads job execution records from a JSON file, validates them and prints a summary.
Functional Requirements
- Read records from a JSON file.
- Validate the input and handle malformed records gracefully.
- Calculate execution duration for completed jobs.
- Produce a summary containing:
- total jobs
- successful jobs
- failed jobs
- average successful-job duration
- jobs that required retries
How to run
Needs Python 3.11+ and uv.
uv sync # install dependencies
uv run simplejobanalyzer examples/jobs.json # text summary
uv run simplejobanalyzer examples/jobs.json --out-format json
uv run pytest -q # run the tests
Options:
--in-format -> input format. Only json_array for now.
--out-format -> text (default) or json.
Exit codes:
0 -> summary printed.
1 -> the file could not be read (missing file, not a JSON array, broken JSON).
Sample data:
Assumptions
- A record is one finished attempt of a job.
- Status is either
success or failed. Case does not matter (SUCCESS is fine).
- Both
started_at and finished_at are required, and finished_at cannot be before started_at.
- Anything else (
queued, running, unknown statuses) is invalid. See Extensibility.
- A retry keeps the same
job_id and increases attempt.
- A job's final state is its latest attempt.
- If the same attempt shows up twice as both
success and failed, success wins. This way the result does not depend on the order records arrive in.
- Side effect: if a job's newest attempt is still
running, that record is invalid. The job is judged on its last finished attempt.
- Timestamps are ISO 8601. Timestamps without a time zone are treated as UTC.
- Summary:
- total jobs -> number of unique
job_ids.
- successful jobs -> unique
job_ids whose final state is success.
- failed jobs -> unique
job_ids whose final state is failed.
- Every job is counted once, so successful + failed = total.
- average successful-job duration -> sum of the durations of the successful attempts / number of successful jobs.
- Only the attempt that succeeded counts. Time spent on earlier failed attempts is not included.
- If there are no successful jobs, it is
n/a (null in JSON).
- jobs that required retries ->
job_ids whose latest attempt is greater than 1.
- Text output shows the count and the first 10 ids. JSON output lists all of them.
- Invalid records:
- A bad record (missing field, wrong type, bad timestamp, unknown status, not an object) is skipped and counted. It never stops the run.
- A bad file (missing, empty, not a JSON array, broken JSON) stops the run with exit code 1.
- An empty array
[] is fine. It gives an all zero summary.
Design
JSON file -> Source -> raw records -> JobRecord (validation) -> JobAnalyzer (one entry per job_id) -> Summary -> CLI output
sources/ -> where records come from.
DataSource is the base class. It only has read(), which yields raw records one at a time.
JsonArraySource reads a JSON array with ijson. It parses by JSON structure, not by lines, so pretty printed and minified files both work.
- The source does not know what a job is. It just reads.
models.py -> what a valid record is.
JobRecord.from_raw() turns a raw dict into a JobRecord or raises InvalidRecord.
- Every field has its own validator. One more check across fields:
finished_at >= started_at.
analyzer.py -> the counting.
- Keeps a dictionary:
job_id -> (attempt, status, duration).
- A newer attempt overwrites the entry. An older one is ignored. So every job has exactly one entry.
- The summary is calculated once at the end, from that dictionary.
cli.py -> arguments and output.
--in-format and --out-format are just dictionaries of sources and formatters. No if/else chains.
Why this split:
- Each layer only does one thing, so each can change without touching the others.
- A new source, a new output format or a new rule touches one file.
Performance (not measured)
This has not been properly profiled. The points below are what we expect, not measured results.
- Memory should stay low while reading, because
ijson streams one record at a time.
- Memory should grow with the number of unique jobs, not with the file size.
- Streaming is probably a bit slower than loading the whole file with
json.load, but should use far less memory.
- To confirm any of this, the next step would be profiling with scalene on the generated files.
Known limitations
- Memory grows with the number of unique jobs, not with the file size.
- Reading is streamed, but the analyzer keeps one small entry per
job_id.
- Worst case, every record is a different job. Then the dictionary has as many entries as records.
- See Scaling for how to handle that.
- We assume
ijson uses its C backend (yajl2_c). Its pure python fallback is expected to be much slower. You can check with python -c "import ijson; print(ijson.backend)".
- Only JSON arrays are supported as input. Not JSON Lines.
Out of Scope
- Fine tuning when to use full load vs lazy load.
- Right now we always stream with
ijson.
- Small files could be loaded fully with
json.load. We expect that to be faster for small files, but it holds the whole file in memory.
- Reading from live, append only logs.
- Like Kafka -> reading from append only logs and tracking offsets.
- Would need JSON Lines, saving the byte offset after processing, and handling half written lines and log rotation.
- Reasons for invalid records. Right now we only count them.
- Parallel processing (see Scaling).
Scaling
Streaming vs full load.
- We stream files of all sizes. We assume that is a trade-off, not free.
- Streaming should use much less memory than
json.load, but is probably a bit slower.
- We could pick a size threshold: small files ->
json.load, big files -> stream. The right threshold would come from profiling.
Reading in parallel.
- Reading a local file is IO, but asyncio or thread pools do not really help with local files. Async makes sense if the data came over the network (db, http, S3 etc).
- Validation and counting are CPU bound. So async does not help here either. Neither do thread pools, because the GIL only lets one thread run python code at a time.
- With multiprocessing, we could split the records into batches and process them in parallel:
- The attempts of one job can land in different batches. So a batch cannot calculate the final counts on its own.
- What a batch can do is:
- de-duplicate -> a job_id will have exactly one entry in the batch.
- validate -> all invalid records get counted.
- Then we write the batch results into N partition files using hash partitioning:
zlib.crc32(job_id) % N.
- We use crc32 and not python's
hash(), because hash() for strings is randomised per process. The same job would go to different files.
- So the same job_id always goes to the same file.
- Now each partition file is small enough to load into memory. Since all attempts of a job are in the same file, we can find each job's final state and turn it into counts.
- Partitions have different jobs, so the counts from each partition just add up.
- In short:
- Phase 1: Split into batches -> each batch de-duplicates and validates -> write into partition files by
crc32(job_id) % N.
- Phase 2: Read each partition -> count -> add up the counts.
- How many partitions?
- Ideally
N = ceil(unique_jobs * bytes per job / memory budget).
- The problem: we don't know the number of unique jobs in a file, and we don't know the bytes per job either.
- Bytes per job is a property of our code, not of the input. So we can measure it once, ahead of time (build a dictionary of fake entries and measure it with
tracemalloc).
- For unique jobs, we can use an upper bound, an estimate, or not decide up front at all:
- Upper bound -> we do know the file size. Every record takes at least some minimum number of bytes, so
unique jobs <= records <= file size / smallest record size. This over-estimates N, which is safe. Too many partitions is fine, too few is not.
- Sample -> read the first few thousand records, get the average record size and the ratio of unique job_ids, and scale that up to the file size. Cheap, but a sample can mislead (for example if retries only show up later in the file), so add a safety margin.
- HyperLogLog -> estimates the number of distinct job_ids in one extra pass, using only a few KB of memory.
- Don't decide up front -> pick a generous N (like 64). Extra partitions are cheap, just more files. If a partition still turns out too big in phase 2, split only that file again with a different hash (like
crc32(job_id + "salt") % M).
- I would go with the last one. It works without knowing anything about the data in advance.
After that, we need to find the right batch size, number of workers and max batches running at a time.
- We can do this by profiling with scalene.
- Note: more workers only help as much as the part that runs in parallel. Parsing JSON still happens in one reader (Amdahl's Law).
Extensibility
- What happens if new states like
queued, running come in?
- Then
success and failed are not the only valid states, and we cannot just overwrite by attempt anymore.
queued and running won't have finished_at, so there is no duration for them.
- We would need a status rank:
queued < running < failed / success. The same job can go queued -> running -> success/failed within the same attempt, and we can get them in any order. So we compare (attempt, status rank) instead of just attempt.
- The model validator would need to allow
finished_at to be None for running, and both timestamps to be None for queued.
- The summary would get an "in progress" count.
- New input format (JSON Lines, CSV, S3).
- Add a new
DataSource subclass with a read() method.
- Add it to
SOURCES in cli.py. That's it.
- New output format.
- Add a function to
FORMATTERS in cli.py.
- New metric (like max duration).
- Add a field to
Summary and calculate it in JobAnalyzer.summary().
Testing
uv run pytest -q
test_json_array.py -> reading files, pretty and minified, lazy reading, broken files.
test_models.py -> validation of every field and the reasons for rejecting a record.
test_analyzer.py -> de-duplication, retries, order not mattering, the summary.
test_cli.py -> arguments, text and JSON output, exit codes.
The code also passes mypy --strict:
uv run --with mypy mypy --strict src tests
Time spent
- The core (source, model, analyzer, CLI, tests) took around ~2 hours.
- I spent extra time on purpose thinking about large files: streaming and the two phase design. That is beyond the brief.