Magrep
Python
FastAPI
Oracle
Docker

MagRep generates witness reports for Nb₃Sn superconducting cable at CERN, documenting critical current (Ic), residual resistivity ratio (RRR) and heat treatment characterisation for accelerator magnet programmes. The original was a ~72,000-line C++/Qt desktop application.
When I inherited it, the application couldn't be distributed at all: there was no installer, and running it required every dependency to already be in exactly the right place on the machine. The RRR section of the report was also largely unimplemented. I built Windows and macOS installers so it could reach end users, and completed the RRR reporting to parity with the rest of the report.
Even once packaged, the structural problems remained. Business logic, data access, physics and LaTeX generation were all compiled into one binary, so any change meant a full rebuild and redistribution. More seriously, users had no visibility into the data feeding a report, and the class-based selection logic occasionally picked up the wrong sample or measurement.
Those are fundamentally problems of data selection and joining, so rather than porting the same class hierarchy to another language, the reimplementation moves that logic into SQL, where every join and filter is visible and can be checked independently.
I first built a Python proof of concept to validate the approach: an ETL pipeline extracting Oracle data into SQLite with Pydantic validation at every stage, a reproduction of the scaling-law analysis, and PDF output matching the C++ reports. It also added a key feature the original lacked: letting users inspect and edit the underlying data before a report is generated.
The production system has three independent layers:
Unusually, Oracle drives generation rather than the frontend calling the service:
Python performs only rendering and placeholder substitution, so a data or logic change never requires touching the service.
Rather than hardcoded report classes, each report is a JSON specification: a tree of typed blocks (title_page, section, text, table, figure, repeater), each declaring the Oracle function that supplies its data. {{token}} placeholders are substituted from request metadata at runtime, and repeater blocks render their children once per item in a list, for example one table per magnet aperture type.
Every renderer is implemented once for LaTeX and once for HTML, so a single spec produces both a PDF and a web report. Adding a new report type needs only a new spec and its Oracle functions, with no Python changes.
An /analyse endpoint statically validates a spec without generating anything, listing every Oracle function referenced and every token, and whether each is satisfied. This lets a database engineer check a spec's wiring before a full generation.
A core part of the analysis is fitting the Summers scaling law to critical current measurements:
Given measured triplets, a dedicated fitting service uses Levenberg-Marquardt least squares (scipy.optimize.least_squares) to extract the material parameters and , and corrects between 1.9 K and 4.3 K using the fitted law. It also finds where a magnet's load line intersects the fitted critical surface, using Brent root-finding, which is a key acceptance criterion for accelerator magnets. PL/SQL calls the fitting service over HTTP, so numerical results arrive in the same call as the rest of the business logic.
Material constants are looked up by fabrication type and strand diameter, with a designated default as a fallback.
Every computed value (band fit parameters, degradation percentages, RRR pairings) was cross-checked against the C++ application's output for known heat treatment orders. Agreement is typically within a few percent, and each residual difference was traced to a specific, documented methodological difference rather than an implementation bug.
The microservice runs as a container (python:3.12-slim, TeX Live, Oracle Instant Client) on CERN OKD, behind a TLS-terminated route with readiness and liveness probes. The image also builds a MkDocs user guide, served from the running service.
The biggest lesson was that the hardest bugs weren't in the physics or the rendering; they were in which data got selected. Moving that logic somewhere it could be inspected did more for correctness than any amount of careful porting would have. Next on my list would be an automated regression suite comparing Oracle output and rendered reports against recorded references, plus a CI/CD pipeline for the service.