README.md 10.9 KB
Newer Older
Anita Graser's avatar
Anita Graser committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[[_TOC_]]


# DETECT

This guide explains how to set up, develop, and deploy the DETECT application "Fake-Shop Explorer". 

Fake-Shop Explorer is a prototype of a browser-based gamified crowd-sourcing application (i.e. a game) that collects user/player input on potential fake shops. 



Fake-Shop Explorer has been developed and tested on Ubuntu 20.04.4 LTS (native and WSL Windows Subsystem for Linux) for use in Firefox and Chrome.


<img align="right" src="./docs/landing-page.png">




## Overview

Fake-Shop Explorer is implemented as a [Flask](https://flask.palletsprojects.com/en/2.0.x/) application. 
It can be run in a [containerised](#docker-setup) setup (using `Docker` and `docker-compose`) or locally without Docker.

In production deployment, Fake-Shop Explorer uses two web services: 

1. **Fake shop detector** which has a public API: https://api.fakeshop.at/fake-shop-detector/api/1.2/ui/ and
2. **Fake shop database** https://db.malzwei.at/api/ which requires an access token to use. If you want to use the fake shop database, obtain a token and place it in the path that is defined in `detect/settings.py` for `DEV_DB_API_KEY_LOC`. 
As a **fallback** (without token) sample URLs are read from a **local** resource file. 


Depending on the game level, players are asked different questions about the websites (potential fake shops). For this purpose, the websites are embedded into the Fake-Shop Explorer interface:

<img align="right" src="./docs/shop-noshop-question.png">

Player inputs are saved to a database (MySQL in production, Sqlite for development).

Since Fake-Shop Explorer can be run locally or using Docker, the following instructions describe both options. 


## Local environment setup

To get started, download the sources:

```bash
Anita Graser's avatar
Anita Graser committed
46
git clone https://git-service.ait.ac.at/dsai-public/detect
Anita Graser's avatar
Anita Graser committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
```

and change your working directory to the download folder. 


### Node and npm

Fake-Shop Explorer development requires **node 16**. (Don't install node 17+ because of https://stackoverflow.com/questions/69719601/getting-error-digital-envelope-routines-reason-unsupported-code-err-oss) 

It is recommended to install `nvm`. Based on https://askubuntu.com/a/1009527: To avoid conflicts, remove existing installs:

```bash
sudo apt purge nodejs npm
```

then install nvm LTS and use it

```bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
nvm install --lts
```

Finally, run `npm install` to install the dependencies specified in package.json


### Python environment setup

Fake-Shop Explorer development requires **Python >= 3.9**. A good option to create a dedicated `detect` Python environment for development purposes is to use conda:

```bash
conda create --name detect python=3
```

and - once the `detect` environment is created - activate it and install the development dependencies: 

```bash
conda activate detect
pip install -r requirements/dev.txt
```


### Local deployment 

After installing node, npm, and Python, the Fake-Shop Explorer can be built and run using: 

```bash
npm run-script build # use build-debug for non-minimized javascript
npm start  # run the webpack dev server and flask server using concurrently
```

When the app starts, the log will show where it is launching, something like `http://172.30.212.54:5000/`. 

The local version of this app uses Sqlite. You can access the database using:

```
sqlite3 /tmp/dev.db
```


## Docker setup

The containerized version of Fake-Shop Explorer consists of three main services: `flask-dev`, `flask-prod`, and `manage`.

Services and images are defined in `docker-compose.yml` resp. `Dockerfile`.


### Steps to start from scratch: development

To get started, clone the repo, build and start the **dev** service:

```bash
Anita Graser's avatar
Anita Graser committed
118
git clone https://git-service.ait.ac.at/dsai-public/detect.git
Anita Graser's avatar
Anita Graser committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
cd detect-prototype

docker-compose build --no-cache flask-dev
docker-compose up -d flask-dev
```

Then build the app resources inside the dev container:

```bash
docker exec -it detect-prototype_flask-dev_1 /bin/bash
npm run-script build
exit

docker-compose down
```

*Note: A docker volume `node-modules` is created to store NPM packages and is reused across the dev and prod containers. For the purposes of DB testing with `sqlite`, the file `dev.db` is mounted to all containers. This volume mount should be removed from `docker-compose.yml` if a production DB server is used.*

*Note: The list of `environment:` variables in the `docker-compose.yml` file takes precedence over any variables specified in `.env`.*

#### Cleaning up old containers and database content

In development, it is often easier to start from scratch with an empty database and new containers when the schema or initial table content change. You can remove the old containers and database content using:  

```bash
docker image remove detect-development:latest
docker image remove detect-manage:latest
sudo rm -r .mysql-data/
```


### Production deployment

[supervisord](http://supervisord.org/) is used for process management and [gunicorn](https://gunicorn.org/) is used as HTTP server. 
Settings for supervisord are defined in `supervisord-conf`, settings for gunicorn are defined in `supervisord_programs/gunicorn.conf`,  

Build the production container:

```bash
docker-compose build --no-cache flask-prod
```

Start the container in detached mode:

```bash
docker-compose up -d flask-prod
```

Check if detect-prototype_flask-prod_1 and detect-prototype_db_1 run: 

```bash
docker ps
```

To access the logs of a background container (started with -d) use:

```bash
docker logs detect-prototype_flask-dev_1
```

To access the containerized MySQL database, connect to the container: 

```bash
docker exec -it detect-prototype_db_1 /bin/bash
```

and run

```bash
mysql -u detect -p detect
```


### Manage

The manage container is used to run commands using the `Flask CLI`:

```bash
docker-compose run --rm manage <<COMMAND>>
```

Therefore, to initialize a database you can run:

```bash
docker-compose run --rm manage db init
docker-compose run --rm manage db migrate
docker-compose run --rm manage db upgrade
```

Similarly, to open the interactive Flask shell, run

```bash
docker-compose run --rm manage db shell
```


## Advanced Developer documentation

### Application settings

Settings can be modified in `detect/settings.py`

### Asset Management

Files placed inside the `assets` directory and its subdirectories
(excluding `js` and `css`) will be copied by webpack's
`file-loader` into the `static/build` directory. In production, the plugin
`Flask-Static-Digest` zips the webpack content and tags them with a MD5 hash.
As a result, you must use the `static_url_for` function when including static content,
as it resolves the correct file name, including the MD5 hash.
For example

```html
<link rel="shortcut icon" href="{{static_url_for('static', filename='build/img/favicon.ico') }}">
```

If all of your static files are managed this way, then their filenames will change whenever their
contents do, and you can ask Flask to tell web browsers that they
should cache all your assets forever by including the following line
in ``.env``:

```text
SEND_FILE_MAX_AGE_DEFAULT=31556926  # one year
```


### DETECT Session

`detect/detect_session.py` wraps the Flask session object and should be used instead of the session object to store user-specific information.


### Concepts

In the game, users travel through space and visit unknown planets. 

#### Tasks
Tasks have to be solved to collect fuel for the spaceship, and to map/categorize new planets.
Thus, two task types exist:

  * **fuel**/**simple** tasks (level 0): replenish energy for the spaceship by answering a simple yes/no decision "shop or not?"
  * **planet** tasks (level 1-7): answer detailed questions about potential web/fake shop sites. Planet tasks become increasingly complicated with level progress.  


In the data model, `task` is used synonymously for website url, while `subtask` is synonymous for answering questions about the website  url.



### Database schema / Flask models

<img align="right" width="70%" src="./docs/db-schema.png">

`detect/user/models.py` contains the app-specific database tables and view definitions. The full schema is shown in the ER diagram on the right. The tables contain the following information

* `events`: logs user actions, such as visiting planets or gathering fuel
* `options`: answer options for game level questions
* `roles`: user roles, not used yet
* `subtasks`: game level questions
* `tasks`: URLs of the websites / fake-shops
* `user_task_link`: logs user guess regarding shop / no-shop / fake shop classification
* `user_task_subtask_link`: logs user answer to game level questions (e.g. if "Impressum" (site notice) exists or looks plausible)

The views `v_agreement`, `v_accuracy`, and `v_accuracy_monthly` are used to compute how well a user's answers agree with the majority opinion of other users. 

`detect/user/models.py` also contains two functions that are called from `app.py` before the database is first queried:

* `create_subtasks_and_options` parses the subtasks (game level questions) defined in `detect/resources/subtasks.json` and inserts them into the the `subtasks` table. 
* `create_users` creates some test users for easier testing and should be removed in production.


### Testing

Fake-Shop Explorer comes with an extensive suit of tests.

To run all tests with Docker run:

```bash
docker-compose run --rm manage test
```

Alternatively, without Docker, run: 

```bash
flask test  
```


#### Running Tests in VSCode

In VSCode, the following launch.json can be used:

```
{
    "configurations": [
        {
            "name": "DETECT local debugger",
            "type": "python",
            "request": "launch",
            "program": "main.py",
            "console": "integratedTerminal",            
            "cwd": "/<your-project-path-here>/detect-prototype",
            "python": "/<your-python-path-here>/anaconda3/envs/detect/bin/python",
        }
    ]
}
```


### Linting

The `lint` command will attempt to fix any linting/style errors in the code. If you only want to know if the code will pass CI and do not wish for the linter to make changes, add the `--check` argument.

To run the linter, run:

```bash
docker-compose run --rm manage lint
flask lint # If running locally without Docker
```



## Advanced Devops Documentation

### Database migrations

Whenever a database migration needs to be made. 
To generate a new migration script, run: 

```bash
docker-compose run --rm manage db migrate
flask db migrate # If running locally without Docker
```

Then apply the migration:

```bash
docker-compose run --rm manage db upgrade
flask db upgrade # If running locally without Docker
```

For a full migration command reference, run `docker-compose run --rm manage db --help`.

If you will deploy your application remotely (e.g on Heroku) you should add the `migrations` folder to version control.
You can do this after `flask db migrate` by running the following commands

```bash
git add migrations/*
git commit -m "Add migrations"
```

Make sure folder `migrations/versions` is not empty.