# ows-product-staging

Orchard web service to serve as staging area for content before ingest

## Setup

We use Poetry to manage dependencies. Go to https://python-poetry.org/docs/#installing-with-the-official-installer to follow the steps to install Poetry with the official installer.

Ensure you have python3.13 installed.

```sh
$ brew update
$ brew install pyenv
$ pyenv install 3.13
$ pyenv local 3.13
```

## Usage

```sh
$ git clone git@github.com:theorchard/ows-product-staging.git
$ cd ows-product-staging
$ make clean env lint fmt test_unit

$ make dev
```

If you find you want to have DataDog trace enabled while running `make dev`, make sure you've copied your `.env.shadow` to `.env`. Then run `make trace-dev`

```sh
$ make test_unit
$ make watch_unit
```

## Docker Usage

```sh
$ awsume prod
$ make docker_unit_lint
```

Force Docker images to build:

```sh
$ docker compose build
```

Run the service with hot-reloading:

```sh
$ make up_dev
$ curl http://localhost:8888/hello/
```

Run the service like it would run in deployed environment:

```sh
$ make up_deploy
$ curl http://localhost:8889/hello/
```

Run integration tests[^1]:

```sh
$ make docker_integration_test
```

## 🎛️ Feature Flags

`ows-product-staging` uses [Split.io](https://split.io) for feature flag management. Patterns are borrowed from `ows-pdp`.

### Using Feature Flags in Code

```python
from product_staging.connectors.features import SplitioClient, BooleanFeature, Feature, splitio_client_factory

splitio_client: SplitioClient = splitio_client_factory()

def fastapi_handler_with_boolean_feature():
   """Awesome API handler.
   
   Feature Flags: 
     - my_feature_flag
   """
   my_feature = BooleanFeature(client=splitio_client, feature_name="my_feature_flag")
   if my_feature.is_on_for_identity("test_identity_uuid"):
      # perform flag dependent behavior
      pass

def fastapi_handler_with_multivalue_feature():
   """Awesome API handler.
   
   Feature Flags: 
     - another_feature_flag
   """
   # Use the `Feature` feature for other flags types.
   my_feature = Feature(client=splitio_client, feature_name="another_feature_flag")
   if my_feature.get_value_for_identity("test_identity_uuid") == "special_ff_value":
      # perform flag dependent behavior
      pass
```

[^1]: Integration tests are dockerized and run against the QA service. If you want to run integration tests against a 
local build, use the `local_docker_integration_test` Makefile target

## Running integration tests locally

### ows-product-staging docker-database-image

The ows-product-staging docker-database-image is used in this configuration. You can refresh the database to get the latest schema (although it will clear any data you have on the volume) using:

```sh
awsume prod
make local_docker_db_refresh
```

You will have to do this step if someone has recently introduced DDL changes to `ows-product-staging` database

### Environment variables

In `.env` ensure these variables are set:

```sh
INGESTION_SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/437795906767/qa-bulk-session-ingest-execution-fifo-queue.fifo
SPLIT_FILE_PATH=.split

NEO4J_USERNAME=<your username>
# Be sure to escape characters such as `$`, `?`, etc.
NEO4J_PASSWORD=<your password>

# other values
```

### Split file

By default, the dockerized version of `ows-product-staging` will use the `tests/.split` file. Modify it accordingly on your localhost, or introduce your own and override the volume mount

```yml
    volumes:
      - ${PWD}/tests/.split:/.split
```

becomes

```yml
    volumes:
      - ${PWD}/<your new file>:/.split
```

### Run the docker-ized version of your local `ows-product-staging` code

Start ows-product-staging service in a terminal and tail the logs (this will enable you to watch the behavior of the service as the integration tests execute). You'll also be able to directly make calls to this at http://localhost:8889.

```sh
awsume prod
make down_deploy up_deploy

# tail the logs
docker compose logs product_staging-deploy -f
```

### Run the integration tests against the docker-ized local `ows-product-staging` code

```sh
awsume prod
make local_docker_integration_test
```
