Skip to content

Commit

Permalink
Merge pull request #25 from neutrons/expiration-field
Browse files Browse the repository at this point in the history
Add expiration field to DataRun model
  • Loading branch information
glass-ships authored Aug 13, 2024
2 parents cfe843b + 5e67172 commit 25a1c73
Show file tree
Hide file tree
Showing 23 changed files with 568 additions and 204 deletions.
29 changes: 21 additions & 8 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
name: unit-test
name: Run unit tests

on:
workflow_dispatch:
pull_request:
push:
branches: [next, qa, main]
tags: ['v*']
tags: ["v*"]

jobs:
linux:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}

env:
DATABASE_NAME: livedatadb
DATABASE_USER: livedatauser
Expand All @@ -23,29 +24,39 @@ jobs:
DJANGO_SUPERUSER_USERNAME: livedatauser
DJANGO_SUPERUSER_PASSWORD: livedatapass
COVERAGE_RUN: coverage run -m

steps:
- uses: actions/checkout@v4
- uses: conda-incubator/setup-miniconda@v3
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Conda environment
uses: conda-incubator/setup-miniconda@v3
with:
auto-update-conda: true
channels: conda-forge,defaults
mamba-version: "*"
environment-file: environment.yml
cache-environment-key: ${{ runner.os }}-env-${{ hashFiles('**/environment.yml') }}
cache-downloads-key: ${{ runner.os }}-downloads-${{ hashFiles('**/environment.yml') }}

- name: Start docker containers
run: |
cp ./config/docker-compose.envlocal.yml docker-compose.yml
docker-compose up --build -d
docker compose up --build -d
- name: Sleep, wait for containers to start up
run: sleep 30

- name: Run unit tests
run: python -m pytest tests/

- name: Stop the coverage process
# Stopping the coverage process allows the code coverage to be written to disk
run: docker exec live_data_server_livedata_1 /bin/bash -c "pkill coverage"
run: docker exec live_data_server-livedata-1 /bin/bash -c "pkill coverage"

- name: Copy code coverage out of docker container
run: docker cp live_data_server_livedata_1:/var/www/livedata/app /tmp/
run: docker cp live_data_server-livedata-1:/var/www/livedata/app /tmp/

- name: Combine and show code coverage
shell: bash -l {0}
run: |
Expand All @@ -54,9 +65,11 @@ jobs:
coverage xml
cp coverage.xml $OLDPWD
coverage report
- name: Bring down docker containers completely now
# This will completely remove the containers
run: docker-compose down
run: docker compose down

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/.envrc
/docker-compose.yml
_version.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ app_dir := live_data_server
DJANGO_COMPATIBLE:=$(shell python -c "import django;t=0 if django.VERSION[0]<4 else 1; print(t)")
DJANGO_VERSION:=$(shell python -c "import django;print(django.__version__)")

# command to run docker compose. change this to be what you have installed
# this can be overriden on the command line
# DOCKER_COMPOSE="docker compose" make docker/pruneall
DOCKER_COMPOSE ?= docker-compose
ifneq ($(shell docker compose version 2>/dev/null),)
DOCKER_COMPOSE=docker compose
else ifneq ($(shell docker-compose --version 2>/dev/null),)
DOCKER_COMPOSE=docker-compose
endif


help:
# this nifty perl one-liner collects all comments headed by the double "#" symbols next to each target and recycles them as comments
Expand All @@ -30,10 +32,17 @@ docker/pruneall: docker/compose/validate ## stop all containers, then remove al
docker/compose/validate: ## validate the version of the docker-compose command. Exits quietly if valid.
@./scripts/docker-compose_validate.sh $(DOCKER_COMPOSE)

local/docker/up: docker/compose/validate ## compose and start the service locally
docker/compose/local: docker/compose/validate ## compose and start the service locally
\cp ./config/docker-compose.envlocal.yml docker-compose.yml
$(DOCKER_COMPOSE) up --build

.PHONY: clean
clean: ## remove all local compiled Python files
rm -f `find . -type f -name '*.py[co]' ` \
`find . -type f -name '_version.py'`
rm -rf `find . -name __pycache__ -o -name "*.egg-info"` \
.ruff_cache .pytest_cache

.PHONY: check
.PHONY: first_install
.PHONY: help
Expand All @@ -42,4 +51,4 @@ local/docker/up: docker/compose/validate ## compose and start the service locall
.PHONY: webapp/core
.PHONY: docker/compose/validate
.PHONY: docker/pruneall
.PHONY: local/docker/up
.PHONY: docker/compose/local
114 changes: 73 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,102 @@
## live_data_server
Data server for data plots
# live_data_server

<!-- Badges -->

[![Documentation Status](https://readthedocs.org/projects/livedata-ornl/badge/?version=latest)](https://livedata-ornl.readthedocs.io/en/latest/?badge=latest)
[![codecov](https://codecov.io/gh/neutrons/live_data_server/graph/badge.svg?token=niQ0AWldBd)](https://codecov.io/gh/neutrons/live_data_server)

Developer documentation at https://livedata-ornl.readthedocs.io/en/latest/
Data server for data plots.

## Contributing
## Development

Create a conda environment `livedata`, containing all the dependencies
```python
conda env create -f environment.yml
conda activate livedata
```
Developer documentation at <https://livedata-ornl.readthedocs.io/en/latest/>

### Containerization
### Dependencies

To deploy this application locally for development you will need to assign values to the following secrets
as environment variables defined in the shell's environment:
```bash
DATABASE_NAME
DATABASE_USER
DATABASE_PASS
DATABASE_HOST
DATABASE_PORT
LIVE_PLOT_SECRET_KEY
```
It is recommended to save these variables into an `.envrc` file which can be managed by
[envdir](https://direnv.net/).
- [Conda](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) / [Mamba/Miniforge](https://github.com/conda-forge/miniforge)
- [Docker](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/)
- [direnv](https://direnv.net/) (optional)

After the secrets are set, type in the terminal shell:
```bash
make local/docker/up
```
This command will copy `config/docker-compose.envlocal.yml` into `docker-compose.yml` before composing
all the services.
### Setup for Local Development

1. Clone the repository and `cd` into the project directory.

1. Create a conda environment `livedata`, containing all the dependencies

```python
conda env create -f environment.yml
conda activate livedata
```

1. To deploy this application locally, you will need to set a number of environment variables,
for example (bash):

```bash
export DATABASE_NAME=livedatadb
export DATABASE_USER=livedatauser
export DATABASE_PASS=livedatapass
export DATABASE_HOST=db
export DATABASE_PORT=5432
export LIVE_PLOT_SECRET_KEY="secretKey"

# These need to be set for `pytest`,
# but are not used in the docker compose
export DJANGO_SUPERUSER_USERNAME=$DATABASE_USER
export DJANGO_SUPERUSER_PASSWORD=$DATABASE_PASS
```

**Notes**:

Type `make help` to learn about other macros available as make targets.
For instance, `make docker/pruneall` will stop all containers, then remove
all containers, images, networks, and volumes.
- The `DATABASE_PORT` _must_ be set to `5432`, as Postgres is configured to listen on that port by default.
If you need to change the port, you will need to modify the `docker-compose.yml` file accordingly.

## Test & Verification
- It is recommended to save these variables into an `.envrc` file which can be managed by [direnv](https://direnv.net/).
direnv will automatically load the variables when you `cd` into the project directory.

After starting the services with `make local/docker/up`, run the test that will post and get data:
1. After the secrets are set, type in the terminal shell:

```bash
make docker/compose/local
```

This command will copy `config/docker-compose.envlocal.yml` into `docker-compose.yml` before composing all the services.

Type `make help` to learn about other macros available as make targets.
For instance, `make docker/pruneall` will stop all containers, then remove all containers, images, networks, and volumes.

### Testing

After the setup, with the server running, you can test your setup by running `pytest`:

```bash
DJANGO_SUPERUSER_USERNAME=***** DJANGO_SUPERUSER_PASSWORD=***** pytest tests/test_post_get.py
pytest tests/test_post_get.py
# or simply
pytest
```

Environment variables `DJANGO_SUPERUSER_USERNAME` and `DJANGO_SUPERUSER_PASSWORD` are defined in
file `docker-compose.envlocal.yml`. You need to either pass these variables with the correct values or have
them exported to the shell where `pytest` is to be run.
**NOTE:**
The environment variables `DJANGO_SUPERUSER_USERNAME` and `DJANGO_SUPERUSER_PASSWORD` are defined in the `docker-compose.envlocal.yml` file, but `pytest` does not read this file.
You must either have them exported to the shell where `pytest` is to be run, as described above, or modify the `pytest` command to include them, e.g.:

```bash
DJANGO_SUPERUSER_USERNAME=***** DJANGO_SUPERUSER_PASSWORD=***** pytest
```

## Deployment to the Test Environment
- Repository managing the provision for deployment:
+ hardware and networking for deployment: https://code.ornl.gov/sns-hfir-scse/infrastructure/neutrons-test-environment/-/blob/main/terraform/servers.tf#L85-97
+ configuration independent of source code changes: https://code.ornl.gov/sns-hfir-scse/infrastructure/neutrons-test-environment/-/blob/main/ansible/testfixture02-test.yaml
- Repository managing deployment of the source to the provisioned hardware: https://code.ornl.gov/sns-hfir-scse/deployments/livedata-deploy

- Repository managing the provision for deployment:
- hardware and networking for deployment: <https://code.ornl.gov/sns-hfir-scse/infrastructure/neutrons-test-environment/-/blob/main/terraform/servers.tf#L85-97>
- configuration independent of source code changes: <https://code.ornl.gov/sns-hfir-scse/infrastructure/neutrons-test-environment/-/blob/main/ansible/testfixture02-test.yaml>
- Repository managing deployment of the source to the provisioned hardware: <https://code.ornl.gov/sns-hfir-scse/deployments/livedata-deploy>

## Building the Documentation

Additional documentation is available in the `docs` directory. To build the documentation in your local machine,
run the following command from within directory `docs/`:

```bash
make html
```

The documentation will be built in the `docs/_build/html` directory. To view the documentation,
open the `docs/_build/html/index.html` file in a web browser.
10 changes: 10 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
coverage:
status:
project:
default:
target: 80%
threshold: 3%
patch:
default:
target: 80%
threshold: 10%
4 changes: 2 additions & 2 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M help $(SOURCEDIR) $(BUILDDIR) $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
$(SPHINXBUILD) -M $@ $(SOURCEDIR) $(BUILDDIR) $(SPHINXOPTS) $(O)
13 changes: 8 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import os
import sys

sys.path.insert(0, os.path.abspath("../live_data_server"))
# import os
# import sys
from importlib import metadata

# sys.path.insert(0, os.path.abspath("../live_data_server"))
# from live_data_server import __version__ as release
release = metadata.version("live_data_server")

from live_data_server import __version__ as release

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
Expand Down Expand Up @@ -67,7 +70,7 @@
autosummary_generate = True

# Napoleon settings
napoleon_google_docstring = False
napoleon_google_docstring = True
napoleon_numpy_docstring = True

# Add any paths that contain custom static files (such as style sheets) here,
Expand Down
Loading

0 comments on commit 25a1c73

Please sign in to comment.