Update on entire docs:

- Added landing page for REST API and handled route package structure
- Alphabetically ordered form inputs and fixed documentation on REST API
- Added new logo in documentation UI and added section for REST API in documentation
This commit is contained in:
lrdossan 2024-11-27 15:36:11 +01:00
parent 50edc1adb7
commit 9f3a406d46
14 changed files with 331 additions and 235 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View file

@ -34,7 +34,7 @@ The newly implemented REST API allows external applications to interact with CAi
### API Structure
The API is served by the **Tornado** web framework, which provides asynchronous handling for client requests. The API serves requests on a specific port (by default, `8088`), and support standard HTTP methods, such as `POST` for data submission.
The API is served by the **Tornado** web framework, which provides asynchronous handling for client requests. The API serves requests on a specific port (by default, `8081`), and support standard HTTP methods, such as `POST` for data submission.
#### Key Features
@ -56,7 +56,7 @@ To run the API, follow these steps from the root directory of the project:
```
python -m caimira.api.app
```
The web server will be accessible on port `8088`.
The web server will be accessible on port `8081`.
#### API Endpoints
@ -82,7 +82,7 @@ As the project is growing, more endpoints targeted to specific tasks will be dev
{
"activity_type": "office",
"calculator_version": "NA",
"calculator_version": "N/A",
"event_month": "January",
"exposed_finish": "18:00",
"exposed_lunch_finish": "13:30",
@ -118,11 +118,11 @@ As the project is growing, more endpoints targeted to specific tasks will be dev
??? note "Example cURL (with the above body)"
curl -X POST "http://localhost:8088/virus_report" \
curl -X POST "http://localhost:8081/virus_report" \
-H "Content-Type: application/json" \
-d '{
"activity_type": "office",
"calculator_version": "NA",
"calculator_version": "N/A",
"event_month": "January",
"exposed_finish": "18:00",
"exposed_lunch_finish": "13:30",
@ -165,7 +165,6 @@ As the project is growing, more endpoints targeted to specific tasks will be dev
{
"CO2_data": "{\"times\":[8,8,8.1,8.1,8.1,8.2,8.2,8.2,8.3,8.3,8.3,8.4,8.4,8.4,8.5,8.5,8.5,8.6,8.6,8.6,8.7,8.7,8.7,8.8,8.8,8.8,8.9,8.9,8.9,9,9,9,9.1,9.1,9.1,9.2,9.2,9.2,9.3,9.3,9.3,9.4,9.4,9.4,9.5,9.5,9.5,9.6,9.6,9.6,9.7,9.7,9.7,9.8,9.8,9.8,9.9,9.9,9.9,10,10,10,10.1,10.1,10.1,10.2,10.2,10.2,10.3,10.3,10.3,10.4,10.4,10.4,10.5,10.5,10.5,10.6,10.6,10.6,10.7,10.7,10.7,10.8,10.8,10.8,10.9,10.9,10.9,11,11,11,11.1,11.1,11.1,11.2,11.2,11.2,11.3,11.3,11.3,11.4,11.4,11.4,11.5,11.5,11.5,11.6,11.6,11.6,11.7,11.7,11.7,11.8,11.8,11.8,11.9,11.9,11.9,12,12,12,12.1,12.1,12.1,12.2,12.2,12.2,12.3,12.3,12.3,12.4,12.4,12.4,12.5,12.5,12.5,12.6,12.6,12.6,12.7,12.7,12.7,12.8,12.8,12.8,12.9,12.9,12.9,13,13,13,13.1,13.1,13.1,13.2,13.2,13.2,13.3,13.3,13.3,13.4,13.4,13.4,13.5,13.5,13.5,13.6,13.6,13.6,13.7,13.7,13.7,13.8,13.8,13.8,13.9,13.9,13.9,14,14,14,14.1,14.1,14.1,14.2,14.2,14.2,14.3,14.3,14.3,14.4,14.4,14.4,14.5,14.5,14.5,14.6,14.6,14.6,14.7,14.7,14.7,14.8,14.8,14.8,14.9,14.9,14.9,15,15,15,15.1,15.1,15.1,15.2,15.2,15.2,15.3,15.3,15.3,15.4,15.4,15.4,15.5,15.5,15.5,15.6,15.6,15.6,15.7,15.7,15.7,15.8,15.8,15.8,15.9,15.9,15.9,16,16,16,16.1,16.1,16.1,16.2,16.2,16.2,16.3,16.3,16.3,16.4,16.4,16.4,16.5,16.5,16.5,16.6,16.6,16.6,16.7,16.7,16.7,16.8,16.8,16.8,16.9,16.9,16.9,17,17,17,17.1,17.1,17.1,17.2,17.2,17.2,17.3,17.3,17.3,17.4,17.4,17.4,17.5,17.5,17.5,17.6,17.6,17.6,17.7,17.7,17.7,17.8,17.8,17.8,17.9,17.9,17.9,18,18,18,18.1,18.1,18.1,18.2,18.2,18.2,18.3,18.3,18.3,18.4,18.4,18.4,18.5,18.5,18.5,18.6,18.6,18.6,18.7,18.7,18.7,18.8,18.8,18.8,18.9,18.9,18.9,19,19,19,19.1,19.1,19.1,19.2,19.2,19.2,19.3,19.3,19.3,19.4,19.4,19.4,19.5,19.5,19.5,19.6,19.6,19.6,19.7,19.7,19.7,19.8,19.8,19.8,19.9,19.9,19.9,20],\"CO2\":[445.2,443.3,440.9,443.4,442.4,444.1,445.2,445.7,448,448,444,442.5,439.3,438.2,441.4,441.2,443.8,445.2,446.5,445.3,452.1,458.8,470.8,478.1,488.3,502.1,522.1,545.5,579.9,616.2,641.2,676.3,701.9,720.5,746.9,765.8,779.1,794.2,810.6,826,838.3,854.4,876.4,886.2,898.4,921.7,942.8,953.8,979,990.3,1002.9,1017.4,1029.4,1041,1051.9,1067.2,1073.5,1079.7,1093.7,1104.8,1125.8,1141.1,1151,1160.1,1176.4,1193.7,1180.1,1015.3,864.7,802.7,774.5,728.3,697.3,676.1,657.6,640.6,606.5,595.9,577.8,553.6,530.2,525,523.2,521.5,512.9,505.3,502.1,502.5,505.2,507.5,509.2,511.3,513.8,520.4,529.1,532.8,530.1,524,521.6,519.1,510.3,510,514.3,518.4,524.6,521,519.4,523.3,527.5,528.3,526.4,527,530,534,535.6,533.5,530.6,522.3,524.2,532,539.1,538.8,526.2,517.5,508,493.7,485.6,479.5,471.6,472.2,468.2,463.1,461,459,456.4,458.6,459.2,463,465.6,468.4,475.2,480.3,489,528,579.6,606.6,611.2,617,635.9,651.1,676.6,696.6,714.6,729.9,744.7,766,788.5,812.1,832.8,854.7,883.9,895.6,910,924.4,944.5,956.8,971.4,981.3,993.6,1004.4,1021.6,1035.2,1043.8,1063.7,1071,1065.6,1065.9,1073.7,1086.4,1093.5,1120.1,1189.3,1202.9,1218.6,1238.5,1250.1,1263.5,1265,1270.1,1281.6,1294.9,1304.2,1315.5,1338.4,1351.5,1353.4,1364,1361.7,1343.3,1329.7,1320.4,1310.5,1313.6,1305.5,1313.4,1307.5,1290,1286.9,1289.3,1276.8,1268.9,1266.1,1264,1271.8,1268.5,1244.5,1206.4,1173.6,1145,1157.2,1194.4,1198.3,1196.1,1182.5,1167.9,1150.4,1132.8,1108.1,1097.4,1099.8,1093.4,1086.8,1086.9,1083.8,1075.5,1059.9,1048.4,1047.4,1042.6,1036.1,1026.9,1022.7,1017.6,1023.5,1021,1017.3,1004.6,908.3,906.5,979.2,955.8,928.9,915.3,914.1,930.1,923.3,921,865.9,860.2,867,869.7,871.4,861.5,862.9,850.4,843.9,839.7,838.1,839.8,849.7,841.6,820.8,825,838.5,853.8,855.6,838.7,818.1,811.7,804.3,794.5,790.6,782,788.6,779.6,804.2,836.9,852.4,856.9,858.1,857.9,856.5,856.7,851.7,849.6,849.2,846,846.8,844.8,842,839,836,833.2,832.6,830.9,825.9,823.6,823.7,818.2,812.4,810.2,808.4,806.7,803,800.2,794.3,790.5,790.4,787.7,783.3,780.5,784,780.9,780.8,777.2,775,768.7,763.6,761.5,757.8,760,762.1,761.6,761.2,761.5,757.5,754.5,752.6,752.1,751.7,748.6,744.3,742.1,737.6,731,732.6,726.8,726.2,726.9,727.1,726.8,728.9,729.9,726.5,724.8,723.9,723,721.1,720.2,721.1]}",
"activity_type": "office",
"exposed_lunch_finish": "13:30",
"exposed_lunch_start": "12:30",
"exposed_start": "08:30",
@ -184,11 +183,10 @@ As the project is growing, more endpoints targeted to specific tasks will be dev
??? "**Example cURL** (with the above body)"
curl -X POST "http://localhost:8088/co2_report" \
curl -X POST "http://localhost:8081/co2_report" \
-H "Content-Type: application/json" \
-d '{
"CO2_data": "{\"times\":[8,8,8.1,8.1,8.1,8.2,8.2,8.2,8.3,8.3,8.3,8.4,8.4,8.4,8.5,8.5,8.5,8.6,8.6,8.6,8.7,8.7,8.7,8.8,8.8,8.8,8.9,8.9,8.9,9,9,9,9.1,9.1,9.1,9.2,9.2,9.2,9.3,9.3,9.3,9.4,9.4,9.4,9.5,9.5,9.5,9.6,9.6,9.6,9.7,9.7,9.7,9.8,9.8,9.8,9.9,9.9,9.9,10,10,10,10.1,10.1,10.1,10.2,10.2,10.2,10.3,10.3,10.3,10.4,10.4,10.4,10.5,10.5,10.5,10.6,10.6,10.6,10.7,10.7,10.7,10.8,10.8,10.8,10.9,10.9,10.9,11,11,11,11.1,11.1,11.1,11.2,11.2,11.2,11.3,11.3,11.3,11.4,11.4,11.4,11.5,11.5,11.5,11.6,11.6,11.6,11.7,11.7,11.7,11.8,11.8,11.8,11.9,11.9,11.9,12,12,12,12.1,12.1,12.1,12.2,12.2,12.2,12.3,12.3,12.3,12.4,12.4,12.4,12.5,12.5,12.5,12.6,12.6,12.6,12.7,12.7,12.7,12.8,12.8,12.8,12.9,12.9,12.9,13,13,13,13.1,13.1,13.1,13.2,13.2,13.2,13.3,13.3,13.3,13.4,13.4,13.4,13.5,13.5,13.5,13.6,13.6,13.6,13.7,13.7,13.7,13.8,13.8,13.8,13.9,13.9,13.9,14,14,14,14.1,14.1,14.1,14.2,14.2,14.2,14.3,14.3,14.3,14.4,14.4,14.4,14.5,14.5,14.5,14.6,14.6,14.6,14.7,14.7,14.7,14.8,14.8,14.8,14.9,14.9,14.9,15,15,15,15.1,15.1,15.1,15.2,15.2,15.2,15.3,15.3,15.3,15.4,15.4,15.4,15.5,15.5,15.5,15.6,15.6,15.6,15.7,15.7,15.7,15.8,15.8,15.8,15.9,15.9,15.9,16,16,16,16.1,16.1,16.1,16.2,16.2,16.2,16.3,16.3,16.3,16.4,16.4,16.4,16.5,16.5,16.5,16.6,16.6,16.6,16.7,16.7,16.7,16.8,16.8,16.8,16.9,16.9,16.9,17,17,17,17.1,17.1,17.1,17.2,17.2,17.2,17.3,17.3,17.3,17.4,17.4,17.4,17.5,17.5,17.5,17.6,17.6,17.6,17.7,17.7,17.7,17.8,17.8,17.8,17.9,17.9,17.9,18,18,18,18.1,18.1,18.1,18.2,18.2,18.2,18.3,18.3,18.3,18.4,18.4,18.4,18.5,18.5,18.5,18.6,18.6,18.6,18.7,18.7,18.7,18.8,18.8,18.8,18.9,18.9,18.9,19,19,19,19.1,19.1,19.1,19.2,19.2,19.2,19.3,19.3,19.3,19.4,19.4,19.4,19.5,19.5,19.5,19.6,19.6,19.6,19.7,19.7,19.7,19.8,19.8,19.8,19.9,19.9,19.9,20],\"CO2\":[445.2,443.3,440.9,443.4,442.4,444.1,445.2,445.7,448,448,444,442.5,439.3,438.2,441.4,441.2,443.8,445.2,446.5,445.3,452.1,458.8,470.8,478.1,488.3,502.1,522.1,545.5,579.9,616.2,641.2,676.3,701.9,720.5,746.9,765.8,779.1,794.2,810.6,826,838.3,854.4,876.4,886.2,898.4,921.7,942.8,953.8,979,990.3,1002.9,1017.4,1029.4,1041,1051.9,1067.2,1073.5,1079.7,1093.7,1104.8,1125.8,1141.1,1151,1160.1,1176.4,1193.7,1180.1,1015.3,864.7,802.7,774.5,728.3,697.3,676.1,657.6,640.6,606.5,595.9,577.8,553.6,530.2,525,523.2,521.5,512.9,505.3,502.1,502.5,505.2,507.5,509.2,511.3,513.8,520.4,529.1,532.8,530.1,524,521.6,519.1,510.3,510,514.3,518.4,524.6,521,519.4,523.3,527.5,528.3,526.4,527,530,534,535.6,533.5,530.6,522.3,524.2,532,539.1,538.8,526.2,517.5,508,493.7,485.6,479.5,471.6,472.2,468.2,463.1,461,459,456.4,458.6,459.2,463,465.6,468.4,475.2,480.3,489,528,579.6,606.6,611.2,617,635.9,651.1,676.6,696.6,714.6,729.9,744.7,766,788.5,812.1,832.8,854.7,883.9,895.6,910,924.4,944.5,956.8,971.4,981.3,993.6,1004.4,1021.6,1035.2,1043.8,1063.7,1071,1065.6,1065.9,1073.7,1086.4,1093.5,1120.1,1189.3,1202.9,1218.6,1238.5,1250.1,1263.5,1265,1270.1,1281.6,1294.9,1304.2,1315.5,1338.4,1351.5,1353.4,1364,1361.7,1343.3,1329.7,1320.4,1310.5,1313.6,1305.5,1313.4,1307.5,1290,1286.9,1289.3,1276.8,1268.9,1266.1,1264,1271.8,1268.5,1244.5,1206.4,1173.6,1145,1157.2,1194.4,1198.3,1196.1,1182.5,1167.9,1150.4,1132.8,1108.1,1097.4,1099.8,1093.4,1086.8,1086.9,1083.8,1075.5,1059.9,1048.4,1047.4,1042.6,1036.1,1026.9,1022.7,1017.6,1023.5,1021,1017.3,1004.6,908.3,906.5,979.2,955.8,928.9,915.3,914.1,930.1,923.3,921,865.9,860.2,867,869.7,871.4,861.5,862.9,850.4,843.9,839.7,838.1,839.8,849.7,841.6,820.8,825,838.5,853.8,855.6,838.7,818.1,811.7,804.3,794.5,790.6,782,788.6,779.6,804.2,836.9,852.4,856.9,858.1,857.9,856.5,856.7,851.7,849.6,849.2,846,846.8,844.8,842,839,836,833.2,832.6,830.9,825.9,823.6,823.7,818.2,812.4,810.2,808.4,806.7,803,800.2,794.3,790.5,790.4,787.7,783.3,780.5,784,780.9,780.8,777.2,775,768.7,763.6,761.5,757.8,760,762.1,761.6,761.2,761.5,757.5,754.5,752.6,752.1,751.7,748.6,744.3,742.1,737.6,731,732.6,726.8,726.2,726.9,727.1,726.8,728.9,729.9,726.5,724.8,723.9,723,721.1,720.2,721.1]}",
"activity_type": "office",
"exposed_lunch_finish": "13:30",
"exposed_lunch_start": "12:30",
"exposed_start": "08:30",

View file

@ -5,7 +5,7 @@ This documentation provides comprehensive guidance on the CERN Airborne Model fo
It includes details on the diameter-dependent mathematical model and the code developed based on the underlying published research. It also covers documentation on the code itself, including explanations of the main module (`models.py`), and the recently added REST API feature, providing a through understanding of the CAiMIRA's functionality.
<figure markdown="span">
[![CAiMIRA logo](assets/caimira_logo.png){ width="100" }](https://caimira.web.cern.ch/)
[![CAiMIRA logo](assets/caimira_full_logo.png){ width="50%" }](https://caimira.web.cern.ch/)
</figure>
# Contents:

View file

@ -1,190 +1,226 @@
## Docker
This installation guide provides instructions for both quickly running CAiMIRA with the Calculator App using a pre-built Docker image, as well as instructions on how to set up a development environment for advanced use cases. It is therefore structured to accommodate a variety of users, from those seeking a simple, quick-start solution, to developers requiring a detailed setup for customization and development.
### Using the pre-built image
**Guide Overview**:
The easiest way to run a version of CAiMIRA Calculator is to use docker. A pre-built
image of CAiMIRA is made available at [https://gitlab.cern.ch/caimira/caimira/container_registry](https://gitlab.cern.ch/caimira/caimira/container_registry).
In order to run CAiMIRA locally with docker, run the following:
1. **[Quick Start](#quick-start)**: Designed for users aiming to run CAiMIRA with minimal setup. The Quick Guide section outlines the fastest approach to using CAiMIRA through a pre-built Docker image.
2. **[Development Installation](#development-installation)**: Intended for developers or those requiring advanced customization. This section includes detailed steps for setting up the environment, running the backend REST API, and contributing to the project.
## Quick Start
The easiest way to run a version of CAiMIRA with its Calculator App is to use Docker. A pre-built image of CAiMIRA is made available at [this](https://gitlab.cern.ch/caimira/caimira/container_registry) GitLab container registry.
### Prerequisites
Before proceeding, ensure the following prerequisites are met:
1. **Docker Installed**:
Install Docker on the system. Instructions for installation can be found at [Docker's official website](https://www.docker.com/products/docker-desktop/).
Verify the installation by opening a terminal and running:
docker --version
2. **Network Access**:
Ensure the machine has internet access to pull the CAiMIRA image from the container registry.
### Running the pre-built Image
In order to run CAiMIRA locally with Docker, run the following instruction in a terminal:
docker run -it -p 8080:8080 -e APP_NAME='calculator-app' gitlab-registry.cern.ch/caimira/caimira/calculator-app
This will start a local version of CAiMIRA, which can be visited at [http://localhost:8080/](http://localhost:8080/).
This will start a local version of CAiMIRA, which can be visited by loading this URL in your browser [http://localhost:8080/](http://localhost:8080/).
### Building the whole environment
## Development Installation
To build the whole environment for local development, from the root directory of the project, run:
This section provides comprehensive instructions for setting up the CAiMIRA environment in development mode. The guide is tailored for contributors and advanced users who want to build the project from source, customize its features, or contribute to its codebase<sup>1</sup>.
```
docker build -f app-config/api-app/Dockerfile -t api-app .
docker build -f app-config/calculator-app/Dockerfile -t calculator-app .
docker build ./app-config/auth-service -t auth-service
```
If you are using a computer with ARM CPU (Mac M1/2/3), then add the arg `--platform linux/arm64` to the docker build cmd.
If you need to debug the Docker build, add the args `--no-cache --progress=plain` to see a more verbose output in your terminal.
Get the client secret from the CERN Application portal for the `caimira-test` app. See [CERN-SSO-integration](deployment.md#cern-sso-integration) for more info.
```
read CLIENT_SECRET
```
Define some env vars (copy/paste):
```
export COOKIE_SECRET=$(openssl rand -hex 50)
export OIDC_SERVER=https://auth.cern.ch/auth
export OIDC_REALM=CERN
export CLIENT_ID=caimira-test
export CLIENT_SECRET=$CLIENT_SECRET
```
Run docker compose:
```
cd app-config
CURRENT_UID=$(id -u):$(id -g) docker compose up
```
Then visit [http://localhost:8080/](http://localhost:8080/).
## Development mode
CAiMIRA is mirrored to Github if you wish to collaborate on development and can be found at: [https://github.com/CERN/caimira](https://github.com/CERN/caimira)
### Folder structure
Developers can follow the steps below to build and run the CAiMIRA backend and frontend applications, set up APIs, run tests, and generate the documentation. Additionally, this section includes instructions for running legacy expert applications, profiling the system, and testing specific features.
The project contains two different Python packages:
- `caimira`: Contains all the backend logic and the calculator model. It is the package published in PyPI.
- `cern_caimira`: Imports and uses the backend package (`caimira`) and includes CERN-specific UI implementation.
- `caimira`: Contains the backend logic and the APIs logic. This package is published in [PyPI](https://pypi.org/project/caimira/).
- `cern_caimira`: Imports and uses the backend package (`caimira`) and includes the CAiMIRA-native UI features.
The folder layout follows best practices as described [here](https://ianhopkinson.org.uk/2022/02/understanding-setup-py-setup-cfg-and-pyproject-toml-in-python/).
### Installing and running CAiMIRA
!!! info
<sup>1</sup> The project's main repository is hosted on [GitLab](https://gitlab.cern.ch/caimira/caimira). Even though the repository is public, contributions to GitLab are restricted to users with a valid CERN SSO.
For external contributions and collaboration, a mirror is maintained on [GitHub](https://github.com/CERN/caimira).
Installing CAiMIRA in editable mode and running the Calculator App.
### Prerequisites
#### Installing
Before proceeding with the development installation of CAiMIRA, ensure that your system meets the following prerequisites:
In order to install the CAiMIRA's backend logic, create your own `virtualenv` and, from the root directory of the project, run:
1. **Python Environment**:
```
cd caimira
pip install -e .
```
- Python `3.9` or later.
- `pip`, `setuptools` must but installed and up-to-date. You can update them by opening a terminal and running:
In order to install the CERN-specific UI version, that links to the previously installed backend, activate your `virtualenv` and, from the root directory of the project, run:
python -m pip install --upgrade pip setuptools
```
cd cern_caimira
pip install -e .
```
!!! note
It's recommended to use `pyenv` or similar tools to manage Python versions. For details on how to install and use `pyenv`, see [here](https://github.com/pyenv/pyenv). While not mandatory, it is also recommended to create a virtual environment (`virtualenv`) to avoid conflicts between different package versions. For more information on setting up a virtual environment, see [here](https://virtualenv.pypa.io/en/latest/).
#### Running
2. **Git**:
This example describes how to run the calculator with the CERN-specific UI. In the root directory of the project:
- Ensure that Git is installed for cloning the repository. You can download it [here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). Verify installation by opening a terminal and running:
```
python -m cern_caimira.apps.calculator
```
git --version
To run with a specific template theme created:
3. **Docker**:
```
python -m cern_caimira.apps.calculator --theme=cern_caimira/src/cern_caimira/apps/templates/{theme}
```
- While not strictly required for local development, Docker is useful for creating isolated environments and running CAiMIRA in production-like settings. Further details can be found in the [Setting up full environment](#setting-up-the-full-environment) section.
To run the entire app in a different `APPLICATION_ROOT` path:
4. **Development Tools**:
```
python -m cern_caimira.apps.calculator --app_root=/myroot
```
- A code editor such as [VSCode](https://code.visualstudio.com/), or an IDE like [PyCharm](https://www.jetbrains.com/pycharm/).
To run the calculator on a different URL path:
5. **Repository Setup**:
```
python -m cern_caimira.apps.calculator --prefix=/mycalc
```
- Clone the CAiMIRA repository from the GitLab or GitHub mirror by opening a terminal and running:
git clone https://gitlab.cern.ch/caimira/caimira.git
cd caimira
!!! note
The directory in which you run the previous commands will be the root directory of your project.
### Installing and running
This section provides instructions for setting up, running, and testing CAiMIRA in a local development environment. It covers the installation of the backend logic, interaction with user interfaces, and utilization of the API app. Additionally, instructions for legacy applications (expert apps), testing procedures, and tools for profiling and documentation generation are provided.
##### Prerequisites:
After following what is described in the main [prerequisites](#prerequisites) section, ensure the following dependencies are correctly installed:
1. **CAiMIRA repository**: Can be cloned from the official GitLab repository [here](https://gitlab.cern.ch/caimira/caimira).
2. **Python `3.9` or later**: Required for running the CAiMIRA backend and UI.
#### CAiMIRA
CAiMIRA can be installed locally for development and usage in editable mode. This allows full access to both the backend logic, as well as the CAiMIRA-native UI. The following sections provide step-by-step instructions for installing and running CAiMIRA on a local environment.
##### Installing:
In order to install the CAiMIRA's backend logic, from the root directory of the project, run:
cd caimira
pip install -e .
In order to install the CAiMIRA-native UI version, that links to the previously installed backend, from the root directory of the project, run:
cd cern_caimira
pip install -e .
##### Running:
The calculator can be started with the CAiMIRA-native UI using the following command in the project root directory:
python -m cern_caimira.apps.calculator
Additional options allow customization, such as specifying themes, application roots, or URL paths. For example:
- **Run with a specific template theme**:
Within a given UI framework, you can choose different "themes" changing some frontend logic, style and text. To run with a specific template theme created:
python -m cern_caimira.apps.calculator --theme=cern_caimira/src/cern_caimira/apps/templates/{theme}
- **Run the app with a different `APPLICATION_ROOT` path**:
The base path of the web application on the server. It determines where the app is "rooted" in relation to the server's URL structure. To run with a specific application root:
python -m cern_caimira.apps.calculator --app_root=/myroot
- **Run the Calculator App on a different `CAIMIRA_CALCULATOR_PREFIX` URL path**:
The base path of the CAiMIRA calculator. It determines the URL path within the Calculator App itself. To run with a specific prefix:
python -m cern_caimira.apps.calculator --prefix=/mycalc
Each of these commands will start a local version of CAiMIRA, which can be visited at [http://localhost:8080/](http://localhost:8080/).
#### REST API
##### Expert-Apps:
To use the REST API, from the root directory of the project:
The CAiMIRA Expert App and CO2 App are legacy tools designed to provide dynamic interaction with the CAiMIRA model parameters.
1. Run the backend API:
!!! warning
```
python -m caimira.api.app
```
The `ExpertApplication` and `CO2Application` are no longer actively maintained but will remain in the codebase for legacy purposes.
Please note that the functionality of these applications might be compromised due to deprecation issues.
2. The Tornado server will run on port `8081`.
To test the API functionality, you can send a `POST` request to `http://localhost:8081/virus_report` with the required inputs in the request body. For an example of the required inputs, see [the baseline raw form data](https://gitlab.cern.ch/caimira/caimira/blob/master/caimira/src/caimira/calculator/validators/virus/virus_validator.py#L565).
The response format will be:
```json
{
"status": "success",
"message": "Results generated successfully",
"report_data": {
...
},
...
}
```
For further details please refer to the [REST API documentation page](../code/rest_api.md).
#### Running the Expert-Apps
The CAiMIRA Expert App and the CO2 App are tools to dynamically interact with various parameters of the CAiMIRA model.
##### Disclaimer
The `ExpertApplication` and `CO2Application` are no longer actively maintained but will remain in the codebase for legacy purposes.
Please note that the functionality of these applications might be compromised due to deprecation issues.
##### Running the Applications
###### Running the Applications
These applications only work within Jupyter notebooks. Attempting to run them outside of a Jupyter environment may result in errors or degraded functionality.
Make sure you have the needed dependencies installed:
1. **Install dependencies**:
```
pip install notebook jupyterlab
```
Install Jupyter Notebook and JupyterLab to use the applications:
Running with Visual Studio Code (VSCode):
pip install notebook jupyterlab
1. Ensure you have the following extensions installed in VSCode: `Jupyter` and `Python`.
2. **Run with Visual Studio Code**:
2. Open VSCode and navigate to the directory containing the notebook.
- Ensure you have the following extensions installed in VSCode: `Jupyter` and `Python`.
3. Open the notebook (e.g. `caimira/apps/expert/caimira.ipynb`) and run the cells by clicking the `run` button next to each cell.
- Open VSCode and navigate to the directory containing the notebook.
- Open the notebook (e.g. `caimira/apps/expert/caimira.ipynb`) and run the cells by clicking the `run` button next to each cell.
### Installing and running tests
##### API app
The project contains test files that separately test the functionality of the `caimira` backend and `cern_caimira` UI.
CAiMIRA includes a REST API for programmatic interaction with its models. The following steps describe how to run and test the API locally.
To test the `caimira` package, from the root repository of the project:
1. **Run the backend API**:
python -m caimira.api.app
```
cd caimira
pip install -e .[test]
python -m pytest
```
The Tornado server will be accessible on port `8081`.
To test the `cern_caimira` package, from the root repository of the project:
2. **Test the API**:
```
cd cern_caimira
pip install -e .[test]
python -m pytest
```
Send a `POST` request to `http://localhost:8081/virus_report` with the required inputs in the body. Example request formats can be found in the [Virus Validator Code](https://gitlab.cern.ch/caimira/caimira/blob/master/caimira/src/caimira/calculator/validators/virus/virus_validator.py#L565).
### Running the profiler
3. **Example Response**:
A successful response will return data in the following format:
!!! success
{
"status": "success",
"message": "Results generated successfully",
"report_data": {
...
},
...
}
For further details please refer to the [REST API documentation page](../code/rest_api.md).
#### Tests
CAiMIRA includes a suite of tests to validate the functionality of both its backend (`caimira` package) and UI components (`cern_caimira` package).
1. **Testing the backend (`caimira` package)**:
Install test dependencies and run the tests. From the root directory run:
cd caimira
pip install -e .[test]
python -m pytest
2. **Testing the UI (`cern_caimira` package)**:
Install test dependencies and run the tests. From the root directory run:
cd cern_caimira
pip install -e .[test]
python -m pytest
#### Profiler
CAiMIRA includes a profiler designed to identify performance bottlenecks. The profiler is enabled when the environment variable `CAIMIRA_PROFILER_ENABLED` is set to 1.
@ -194,25 +230,25 @@ Keep the profiler page open. Then, in another window, navigate to any page in CA
The sessions are stored in a local file in the `/tmp` folder. To share it across multiple web nodes, a shared storage should be added to all web nodes. The folder can be customized via the environment variable `CAIMIRA_PROFILER_CACHE_DIR`.
### Compiling and viewing the docs
#### Docs
To compile and view CAiMIRA's documentation, follow these steps:
CAiMIRA includes comprehensive documentation, which can be compiled and viewed locally:
1. Install CAiMIRA with Documentation Dependencies
1. **Install CAiMIRA with documentation dependencies**:
First, ensure CAiMIRA is installed along with the `doc` dependencies:
cd caimira
pip install -e .[doc]
2. Generate Code Documentation in Markdown
2. **Generate code documentation in markdown**:
Use `sphinx` with `sphinx_markdown_builder` to generate the documentation in `Markdown` format:
cd docs/sphinx
sphinx-build -b markdown . _build/markdown
3. Customize and Organize Documentation
3. **Customize and organize documentation**:
Run the `style_docs.py` script to apply custom styles, move required files, and generate a UML diagram:
@ -220,7 +256,7 @@ To compile and view CAiMIRA's documentation, follow these steps:
&& mv sphinx/_build/markdown/index.md mkdocs/docs/code/models.md \
&& pyreverse -o png -p UML-CAiMIRA --output-directory mkdocs/docs ../src/caimira/calculator/models/models.py
4. Start the documentation server
4. **Start the documentation server**:
To view the documentation locally, use MkDocs to serve it:
@ -228,3 +264,55 @@ To compile and view CAiMIRA's documentation, follow these steps:
python -m mkdocs serve --dev-addr=0.0.0.0:8080
The documentation can now be accessed at [http://0.0.0.0:8080/](http://0.0.0.0:8080/).
### Setting up the full environment
This section outlines the steps to build and run the full CAiMIRA environment locally using Docker. It provides instructions for creating the necessary Docker images, configuring authentication, and running the application with Docker Compose.
#### Prerequisites
After following what is described in the main [prerequisites](#prerequisites) section, ensure the following tools are installed and properly configured:
1. **CAiMIRA repository**: Can be cloned from the official GitLab repository [here](https://gitlab.cern.ch/caimira/caimira).
2. **Docker**: Download from the [Docker's official website](https://www.docker.com/products/docker-desktop/).
3. **Docker Compose**: Instructions on [DockerDocks official page](https://docs.docker.com/compose/install/).
#### Build and run
To build and run the environment, the following steps can be performed from the root directory of the project:
1. **Build Docker Images**:
To build the full environment for local development, from the root directory of the project, run:
docker build -f app-config/api-app/Dockerfile -t api-app .
docker build -f app-config/calculator-app/Dockerfile -t calculator-app .
docker build ./app-config/auth-service -t auth-service
For systems with ARM CPUs (e.g. Mac M1/M2/M3), the argument `--platform linux/arm64` should be appended to each `docker build` command.
For more verbose output during the Docker build process, additional arguments `no-cache --progress=plain` can be include to each command.
2. **Obtain the Client Secret**:
The client secret for the `caimira-test` application must be retrieved from the CERN Application Portal. Further details can be found in the[CERN-SSO-integration](deployment.md#cern-sso-integration) documentation.
read CLIENT_SECRET
3. **Set Environment Variables**:
Required environment variables can be defined as follows (copy/paste):
export COOKIE_SECRET=$(openssl rand -hex 50)
export OIDC_SERVER=https://auth.cern.ch/auth
export OIDC_REALM=CERN
export CLIENT_ID=caimira-test
export CLIENT_SECRET=$CLIENT_SECRET
4. **Run the Application**:
Start the application using Docker Compose running in the root directory:
cd app-config
CURRENT_UID=$(id -u):$(id -g) docker compose up
Then visit [http://localhost:8080/](http://localhost:8080/).

View file

@ -31,7 +31,7 @@ markdown_extensions:
- pymdownx.arithmatex:
generic: true
- toc:
toc_depth: '1-3'
toc_depth: '1-4'
- admonition
- pymdownx.details
- attr_list

View file

@ -7,22 +7,17 @@ import tornado.ioloop
import tornado.web
import tornado.log
import logging
from caimira.api.routes.report_routes import VirusReportHandler, CO2ReportHandler
from caimira.api.routes.routes import routes
logging.basicConfig(format="%(message)s", level=logging.INFO)
class Application(tornado.web.Application):
def __init__(self, debug):
handlers = [
(r"/co2_report", CO2ReportHandler),
(r"/virus_report", VirusReportHandler),
]
settings = dict(
debug=debug,
)
super().__init__(handlers, **settings)
super().__init__(routes, **settings)
if __name__ == "__main__":

View file

@ -0,0 +1,13 @@
import tornado.web
class BaseRequestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
def write_error(self, status_code, **kwargs):
self.set_status(status_code)
self.write({"message": kwargs.get('exc_info')[1].__str__()})

View file

@ -0,0 +1,11 @@
from caimira.api.routes.base_handler import BaseRequestHandler
from caimira import __version__
class LandingPageHandler(BaseRequestHandler):
def get(self):
self.write("<h1>Welcome to the CAiMIRA REST API</h1>")
self.write(f"<h4>Code Version: {__version__}</h4>")
self.write("<h4>Official GitLab: <a href='https://gitlab.cern.ch/caimira/caimira' target='_blank'>https://gitlab.cern.ch/caimira/caimira</a></h4>")
self.write("<h4>Documentation: <a href='https://caimira.docs.cern.ch/' target='_blank'>https://caimira.docs.cern.ch/</a><h4>")

View file

@ -1,24 +1,12 @@
import json
import traceback
import tornado.web
import sys
from caimira.api.routes.base_handler import BaseRequestHandler
from caimira.api.controller.virus_report_controller import submit_virus_form
from caimira.api.controller.co2_report_controller import submit_CO2_form
class BaseReportHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
def write_error(self, status_code, **kwargs):
self.set_status(status_code)
self.write({"message": kwargs.get('exc_info')[1].__str__()})
class VirusReportHandler(BaseReportHandler):
class VirusReportHandler(BaseRequestHandler):
def post(self):
try:
form_data = json.loads(self.request.body)
@ -43,7 +31,7 @@ class VirusReportHandler(BaseReportHandler):
self.write_error(status_code=400, exc_info=sys.exc_info())
class CO2ReportHandler(BaseReportHandler):
class CO2ReportHandler(BaseRequestHandler):
def post(self):
try:
form_data = json.loads(self.request.body)

View file

@ -0,0 +1,8 @@
from caimira.api.routes.landing_routes import LandingPageHandler
from caimira.api.routes.report_routes import VirusReportHandler, CO2ReportHandler
routes = [
(r"/", LandingPageHandler),
(r"/co2_report", CO2ReportHandler),
(r"/virus_report", VirusReportHandler),
]

View file

@ -24,11 +24,12 @@ class CO2FormData(FormData):
fitting_ventilation_states: list
room_capacity: typing.Optional[int]
#: The default values for undefined fields. Note that the defaults here
#: and the defaults in the html form must not be contradictory.
# The default values for undefined fields. Note that the defaults here
# and the defaults in any html form must not be contradictory.
_DEFAULTS: typing.ClassVar[typing.Dict[str, typing.Any]] = {
'CO2_data': '{}',
'fitting_ventilation_states': '[]',
'dynamic_infected_occupancy': '[]',
'dynamic_exposed_occupancy': '[]',
'exposed_coffee_break_option': 'coffee_break_0',
'exposed_coffee_duration': 5,
'exposed_finish': '17:30',
@ -36,6 +37,7 @@ class CO2FormData(FormData):
'exposed_lunch_option': True,
'exposed_lunch_start': '12:30',
'exposed_start': '08:30',
'fitting_ventilation_states': '[]',
'infected_coffee_break_option': 'coffee_break_0',
'infected_coffee_duration': 5,
'infected_dont_have_breaks_with_exposed': False,
@ -44,14 +46,12 @@ class CO2FormData(FormData):
'infected_lunch_option': True,
'infected_lunch_start': '12:30',
'infected_people': 1,
'dynamic_infected_occupancy': '[]',
'infected_start': '08:30',
'occupancy_format': 'static',
'room_capacity': None,
'room_volume': NO_DEFAULT,
'specific_breaks': '{}',
'total_people': NO_DEFAULT,
'dynamic_exposed_occupancy': '[]',
'occupancy_format': 'static',
}
def __init__(self, **kwargs):

View file

@ -7,16 +7,18 @@ NO_DEFAULT = object()
#: The default values for undefined fields. Note that the defaults here
#: and the defaults in the html form must not be contradictory.
DEFAULTS = {
'CO2_fitting_result': '{}',
'activity_type': 'office',
'air_changes': 0.,
'air_supply': 0.,
'arve_sensors_option': False,
'specific_breaks': '{}',
'precise_activity': '{}',
'ascertainment_bias': 'confidence_low',
'calculator_version': NO_DEFAULT,
'ceiling_height': 0.,
'conditional_probability_viral_loads': False,
'CO2_fitting_result': '{}',
'dynamic_exposed_occupancy': '[]',
'dynamic_infected_occupancy': '[]',
'event_month': 'January',
'exposed_coffee_break_option': 'coffee_break_0',
'exposed_coffee_duration': 5,
'exposed_finish': '17:30',
@ -24,8 +26,10 @@ DEFAULTS = {
'exposed_lunch_option': True,
'exposed_lunch_start': '12:30',
'exposed_start': '08:30',
'event_month': 'January',
'exposure_option': 'p_deterministic_exposure',
'floor_area': 0.,
'geographic_cases': 0,
'geographic_population': 0,
'hepa_amount': 0.,
'hepa_option': False,
'humidity': '',
@ -37,45 +41,41 @@ DEFAULTS = {
'infected_lunch_option': True,
'infected_lunch_start': '12:30',
'infected_people': 1,
'dynamic_infected_occupancy': NO_DEFAULT,
'infected_start': '08:30',
'inside_temp': NO_DEFAULT,
'location_latitude': NO_DEFAULT,
'location_longitude': NO_DEFAULT,
'location_name': NO_DEFAULT,
'geographic_population': 0,
'geographic_cases': 0,
'ascertainment_bias': 'confidence_low',
'exposure_option': 'p_deterministic_exposure',
'mask_type': 'Type I',
'mask_wearing_option': 'mask_off',
'mechanical_ventilation_type': 'not-applicable',
'occupancy_format': 'static',
'opening_distance': 0.,
'precise_activity': '{}',
'room_heating_option': False,
'room_number': NO_DEFAULT,
'room_volume': 0.,
'sensor_in_use': '',
'short_range_interactions': '[]',
'short_range_occupants': 0,
'short_range_option': 'short_range_no',
'simulation_name': NO_DEFAULT,
'specific_breaks': '{}',
'total_people': NO_DEFAULT,
'dynamic_exposed_occupancy': NO_DEFAULT,
'vaccine_option': False,
'vaccine_booster_option': False,
'vaccine_type': 'AZD1222_(AstraZeneca)',
'vaccine_booster_type': 'AZD1222_(AstraZeneca)',
'vaccine_option': False,
'vaccine_type': 'AZD1222_(AstraZeneca)',
'ventilation_type': 'no_ventilation',
'virus_type': 'SARS_CoV_2',
'volume_type': NO_DEFAULT,
'window_type': 'window_sliding',
'window_height': 0.,
'window_opening_regime': 'windows_open_permanently',
'window_type': 'window_sliding',
'window_width': 0.,
'windows_duration': 10.,
'windows_frequency': 60.,
'windows_number': 0,
'window_opening_regime': 'windows_open_permanently',
'sensor_in_use': '',
'short_range_option': 'short_range_no',
'short_range_interactions': NO_DEFAULT,
'short_range_occupants': 0,
'windows_number': 0
}
# ------------------ Activities ----------------------
@ -98,8 +98,8 @@ VACCINE_BOOSTER_TYPE = ['AZD1222_(AstraZeneca)', 'Ad26.COV2.S_(Janssen)', 'BNT16
VACCINE_TYPE = ['Ad26.COV2.S_(Janssen)', 'Any_mRNA_-_heterologous', 'AZD1222_(AstraZeneca)', 'AZD1222_(AstraZeneca)_and_any_mRNA_-_heterologous', 'AZD1222_(AstraZeneca)_and_BNT162b2_(Pfizer)',
'BBIBP-CorV_(Beijing_CNBG)', 'BNT162b2_(Pfizer)', 'BNT162b2_(Pfizer)_and_mRNA-1273_(Moderna)', 'CoronaVac_(Sinovac)', 'CoronaVac_(Sinovac)_and_AZD1222_(AstraZeneca)', 'Covishield',
'mRNA-1273_(Moderna)', 'Sputnik_V_(Gamaleya)', 'CoronaVac_(Sinovac)_and_BNT162b2_(Pfizer)']
VENTILATION_TYPES = {'natural_ventilation', 'mechanical_ventilation',
'no_ventilation', 'from_fitting'}
VENTILATION_TYPES = {'from_fitting', 'natural_ventilation',
'mechanical_ventilation', 'no_ventilation'}
VOLUME_TYPES = {'room_volume_explicit', 'room_volume_from_dimensions'}
WINDOWS_OPENING_REGIMES = {'windows_open_permanently',
'windows_open_periodically', 'not-applicable'}

View file

@ -22,53 +22,53 @@ minutes_since_midnight = typing.NewType('minutes_since_midnight', int)
@dataclasses.dataclass
class VirusFormData(FormData):
ascertainment_bias: str
activity_type: str
air_changes: float
air_supply: float
arve_sensors_option: bool
precise_activity: dict
calculator_version: str
ceiling_height: float
conditional_probability_viral_loads: bool
CO2_fitting_result: dict
conditional_probability_viral_loads: bool
event_month: str
exposure_option: str
floor_area: float
geographic_cases: int
geographic_population: int
hepa_amount: float
hepa_option: bool
humidity: str
inside_temp: float
location_name: str
location_latitude: float
location_longitude: float
geographic_population: int
geographic_cases: int
ascertainment_bias: str
exposure_option: str
location_name: str
mask_type: str
mask_wearing_option: str
mechanical_ventilation_type: str
calculator_version: str
opening_distance: float
event_month: str
precise_activity: dict
room_heating_option: bool
room_number: str
sensor_in_use: str
short_range_interactions: list
short_range_occupants: int
short_range_option: str
simulation_name: str
vaccine_option: bool
vaccine_booster_option: bool
vaccine_type: str
vaccine_booster_type: str
vaccine_option: bool
vaccine_type: str
ventilation_type: str
virus_type: str
volume_type: str
windows_duration: float
windows_frequency: float
window_height: float
window_opening_regime: str
window_type: str
window_width: float
windows_duration: float
windows_frequency: float
windows_number: int
window_opening_regime: str
sensor_in_use: str
short_range_option: str
short_range_interactions: list
short_range_occupants: int
_DEFAULTS: typing.ClassVar[typing.Dict[str, typing.Any]] = DEFAULTS
@ -77,21 +77,18 @@ class VirusFormData(FormData):
self.validate_population_parameters()
validation_tuples = [('activity_type', self.data_registry.population_scenario_activity.keys()),
('mechanical_ventilation_type',
MECHANICAL_VENTILATION_TYPES),
('mask_type', list(mask_distributions(
self.data_registry).keys())),
('ascertainment_bias', CONFIDENCE_LEVEL_OPTIONS),
('event_month', MONTH_NAMES),
('mechanical_ventilation_type', MECHANICAL_VENTILATION_TYPES),
('mask_type', list(mask_distributions(self.data_registry).keys())),
('mask_wearing_option', MASK_WEARING_OPTIONS),
('vaccine_type', VACCINE_TYPE),
('vaccine_booster_type', VACCINE_BOOSTER_TYPE),
('ventilation_type', VENTILATION_TYPES),
('virus_type', list(virus_distributions(
self.data_registry).keys())),
('virus_type', list(virus_distributions(self.data_registry).keys())),
('volume_type', VOLUME_TYPES),
('window_opening_regime', WINDOWS_OPENING_REGIMES),
('window_type', WINDOWS_TYPES),
('event_month', MONTH_NAMES),
('ascertainment_bias', CONFIDENCE_LEVEL_OPTIONS),
('vaccine_type', VACCINE_TYPE),
('vaccine_booster_type', VACCINE_BOOSTER_TYPE),]
('window_type', WINDOWS_TYPES)]
for attr_name, valid_set in validation_tuples:
if getattr(self, attr_name) not in valid_set:
@ -569,6 +566,7 @@ def baseline_raw_form_data() -> typing.Dict[str, typing.Union[str, float]]:
'air_changes': '',
'air_supply': '',
'ascertainment_bias': 'confidence_low',
'calculator_version': calculator_version,
'ceiling_height': '',
'conditional_probability_viral_loads': '0',
'dynamic_exposed_occupancy': '[]',
@ -603,9 +601,8 @@ def baseline_raw_form_data() -> typing.Dict[str, typing.Union[str, float]]:
'mask_type': 'Type I',
'mask_wearing_option': 'mask_off',
'mechanical_ventilation_type': '',
'calculator_version': calculator_version,
'opening_distance': '0.2',
'occupancy_format': 'static',
'opening_distance': '0.2',
'room_heating_option': '0',
'room_number': '123',
'room_volume': '75',
@ -622,12 +619,11 @@ def baseline_raw_form_data() -> typing.Dict[str, typing.Union[str, float]]:
'volume_type': 'room_volume_explicit',
'window_height': '2',
'window_opening_regime': 'windows_open_permanently',
'window_type': 'window_sliding',
'window_width': '2',
'windows_duration': '10',
'windows_frequency': '60',
'windows_number': '1',
'window_type': 'window_sliding',
'window_width': '2',
}
cast_class_fields(VirusFormData)

View file

@ -116,7 +116,6 @@ def test_expected_new_cases(baseline_form_with_sr: VirusFormData):
sr_lr_prob_inf = report_data['prob_inf']/100
# Long-range contributions alone
scenario_sample_times = report_data['times']
alternative_scenarios = rep_gen.manufacture_alternative_scenarios(baseline_form_with_sr)
alternative_statistics = rep_gen.comparison_report(
baseline_form_with_sr, report_data, alternative_scenarios, executor_factory=executor_factory,