Commit c9c242e6 authored by vuong's avatar vuong

init code

parents
**/__pycache__
*.pyc
**/venv
modules
modules/*
modules*
*/modules
**/modules
**/.idea
dockerfile
docker-compose.yml
FROM python:3.8
# RUN pip install flask
# RUN pip install pymongo
# RUN pip install datetime
# COPY main.py /app/main.py
WORKDIR /app
RUN pip install --upgrade pip
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY response.json ./
ENV FLASK_HOST=0.0.0.0
ENV FLASK_PORT=5000
EXPOSE 5000
# CMD ["python", "main.py"]
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
#!make
# show some help
help:
@echo
@echo 'Usage:'
@echo ' make <target>'
@echo
@echo 'Most used Targets:'
@echo ' build: Build the application in docker'
@echo ' up: Start the application in docker'
@echo ' down: Stop the application'
@echo ' dv: Stop the application and delete volumes'
@echo ' restart: Restart the application'
@echo
build:
docker-compose build --no-cache
up:
docker-compose up -d
down:
docker-compose down
dv:
docker compose down
restart:
docker-compose restart
from flask import Flask, request, jsonify, Response, make_response
from pymongo import MongoClient, ASCENDING
from os import environ
from datetime import datetime
from sys import stderr
app = Flask(__name__)
def startup():
global collection
try:
try:
client = MongoClient(host=environ['MONGO_HOST'], port=int(environ['MONGO_PORT']),
username=environ['MONGO_USERNAME'], password=environ['MONGO_PASSWORD'])
db = client['database']
except Exception as e:
print(e)
try:
collection = db.estimate_restock_quantity
print(collection)
except:
print('collection = mydb.estimate_restock_quantity')
try:
print(db.list_collection_names())
except Exception as e:
print(e)
# try:
# collection.create_index([('id', ASCENDING)], unique=True)
# print('create_index dones')
# except Exception as e:
# print(e)
except:
print('='*50)
print('someting went wrong')
print('='*50)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
@app.route('/api/estimate/update', methods=['POST'])
def post_estimate():
params = request.get_json(silent=True)
if not params.get('result'):
return Response(status=409)
data = params['result']
if not data:
return Response(status=400)
# if not data.get('product_id') or not data.get('sku') or not data.get('order_qty'):
# return Response(status=409)
if params.get('reset_collection'):
collection.delete_many({})
try:
collection.insert_many(data)
print('test')
except:
return Response(status=409)
return make_response(jsonify({'message': 'Data inserted'}), 201)
@app.route('/api/estimate/all', methods=['GET'])
def get_estimate():
l = [{k: v for k, v in c.items() if k != '_id'} for c in collection.find()]
return jsonify(l)
@app.route('/api/estimate/get/<product_id>', methods=['GET'])
def get_estimate_by_product(product_id):
l = [{k: v for k, v in c.items() if k != '_id'} for c in collection.find({'product_id': int(product_id)})]
print(*l, '\n', sep='\n', file=stderr)
return jsonify(l)
def main():
startup()
app.run(host=environ['FLASK_HOST'], port=int(environ['FLASK_PORT']), debug=True)
if __name__ == '__main__':
main()
version: '3.8'
services:
# cron:
# image: python:3.8
# container_name: cron-service
# volumes:
# - ./cronjob:/app
# command: python /app/script.py
# estimate:
# build:
# context: ./
# dockerfile: ./.docker/Dockerfile
# image: estimate-restock
# container_name: estimate-restock
# volumes:
# - ./app:/app:rw
# # - ./script.py:/app/script.py
# # - ./crontab:/etc/cron.d/crontab
# restart: always
app:
container_name: estimate-restock
build: .
# ports:
# - 8080:8080
ports:
- 5000:5000
volumes:
- ./api.py:/app/api.py
- ./main.py:/app/main.py
depends_on:
- mongo
environment:
MONGO_USERNAME: root
MONGO_PASSWORD: admin
MONGO_PORT: 27017
MONGO_HOST: mongo
networks:
- server
mongo:
image: mongo
restart: always
container_name: mongo-docker
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: admin
networks:
- gui
- server
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "28081:8081"
depends_on:
- mongo
environment:
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: admin
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: admin
ME_CONFIG_MONGODB_URL: mongodb://root:admin@mongo:27017/
networks:
- gui
networks:
server:
name: server
gui:
name: gui
#!/bin/bash
python api.py
import requests
def call_api():
url = 'http://0.0.0.0:5000/api/estimate/update'
data = {
"result": [
{
"product_id": 126543,
"sku": 141589,
"order_qty": 3,
"reason": "The product 'Tyrker Shot - Bland Din Egen Shot' has been selling steadily with an average of 0.4 units per month. Despite low sales in Q1 and Q2, recent trends show an increase in sales. Considering the upcoming festive season and recent sales patterns, restocking 3 units for the next month is recommended."
},
{
"product_id": 217688,
"sku": 191498,
"order_qty": 7,
"reason": "The product 'Super Sur - Real Candy Shot i Patentflaske' has shown a consistent increase in sales with an average of 1.3 units per month. With higher sales in Q3 and Q4 and considering the upcoming holidays, restocking 7 units for the next month is recommended."
},
{
"product_id": 221220,
"sku": 122199,
"order_qty": 4,
"reason": "The product 'Liverpool FC Nøkkelring' has shown stable sales with an average of 0.7 units per month. Recent sales in Q4 indicate an increase in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 217643,
"sku": 42511,
"order_qty": 4,
"reason": "The product 'Morsom T-Rex Kulepenn - Assorterte Farger' has been consistently selling with an average of 0.8 units per month. Recent sales in Q4 show an increase in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 220858,
"sku": 346260,
"order_qty": 3,
"reason": "The product '5 stk Viskelær Formet som Iskrem' has shown steady sales with an average of 0.5 units per month. Recent sales in Q3 and Q4 indicate a slight increase in demand. Restocking 3 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 217949,
"sku": 173746,
"order_qty": 7,
"reason": "The product '8 stk Pappkrus med Gullfolierte Fotavtrykk 270 ml' has been selling well with an average of 1.6 units per month. Recent sales trends show a consistent demand. Restocking 7 units for the next month is recommended based on the current sales pattern."
},
{
"product_id": 218564,
"sku": 42613,
"order_qty": 4,
"reason": "The product '6 stk. 30 cm - Glossy Mirror Sølvfargede Ballonger' has shown a stable sales pattern with an average of 0.9 units per month. Recent sales in Q4 indicate a slight decrease in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 211858,
"sku": 131563,
"order_qty": 13,
"reason": "The product '60 år - 6 stk Svarte, Sølv- og Gullfarget Ballonger 30 cm' has been consistently popular with an average of 2.9 units sold per month. Recent sales trends indicate a steady demand. Restocking 13 units for the next month is recommended based on the current sales pattern and upcoming festive seasons."
},
{
"product_id": 226240,
"sku": 603428,
"order_qty": 2,
"reason": "The product 'Inflatimals Panda - Stor Uppblåsbar Panda på Pinne 1,4 Meter' has shown an increasing trend with an average of 1.3 units sold per month. Recent sales indicate a growing demand. Restocking 2 units for the next month is recommended based on the current sales pattern."
},
{
"product_id": 217958,
"sku": 42607,
"order_qty": 10,
"reason": "The product 'Craze Inkee Enhjørning Badebombe - Søt Popkorn Aroma - Med Overraskelse!' has been very popular with an average of 6.7 units sold per month. Recent sales trends show a consistent high demand. Restocking 10 units for the next month is recommended based on the current sales pattern and upcoming holidays."
}
],
"reset_collection": '1'
}
response = requests.post(url, json=data)
print(response)
if response.status_code < 200 or response.status_code >= 300 :
print('API request failed')
else:
print('API request successful')
if __name__ == '__main__':
call_api()
{
"result": [
{
"product_id": 126543,
"sku": 141589,
"order_qty": 3,
"reason": "The product 'Tyrker Shot - Bland Din Egen Shot' has been selling steadily with an average of 0.4 units per month. Despite low sales in Q1 and Q2, recent trends show an increase in sales. Considering the upcoming festive season and recent sales patterns, restocking 3 units for the next month is recommended."
},
{
"product_id": 217688,
"sku": 191498,
"order_qty": 7,
"reason": "The product 'Super Sur - Real Candy Shot i Patentflaske' has shown a consistent increase in sales with an average of 1.3 units per month. With higher sales in Q3 and Q4 and considering the upcoming holidays, restocking 7 units for the next month is recommended."
},
{
"product_id": 221220,
"sku": 122199,
"order_qty": 4,
"reason": "The product 'Liverpool FC Nøkkelring' has shown stable sales with an average of 0.7 units per month. Recent sales in Q4 indicate an increase in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 217643,
"sku": 42511,
"order_qty": 4,
"reason": "The product 'Morsom T-Rex Kulepenn - Assorterte Farger' has been consistently selling with an average of 0.8 units per month. Recent sales in Q4 show an increase in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 220858,
"sku": 346260,
"order_qty": 3,
"reason": "The product '5 stk Viskelær Formet som Iskrem' has shown steady sales with an average of 0.5 units per month. Recent sales in Q3 and Q4 indicate a slight increase in demand. Restocking 3 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 217949,
"sku": 173746,
"order_qty": 7,
"reason": "The product '8 stk Pappkrus med Gullfolierte Fotavtrykk 270 ml' has been selling well with an average of 1.6 units per month. Recent sales trends show a consistent demand. Restocking 7 units for the next month is recommended based on the current sales pattern."
},
{
"product_id": 218564,
"sku": 42613,
"order_qty": 4,
"reason": "The product '6 stk. 30 cm - Glossy Mirror Sølvfargede Ballonger' has shown a stable sales pattern with an average of 0.9 units per month. Recent sales in Q4 indicate a slight decrease in demand. Restocking 4 units for the next month is recommended based on the current sales trend."
},
{
"product_id": 211858,
"sku": 131563,
"order_qty": 13,
"reason": "The product '60 år - 6 stk Svarte, Sølv- og Gullfarget Ballonger 30 cm' has been consistently popular with an average of 2.9 units sold per month. Recent sales trends indicate a steady demand. Restocking 13 units for the next month is recommended based on the current sales pattern and upcoming festive seasons."
},
{
"product_id": 226240,
"sku": 603428,
"order_qty": 2,
"reason": "The product 'Inflatimals Panda - Stor Uppblåsbar Panda på Pinne 1,4 Meter' has shown an increasing trend with an average of 1.3 units sold per month. Recent sales indicate a growing demand. Restocking 2 units for the next month is recommended based on the current sales pattern."
},
{
"product_id": 217958,
"sku": 42607,
"order_qty": 10,
"reason": "The product 'Craze Inkee Enhjørning Badebombe - Søt Popkorn Aroma - Med Overraskelse!' has been very popular with an average of 6.7 units sold per month. Recent sales trends show a consistent high demand. Restocking 10 units for the next month is recommended based on the current sales pattern and upcoming holidays."
}
]
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment