ows-analytics-sr
==========

Provides analytics-sound-recordings data endpoints. 
Python flask application

## Install

ows-analytics-sr requires Python 3.6. It also uses [pipenv](https://docs.pipenv.org/),
so make sure you have it installed before using this service.

To install dependencies, run

```bash
make pip_dev
```

Did you see something like this?
```bash
No local packages or working download links found for setuptools>=xx.x.x
```
If so, try this
```bash
env/bin/pip3.6 install --upgrade -vv setuptools
```
Then try again

### pre-commit
To catch flake8 errors and format your source with black in local development, otherwise PR checks should catch these.

Run `brew install pre-commit` if you don't already have it.

In the root of the repo, run 

```
pre-commit install
```

  - This will update your git hooks in `.git/hooks`
  - If you get a hook fail: fix the files, `git add` them again and `git commit` until fixed.
  - You can also skip hook checks with `git commit --no-verify`
  - To exclude the reformatting commit from history use: `git config blame.ignoreRevsFile .git-blame-ignore-revs`


## Run

Copy `.env.shadow` to `.env` and fill in variables. Then run the application with

```bash
make run
```

### Local Grass bypass

If you would like to patch your own local account information for every request,
fill in the following environment variables in the `.env` file with similar values,
depending on which account you would like to impersonate:

```
# Authorization overrides
GRASS_ACCOUNT_TYPE=subaccount
GRASS_ACCOUNT_ID=21989
ORCHARD_USER_ID=alw:1000
```

Note that if you leave these environment values defined while running integration
tests against the server, they will fail (as the integration tests are expecting
a particular label).

## Test

You must have a region in your default aws settings i.e. your 
`~/.aws/credentials` must have something like `region = us-east-1`

There are three kinds of tests: unit, lint, and integration.

### Unit

Simply do

```bash
make test_unit
```

To produce coverage reports, run `make test_unit_cov`.

### Lint

Run lint checks with

```bash
make lint
```

### Integration

Make sure the application is running, then do

```bash
make test_integation
```

## Clean up 

Remove the virtual env directory by running

```bash
make clean
```

## Snowflake authentication using key pair

To authenticate using SSH use following documentation:
https://docs.snowflake.net/manuals/user-guide/snowsql-start.html#using-key-pair-authentication

TL;DR:
```bash
mkdir ~/.ssh/snowflake && cd ~/.ssh/snowflake # recommended
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
```

Make sure to set a password and then put this into the `SNOWFLAKE_KEY_PASSPHRASE` environment
variable in your `.env` file.

Then send newly generated public key to systems@theorchard.com and create SYS ticket.
Note: if you put key not in ~/.ssh/snowflake, then enter location in SNOWFLAKE_PRIVATE_KEY_PATH variable.

## Application Handlers
If you create a new handler or update an existing one, please document this in the `ows-analytics-sr-1.0.0.yaml` Swagger file.
