# ows-bulkperformancerights

# Bulk Performance Rights Management

A micro-service to handle bulk update of performance rights data from xlsx files.

## Install:

```bash
pyvenv-3.4 env
source env/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt  # additionally needed for dev
```

## Run:
In development, you must set the environment variables yourself in `.env` honcho will source them for you.
Then, start honcho.  This will spawn two threads, one worker and one web app.

```bash
honcho start
```

In production it is assumed that the AWS environment variables listed in `.env` are set.

## Test:

```
py.test --cov bulkperformancerights tests/ 
```

## Try it out:
With the dev server running as instructed above, you can run the following to create a spreadsheet of a users tracks.

**Note:** You will need [ows-xlsx](http://github.com/theorchard/ows-xlsx) running.

```bash
curl -H 'WORKSTATION-TRUSTED: true' -X POST http://127.0.0.1:5000/export/vendor/7123
```

This might take some time, you will want to know the status. open up your browser to:
`http://127.0.0.1:5000/export/vendor/7123`

This will return to you a list of statuses. Once the upload is complete, you should be able to download the file at the path on s3 specified by the `output_file_path` property of an export_status record.
After downloading the spreadsheet, modify the bulk performance rights values in the file outputted and upload it to the service.

```bash
curl -H 'WORKSTATION-TRUSTED: true' -F "email=bob@co.com" -F file=@/file/location/filename.xlsx http://127.0.0.1:5000/upload/vendor/1
```


At some point, after uploading your spreadsheet, you will want to know the status. open up your browser to:
`http://127.0.0.1:5000/status/vendor/1`

If you know the specific job identifier to your uploaded file (say, after you've gotten the above list) and want to narrow your results to just that job id, open up your browser to:
`http://127.0.0.1:5000/status/vendor/1/1`

You'll notice all url fragments are of the pattern {endpoint}/{user_type}/{user_id}/{job_id}

## S3 Flow
The way files are moved around are complicated. Here is a summary.

/upload 
1. ows-bpr places the uploaded file to  s3://config.bucket_name/config.prefix/user_type/user_id
2. ows-bpr copies s3://config.bucket_name/config.prefix/user_type/user_id to s3://config.bucket_name/config.xlsx_prefix/xlsx/user_type/user_id for ows-xlsx to process
3. ows-xlsx processes the xlsx and places the json file at s3://config.bucket_name/config.xlsx_prefix/json/user_type/user_id

/export
1. ows-bpr converts art_relations data to json and places it at s3://config.bucket_name/config.xlsx_prefix/json/user_type/user_id for ows-xlsx to process
2. ows-bpr copies json to s3://config.bucket_name/config.prefix/user_type/user_id 
3. ows-xlsx converts json to xlsx and places the xlsx file at s3://config.bucket_name/config.xlsx_prefix/xlsx/user_type/user_id 
4. This xlsx is then copied to s3://config.bucket_name/config.prefix/user_type/user_id to be downloaded

### OS-X Docker instructions
To make sure you can ship your code to production it is wise to make sure your Dockerfile, etc. is working as expected in a local isolated environment.  On OS-X you can use boot2docker to host your docker containers:

Once installed, to start up boot2docker and get your environment configured:

```bash
boot2docker up
$(boot2docker shellinit) #this will set docker environment variables in your terminal
```
Then, build your docker image.  You should only have to do this initially or after you've modified any dockerfile/system files:

```bash
docker build -t bulkperformancerights ./
```
Now, run your docker container within your boot2docker host, passing envrionment variables through

```bash
docker run -i -t bulkperformancerights -e AWS_ACCESS_KEY_ID=REPLACE_ME -e AWS_SECRET_ACCESS_KEY=REPLACE_ME -e Environment=dev -e RAVEN_DSN=REPLACE_ME -e XLSXTOJSON_URL=REPLACE_ME
```
