9.6 KiB
9.6 KiB
MySQL Integration Summary - Prediksi Stok Bahan Kue
✅ What's Done
Backend (Flask) - NEW FILES & UPDATES
New Files Created:
-
database_setup.py- Script untuk initialize MySQL database- Buat database
prediksi_stok_db - Buat 3 tables:
products,transactions,predictions - Insert 8 default products
- Buat database
-
MYSQL_SETUP.md- Comprehensive setup guide- MySQL installation instructions
- Database setup steps (automatic & manual)
- API endpoint documentation
- Troubleshooting guide
Updated Files:
-
requirements.txt- Added MySQL dependenciesflask-sqlalchemy==3.0.0 PyMySQL==1.1.0 -
app.py- Added 6 new endpointsGET /products- Ambil daftar produkGET /products/<id>- Ambil 1 produk by IDPOST /transactions- Simpan transaksiGET /transactions- Ambil history transaksiPOST /predictions- Simpan prediction results- Updated
/infoendpoint
Frontend (Flutter) - UPDATES
Updated Files:
-
lib/services/ml_service.dart- Added 5 new methodsgetProducts()- Get products from APIgetProduct(id)- Get single productsaveTransaction()- Save transaction to MySQLgetTransactions()- Get transaction historysavePrediction()- Save prediction to MySQL
-
lib/screens/transaction_screen.dart- Integrated with API_submitTransaction()now calls API- Added loading state during save
- Show success/error messages
- Data di-save ke MySQL, bukan cuma local
📋 Architecture
┌─────────────────┐
│ FLUTTER APP │
│ (Mobile) │
└────────┬────────┘
│
┌────────▼────────┐
│ Flask API │
│ (Python) │
│ on localhost: │
│ 5000 │
└────────┬────────┘
│
┌────────▼────────┐
│ MySQL Database │
│ localhost:3306 │
└─────────────────┘
Data Flow:
User Input → Flutter → API → MySQL Database
🚀 Quick Start (3 Steps)
Step 1: Install MySQL & Setup Database
# Navigate to ml_model folder
cd c:\fluuter.u\permintaandanprediksi_stok_bahan_kue\finalproject\ml_model
# Install Python dependencies
pip install -r requirements.txt
# Setup MySQL database
python database_setup.py
Expected Output:
✅ Database 'prediksi_stok_db' created successfully
✅ Products table created successfully
✅ Transactions table created successfully
✅ Predictions table created successfully
✅ Inserted 8 default products
✅ Database setup completed successfully!
Step 2: Start Flask API
# From ml_model folder
python app.py
Expected Output:
Starting Prediksi Stok API
Model: RandomForest
Accuracy (R²): 0.9964
Endpoints: /health, /metadata, /info, /prediksi, /batch-prediksi, /products, /transactions, /predictions
Access API at: http://localhost:5000
Step 3: Run Flutter App
# From finalproject folder
flutter run
🔌 API Endpoints
Products
GET /products
Response: {
"status": "success",
"total": 8,
"products": [
{"id": 1, "name": "Tepung Terigu 1kg", "category": "Tepung", "price": 15000, "stock": 45, ...},
...
]
}
GET /products/1
Response: {
"status": "success",
"product": {"id": 1, "name": "Tepung Terigu 1kg", ...}
}
Transactions
POST /transactions
Body: {
"product_name": "Tepung Terigu 1kg",
"category": "Tepung",
"quantity": 5,
"unit_price": 15000,
"total_price": 75000,
"transaction_date": "2024-04-05"
}
Response: {
"status": "success",
"message": "Transaction saved successfully",
"transaction_id": 1
}
GET /transactions?limit=100&offset=0
Response: {
"status": "success",
"total": 10,
"transactions": [...]
}
GET /transactions?product_name=Tepung
Response: {...} // Filtered by product name
Predictions
POST /predictions
Body: {
"product_name": "Tepung Terigu 1kg",
"category": "Tepung",
"unit_price": 15000,
"prediction_date": "2024-04-05",
"predicted_quantity": 45,
"raw_value": 44.8,
"estimated_total_price": 672000,
"accuracy_r2": 0.9964,
"error_mae": 2.51
}
Response: {
"status": "success",
"message": "Prediction saved successfully",
"prediction_id": 1
}
📊 Database Schema
Products Table
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
category VARCHAR(100) NOT NULL,
price INT NOT NULL,
stock INT NOT NULL DEFAULT 0,
status VARCHAR(50) NOT NULL DEFAULT 'tersedia',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Transactions Table
CREATE TABLE transactions (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(255) NOT NULL,
category VARCHAR(100) NOT NULL,
quantity INT NOT NULL,
unit_price INT NOT NULL,
total_price INT NOT NULL,
transaction_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Predictions Table
CREATE TABLE predictions (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(255) NOT NULL,
category VARCHAR(100) NOT NULL,
unit_price INT NOT NULL,
prediction_date DATE NOT NULL,
predicted_quantity INT,
raw_value DOUBLE,
estimated_total_price INT,
accuracy_r2 DOUBLE,
error_mae DOUBLE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
🔄 Flutter Integration Examples
Example 1: Get All Products
List<Map<String, dynamic>> products = await MLService.getProducts();
Example 2: Save Transaction
bool success = await MLService.saveTransaction(
productName: 'Tepung Terigu 1kg',
category: 'Tepung',
quantity: 5,
unitPrice: 15000,
totalPrice: 75000,
transactionDate: '2024-04-05',
);
Example 3: Get Transaction History
List<Map<String, dynamic>> transactions = await MLService.getTransactions(
limit: 50,
offset: 0,
productName: null,
);
Example 4: Save Prediction
bool success = await MLService.savePrediction(
productName: 'Tepung Terigu 1kg',
category: 'Tepung',
unitPrice: 15000,
predictionDate: '2024-04-05',
predictedQuantity: 45,
rawValue: 44.8,
estimatedTotalPrice: 672000,
accuracyR2: 0.9964,
errorMae: 2.51,
);
📝 Files Modified/Created
Backend
ml_model/
├── database_setup.py ✨ NEW
├── MYSQL_SETUP.md ✨ NEW
├── app.py ✏️ UPDATED (added 6 endpoints)
└── requirements.txt ✏️ UPDATED (added MySQL libs)
Frontend
lib/
├── services/
│ └── ml_service.dart ✏️ UPDATED (added 5 methods)
└── screens/
└── transaction_screen.dart ✏️ UPDATED (integrated with API)
✅ Verification Checklist
Before running the app:
- MySQL server installed & running
- Database setup completed (
python database_setup.py) - Flask API running (
python app.py) on port 5000 - Can access
http://localhost:5000/healthin browser - Can access
http://localhost:5000/productsin browser - Flutter app can connect to API
🐛 Common Issues & Solutions
Issue: "Can't connect to MySQL server"
Solution: Check MySQL is running
# Windows: Check Services
# macOS: brew services list
# Linux: sudo systemctl status mysql
Issue: "Database 'prediksi_stok_db' doesn't exist"
Solution: Run the database setup script
python database_setup.py
Issue: "Access denied for user 'root'@'localhost'"
Solution: Update database credentials in app.py and database_setup.py
DB_PASSWORD = 'your_mysql_password'
Issue: "Port 5000 already in use"
Solution: Change port in app.py
app.run(port=5001) # Use different port
🔐 Security Notes
For production use:
- Never use
rootuser without password - Create dedicated database user with restricted privileges
- Use environment variables for credentials
- Enable SSL/TLS for connections
- Implement API authentication (JWT, OAuth)
- Add input validation & SQL injection protection
Example:
# Use environment variables
import os
DB_PASSWORD = os.getenv('DB_PASSWORD', 'default_password')
📚 Next Steps
- ✅ Test all endpoints with Postman
- ✅ Test Transaction Screen with real data
- ✅ Implement Prediction Screen API integration
- ✅ Add local caching for offline support
- ✅ Setup authentication & user management
- ✅ Implement data sync & backup
- ✅ Optimize query performance with indexes
- ✅ Add comprehensive error handling
📞 Support
Lihat dokumentasi lengkap di: ml_model/MYSQL_SETUP.md
Untuk questions atau issues, check logs:
# Flask API logs
python app.py # Check console output
# MySQL logs
# Windows: MySQL Workbench → Administration → Server Logs
# macOS/Linux: /var/log/mysql/error.log
Integration Status: ✅ COMPLETE!
Data flow sekarang:
- Flutter → API → MySQL Database
- Semua transactions & predictions tersimpan di database
- Bisa diakses dari mana saja
- Persistent data untuk analysis