Emmanuel Frimpong Asante
commited on
Commit
·
c514c85
1
Parent(s):
b88d344
"Update system"
Browse filesSigned-off-by: Emmanuel Frimpong Asante <[email protected]>
- .gitignore +2 -0
- app.py +2 -0
- auth/auth_controller.py +10 -16
- auth/auth_routes.py +19 -1
- auth/schemas/user_schema.py +29 -19
- config/db.py +6 -19
- config/settings.py +8 -4
- controllers/data_logging.py +12 -11
- controllers/health_management.py +2 -0
- controllers/inventory.py +2 -0
- controllers/usage_tracking.py +2 -0
- data/logs/daily_logs.py +2 -0
- data/logs/user_usage.py +2 -0
- models/schemas/health_schema.py +2 -0
- models/schemas/inventory_schema.py +3 -0
- models/schemas/log_schema.py +3 -0
- models/schemas/usage_schema.py +3 -0
- requirements.txt +1 -0
- routes/api.py +2 -0
- routes/data_logging_routes.py +3 -0
- routes/health_routes.py +2 -0
- routes/inventory_routes.py +3 -0
- routes/usage_routes.py +2 -0
- services/disease_detection.py +2 -0
- services/gradio_interface.py +3 -0
- services/inventory_manager.py +3 -0
- services/llama_service.py +3 -0
- services/report_generator.py +3 -0
- services/usage_tracker.py +2 -0
.gitignore
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
# created by virtualenv automatically
|
| 2 |
.idea
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 1 |
# created by virtualenv automatically
|
| 2 |
.idea
|
| 3 |
+
.env
|
| 4 |
+
.git
|
| 5 |
|
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tensorflow as tf
|
| 3 |
from pymongo import MongoClient
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import tensorflow as tf
|
| 5 |
from pymongo import MongoClient
|
auth/auth_controller.py
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
import bcrypt
|
| 2 |
import jwt
|
| 3 |
import datetime
|
| 4 |
-
from pymongo import MongoClient
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
from flask import jsonify, request
|
| 7 |
from functools import wraps
|
| 8 |
-
from config.db import
|
| 9 |
-
|
| 10 |
-
# MongoDB setup (this should be moved to config/db.py and imported here)
|
| 11 |
-
client = MongoClient("your_mongo_uri") # Replace with your MongoDB URI
|
| 12 |
-
db = client.poultry_farm # Your database name
|
| 13 |
-
users_collection = db.users # Collection for storing user information
|
| 14 |
-
|
| 15 |
-
# Secret key for JWT (move to config/settings.py and import here)
|
| 16 |
-
SECRET_KEY = "your_secret_key" # Replace with a secure secret key
|
| 17 |
-
|
| 18 |
|
| 19 |
# JWT decorator to protect routes
|
| 20 |
def token_required(f):
|
|
@@ -28,10 +21,11 @@ def token_required(f):
|
|
| 28 |
try:
|
| 29 |
data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
| 30 |
current_user = users_collection.find_one({"_id": ObjectId(data["user_id"])})
|
| 31 |
-
except:
|
|
|
|
|
|
|
| 32 |
return jsonify({'message': 'Token is invalid!'}), 401
|
| 33 |
return f(current_user, *args, **kwargs)
|
| 34 |
-
|
| 35 |
return decorated
|
| 36 |
|
| 37 |
|
|
@@ -46,7 +40,7 @@ def register():
|
|
| 46 |
return jsonify({"message": "User already exists"}), 400
|
| 47 |
|
| 48 |
# Hash the password
|
| 49 |
-
hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'), bcrypt.gensalt())
|
| 50 |
|
| 51 |
# Create user document
|
| 52 |
new_user = {
|
|
@@ -73,7 +67,7 @@ def login():
|
|
| 73 |
return jsonify({"message": "User not found"}), 404
|
| 74 |
|
| 75 |
# Verify password
|
| 76 |
-
if bcrypt.checkpw(data['password'].encode('utf-8'), user['password']):
|
| 77 |
# Create JWT token
|
| 78 |
token = jwt.encode({
|
| 79 |
'user_id': str(user['_id']),
|
|
@@ -85,7 +79,7 @@ def login():
|
|
| 85 |
return jsonify({"message": "Invalid password"}), 401
|
| 86 |
|
| 87 |
|
| 88 |
-
# Get user info (
|
| 89 |
@token_required
|
| 90 |
def get_user_info(current_user):
|
| 91 |
user_info = {
|
|
|
|
| 1 |
+
# auth/auth_controller.py
|
| 2 |
+
|
| 3 |
import bcrypt
|
| 4 |
import jwt
|
| 5 |
import datetime
|
|
|
|
| 6 |
from bson.objectid import ObjectId
|
| 7 |
from flask import jsonify, request
|
| 8 |
from functools import wraps
|
| 9 |
+
from config.db import users_collection # Importing the users collection from the database config
|
| 10 |
+
from config.settings import SECRET_KEY # Import the secret key from settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# JWT decorator to protect routes
|
| 13 |
def token_required(f):
|
|
|
|
| 21 |
try:
|
| 22 |
data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
| 23 |
current_user = users_collection.find_one({"_id": ObjectId(data["user_id"])})
|
| 24 |
+
except jwt.ExpiredSignatureError:
|
| 25 |
+
return jsonify({'message': 'Token has expired!'}), 401
|
| 26 |
+
except jwt.InvalidTokenError:
|
| 27 |
return jsonify({'message': 'Token is invalid!'}), 401
|
| 28 |
return f(current_user, *args, **kwargs)
|
|
|
|
| 29 |
return decorated
|
| 30 |
|
| 31 |
|
|
|
|
| 40 |
return jsonify({"message": "User already exists"}), 400
|
| 41 |
|
| 42 |
# Hash the password
|
| 43 |
+
hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 44 |
|
| 45 |
# Create user document
|
| 46 |
new_user = {
|
|
|
|
| 67 |
return jsonify({"message": "User not found"}), 404
|
| 68 |
|
| 69 |
# Verify password
|
| 70 |
+
if bcrypt.checkpw(data['password'].encode('utf-8'), user['password'].encode('utf-8')):
|
| 71 |
# Create JWT token
|
| 72 |
token = jwt.encode({
|
| 73 |
'user_id': str(user['_id']),
|
|
|
|
| 79 |
return jsonify({"message": "Invalid password"}), 401
|
| 80 |
|
| 81 |
|
| 82 |
+
# Get user info (protected route)
|
| 83 |
@token_required
|
| 84 |
def get_user_info(current_user):
|
| 85 |
user_info = {
|
auth/auth_routes.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint
|
| 2 |
from .auth_controller import register, login, get_user_info, update_user_info, delete_user
|
|
|
|
| 3 |
|
| 4 |
# Create a Blueprint for auth-related routes
|
| 5 |
auth_bp = Blueprint('auth', __name__)
|
| 6 |
|
| 7 |
-
|
| 8 |
# Define the routes
|
|
|
|
| 9 |
@auth_bp.route('/register', methods=['POST'])
|
| 10 |
def register_user():
|
| 11 |
"""
|
|
@@ -16,6 +19,9 @@ def register_user():
|
|
| 16 |
"password": "password123",
|
| 17 |
"role": "user" # Optional, default is "user"
|
| 18 |
}
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
return register()
|
| 21 |
|
|
@@ -29,6 +35,9 @@ def login_user():
|
|
| 29 |
"username": "user1",
|
| 30 |
"password": "password123"
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
return login()
|
| 34 |
|
|
@@ -38,6 +47,9 @@ def get_user_details():
|
|
| 38 |
"""
|
| 39 |
Route for getting the authenticated user's information.
|
| 40 |
Requires the JWT token in the `x-access-token` header.
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
return get_user_info()
|
| 43 |
|
|
@@ -54,6 +66,9 @@ def update_user():
|
|
| 54 |
"role": "manager" # Optional, can be "user" or "manager"
|
| 55 |
}
|
| 56 |
Requires the JWT token in the `x-access-token` header.
|
|
|
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
return update_user_info()
|
| 59 |
|
|
@@ -68,5 +83,8 @@ def remove_user():
|
|
| 68 |
"user_id": "the user's MongoDB ObjectId"
|
| 69 |
}
|
| 70 |
Requires the JWT token in the `x-access-token` header.
|
|
|
|
|
|
|
|
|
|
| 71 |
"""
|
| 72 |
return delete_user()
|
|
|
|
| 1 |
+
# auth/schemas/auth_routes.py
|
| 2 |
+
|
| 3 |
from flask import Blueprint
|
| 4 |
from .auth_controller import register, login, get_user_info, update_user_info, delete_user
|
| 5 |
+
from flask import jsonify
|
| 6 |
|
| 7 |
# Create a Blueprint for auth-related routes
|
| 8 |
auth_bp = Blueprint('auth', __name__)
|
| 9 |
|
|
|
|
| 10 |
# Define the routes
|
| 11 |
+
|
| 12 |
@auth_bp.route('/register', methods=['POST'])
|
| 13 |
def register_user():
|
| 14 |
"""
|
|
|
|
| 19 |
"password": "password123",
|
| 20 |
"role": "user" # Optional, default is "user"
|
| 21 |
}
|
| 22 |
+
Returns:
|
| 23 |
+
- Success message and user ID if successful (201 Created)
|
| 24 |
+
- Error message if user already exists or invalid request (400 Bad Request)
|
| 25 |
"""
|
| 26 |
return register()
|
| 27 |
|
|
|
|
| 35 |
"username": "user1",
|
| 36 |
"password": "password123"
|
| 37 |
}
|
| 38 |
+
Returns:
|
| 39 |
+
- JWT token if login is successful (200 OK)
|
| 40 |
+
- Error message if credentials are incorrect or user not found (401 Unauthorized / 404 Not Found)
|
| 41 |
"""
|
| 42 |
return login()
|
| 43 |
|
|
|
|
| 47 |
"""
|
| 48 |
Route for getting the authenticated user's information.
|
| 49 |
Requires the JWT token in the `x-access-token` header.
|
| 50 |
+
Returns:
|
| 51 |
+
- User information such as username, role, and creation date (200 OK)
|
| 52 |
+
- Error message if the token is missing or invalid (401 Unauthorized)
|
| 53 |
"""
|
| 54 |
return get_user_info()
|
| 55 |
|
|
|
|
| 66 |
"role": "manager" # Optional, can be "user" or "manager"
|
| 67 |
}
|
| 68 |
Requires the JWT token in the `x-access-token` header.
|
| 69 |
+
Returns:
|
| 70 |
+
- Success message if update is successful (200 OK)
|
| 71 |
+
- Error message if the user is not found or permission is denied (403 Forbidden / 404 Not Found)
|
| 72 |
"""
|
| 73 |
return update_user_info()
|
| 74 |
|
|
|
|
| 83 |
"user_id": "the user's MongoDB ObjectId"
|
| 84 |
}
|
| 85 |
Requires the JWT token in the `x-access-token` header.
|
| 86 |
+
Returns:
|
| 87 |
+
- Success message if deletion is successful (200 OK)
|
| 88 |
+
- Error message if the user is not found or permission is denied (403 Forbidden / 404 Not Found)
|
| 89 |
"""
|
| 90 |
return delete_user()
|
auth/schemas/user_schema.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
from bson.objectid import ObjectId
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
db
|
| 7 |
-
|
| 8 |
|
| 9 |
|
| 10 |
# Define the User Schema
|
|
@@ -18,7 +17,7 @@ class User:
|
|
| 18 |
:param role: String, user role (either 'user' or 'manager'), default is 'user'
|
| 19 |
"""
|
| 20 |
self.username = username
|
| 21 |
-
self.password = password
|
| 22 |
self.role = role
|
| 23 |
self.created_at = datetime.datetime.utcnow() # Store account creation time
|
| 24 |
|
|
@@ -29,7 +28,7 @@ class User:
|
|
| 29 |
"""
|
| 30 |
return {
|
| 31 |
"username": self.username,
|
| 32 |
-
"password": self.password, #
|
| 33 |
"role": self.role,
|
| 34 |
"created_at": self.created_at
|
| 35 |
}
|
|
@@ -37,15 +36,27 @@ class User:
|
|
| 37 |
|
| 38 |
# CRUD Operations for User Schema
|
| 39 |
|
| 40 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
Create a new user in the MongoDB collection.
|
| 43 |
|
| 44 |
:param username: String, the username for the user
|
| 45 |
-
:param
|
| 46 |
:param role: String, role of the user (either 'user' or 'manager'), default is 'user'
|
| 47 |
:return: Inserted user's ID (MongoDB ObjectId)
|
| 48 |
"""
|
|
|
|
| 49 |
user = User(username=username, password=hashed_password, role=role)
|
| 50 |
user_id = users_collection.insert_one(user.to_dict()).inserted_id
|
| 51 |
return str(user_id)
|
|
@@ -97,13 +108,12 @@ def delete_user(user_id):
|
|
| 97 |
return result.deleted_count > 0
|
| 98 |
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
sample_username = "sample_user"
|
| 104 |
-
sample_hashed_password = "hashed_password_here" # Use bcrypt to hash passwords
|
| 105 |
-
sample_role = "user"
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# auth/schemas/user_schemas.py
|
|
|
|
| 2 |
|
| 3 |
+
from bson.objectid import ObjectId
|
| 4 |
+
import datetime
|
| 5 |
+
from config.db import users_collection # Import the users collection from config/db
|
| 6 |
+
import bcrypt
|
| 7 |
|
| 8 |
|
| 9 |
# Define the User Schema
|
|
|
|
| 17 |
:param role: String, user role (either 'user' or 'manager'), default is 'user'
|
| 18 |
"""
|
| 19 |
self.username = username
|
| 20 |
+
self.password = password # Password should be hashed
|
| 21 |
self.role = role
|
| 22 |
self.created_at = datetime.datetime.utcnow() # Store account creation time
|
| 23 |
|
|
|
|
| 28 |
"""
|
| 29 |
return {
|
| 30 |
"username": self.username,
|
| 31 |
+
"password": self.password, # Ensure the password is hashed
|
| 32 |
"role": self.role,
|
| 33 |
"created_at": self.created_at
|
| 34 |
}
|
|
|
|
| 36 |
|
| 37 |
# CRUD Operations for User Schema
|
| 38 |
|
| 39 |
+
def hash_password(password):
|
| 40 |
+
"""
|
| 41 |
+
Hashes a plain-text password using bcrypt.
|
| 42 |
+
|
| 43 |
+
:param password: String, plain-text password
|
| 44 |
+
:return: Hashed password
|
| 45 |
+
"""
|
| 46 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 47 |
+
return hashed_password.decode('utf-8')
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def create_user(username, plain_password, role="user"):
|
| 51 |
"""
|
| 52 |
Create a new user in the MongoDB collection.
|
| 53 |
|
| 54 |
:param username: String, the username for the user
|
| 55 |
+
:param plain_password: String, the plain-text password of the user (will be hashed)
|
| 56 |
:param role: String, role of the user (either 'user' or 'manager'), default is 'user'
|
| 57 |
:return: Inserted user's ID (MongoDB ObjectId)
|
| 58 |
"""
|
| 59 |
+
hashed_password = hash_password(plain_password)
|
| 60 |
user = User(username=username, password=hashed_password, role=role)
|
| 61 |
user_id = users_collection.insert_one(user.to_dict()).inserted_id
|
| 62 |
return str(user_id)
|
|
|
|
| 108 |
return result.deleted_count > 0
|
| 109 |
|
| 110 |
|
| 111 |
+
def check_password(plain_password, hashed_password):
|
| 112 |
+
"""
|
| 113 |
+
Check if a plain-text password matches a hashed password.
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
:param plain_password: String, plain-text password
|
| 116 |
+
:param hashed_password: String, hashed password stored in the database
|
| 117 |
+
:return: Boolean, True if passwords match, False otherwise
|
| 118 |
+
"""
|
| 119 |
+
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
config/db.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
-
# Load environment variables (if using environment files like .env)
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# MongoDB URI (retrieved from environment variables)
|
| 11 |
-
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017") #
|
| 12 |
-
DB_NAME = os.getenv("DB_NAME", "poultry_farm") #
|
| 13 |
|
| 14 |
# Initialize MongoDB client
|
| 15 |
client = MongoClient(MONGO_URI)
|
|
@@ -31,16 +31,3 @@ try:
|
|
| 31 |
print(f"Connected to MongoDB database: {DB_NAME}")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Error connecting to MongoDB: {e}")
|
| 34 |
-
|
| 35 |
-
# Example usage of collections
|
| 36 |
-
if __name__ == "__main__":
|
| 37 |
-
# Insert a test document into the users collection
|
| 38 |
-
test_user = {
|
| 39 |
-
"username": "admin",
|
| 40 |
-
"password": "hashed_password", # In practice, you would hash the password using bcrypt
|
| 41 |
-
"role": "manager",
|
| 42 |
-
"created_at": datetime.datetime.utcnow()
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
result = users_collection.insert_one(test_user)
|
| 46 |
-
print(f"Inserted test user with ID: {result.inserted_id}")
|
|
|
|
| 1 |
+
# config/db.py
|
| 2 |
+
|
| 3 |
from pymongo import MongoClient
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load environment variables from a .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
# MongoDB URI and Database name (retrieved from environment variables)
|
| 11 |
+
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017") # Fallback to local MongoDB if not set
|
| 12 |
+
DB_NAME = os.getenv("DB_NAME", "poultry_farm") # Fallback database name if not set
|
| 13 |
|
| 14 |
# Initialize MongoDB client
|
| 15 |
client = MongoClient(MONGO_URI)
|
|
|
|
| 31 |
print(f"Connected to MongoDB database: {DB_NAME}")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Error connecting to MongoDB: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config/settings.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
|
| 4 |
# Load environment variables from .env file
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
-
# Secret
|
| 8 |
-
SECRET_KEY = os.getenv("SECRET_KEY", "
|
| 9 |
|
| 10 |
# MongoDB connection settings
|
| 11 |
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017") # Default to local MongoDB if not set
|
|
@@ -22,7 +24,7 @@ EMAIL_USE_TLS = bool(os.getenv("EMAIL_USE_TLS", True))
|
|
| 22 |
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER", "[email protected]")
|
| 23 |
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "your_email_password")
|
| 24 |
|
| 25 |
-
# Logging settings
|
| 26 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO") # Log level: DEBUG, INFO, WARNING, ERROR, CRITICAL
|
| 27 |
|
| 28 |
# Example external APIs (if required for additional services)
|
|
@@ -31,5 +33,7 @@ EXTERNAL_API_KEY = os.getenv("EXTERNAL_API_KEY", "your_default_api_key")
|
|
| 31 |
# Path settings (if needed for file uploads or storage)
|
| 32 |
UPLOAD_FOLDER = os.getenv("UPLOAD_FOLDER", "/path/to/uploads")
|
| 33 |
|
| 34 |
-
#
|
| 35 |
PAGINATION_LIMIT = int(os.getenv("PAGINATION_LIMIT", 10)) # Default pagination limit for listing endpoints
|
|
|
|
|
|
|
|
|
| 1 |
+
# config/settings.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# Load environment variables from .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Secret key for JWT encoding/decoding
|
| 10 |
+
SECRET_KEY = os.getenv("SECRET_KEY", "your_secret_key") # Use an environment variable for security
|
| 11 |
|
| 12 |
# MongoDB connection settings
|
| 13 |
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017") # Default to local MongoDB if not set
|
|
|
|
| 24 |
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER", "[email protected]")
|
| 25 |
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "your_email_password")
|
| 26 |
|
| 27 |
+
# Logging settings
|
| 28 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO") # Log level: DEBUG, INFO, WARNING, ERROR, CRITICAL
|
| 29 |
|
| 30 |
# Example external APIs (if required for additional services)
|
|
|
|
| 33 |
# Path settings (if needed for file uploads or storage)
|
| 34 |
UPLOAD_FOLDER = os.getenv("UPLOAD_FOLDER", "/path/to/uploads")
|
| 35 |
|
| 36 |
+
# Global pagination settings for APIs
|
| 37 |
PAGINATION_LIMIT = int(os.getenv("PAGINATION_LIMIT", 10)) # Default pagination limit for listing endpoints
|
| 38 |
+
|
| 39 |
+
# Other potential global settings can be added here as needed
|
controllers/data_logging.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import request, jsonify
|
| 2 |
from models.schemas.log_schema import create_log
|
| 3 |
from models.schemas.health_schema import create_health_record
|
| 4 |
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item
|
| 5 |
from models.schemas.usage_schema import create_usage_record
|
| 6 |
-
import datetime
|
| 7 |
|
| 8 |
|
| 9 |
-
# Log
|
| 10 |
def log_user_action(user_id, message, level="INFO", metadata=None):
|
| 11 |
"""
|
| 12 |
Logs a general user action.
|
|
@@ -18,14 +19,14 @@ def log_user_action(user_id, message, level="INFO", metadata=None):
|
|
| 18 |
:return: JSON response indicating success or failure.
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
-
ip_address = request.remote_addr # Get the IP address from the request
|
| 22 |
log_id = create_log(level, message, metadata, ip_address, user_id)
|
| 23 |
return jsonify({"message": "Action logged successfully", "log_id": log_id}), 201
|
| 24 |
except Exception as e:
|
| 25 |
return jsonify({"error": str(e)}), 500
|
| 26 |
|
| 27 |
|
| 28 |
-
# Log a health record for a poultry
|
| 29 |
def log_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path=None):
|
| 30 |
"""
|
| 31 |
Logs a health record for a poultry bird.
|
|
@@ -40,7 +41,7 @@ def log_health_record(user_id, poultry_id, disease_name, status, recommendation,
|
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
record_id = create_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path)
|
| 43 |
-
# Log the action
|
| 44 |
log_message = f"Health record created for poultry ID {poultry_id} - Disease: {disease_name}"
|
| 45 |
log_user_action(user_id, log_message, "INFO")
|
| 46 |
return jsonify({"message": "Health record logged successfully", "record_id": record_id}), 201
|
|
@@ -48,7 +49,7 @@ def log_health_record(user_id, poultry_id, disease_name, status, recommendation,
|
|
| 48 |
return jsonify({"error": str(e)}), 500
|
| 49 |
|
| 50 |
|
| 51 |
-
# Log an inventory
|
| 52 |
def log_inventory_addition(user_id, item_name, category, quantity, unit, description=None):
|
| 53 |
"""
|
| 54 |
Logs an inventory addition (e.g., adding feed, medicine).
|
|
@@ -63,7 +64,7 @@ def log_inventory_addition(user_id, item_name, category, quantity, unit, descrip
|
|
| 63 |
"""
|
| 64 |
try:
|
| 65 |
item_id = create_inventory_item(item_name, category, quantity, unit, user_id, description=description)
|
| 66 |
-
# Log the action
|
| 67 |
log_message = f"Inventory item added: {item_name}, Quantity: {quantity} {unit}"
|
| 68 |
log_user_action(user_id, log_message, "INFO")
|
| 69 |
return jsonify({"message": "Inventory item logged successfully", "item_id": item_id}), 201
|
|
@@ -71,7 +72,7 @@ def log_inventory_addition(user_id, item_name, category, quantity, unit, descrip
|
|
| 71 |
return jsonify({"error": str(e)}), 500
|
| 72 |
|
| 73 |
|
| 74 |
-
# Log an inventory
|
| 75 |
def log_inventory_update(user_id, item_id, updated_fields):
|
| 76 |
"""
|
| 77 |
Logs an update to an inventory item (e.g., when quantity is used).
|
|
@@ -84,7 +85,7 @@ def log_inventory_update(user_id, item_id, updated_fields):
|
|
| 84 |
try:
|
| 85 |
update_success = update_inventory_item(item_id, updated_fields)
|
| 86 |
if update_success:
|
| 87 |
-
# Log the action
|
| 88 |
log_message = f"Inventory item updated: {item_id} - {updated_fields}"
|
| 89 |
log_user_action(user_id, log_message, "INFO")
|
| 90 |
return jsonify({"message": "Inventory item updated successfully"}), 200
|
|
@@ -94,7 +95,7 @@ def log_inventory_update(user_id, item_id, updated_fields):
|
|
| 94 |
return jsonify({"error": str(e)}), 500
|
| 95 |
|
| 96 |
|
| 97 |
-
# Log assistant usage
|
| 98 |
def log_assistant_usage(user_id, manager_id, interaction_type, metadata=None):
|
| 99 |
"""
|
| 100 |
Logs the usage of the assistant by a user.
|
|
@@ -107,7 +108,7 @@ def log_assistant_usage(user_id, manager_id, interaction_type, metadata=None):
|
|
| 107 |
"""
|
| 108 |
try:
|
| 109 |
usage_record_id = create_usage_record(user_id, manager_id, interaction_type, metadata)
|
| 110 |
-
# Log the action
|
| 111 |
log_message = f"Assistant interaction logged: {interaction_type} by user {user_id}"
|
| 112 |
log_user_action(user_id, log_message, "INFO", metadata)
|
| 113 |
return jsonify({"message": "Assistant usage logged successfully", "usage_record_id": usage_record_id}), 201
|
|
|
|
| 1 |
+
# controllers/data_logging.py
|
| 2 |
+
|
| 3 |
from flask import request, jsonify
|
| 4 |
from models.schemas.log_schema import create_log
|
| 5 |
from models.schemas.health_schema import create_health_record
|
| 6 |
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item
|
| 7 |
from models.schemas.usage_schema import create_usage_record
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
+
# Log a general user action (e.g., a user performs an action in the system)
|
| 11 |
def log_user_action(user_id, message, level="INFO", metadata=None):
|
| 12 |
"""
|
| 13 |
Logs a general user action.
|
|
|
|
| 19 |
:return: JSON response indicating success or failure.
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
+
ip_address = request.remote_addr # Get the user's IP address from the request
|
| 23 |
log_id = create_log(level, message, metadata, ip_address, user_id)
|
| 24 |
return jsonify({"message": "Action logged successfully", "log_id": log_id}), 201
|
| 25 |
except Exception as e:
|
| 26 |
return jsonify({"error": str(e)}), 500
|
| 27 |
|
| 28 |
|
| 29 |
+
# Log a health record for a poultry bird
|
| 30 |
def log_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path=None):
|
| 31 |
"""
|
| 32 |
Logs a health record for a poultry bird.
|
|
|
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
record_id = create_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path)
|
| 44 |
+
# Log the health record creation action
|
| 45 |
log_message = f"Health record created for poultry ID {poultry_id} - Disease: {disease_name}"
|
| 46 |
log_user_action(user_id, log_message, "INFO")
|
| 47 |
return jsonify({"message": "Health record logged successfully", "record_id": record_id}), 201
|
|
|
|
| 49 |
return jsonify({"error": str(e)}), 500
|
| 50 |
|
| 51 |
|
| 52 |
+
# Log an addition to the inventory
|
| 53 |
def log_inventory_addition(user_id, item_name, category, quantity, unit, description=None):
|
| 54 |
"""
|
| 55 |
Logs an inventory addition (e.g., adding feed, medicine).
|
|
|
|
| 64 |
"""
|
| 65 |
try:
|
| 66 |
item_id = create_inventory_item(item_name, category, quantity, unit, user_id, description=description)
|
| 67 |
+
# Log the inventory addition action
|
| 68 |
log_message = f"Inventory item added: {item_name}, Quantity: {quantity} {unit}"
|
| 69 |
log_user_action(user_id, log_message, "INFO")
|
| 70 |
return jsonify({"message": "Inventory item logged successfully", "item_id": item_id}), 201
|
|
|
|
| 72 |
return jsonify({"error": str(e)}), 500
|
| 73 |
|
| 74 |
|
| 75 |
+
# Log updates to an inventory item (e.g., usage or quantity adjustment)
|
| 76 |
def log_inventory_update(user_id, item_id, updated_fields):
|
| 77 |
"""
|
| 78 |
Logs an update to an inventory item (e.g., when quantity is used).
|
|
|
|
| 85 |
try:
|
| 86 |
update_success = update_inventory_item(item_id, updated_fields)
|
| 87 |
if update_success:
|
| 88 |
+
# Log the inventory update action
|
| 89 |
log_message = f"Inventory item updated: {item_id} - {updated_fields}"
|
| 90 |
log_user_action(user_id, log_message, "INFO")
|
| 91 |
return jsonify({"message": "Inventory item updated successfully"}), 200
|
|
|
|
| 95 |
return jsonify({"error": str(e)}), 500
|
| 96 |
|
| 97 |
|
| 98 |
+
# Log assistant usage by a user
|
| 99 |
def log_assistant_usage(user_id, manager_id, interaction_type, metadata=None):
|
| 100 |
"""
|
| 101 |
Logs the usage of the assistant by a user.
|
|
|
|
| 108 |
"""
|
| 109 |
try:
|
| 110 |
usage_record_id = create_usage_record(user_id, manager_id, interaction_type, metadata)
|
| 111 |
+
# Log the assistant usage action
|
| 112 |
log_message = f"Assistant interaction logged: {interaction_type} by user {user_id}"
|
| 113 |
log_user_action(user_id, log_message, "INFO", metadata)
|
| 114 |
return jsonify({"message": "Assistant usage logged successfully", "usage_record_id": usage_record_id}), 201
|
controllers/health_management.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import request, jsonify
|
| 2 |
from models.schemas.health_schema import create_health_record, get_health_record_by_id, get_health_records_by_user
|
| 3 |
from services.disease_detection import detect_disease
|
|
|
|
| 1 |
+
# controllers/health_management.py
|
| 2 |
+
|
| 3 |
from flask import request, jsonify
|
| 4 |
from models.schemas.health_schema import create_health_record, get_health_record_by_id, get_health_records_by_user
|
| 5 |
from services.disease_detection import detect_disease
|
controllers/inventory.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import request, jsonify
|
| 2 |
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item, get_inventory_item_by_id, \
|
| 3 |
get_all_inventory_items
|
|
|
|
| 1 |
+
# controllers/inventory.py
|
| 2 |
+
|
| 3 |
from flask import request, jsonify
|
| 4 |
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item, get_inventory_item_by_id, \
|
| 5 |
get_all_inventory_items
|
controllers/usage_tracking.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import request, jsonify
|
| 2 |
from models.schemas.usage_schema import create_usage_record, get_usage_by_user, get_usage_by_manager, \
|
| 3 |
get_usage_by_interaction_type
|
|
|
|
| 1 |
+
# controllers/usage_tracking.py
|
| 2 |
+
|
| 3 |
from flask import request, jsonify
|
| 4 |
from models.schemas.usage_schema import create_usage_record, get_usage_by_user, get_usage_by_manager, \
|
| 5 |
get_usage_by_interaction_type
|
data/logs/daily_logs.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# data/logs/daily_logs.py
|
| 2 |
+
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from bson.objectid import ObjectId
|
| 5 |
import datetime
|
data/logs/user_usage.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# data/logs/user_usage.py
|
| 2 |
+
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from bson.objectid import ObjectId
|
| 5 |
import datetime
|
models/schemas/health_schema.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# models/schemas/health_schema.py
|
| 2 |
+
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from bson.objectid import ObjectId
|
| 5 |
import datetime
|
models/schemas/inventory_schema.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# models/schemas/inventory_schema.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
import datetime
|
models/schemas/log_schema.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient, IndexModel, ASCENDING
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# models/schemas/log_schema.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from pymongo import MongoClient, IndexModel, ASCENDING
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
import datetime
|
models/schemas/usage_schema.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# models/schemas/usage_schema.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
import datetime
|
requirements.txt
CHANGED
|
@@ -16,6 +16,7 @@ opencv-python
|
|
| 16 |
flask
|
| 17 |
pymongo
|
| 18 |
bson
|
|
|
|
| 19 |
|
| 20 |
# Gradio Interface for the Chatbot
|
| 21 |
gradio
|
|
|
|
| 16 |
flask
|
| 17 |
pymongo
|
| 18 |
bson
|
| 19 |
+
bcrypt
|
| 20 |
|
| 21 |
# Gradio Interface for the Chatbot
|
| 22 |
gradio
|
routes/api.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint, jsonify, request
|
| 2 |
from controllers.health_management import detect_and_log_disease, get_health_record, get_health_records_for_user
|
| 3 |
from controllers.inventory import add_inventory_item, update_inventory_item_by_id, get_inventory_by_id, \
|
|
|
|
| 1 |
+
# routes/api.py
|
| 2 |
+
|
| 3 |
from flask import Blueprint, jsonify, request
|
| 4 |
from controllers.health_management import detect_and_log_disease, get_health_record, get_health_records_for_user
|
| 5 |
from controllers.inventory import add_inventory_item, update_inventory_item_by_id, get_inventory_by_id, \
|
routes/data_logging_routes.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint, request, jsonify
|
| 2 |
from controllers.data_logging import log_user_action, log_health_record, log_inventory_addition, log_inventory_update, \
|
| 3 |
log_assistant_usage
|
|
|
|
| 1 |
+
# routes/data_logging_routes.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from flask import Blueprint, request, jsonify
|
| 5 |
from controllers.data_logging import log_user_action, log_health_record, log_inventory_addition, log_inventory_update, \
|
| 6 |
log_assistant_usage
|
routes/health_routes.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint, request, jsonify
|
| 2 |
from controllers.health_management import detect_and_log_disease, get_health_record, get_health_records_for_user
|
| 3 |
|
|
|
|
| 1 |
+
# routes/health_routes.py
|
| 2 |
+
|
| 3 |
from flask import Blueprint, request, jsonify
|
| 4 |
from controllers.health_management import detect_and_log_disease, get_health_record, get_health_records_for_user
|
| 5 |
|
routes/inventory_routes.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint, request, jsonify
|
| 2 |
from controllers.inventory import add_inventory_item, update_inventory_item_by_id, get_inventory_by_id, \
|
| 3 |
get_all_inventory
|
|
|
|
| 1 |
+
# routes/inventory_routes.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from flask import Blueprint, request, jsonify
|
| 5 |
from controllers.inventory import add_inventory_item, update_inventory_item_by_id, get_inventory_by_id, \
|
| 6 |
get_all_inventory
|
routes/usage_routes.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import Blueprint, request, jsonify
|
| 2 |
from controllers.usage_tracking import log_usage, get_usage_for_user, get_usage_for_manager, get_usage_by_type
|
| 3 |
|
|
|
|
| 1 |
+
# routes/usage_routes.py
|
| 2 |
+
|
| 3 |
from flask import Blueprint, request, jsonify
|
| 4 |
from controllers.usage_tracking import log_usage, get_usage_for_user, get_usage_for_manager, get_usage_by_type
|
| 5 |
|
services/disease_detection.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from keras.models import load_model
|
|
|
|
| 1 |
+
# services/disease_detection.py
|
| 2 |
+
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
from keras.models import load_model
|
services/gradio_interface.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from services.disease_detection import PoultryFarmBot
|
| 3 |
from services.llama_service import llama2_response
|
|
|
|
| 1 |
+
# services/gradio_interface.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
from services.disease_detection import PoultryFarmBot
|
| 6 |
from services.llama_service import llama2_response
|
services/inventory_manager.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# services/inventory_manager.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
import datetime
|
services/llama_service.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
|
| 3 |
# Load Llama 3.2 model and tokenizer for text generation
|
|
|
|
| 1 |
+
# services/llama_service.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
# Load Llama 3.2 model and tokenizer for text generation
|
services/report_generator.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import pandas as pd
|
|
|
|
| 1 |
+
# services/report_generator.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
import pandas as pd
|
services/usage_tracker.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from bson.objectid import ObjectId
|
| 3 |
import datetime
|
|
|
|
| 1 |
+
# services/usage_tracker.py
|
| 2 |
+
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from bson.objectid import ObjectId
|
| 5 |
import datetime
|