Skip to content

Commit

Permalink
add docker configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rideam committed Jul 15, 2023
1 parent f455a41 commit 36522e3
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 19 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
venv
instance
docs
microcontroller
migrations
node_modules
tests
package.json
package-lock.json
serverless.yml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
venv
node_modules
.env
db.env

application/.env
serverless.yml
Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM python:3.9-buster

RUN mkdir app
WORKDIR /app

ENV PATH="${PATH}:/root/.local/bin"
ENV PYTHONPATH=.


EXPOSE 8000
COPY requirements.txt .


RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

COPY . .

#CMD python3 app.py
CMD ["gunicorn" , "--bind", "0.0.0.0:8000", "app:app"]
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.8'

services:

db:
image: postgres:13-alpine
container_name: playgrounddb
ports:
- "5432:5432"
env_file:
- db.env
volumes:
- postgres_data:/var/lib/postgresql/data/

app:
build: .
image: playgroundapp
env_file:
- .env
depends_on:
- db

nginx:
build: ./nginx
image: playgroundnginx
container_name: playgroundnginx
ports:
- 80:80
depends_on:
- app
- db

volumes:
postgres_data:
4 changes: 4 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
#COPY nginx.conf /etc/nginx/conf.d/
COPY nginx.conf /etc/nginx/nginx.conf
17 changes: 17 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# App service scaled
events {}

http {
upstream app {
server app;
server serverless-flask-app-1:8000;
server serverless-flask-app-2:8000;
}
server {
listen 80;
server_name app.com;
location / {
proxy_pass http://app;
}
}
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tomlkit==0.11.6
trio==0.22.0
trio-websocket==0.10.2
typing_extensions==4.5.0
#urllib3==2.0.2
urllib3==1.26.6
Werkzeug==2.2.3
wsproto==1.2.0
WTForms==3.0.1
Expand Down
33 changes: 21 additions & 12 deletions routes/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
__name__
)


class Conditions(Enum):
GT = '>'
LS = '<'
EQ = '='





@testing_bp.route('/joinpolicytest', methods=['POST','GET'])
@testing_bp.route('/joinpolicytest', methods=['POST', 'GET'])
def user_joining_policy_test():
""" Route to allow transaction testing. Simulates user joining policy and policy details being stored onchain.
Also allows testing how much time it takes to send data to the Algorand blockchain.
"""

start_time = time.time()
data = request.get_json()
policy_details_dict = {
'policy': 'Testing Policy',
Expand All @@ -41,7 +40,6 @@ def user_joining_policy_test():
'signature': f'Joined by {data["wallet"]}'
}

start_time = time.time()
success, txid = send_txn(
address_from_private_key(mnemonic.to_private_key(data["passphrase"])),
0,
Expand All @@ -58,9 +56,10 @@ def user_joining_policy_test():
})


@testing_bp.route('/payouttest', methods=['POST','GET'])
@testing_bp.route('/payouttest', methods=['POST', 'GET'])
def payout_test():
""" Route to allow testing of how long payouts take to be issued """
start_time = time.time()
admin = User(settings.account_one_memonic)

data = request.get_json()
Expand All @@ -83,15 +82,14 @@ def payout_test():
}

# send data to chain
admin.send(0, data["wallet"],json.dumps(weather_dict))
admin.send(0, data["wallet"], json.dumps(weather_dict))

condition_pass = {
'temperature': False,
'humidity': False,
'soil_moisture': False
}


# check strike conditions
if event['temperature_condition'] == Conditions.GT.value:
if data["temperature"] > event['temperature']:
Expand Down Expand Up @@ -123,12 +121,19 @@ def payout_test():
success, txid = admin.send(0.1, data["wallet"],
f'Paid by {admin.public_key}')
payout_success = success
return jsonify({ 'payout_success': payout_success})

end_time = time.time()
return jsonify({
'status': payout_success,
'payout_success': payout_success,
'elapsed_time': end_time - start_time
})


@testing_bp.route('/weatherdatatest', methods=['POST','GET'])
@testing_bp.route('/weatherdatatest', methods=['POST', 'GET'])
def weather_data_test():
""" Route to allow simulating microcontrollers sending data and the data being stored on chain """
start_time = time.time()
admin = User(settings.account_one_memonic)

data = request.get_json()
Expand All @@ -142,5 +147,9 @@ def weather_data_test():
}

# send data to chain
success, txid = admin.send(0, data["wallet"],json.dumps(weather_dict))
return jsonify(success)
success, txid = admin.send(0, data["wallet"], json.dumps(weather_dict))
end_time = time.time()
return jsonify({
'status': success,
'success': success,
'elapsed_time': end_time - start_time})
35 changes: 29 additions & 6 deletions tests/transaction_execution_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@
import json
from users import users

user = users[0]
response = requests.post(f'{env.host}/joinpolicytest', json=user)
data = response.json()
# print(json.dumps(data, indent=4))
if data['status']:
print(f"{data['elapsed_time']} seconds")

def join_policy_time():
user = users[0]
response = requests.post(f'{env.host}/joinpolicytest', json=user)
data = response.json()
# print(json.dumps(data, indent=4))
if data['status']:
print(f"Joining policy elapsed time - {round(data['elapsed_time'],2)} seconds")

def payout_time():
user = users[1]
microcontroller_data = {
"temperature": 60,
"humidity": 80,
"soil_moisture": 1,
"farm": "Puma Farm",
"crop": "maize",
"wallet": user['wallet'],
"passphrase": user['passphrase']
}
response = requests.post(f'{env.host}/payouttest', json=microcontroller_data)
data = response.json()
# print(json.dumps(data, indent=2))
if data['status']:
print(f"Payout elapsed time - {round(data['elapsed_time'],2)} seconds")


join_policy_time()
payout_time()

0 comments on commit 36522e3

Please sign in to comment.