first commit

This commit is contained in:
2026-01-16 22:20:18 +03:00
commit 5d437e5e28
56 changed files with 4463 additions and 0 deletions

3
config/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
from .celery import app as celery_app
__all__ = ('celery_app',)

16
config/asgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
ASGI config for config project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/6.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_asgi_application()

68
config/celery.py Normal file
View File

@@ -0,0 +1,68 @@
import os
from celery import Celery
from celery.schedules import crontab
# Set the default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
app = Celery('bitcoin_monitor')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django apps
app.autodiscover_tasks()
# Configure periodic tasks
app.conf.beat_schedule = {
'fetch-bitcoin-price-every-5-minutes': {
'task': 'monitor.tasks.fetch_bitcoin_price_task',
'schedule': 300.0, # 300 seconds = 5 minutes
'options': {
'expires': 300,
'retry': True,
'retry_policy': {
'max_retries': 3,
'interval_start': 60,
'interval_step': 60,
'interval_max': 300,
}
},
},
'run-hourly-analysis-every-hour': {
'task': 'monitor.tasks.run_hourly_analysis_task',
'schedule': 3600.0, # 3600 seconds = 1 hour
'options': {
'expires': 3600,
},
},
'send-daily-digest-at-8am': {
'task': 'monitor.tasks.send_daily_digest_task',
'schedule': crontab(hour=8, minute=0), # 8 AM daily
'options': {
'expires': 3600,
},
},
'cleanup-old-data-daily': {
'task': 'monitor.tasks.cleanup_old_data_task',
'schedule': crontab(hour=0, minute=0), # Midnight daily
},
'check-system-health-every-10-minutes': {
'task': 'monitor.tasks.check_system_health_task',
'schedule': 600.0, # 600 seconds = 10 minutes
},
}
# Schedule for daily/yearly tasks (disabled for now as per your request)
# app.conf.beat_schedule.update({
# 'run-yearly-analysis-daily': {
# 'task': 'monitor.tasks.run_yearly_analysis_task',
# 'schedule': crontab(hour=0, minute=0), # Midnight
# },
# })
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')

View File

@@ -0,0 +1,99 @@
import requests
import logging
from datetime import datetime, timezone
from django.conf import settings
logger = logging.getLogger(__name__)
class CoinGeckoFetcher:
"""Fetches Bitcoin data from CoinGecko API."""
def __init__(self):
self.base_url = "https://api.coingecko.com/api/v3"
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'BitcoinMonitor/1.0',
'Accept': 'application/json',
})
# Optional API key for higher rate limits
api_key = settings.BITCOIN_MONITOR.get('COINGECKO_API_KEY')
if api_key:
self.session.headers['x-cg-pro-api-key'] = api_key
def fetch_current_price(self):
"""Fetch current Bitcoin price."""
try:
url = f"{self.base_url}/simple/price"
params = {
'ids': 'bitcoin',
'vs_currencies': 'usd',
'include_market_cap': 'true',
'include_24hr_vol': 'true',
'include_last_updated_at': 'true',
}
logger.debug(f"Fetching current price from {url}")
response = self.session.get(url, params=params, timeout=10)
response.raise_for_status()
data = response.json()
if 'bitcoin' not in data:
logger.error("Bitcoin data not found in response")
return None
btc_data = data['bitcoin']
return {
'timestamp': datetime.fromtimestamp(
btc_data.get('last_updated_at', datetime.now(timezone.utc).timestamp()),
timezone.utc
),
'price_usd': btc_data['usd'],
'market_cap': btc_data.get('usd_market_cap'),
'volume': btc_data.get('usd_24h_vol'),
}
except requests.exceptions.RequestException as e:
logger.error(f"Request error fetching current price: {e}")
return None
except Exception as e:
logger.error(f"Error fetching current price: {e}")
return None
def fetch_price_history(self, days=30):
"""Fetch historical price data (for future use)."""
try:
url = f"{self.base_url}/coins/bitcoin/market_chart"
params = {
'vs_currency': 'usd',
'days': days,
'interval': 'daily',
}
logger.debug(f"Fetching {days} days of price history")
response = self.session.get(url, params=params, timeout=30)
response.raise_for_status()
data = response.json()
prices = []
for point in data.get('prices', []):
prices.append({
'timestamp': datetime.fromtimestamp(point[0] / 1000, timezone.utc),
'price': point[1],
})
return {
'prices': prices,
'total_points': len(prices),
}
except requests.exceptions.RequestException as e:
logger.error(f"Request error fetching price history: {e}")
return None
except Exception as e:
logger.error(f"Error fetching price history: {e}")
return None

175
config/settings.py Normal file
View File

@@ -0,0 +1,175 @@
"""
Django settings for config project.
Generated by 'django-admin startproject' using Django 6.0.
For more information on this file, see
https://docs.djangoproject.com/en/6.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/6.0/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-mb+4lzhzvs3cu^hb-!n0me7fm@&xc6an4s80bmm6ad71lq23&o'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"rest_framework",
"django_celery_results",
"monitor",
"api",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
# Database
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# REST Framework settings
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny', # Allow public access for now
],
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer', # Nice API browser
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100,
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/hour',
'user': '1000/hour'
}
}
# Internationalization
# https://docs.djangoproject.com/en/6.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
CELERY_BROKER_URL = 'redis://localhost:6379/0' # Redis as broker
CELERY_RESULT_BACKEND = 'django-db' # Use Django database for results
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60 # 30 minutes
BITCOIN_MONITOR = {
'THRESHOLD_PERCENT': 15.0,
'UPDATE_INTERVAL_MINUTES': 5, # Fetch every 5 minutes for testing
'COINGECKO_API_KEY': '', # Optional API key for higher rate limits
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.getenv('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_PORT = int(os.getenv('EMAIL_PORT', 587))
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', 'True') == 'True'
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', '')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '')
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', 'Bitcoin Monitor <noreply@bitcoin-monitor.com>')
# Site domain for email links
SITE_DOMAIN = os.getenv('SITE_DOMAIN', 'localhost:8000')
# Notification settings
NOTIFICATION_SETTINGS = {
'EVENT_COOLDOWN_MINUTES': 60, # Don't send same event alerts within 60 minutes
'MAX_RETRIES': 3,
'DAILY_DIGEST_HOUR': 8, # 8 AM
'TEST_EMAIL_RECIPIENT': 'ali.c.zeybek@gmail.com',
}
STATIC_URL = 'static/'

229
config/tasks.py Normal file
View File

@@ -0,0 +1,229 @@
from celery import shared_task
from celery.utils.log import get_task_logger
import logging
from datetime import timedelta
from django.utils import timezone
from django.db import transaction
from monitor.models import BitcoinPrice, MarketAnalysis, SystemStatus
from monitor.services.analyzer import MarketAnalyzer
from monitor.services.data_fetcher import CoinGeckoFetcher
logger = get_task_logger(__name__)
@shared_task(bind=True, max_retries=3)
def fetch_bitcoin_price_task(self):
"""
Fetch current Bitcoin price and save to database.
Runs every 5 minutes by default.
"""
logger.info("Starting Bitcoin price fetch task...")
try:
fetcher = CoinGeckoFetcher()
# Fetch current price
price_data = fetcher.fetch_current_price()
if not price_data:
logger.error("Failed to fetch price data")
raise Exception("No price data received")
# Save to database in transaction
with transaction.atomic():
bitcoin_price = BitcoinPrice.objects.create(
timestamp=price_data['timestamp'],
price_usd=price_data['price_usd'],
volume=price_data.get('volume'),
market_cap=price_data.get('market_cap'),
)
# Update system status
SystemStatus.objects.create(
current_price=bitcoin_price.price_usd,
last_hourly_update=timezone.now(),
last_successful_fetch=timezone.now(),
is_stale=False,
is_healthy=True,
)
logger.info(f"Successfully fetched and saved Bitcoin price: ${price_data['price_usd']}")
# Trigger analysis if it's been more than 55 minutes since last analysis
# or if this is a significant price change
last_analysis = MarketAnalysis.objects.filter(
period='hourly'
).order_by('-timestamp').first()
should_analyze = False
if not last_analysis:
should_analyze = True
else:
time_since_analysis = timezone.now() - last_analysis.timestamp
if time_since_analysis.total_seconds() > 3300: # 55 minutes
should_analyze = True
if should_analyze:
# Run analysis in separate task
run_hourly_analysis_task.delay()
return {
'success': True,
'price': float(price_data['price_usd']),
'timestamp': price_data['timestamp'].isoformat(),
}
except Exception as e:
logger.error(f"Error in fetch_bitcoin_price_task: {e}")
# Update system status with error
SystemStatus.objects.create(
last_error=str(e),
is_stale=True,
is_healthy=False,
)
# Retry the task
self.retry(exc=e, countdown=60)
return {
'success': False,
'error': str(e),
}
@shared_task
def run_hourly_analysis_task():
"""
Run hourly market analysis.
Runs every hour by default.
"""
logger.info("Starting hourly analysis task...")
try:
analyzer = MarketAnalyzer(threshold_percent=15.0)
# Run hourly analysis
analysis = analyzer.analyze_market('hourly')
if analysis:
logger.info(f"Hourly analysis completed: {analysis.status} at ${analysis.current_price}")
# Check if this is an event and log it
if analysis.is_event:
logger.warning(
f"Market event detected: {analysis.event_type} "
f"at ${analysis.current_price}"
)
return {
'success': True,
'analysis_id': analysis.id,
'status': analysis.status,
'price': float(analysis.current_price),
'is_event': analysis.is_event,
}
else:
logger.warning("Hourly analysis returned no results")
return {'success': False, 'error': 'No analysis results'}
except Exception as e:
logger.error(f"Error in run_hourly_analysis_task: {e}")
return {'success': False, 'error': str(e)}
@shared_task
def cleanup_old_data_task():
"""
Clean up old data to keep database size manageable.
Runs once a day.
"""
logger.info("Starting data cleanup task...")
try:
# Keep only last 30 days of price data for performance
cutoff_date = timezone.now() - timedelta(days=30)
deleted_count, _ = BitcoinPrice.objects.filter(
timestamp__lt=cutoff_date
).delete()
# Keep only last 1000 system status entries
status_entries = SystemStatus.objects.all().order_by('-timestamp')
if status_entries.count() > 1000:
status_to_delete = status_entries[1000:]
deleted_status_count, _ = status_to_delete.delete()
# Keep only last 365 analyses
analyses = MarketAnalysis.objects.all().order_by('-timestamp')
if analyses.count() > 365:
analyses_to_delete = analyses[365:]
deleted_analyses_count, _ = analyses_to_delete.delete()
logger.info(f"Cleanup completed. Deleted {deleted_count} old price records.")
return {
'success': True,
'deleted_prices': deleted_count,
}
except Exception as e:
logger.error(f"Error in cleanup_old_data_task: {e}")
return {'success': False, 'error': str(e)}
@shared_task
def check_system_health_task():
"""
Check system health and log status.
Runs every 10 minutes.
"""
logger.info("Checking system health...")
try:
# Check if we have recent data
recent_price = BitcoinPrice.objects.order_by('-timestamp').first()
recent_analysis = MarketAnalysis.objects.order_by('-timestamp').first()
is_healthy = True
issues = []
if not recent_price:
issues.append("No price data available")
is_healthy = False
else:
price_age = (timezone.now() - recent_price.timestamp).total_seconds()
if price_age > 3600: # More than 1 hour old
issues.append(f"Price data is {price_age/60:.0f} minutes old")
is_healthy = False
if not recent_analysis:
issues.append("No analysis data available")
is_healthy = False
else:
analysis_age = (timezone.now() - recent_analysis.timestamp).total_seconds()
if analysis_age > 7200: # More than 2 hours old
issues.append(f"Analysis data is {analysis_age/3600:.1f} hours old")
is_healthy = False
# Log health status
if is_healthy:
logger.info("System is healthy")
else:
logger.warning(f"System has issues: {', '.join(issues)}")
# Update system status
SystemStatus.objects.create(
is_healthy=is_healthy,
last_error=', '.join(issues) if issues else None,
)
return {
'healthy': is_healthy,
'issues': issues,
'last_price_time': recent_price.timestamp.isoformat() if recent_price else None,
'last_analysis_time': recent_analysis.timestamp.isoformat() if recent_analysis else None,
}
except Exception as e:
logger.error(f"Error in check_system_health_task: {e}")
return {'healthy': False, 'error': str(e)}

View File

@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<title>Bitcoin Monitor</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
}
.status {
padding: 15px;
margin: 20px 0;
background: #4CAF50;
color: white;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>₿ Bitcoin Monitor</h1>
<p>Welcome to your Bitcoin monitoring dashboard!</p>
<div class="status">
<h2>Status: Running</h2>
<p>This is a simple Django app for monitoring Bitcoin prices.</p>
</div>
<h3>Next Steps:</h3>
<ul>
<li>Connect to Bitcoin API</li>
<li>Display real-time prices</li>
<li>Add price charts</li>
<li>Set up alerts</li>
</ul>
<p>Check the API: <a href="/">Hello World</a></p>
</div>
</body>
</html>

30
config/urls.py Normal file
View File

@@ -0,0 +1,30 @@
"""
URL configuration for config project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/6.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from . import views
from monitor import views as monitor_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', monitor_views.bitcoin_data, name='bitcoin_data'),
path('dashboard/', views.dashboard), # Dashboard URL
path('fetch-price/', monitor_views.fetch_bitcoin_price, name='fetch_price'),
path('analysis/run/', monitor_views.run_analysis, name='run_analysis'),
path('analysis/views/', monitor_views.view_analysis, name='view_analysis'),
path('api/', include('api.urls')),
]

9
config/views.py Normal file
View File

@@ -0,0 +1,9 @@
# config/views.py
from django.http import HttpResponse
from django.shortcuts import render
def hello_world(request):
return HttpResponse("Hello, Bitcoin Monitor World!")
def dashboard(request):
return render(request, 'dashboard.html')

16
config/wsgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
WSGI config for config project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_wsgi_application()