# streamlit-fan-export-app
Streamlit app for exporting CRM campaign, newsletter related fan data



* Project setup

After cloning this repository run following command to
create virtual environment and install required packages:

```poetry install```


* Prerequisites:

Make sure you have Snowflake CLI installed:
https://docs.snowflake.com/en/developer-guide/snowflake-cli/installation/installation

Make sure your that for Snowflake CLI you have connection profile called sme_merch that uses
FANSIFTER_ENGINEERING_PRIVACY profile to deploy/manage Streamlit app related resources. 
This role is also used by local Streamlit app to run queries or to run ETL scripts.

```
[connections.fan_data_export]
account = "delphi.us-east-1"
user = "xxxxxx@sonymusic-pde.com"
role = "FANSIFTER_ENGINEERING_PRIVACY"
database = "FANSIFTER_APP_REPORTING"
schema = "PROD_STREAMLIT_FAN_EXPORT"
warehouse = "QA_ETL_WH"
authenticator = "SNOWFLAKE_JWT"
private_key_file = "/Users/xxxxx/.ssh/snowflake/rsa_key.p8"
```

Makefile is used to run related tasks. To see all available tasks:
type ```make help```

Most of the commands take advantage of Snowflake CLI.
Here are Snow CLI commands for Streamlit:
https://docs.snowflake.com/en/developer-guide/snowflake-cli/command-reference/streamlit-commands/overview

* Updating Streamlit app

As the app is only used in PROD environment then run following code to apply changes:
```make deploy_streamlit_app```

* Running streamlit app locally

To run Streamlit app locally, type:
```make run_streamlit_locally```

Local deployment takes advantage of connections.json file that
contains profile used by Snowpark to connect to Snowflake.
Inside the Snowflake's Streamlit app, the connection is automatically
assigned but not in local Streamlit deployment.


* User permissions (managed by Terraform)

Users belonging to Snowflake role called SME_FAN_EXPORT_STREAMLIT_ACCESS 
should have access to this Streamlit app.

This role is granted database role ```FANSIFTER_APP_REPORTING.DB_PROD_STREAMLIT_FAN_EXPORT_SCHEMA_FAN_DATA_EXPORT_STREAMLIT```.

And these roles are results of ```streamlit_database_role_FAN_DATA_EXPORT```
module in Terraform.

This means that there is currently no need to manage
user permissions directly from Streamlit app's share button.


* Running tests

```make test```

This will run all tests in folder: streamlit_app/tests/.
Currently it will mock Snowflake connection using local
testing framework: https://docs.snowflake.com/en/developer-guide/snowpark/python/testing-locally
or https://docs.snowflake.com/en/developer-guide/snowpark/python/tutorials/testing-tutorial#introduction
