Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,127 +1,127 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FastAPI server for Bitcoin mining dashboard
|
| 3 |
-
"""
|
| 4 |
-
from fastapi import FastAPI, HTTPException
|
| 5 |
-
from fastapi.staticfiles import StaticFiles
|
| 6 |
-
from fastapi.responses import FileResponse
|
| 7 |
-
import uvicorn
|
| 8 |
-
from parallel_miner_v3 import ParallelMiner
|
| 9 |
-
import threading
|
| 10 |
-
from typing import Dict, Optional
|
| 11 |
-
import logging
|
| 12 |
-
|
| 13 |
-
# Configure logging
|
| 14 |
-
logging.basicConfig(
|
| 15 |
-
level=logging.INFO,
|
| 16 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
app = FastAPI(title="Bitcoin Mining Dashboard")
|
| 20 |
-
|
| 21 |
-
# Mount static files
|
| 22 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 23 |
-
app.mount("/css", StaticFiles(directory="static/css"), name="css")
|
| 24 |
-
app.mount("/js", StaticFiles(directory="static/js"), name="js")
|
| 25 |
-
|
| 26 |
-
# Global state
|
| 27 |
-
miner_instance: Optional[ParallelMiner] = None
|
| 28 |
-
mining_thread: Optional[threading.Thread] = None
|
| 29 |
-
is_mining: bool = False
|
| 30 |
-
|
| 31 |
-
@app.get("/")
|
| 32 |
-
async def get_index():
|
| 33 |
-
"""Serve the dashboard HTML"""
|
| 34 |
-
return FileResponse("static/index.html")
|
| 35 |
-
|
| 36 |
-
@app.post("/start_mining")
|
| 37 |
-
async def start_mining():
|
| 38 |
-
"""Start the mining process"""
|
| 39 |
-
global miner_instance, mining_thread, is_mining
|
| 40 |
-
|
| 41 |
-
if is_mining:
|
| 42 |
-
raise HTTPException(status_code=400, detail="Mining is already running")
|
| 43 |
-
|
| 44 |
-
try:
|
| 45 |
-
miner_instance = ParallelMiner(num_cores=5)
|
| 46 |
-
miner_instance.mining = True
|
| 47 |
-
is_mining = True
|
| 48 |
-
|
| 49 |
-
# Start mining in background thread
|
| 50 |
-
mining_thread = threading.Thread(
|
| 51 |
-
target=miner_instance.start_mining,
|
| 52 |
-
kwargs={"duration": None}
|
| 53 |
-
)
|
| 54 |
-
mining_thread.daemon = True
|
| 55 |
-
mining_thread.start()
|
| 56 |
-
|
| 57 |
-
return {"message": "Mining started successfully"}
|
| 58 |
-
except Exception as e:
|
| 59 |
-
logging.error(f"Error starting mining: {e}")
|
| 60 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
-
|
| 62 |
-
@app.post("/stop_mining")
|
| 63 |
-
async def stop_mining():
|
| 64 |
-
"""Stop the mining process"""
|
| 65 |
-
global miner_instance, is_mining
|
| 66 |
-
|
| 67 |
-
if not is_mining:
|
| 68 |
-
raise HTTPException(status_code=400, detail="Mining is not running")
|
| 69 |
-
|
| 70 |
-
try:
|
| 71 |
-
if miner_instance:
|
| 72 |
-
# Log final stats
|
| 73 |
-
logging.info("\n=== Final Mining Statistics ===")
|
| 74 |
-
grand_total = 0
|
| 75 |
-
for core_idx, core in enumerate(miner_instance.cores):
|
| 76 |
-
core_total = core.total_hashes
|
| 77 |
-
grand_total += core_total
|
| 78 |
-
logging.info(f"Core {core_idx}: {core_total:,} hashes")
|
| 79 |
-
logging.info(f"Grand Total: {grand_total:,} hashes")
|
| 80 |
-
logging.info(f"Overall Hashrate: {miner_instance.current_hashrate/1000:.2f} KH/s")
|
| 81 |
-
logging.info(f"Blocks Found: {miner_instance.blocks_found}")
|
| 82 |
-
logging.info("============================\n")
|
| 83 |
-
|
| 84 |
-
miner_instance.mining = False
|
| 85 |
-
is_mining = False
|
| 86 |
-
return {"message": "Mining stopped successfully"}
|
| 87 |
-
|
| 88 |
-
raise HTTPException(status_code=400, detail="No active mining instance")
|
| 89 |
-
except Exception as e:
|
| 90 |
-
logging.error(f"Error stopping mining: {e}")
|
| 91 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
-
|
| 93 |
-
@app.get("/get_stats")
|
| 94 |
-
async def get_mining_stats():
|
| 95 |
-
"""Get current mining statistics"""
|
| 96 |
-
global miner_instance, is_mining
|
| 97 |
-
|
| 98 |
-
if not miner_instance or not is_mining:
|
| 99 |
-
return {
|
| 100 |
-
"status": "Stopped",
|
| 101 |
-
"hashrate": "0 H/s",
|
| 102 |
-
"total_hashes": "0",
|
| 103 |
-
"blocks_found": "0",
|
| 104 |
-
"best_hash": "None",
|
| 105 |
-
"difficulty": "0",
|
| 106 |
-
"block_alert": "Mining stopped"
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
# Create block alert message
|
| 110 |
-
if miner_instance.blocks_found > 0:
|
| 111 |
-
block_alert = f"🎉 FOUND {miner_instance.blocks_found} BLOCK(S)! Last block hash: {miner_instance.best_hash.hex() if miner_instance.best_hash else 'None'}"
|
| 112 |
-
else:
|
| 113 |
-
progress = miner_instance.best_hash_difficulty * 100 if miner_instance.best_hash_difficulty else 0
|
| 114 |
-
block_alert = f"Mining in progress... Best hash difficulty: {progress:.8f}%"
|
| 115 |
-
|
| 116 |
-
return {
|
| 117 |
-
"status": "Running" if is_mining else "Stopped",
|
| 118 |
-
"hashrate": f"{miner_instance.current_hashrate/1000:.2f} KH/s",
|
| 119 |
-
"total_hashes": f"{miner_instance.total_hashes:,}",
|
| 120 |
-
"blocks_found": str(miner_instance.blocks_found),
|
| 121 |
-
"best_hash": miner_instance.best_hash.hex() if miner_instance.best_hash else "None",
|
| 122 |
-
"difficulty": f"{miner_instance.best_hash_difficulty:,}",
|
| 123 |
-
"block_alert": block_alert
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
uvicorn.run("
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FastAPI server for Bitcoin mining dashboard
|
| 3 |
+
"""
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
import uvicorn
|
| 8 |
+
from parallel_miner_v3 import ParallelMiner
|
| 9 |
+
import threading
|
| 10 |
+
from typing import Dict, Optional
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
level=logging.INFO,
|
| 16 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
app = FastAPI(title="Bitcoin Mining Dashboard")
|
| 20 |
+
|
| 21 |
+
# Mount static files
|
| 22 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 23 |
+
app.mount("/css", StaticFiles(directory="static/css"), name="css")
|
| 24 |
+
app.mount("/js", StaticFiles(directory="static/js"), name="js")
|
| 25 |
+
|
| 26 |
+
# Global state
|
| 27 |
+
miner_instance: Optional[ParallelMiner] = None
|
| 28 |
+
mining_thread: Optional[threading.Thread] = None
|
| 29 |
+
is_mining: bool = False
|
| 30 |
+
|
| 31 |
+
@app.get("/")
|
| 32 |
+
async def get_index():
|
| 33 |
+
"""Serve the dashboard HTML"""
|
| 34 |
+
return FileResponse("static/index.html")
|
| 35 |
+
|
| 36 |
+
@app.post("/start_mining")
|
| 37 |
+
async def start_mining():
|
| 38 |
+
"""Start the mining process"""
|
| 39 |
+
global miner_instance, mining_thread, is_mining
|
| 40 |
+
|
| 41 |
+
if is_mining:
|
| 42 |
+
raise HTTPException(status_code=400, detail="Mining is already running")
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
miner_instance = ParallelMiner(num_cores=5)
|
| 46 |
+
miner_instance.mining = True
|
| 47 |
+
is_mining = True
|
| 48 |
+
|
| 49 |
+
# Start mining in background thread
|
| 50 |
+
mining_thread = threading.Thread(
|
| 51 |
+
target=miner_instance.start_mining,
|
| 52 |
+
kwargs={"duration": None}
|
| 53 |
+
)
|
| 54 |
+
mining_thread.daemon = True
|
| 55 |
+
mining_thread.start()
|
| 56 |
+
|
| 57 |
+
return {"message": "Mining started successfully"}
|
| 58 |
+
except Exception as e:
|
| 59 |
+
logging.error(f"Error starting mining: {e}")
|
| 60 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
+
|
| 62 |
+
@app.post("/stop_mining")
|
| 63 |
+
async def stop_mining():
|
| 64 |
+
"""Stop the mining process"""
|
| 65 |
+
global miner_instance, is_mining
|
| 66 |
+
|
| 67 |
+
if not is_mining:
|
| 68 |
+
raise HTTPException(status_code=400, detail="Mining is not running")
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
if miner_instance:
|
| 72 |
+
# Log final stats
|
| 73 |
+
logging.info("\n=== Final Mining Statistics ===")
|
| 74 |
+
grand_total = 0
|
| 75 |
+
for core_idx, core in enumerate(miner_instance.cores):
|
| 76 |
+
core_total = core.total_hashes
|
| 77 |
+
grand_total += core_total
|
| 78 |
+
logging.info(f"Core {core_idx}: {core_total:,} hashes")
|
| 79 |
+
logging.info(f"Grand Total: {grand_total:,} hashes")
|
| 80 |
+
logging.info(f"Overall Hashrate: {miner_instance.current_hashrate/1000:.2f} KH/s")
|
| 81 |
+
logging.info(f"Blocks Found: {miner_instance.blocks_found}")
|
| 82 |
+
logging.info("============================\n")
|
| 83 |
+
|
| 84 |
+
miner_instance.mining = False
|
| 85 |
+
is_mining = False
|
| 86 |
+
return {"message": "Mining stopped successfully"}
|
| 87 |
+
|
| 88 |
+
raise HTTPException(status_code=400, detail="No active mining instance")
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logging.error(f"Error stopping mining: {e}")
|
| 91 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
+
|
| 93 |
+
@app.get("/get_stats")
|
| 94 |
+
async def get_mining_stats():
|
| 95 |
+
"""Get current mining statistics"""
|
| 96 |
+
global miner_instance, is_mining
|
| 97 |
+
|
| 98 |
+
if not miner_instance or not is_mining:
|
| 99 |
+
return {
|
| 100 |
+
"status": "Stopped",
|
| 101 |
+
"hashrate": "0 H/s",
|
| 102 |
+
"total_hashes": "0",
|
| 103 |
+
"blocks_found": "0",
|
| 104 |
+
"best_hash": "None",
|
| 105 |
+
"difficulty": "0",
|
| 106 |
+
"block_alert": "Mining stopped"
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# Create block alert message
|
| 110 |
+
if miner_instance.blocks_found > 0:
|
| 111 |
+
block_alert = f"🎉 FOUND {miner_instance.blocks_found} BLOCK(S)! Last block hash: {miner_instance.best_hash.hex() if miner_instance.best_hash else 'None'}"
|
| 112 |
+
else:
|
| 113 |
+
progress = miner_instance.best_hash_difficulty * 100 if miner_instance.best_hash_difficulty else 0
|
| 114 |
+
block_alert = f"Mining in progress... Best hash difficulty: {progress:.8f}%"
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
"status": "Running" if is_mining else "Stopped",
|
| 118 |
+
"hashrate": f"{miner_instance.current_hashrate/1000:.2f} KH/s",
|
| 119 |
+
"total_hashes": f"{miner_instance.total_hashes:,}",
|
| 120 |
+
"blocks_found": str(miner_instance.blocks_found),
|
| 121 |
+
"best_hash": miner_instance.best_hash.hex() if miner_instance.best_hash else "None",
|
| 122 |
+
"difficulty": f"{miner_instance.best_hash_difficulty:,}",
|
| 123 |
+
"block_alert": block_alert
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7868, reload=True)
|