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

0
monitor/__init__.py Normal file
View File

130
monitor/admin.py Normal file
View File

@@ -0,0 +1,130 @@
from django.contrib import admin
from .models import BitcoinPrice, MarketAnalysis, SystemStatus, NotificationPreference, EmailNotification
@admin.register(BitcoinPrice)
class BitcoinPriceAdmin(admin.ModelAdmin):
list_display = ('timestamp', 'price_usd', 'volume', 'market_cap')
list_filter = ('timestamp',)
search_fields = ('price_usd',)
date_hierarchy = 'timestamp'
@admin.register(MarketAnalysis)
class MarketAnalysisAdmin(admin.ModelAdmin):
list_display = ('timestamp', 'period', 'status', 'current_price', 'average_price')
list_filter = ('period', 'status', 'is_event')
search_fields = ('status', 'event_type')
date_hierarchy = 'timestamp'
fieldsets = (
('Basic Info', {
'fields': ('timestamp', 'period', 'status')
}),
('Prices', {
'fields': ('current_price', 'average_price', 'min_price', 'max_price')
}),
('Analysis', {
'fields': ('threshold_percent', 'lower_threshold', 'upper_threshold')
}),
('Events', {
'fields': ('is_event', 'event_type')
}),
)
@admin.register(SystemStatus)
class SystemStatusAdmin(admin.ModelAdmin):
list_display = ('timestamp', 'is_healthy', 'is_stale', 'current_price')
list_filter = ('is_healthy', 'is_stale')
readonly_fields = ('timestamp',)
date_hierarchy = 'timestamp'
fieldsets = (
('Status', {
'fields': ('timestamp', 'is_healthy', 'is_stale')
}),
('Updates', {
'fields': ('last_hourly_update', 'last_successful_fetch')
}),
('Current State', {
'fields': ('current_price',)
}),
('Errors', {
'fields': ('last_error',),
'classes': ('collapse',)
}),
)
@admin.register(NotificationPreference)
class NotificationPreferenceAdmin(admin.ModelAdmin):
list_display = ('email_address', 'is_active', 'receive_event_alerts',
'receive_system_alerts', 'receive_daily_digest', 'created_at')
list_filter = ('is_active', 'receive_event_alerts', 'receive_system_alerts',
'receive_daily_digest')
search_fields = ('email_address',)
list_editable = ('is_active', 'receive_event_alerts', 'receive_system_alerts',
'receive_daily_digest')
fieldsets = (
('Email Settings', {
'fields': ('email_address', 'is_active')
}),
('Notification Types', {
'fields': ('receive_event_alerts', 'receive_system_alerts', 'receive_daily_digest'),
'classes': ('collapse',)
}),
('Timestamps', {
'fields': ('created_at', 'updated_at'),
'classes': ('collapse',)
}),
)
readonly_fields = ('created_at', 'updated_at')
@admin.register(EmailNotification)
class EmailNotificationAdmin(admin.ModelAdmin):
list_display = ('recipient', 'subject', 'notification_type', 'status',
'sent_at', 'created_at')
list_filter = ('notification_type', 'status', 'recipient')
search_fields = ('recipient', 'subject', 'error_message')
readonly_fields = ('created_at', 'sent_at', 'retry_count')
fieldsets = (
('Basic Info', {
'fields': ('recipient', 'subject', 'notification_type', 'status')
}),
('Content', {
'fields': ('content_text', 'content_html'),
'classes': ('collapse',)
}),
('Delivery Info', {
'fields': ('sent_at', 'retry_count', 'error_message')
}),
('Timestamps', {
'fields': ('created_at',),
'classes': ('collapse',)
}),
)
actions = ['resend_failed_emails']
def resend_failed_emails(self, request, queryset):
"""Resend failed emails."""
from monitor.services.email_service import EmailService
email_service = EmailService()
resend_count = 0
for email in queryset.filter(status='failed'):
try:
# Create a copy of the email and send
email_service._send_single_email(email, '', {})
resend_count += 1
except Exception as e:
self.message_user(request, f"Failed to resend to {email.recipient}: {e}", level='error')
self.message_user(request, f"Resent {resend_count} email(s)")
resend_failed_emails.short_description = "Resend selected failed emails"

5
monitor/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class MonitorConfig(AppConfig):
name = 'monitor'

View File

@@ -0,0 +1,156 @@
from django.core.management.base import BaseCommand
from monitor.services.historical_data import HistoricalDataFetcher
from monitor.models import BitcoinPrice
from django.db.models import Min, Max, Count
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Check quality and statistics of Bitcoin price data in database'
def add_arguments(self, parser):
parser.add_argument(
'--fetch-sample',
action='store_true',
help='Fetch sample data for comparison'
)
def handle(self, *args, **options):
self.stdout.write(self.style.HTTP_INFO("Bitcoin Data Quality Check"))
self.stdout.write("=" * 50)
# Get database statistics
total_records = BitcoinPrice.objects.count()
if total_records == 0:
self.stdout.write(self.style.ERROR("No Bitcoin price data in database."))
self.stdout.write("Run: python manage.py load_historical_data")
return
# Get date range
date_range = BitcoinPrice.objects.aggregate(
earliest=Min('timestamp'),
latest=Max('timestamp')
)
# Calculate time span
if date_range['earliest'] and date_range['latest']:
time_span = date_range['latest'] - date_range['earliest']
days_span = time_span.days
self.stdout.write(f"Data range: {date_range['earliest'].strftime('%Y-%m-%d')} "
f"to {date_range['latest'].strftime('%Y-%m-%d')}")
self.stdout.write(f"Time span: {days_span} days")
self.stdout.write(f"Total records: {total_records}")
# Calculate records per day
if days_span > 0:
records_per_day = total_records / days_span
self.stdout.write(f"Records per day: {records_per_day:.2f}")
if records_per_day < 0.9:
self.stdout.write(
self.style.WARNING("⚠️ Less than 1 record per day - data may be incomplete")
)
elif records_per_day > 24:
self.stdout.write("📈 More than hourly data - good coverage")
else:
self.stdout.write("📊 Daily data coverage")
# Check for missing values
missing_volume = BitcoinPrice.objects.filter(volume__isnull=True).count()
missing_market_cap = BitcoinPrice.objects.filter(market_cap__isnull=True).count()
if missing_volume > 0:
self.stdout.write(
self.style.WARNING(f"Missing volume data: {missing_volume} records ({missing_volume/total_records*100:.1f}%)")
)
if missing_market_cap > 0:
self.stdout.write(
self.style.WARNING(f"Missing market cap: {missing_market_cap} records ({missing_market_cap/total_records*100:.1f}%)")
)
# Get price statistics
prices = BitcoinPrice.objects.all().order_by('timestamp')
price_list = [float(p.price_usd) for p in prices]
if price_list:
min_price = min(price_list)
max_price = max(price_list)
avg_price = sum(price_list) / len(price_list)
self.stdout.write("\n" + self.style.SUCCESS("Price Statistics"))
self.stdout.write("-" * 30)
self.stdout.write(f"Minimum price: ${min_price:.2f}")
self.stdout.write(f"Maximum price: ${max_price:.2f}")
self.stdout.write(f"Average price: ${avg_price:.2f}")
self.stdout.write(f"Price range: ${max_price - min_price:.2f} "
f"({((max_price - min_price) / min_price * 100):.1f}%)")
# Check for time gaps
time_gaps = []
prev_timestamp = None
for price in prices.order_by('timestamp'):
if prev_timestamp:
gap_hours = (price.timestamp - prev_timestamp).total_seconds() / 3600
if gap_hours > 24: # More than 1 day gap
time_gaps.append({
'from': prev_timestamp,
'to': price.timestamp,
'gap_days': gap_hours / 24,
})
prev_timestamp = price.timestamp
if time_gaps:
self.stdout.write("\n" + self.style.WARNING("Time Gaps Detected"))
self.stdout.write("-" * 30)
for gap in time_gaps[:3]: # Show first 3 gaps
self.stdout.write(
f"Gap of {gap['gap_days']:.1f} days from "
f"{gap['from'].strftime('%Y-%m-%d')} to {gap['to'].strftime('%Y-%m-%d')}"
)
if len(time_gaps) > 3:
self.stdout.write(f"... and {len(time_gaps) - 3} more gaps")
# Compare with fresh data if requested
if options['fetch_sample']:
self.stdout.write("\n" + self.style.INFO("Fetching sample data for comparison..."))
fetcher = HistoricalDataFetcher()
sample_data = fetcher.fetch_historical_data(days=30)
if sample_data:
self.stdout.write(f"Sample data points: {len(sample_data)}")
sample_prices = [d['price_usd'] for d in sample_data]
sample_min = min(sample_prices)
sample_max = max(sample_prices)
sample_avg = sum(sample_prices) / len(sample_prices)
self.stdout.write(f"Sample min: ${sample_min:.2f}")
self.stdout.write(f"Sample max: ${sample_max:.2f}")
self.stdout.write(f"Sample avg: ${sample_avg:.2f}")
# Recommendations
self.stdout.write("\n" + self.style.HTTP_INFO("Recommendations"))
self.stdout.write("-" * 30)
if total_records < 100:
self.stdout.write("1. Load more data: python manage.py load_historical_data --days 365")
if missing_volume > total_records * 0.5:
self.stdout.write("2. Consider fetching data with volume information")
if time_gaps:
self.stdout.write("3. Consider filling time gaps with additional data")
if total_records >= 100 and not time_gaps and missing_volume < total_records * 0.1:
self.stdout.write("✅ Data quality looks good!")
self.stdout.write("\n" + self.style.SUCCESS("Quality check complete!"))

View File

@@ -0,0 +1,211 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import timedelta
import logging
from monitor.services.historical_data import HistoricalDataFetcher
from monitor.services.analyzer import MarketAnalyzer
from monitor.services.email_service import EmailService
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Load historical Bitcoin price data into the database'
def add_arguments(self, parser):
parser.add_argument(
'--days',
type=int,
default=365,
help='Number of days of historical data to fetch (default: 365)'
)
parser.add_argument(
'--clear',
action='store_true',
help='Clear existing data before loading new data'
)
parser.add_argument(
'--generate-test',
action='store_true',
help='Generate synthetic test data instead of fetching real data'
)
parser.add_argument(
'--test-days',
type=int,
default=30,
help='Days of test data to generate (default: 30)'
)
parser.add_argument(
'--analyze',
action='store_true',
help='Run market analysis on historical data after loading'
)
parser.add_argument(
'--notify',
action='store_true',
help='Send email notification when data loading is complete'
)
parser.add_argument(
'--quality-check',
action='store_true',
help='Perform data quality analysis'
)
def handle(self, *args, **options):
self.stdout.write(self.style.HTTP_INFO("Bitcoin Historical Data Loader"))
self.stdout.write("=" * 50)
days = options['days']
clear_existing = options['clear']
generate_test = options['generate_test']
test_days = options['test_days']
analyze = options['analyze']
notify = options['notify']
quality_check = options['quality_check']
fetcher = HistoricalDataFetcher()
if generate_test:
self.stdout.write(
self.style.WARNING(
f"Generating {test_days} days of synthetic test data..."
)
)
historical_data = fetcher.generate_test_data(days=test_days)
else:
self.stdout.write(
(f"Fetching {days} days of historical Bitcoin data...")
)
historical_data = fetcher.fetch_historical_data(days=days)
if not historical_data:
self.stdout.write(
self.style.ERROR("No data fetched. Check your internet connection and API status.")
)
return
# Perform quality check if requested
if quality_check:
self.stdout.write(self.style.INFO("Performing data quality analysis..."))
quality_metrics = fetcher.analyze_historical_data_quality(historical_data)
self.stdout.write(f"Total data points: {quality_metrics['total_points']}")
self.stdout.write(
f"Date range: {quality_metrics['date_range']['start'].strftime('%Y-%m-%d')} "
f"to {quality_metrics['date_range']['end'].strftime('%Y-%m-%d')} "
f"({quality_metrics['date_range']['days']} days)"
)
price_stats = quality_metrics['price_stats']
self.stdout.write(f"Price range: ${price_stats['min']:.2f} - ${price_stats['max']:.2f}")
self.stdout.write(f"Average price: ${price_stats['average']:.2f}")
if quality_metrics['data_quality']['missing_prices'] > 0:
self.stdout.write(
self.style.WARNING(
f"Missing prices: {quality_metrics['data_quality']['missing_prices']}"
)
)
if quality_metrics['data_quality']['time_gaps'] > 0:
self.stdout.write(
self.style.WARNING(
f"Time gaps found: {quality_metrics['data_quality']['time_gaps']}"
)
)
for suggestion in quality_metrics.get('suggestions', []):
self.stdout.write(f"💡 {suggestion}")
# Save data to database
self.stdout.write(("Saving data to database..."))
save_stats = fetcher.save_historical_data(
historical_data=historical_data,
clear_existing=clear_existing
)
# Display save statistics
self.stdout.write("\n" + self.style.SUCCESS("Data Load Summary"))
self.stdout.write("-" * 30)
self.stdout.write(f"Total records processed: {save_stats['total']}")
self.stdout.write(f"New records saved: {save_stats['saved']}")
self.stdout.write(f"Existing records skipped: {save_stats['skipped']}")
self.stdout.write(f"Errors: {save_stats['errors']}")
if save_stats['errors'] > 0:
self.stdout.write(
self.style.WARNING(f"⚠️ {save_stats['errors']} records had errors and were not saved")
)
# Run analysis if requested
if analyze and save_stats['saved'] > 0:
self.stdout.write("\n" + ("Running market analysis on historical data..."))
analyzer = MarketAnalyzer()
analysis_count = 0
# Run analysis for different time periods
for period in ['hourly', 'daily', 'weekly', 'yearly']:
analysis = analyzer.analyze_market(period)
if analysis:
analysis_count += 1
self.stdout.write(
f" {period.capitalize()} analysis: {analysis.status} at ${analysis.current_price}"
)
self.stdout.write(
self.style.SUCCESS(f"Completed {analysis_count} market analyses")
)
# Send notification if requested
if notify:
self.stdout.write("\n" + self.style.INFO("Sending completion notification..."))
try:
email_service = EmailService()
subject = f"✅ Historical Data Loaded: {save_stats['saved']} records"
# Create a simple notification
email_service.send_system_alert(
alert_title="Historical Data Load Complete",
alert_message=(
f"Successfully loaded {save_stats['saved']} historical Bitcoin price records.\n"
f"Date range: {days} days\n"
f"Errors: {save_stats['errors']}\n"
f"Total in database: {save_stats['total']}"
),
severity='info',
affected_component='data_loader'
)
self.stdout.write(self.style.SUCCESS("Notification sent!"))
except Exception as e:
self.stdout.write(
self.style.ERROR(f"Failed to send notification: {e}")
)
# Display database stats
from monitor.models import BitcoinPrice
total_records = BitcoinPrice.objects.count()
latest_record = BitcoinPrice.objects.order_by('-timestamp').first()
self.stdout.write("\n" + self.style.SUCCESS("Database Status"))
self.stdout.write("-" * 30)
self.stdout.write(f"Total Bitcoin price records: {total_records}")
if latest_record:
self.stdout.write(
f"Latest price: ${latest_record.price_usd} "
f"at {latest_record.timestamp.strftime('%Y-%m-%d %H:%M UTC')}"
)
self.stdout.write("\n" + self.style.SUCCESS("✅ Historical data loading complete!"))
# Provide next steps
self.stdout.write("\n" + self.style.HTTP_INFO("Next Steps:"))
self.stdout.write("1. View data in admin: http://localhost:8000/admin/monitor/bitcoinprice/")
self.stdout.write("2. Run analysis: python manage.py load_historical_data --analyze")
self.stdout.write("3. View dashboard: http://localhost:8000/")
self.stdout.write("4. Test email notifications by running an analysis")

View File

@@ -0,0 +1,33 @@
from django.core.management.base import BaseCommand
from monitor.models import BitcoinPrice
from datetime import datetime, timedelta, timezone
import random
class Command(BaseCommand):
help = 'Load sample Bitcoin price data'
def handle(self, *args, **options):
# Clear existing data
BitcoinPrice.objects.all().delete()
# Create sample data (last 7 days)
base_price = 45000
now = datetime.now(timezone.utc)
for i in range(168): # 7 days * 24 hours = 168 hours
timestamp = now - timedelta(hours=i)
# Random price variation ±5%
variation = random.uniform(0.95, 1.05)
price = round(base_price * variation, 2)
BitcoinPrice.objects.create(
timestamp=timestamp,
price_usd=price,
volume=random.uniform(20000000000, 40000000000),
market_cap=random.uniform(800000000000, 900000000000),
)
self.stdout.write(
self.style.SUCCESS(f'Successfully created {BitcoinPrice.objects.count()} sample records')
)

View File

@@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand
from monitor.services.email_service import EmailService
class Command(BaseCommand):
help = 'Send a test email to verify configuration'
def add_arguments(self, parser):
parser.add_argument(
'--email',
type=str,
required=True,
help='Email address to send test to'
)
def handle(self, *args, **options):
email = options['email']
email_service = EmailService()
self.stdout.write(f'Sending test email to {email}...')
success, message = email_service.send_test_email(email)
if success:
self.stdout.write(self.style.SUCCESS('Test email sent successfully!'))
self.stdout.write('Check your inbox (and spam folder).')
else:
self.stdout.write(self.style.ERROR(f'Failed to send test email: {message}'))

View File

@@ -0,0 +1,43 @@
from django.core.management.base import BaseCommand
from monitor.models import NotificationPreference
class Command(BaseCommand):
help = 'Setup initial notification preferences'
def add_arguments(self, parser):
parser.add_argument(
'--emails',
nargs='+',
type=str,
default=['ali.c.zeybek@gmail.com', 'alican@alicanzeybek.xyz'],
help='Email addresses to setup notifications for'
)
def handle(self, *args, **options):
emails = options['emails']
for email in emails:
# Check if preference already exists
pref, created = NotificationPreference.objects.get_or_create(
email_address=email,
defaults={
'receive_event_alerts': True,
'receive_system_alerts': True,
'receive_daily_digest': True,
'is_active': True,
}
)
if created:
self.stdout.write(
self.style.SUCCESS(f'Created notification preference for {email}')
)
else:
self.stdout.write(
self.style.WARNING(f'Notification preference for {email} already exists')
)
self.stdout.write(
self.style.SUCCESS(f'Setup complete for {len(emails)} email(s)')
)

View File

@@ -0,0 +1,27 @@
# Generated by Django 6.0 on 2025-12-09 20:45
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='BitcoinPrice',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(auto_now_add=True)),
('price_usd', models.DecimalField(decimal_places=2, max_digits=20)),
('volume', models.DecimalField(blank=True, decimal_places=2, max_digits=30, null=True)),
('market_cap', models.DecimalField(blank=True, decimal_places=2, max_digits=30, null=True)),
],
options={
'ordering': ['-timestamp'],
},
),
]

View File

@@ -0,0 +1,37 @@
# Generated by Django 6.0 on 2025-12-09 21:03
import django.core.validators
from decimal import Decimal
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('monitor', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='MarketAnalysis',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(auto_now_add=True)),
('period', models.CharField(choices=[('hourly', 'Hourly'), ('daily', 'Daily'), ('weekly', 'Weekly'), ('yearly', 'Yearly')], default='hourly', max_length=10)),
('current_price', models.DecimalField(decimal_places=2, max_digits=20)),
('average_price', models.DecimalField(decimal_places=2, max_digits=20)),
('min_price', models.DecimalField(decimal_places=2, max_digits=20)),
('max_price', models.DecimalField(decimal_places=2, max_digits=20)),
('status', models.CharField(choices=[('dip', 'Dip'), ('peak', 'Peak'), ('neutral', 'Neutral')], default='neutral', max_length=10)),
('threshold_percent', models.DecimalField(decimal_places=2, default=15.0, max_digits=5, validators=[django.core.validators.MinValueValidator(Decimal('0.01')), django.core.validators.MaxValueValidator(Decimal('100'))])),
('lower_threshold', models.DecimalField(decimal_places=2, max_digits=20)),
('upper_threshold', models.DecimalField(decimal_places=2, max_digits=20)),
('is_event', models.BooleanField(default=False)),
('event_type', models.CharField(blank=True, choices=[('dip_below', 'Dip Below Threshold'), ('rise_above', 'Rise Above Threshold'), ('neutralized', 'Neutralized')], max_length=20)),
],
options={
'verbose_name_plural': 'Market Analyses',
'ordering': ['-timestamp'],
},
),
]

View File

@@ -0,0 +1,30 @@
# Generated by Django 6.0 on 2025-12-09 21:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('monitor', '0002_marketanalysis'),
]
operations = [
migrations.CreateModel(
name='SystemStatus',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(auto_now_add=True)),
('last_hourly_update', models.DateTimeField(blank=True, null=True)),
('last_successful_fetch', models.DateTimeField(blank=True, null=True)),
('current_price', models.DecimalField(blank=True, decimal_places=2, max_digits=20, null=True)),
('last_error', models.TextField(blank=True)),
('is_stale', models.BooleanField(default=True)),
('is_healthy', models.BooleanField(default=True)),
],
options={
'verbose_name_plural': 'System Statuses',
'ordering': ['-timestamp'],
},
),
]

View File

@@ -0,0 +1,49 @@
# Generated by Django 6.0 on 2025-12-09 22:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('monitor', '0003_systemstatus'),
]
operations = [
migrations.CreateModel(
name='NotificationPreference',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email_address', models.EmailField(max_length=254, unique=True)),
('receive_event_alerts', models.BooleanField(default=True, help_text='Receive immediate alerts for threshold events')),
('receive_system_alerts', models.BooleanField(default=True, help_text='Receive alerts for system failures and issues')),
('receive_daily_digest', models.BooleanField(default=True, help_text='Receive daily summary email at 8 AM')),
('is_active', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
options={
'ordering': ['-created_at'],
},
),
migrations.CreateModel(
name='EmailNotification',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('recipient', models.EmailField(max_length=254)),
('subject', models.CharField(max_length=255)),
('notification_type', models.CharField(choices=[('event', 'Event Alert'), ('system', 'System Alert'), ('digest', 'Daily Digest'), ('test', 'Test Email')], max_length=20)),
('status', models.CharField(choices=[('pending', 'Pending'), ('sent', 'Sent'), ('failed', 'Failed'), ('retrying', 'Retrying')], default='pending', max_length=20)),
('content_html', models.TextField(blank=True)),
('content_text', models.TextField(blank=True)),
('error_message', models.TextField(blank=True)),
('sent_at', models.DateTimeField(blank=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('retry_count', models.IntegerField(default=0)),
],
options={
'ordering': ['-created_at'],
'indexes': [models.Index(fields=['recipient', 'created_at'], name='monitor_ema_recipie_afeb01_idx'), models.Index(fields=['notification_type', 'status'], name='monitor_ema_notific_6e50b6_idx')],
},
),
]

View File

167
monitor/models.py Normal file
View File

@@ -0,0 +1,167 @@
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from decimal import Decimal
class BitcoinPrice(models.Model):
"""
Stores historical Bitcoin price data.
"""
timestamp = models.DateTimeField(auto_now_add=True)
price_usd = models.DecimalField(max_digits=20, decimal_places=2)
volume = models.DecimalField(max_digits=30, decimal_places=2, null=True, blank=True)
market_cap = models.DecimalField(max_digits=30, decimal_places=2, null=True, blank=True)
def __str__(self):
return f"Bitcoin: ${self.price_usd} at {self.timestamp.strftime('%Y-%m-%d %H:%M')}"
class Meta:
ordering = ['-timestamp'] # Newest first
class MarketAnalysis(models.Model):
"""
Stores market analysis results.
"""
STATUS_CHOICES = [
('dip', 'Dip'),
('peak', 'Peak'),
('neutral', 'Neutral'),
]
timestamp = models.DateTimeField(auto_now_add=True)
period = models.CharField(max_length=10, choices=[
('hourly', 'Hourly'),
('daily', 'Daily'),
('weekly', 'Weekly'),
('yearly', 'Yearly'),
], default='hourly')
# Price statistics
current_price = models.DecimalField(max_digits=20, decimal_places=2)
average_price = models.DecimalField(max_digits=20, decimal_places=2)
min_price = models.DecimalField(max_digits=20, decimal_places=2)
max_price = models.DecimalField(max_digits=20, decimal_places=2)
# Analysis results
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='neutral')
threshold_percent = models.DecimalField(
max_digits=5,
decimal_places=2,
default=15.0,
validators=[MinValueValidator(Decimal('0.01')), MaxValueValidator(Decimal('100'))]
)
# Calculated thresholds
lower_threshold = models.DecimalField(max_digits=20, decimal_places=2)
upper_threshold = models.DecimalField(max_digits=20, decimal_places=2)
# Event tracking
is_event = models.BooleanField(default=False)
event_type = models.CharField(max_length=20, blank=True, choices=[
('dip_below', 'Dip Below Threshold'),
('rise_above', 'Rise Above Threshold'),
('neutralized', 'Neutralized'),
])
def __str__(self):
return f"Analysis: {self.status.upper()} at ${self.current_price}"
class Meta:
ordering = ['-timestamp']
verbose_name_plural = "Market Analyses"
class SystemStatus(models.Model):
"""
Tracks system health and status for monitoring.
"""
timestamp = models.DateTimeField(auto_now_add=True)
# Update status
last_hourly_update = models.DateTimeField(null=True, blank=True)
last_successful_fetch = models.DateTimeField(null=True, blank=True)
# Current status
current_price = models.DecimalField(
max_digits=20, decimal_places=2, null=True, blank=True
)
# Error tracking
last_error = models.TextField(blank=True)
is_stale = models.BooleanField(default=True)
is_healthy = models.BooleanField(default=True)
class Meta:
ordering = ['-timestamp']
verbose_name_plural = "System Statuses"
def __str__(self):
status = "Healthy" if self.is_healthy else "Unhealthy"
return f"System Status: {status} at {self.timestamp}"
class NotificationPreference(models.Model):
"""
Stores notification preferences for each email address.
"""
email_address = models.EmailField(unique=True)
receive_event_alerts = models.BooleanField(
default=True,
help_text="Receive immediate alerts for threshold events"
)
receive_system_alerts = models.BooleanField(
default=True,
help_text="Receive alerts for system failures and issues"
)
receive_daily_digest = models.BooleanField(
default=True,
help_text="Receive daily summary email at 8 AM"
)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.email_address} (Active: {self.is_active})"
class Meta:
ordering = ['-created_at']
class EmailNotification(models.Model):
"""
Records of all emails sent by the system.
"""
NOTIFICATION_TYPES = [
('event', 'Event Alert'),
('system', 'System Alert'),
('digest', 'Daily Digest'),
('test', 'Test Email'),
]
STATUS_CHOICES = [
('pending', 'Pending'),
('sent', 'Sent'),
('failed', 'Failed'),
('retrying', 'Retrying'),
]
recipient = models.EmailField()
subject = models.CharField(max_length=255)
notification_type = models.CharField(max_length=20, choices=NOTIFICATION_TYPES)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
content_html = models.TextField(blank=True)
content_text = models.TextField(blank=True)
error_message = models.TextField(blank=True)
sent_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
retry_count = models.IntegerField(default=0)
def __str__(self):
return f"{self.notification_type} to {self.recipient} - {self.status}"
class Meta:
ordering = ['-created_at']
indexes = [
models.Index(fields=['recipient', 'created_at']),
models.Index(fields=['notification_type', 'status']),
]

View File

@@ -0,0 +1,124 @@
import logging
from datetime import timedelta
from django.utils import timezone
from django.db.models import Avg, Min, Max
from decimal import Decimal
from monitor.models import BitcoinPrice, MarketAnalysis
logger = logging.getLogger(__name__)
class MarketAnalyzer:
"""Service for analyzing Bitcoin market data."""
def __init__(self, threshold_percent=15.0):
self.threshold_percent = Decimal(str(threshold_percent))
def analyze_market(self, period='hourly'):
"""Analyze the Bitcoin market for a given period."""
try:
# Get data for the period
time_filter = self._get_time_filter(period)
prices = BitcoinPrice.objects.filter(timestamp__gte=time_filter)
if not prices.exists():
logger.warning(f"No price data available for {period} analysis")
return None
# Get latest price
latest_price = prices.latest('timestamp')
# Calculate statistics
stats = prices.aggregate(
avg=Avg('price_usd'),
min=Min('price_usd'),
max=Max('price_usd'),
)
avg_price = stats['avg'] or latest_price.price_usd
min_price = stats['min'] or latest_price.price_usd
max_price = stats['max'] or latest_price.price_usd
# Calculate thresholds
lower_threshold = avg_price * (1 - self.threshold_percent / 100)
upper_threshold = avg_price * (1 + self.threshold_percent / 100)
# Determine status
current_price = latest_price.price_usd
if current_price < lower_threshold:
status = 'dip'
is_event = True
event_type = 'dip_below'
elif current_price > upper_threshold:
status = 'peak'
is_event = True
event_type = 'rise_above'
else:
status = 'neutral'
is_event = False
event_type = ''
# Save analysis
analysis = MarketAnalysis.objects.create(
period=period,
current_price=current_price,
average_price=avg_price,
min_price=min_price,
max_price=max_price,
status=status,
threshold_percent=self.threshold_percent,
lower_threshold=lower_threshold,
upper_threshold=upper_threshold,
is_event=is_event,
event_type=event_type,
)
logger.info(f"Market analysis saved: {status} at ${current_price}")
return analysis
except BitcoinPrice.DoesNotExist:
logger.error("No Bitcoin price data found")
return None
except Exception as e:
logger.error(f"Error analyzing market: {e}")
return None
def _get_time_filter(self, period):
"""Get datetime filter based on period."""
now = timezone.now()
if period == 'hourly':
return now - timedelta(hours=1)
elif period == 'daily':
return now - timedelta(days=1)
elif period == 'weekly':
return now - timedelta(weeks=1)
elif period == 'yearly':
return now - timedelta(days=365)
else:
return now - timedelta(days=1) # Default to daily
def get_latest_analysis(self, period='hourly'):
"""Get the latest analysis for a period."""
try:
return MarketAnalysis.objects.filter(period=period).latest('timestamp')
except MarketAnalysis.DoesNotExist:
return None
def get_analysis_summary(self):
"""Get summary of all analyses."""
summary = {}
for period in ['hourly', 'daily', 'weekly', 'yearly']:
analysis = self.get_latest_analysis(period)
if analysis:
summary[period] = {
'status': analysis.status,
'current_price': float(analysis.current_price),
'average_price': float(analysis.average_price),
'threshold_percent': float(analysis.threshold_percent),
'is_event': analysis.is_event,
}
return summary

View File

@@ -0,0 +1,376 @@
import logging
from datetime import datetime, timezone, timedelta
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
from django.urls import reverse
from monitor.models import NotificationPreference, EmailNotification
logger = logging.getLogger(__name__)
class EmailService:
"""Service for sending Bitcoin monitor notifications."""
def __init__(self):
self.site_domain = getattr(settings, 'SITE_DOMAIN', 'localhost:8000')
self.from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@bitcoin-monitor.com')
def get_notification_preferences(self):
"""Get all active notification preferences."""
return NotificationPreference.objects.filter(is_active=True)
def create_email_notification_record(self, recipient, subject, notification_type):
"""Create a record of the email to be sent."""
return EmailNotification.objects.create(
recipient=recipient,
subject=subject,
notification_type=notification_type,
status='pending'
)
def update_notification_status(self, email_notification, status, error_message=''):
"""Update the status of an email notification."""
email_notification.status = status
email_notification.error_message = error_message
if status == 'sent':
email_notification.sent_at = datetime.now(timezone.utc)
email_notification.save()
def render_email_template(self, template_name, context):
"""Render email template with context."""
# Add common context variables
context.update({
'dashboard_url': f'http://{self.site_domain}',
'admin_url': f'http://{self.site_domain}/admin/',
'unsubscribe_url': f'http://{self.site_domain}/api/notifications/unsubscribe/',
})
# Render HTML and plain text versions
html_content = render_to_string(f'emails/{template_name}', context)
text_content = strip_tags(html_content)
return html_content, text_content
def send_event_alert(self, analysis):
"""Send event alert email."""
try:
# Get recipients who want event alerts
preferences = self.get_notification_preferences().filter(
receive_event_alerts=True
)
if not preferences.exists():
logger.info("No recipients for event alerts")
return
# Calculate percent change
percent_change = abs(
(float(analysis.current_price) - float(analysis.average_price)) /
float(analysis.average_price) * 100
)
# Prepare context
context = {
'alert_type': analysis.status,
'current_price': float(analysis.current_price),
'yearly_average': float(analysis.average_price),
'threshold_percent': float(analysis.threshold_percent),
'lower_threshold': float(analysis.lower_threshold),
'upper_threshold': float(analysis.upper_threshold),
'percent_change': percent_change,
'detected_at': analysis.timestamp,
'previous_status': 'neutral', # Could be tracked
'recommendation': self._get_event_recommendation(analysis),
}
# Send to each recipient
for pref in preferences:
try:
email_notification = self.create_email_notification_record(
recipient=pref.email_address,
subject=f"🚨 Bitcoin {analysis.status.upper()} Alert: "
f"${float(analysis.current_price):.2f}",
notification_type='event'
)
self._send_single_email(
email_notification,
'event_alert.html',
context
)
except Exception as e:
logger.error(f"Failed to send event alert to {pref.email_address}: {e}")
logger.info(f"Event alert sent to {preferences.count()} recipients")
except Exception as e:
logger.error(f"Error sending event alert: {e}")
def send_system_alert(self, alert_title, alert_message, severity='warning',
affected_component='system', error_code=None):
"""Send system alert email."""
try:
# Get recipients who want system alerts
preferences = self.get_notification_preferences().filter(
receive_system_alerts=True
)
if not preferences.exists():
logger.info("No recipients for system alerts")
return
# Prepare context
context = {
'alert_title': alert_title,
'alert_message': alert_message,
'severity': severity,
'affected_component': affected_component,
'error_code': error_code,
'occurred_at': datetime.now(timezone.utc),
'troubleshooting_steps': self._get_troubleshooting_steps(severity),
}
# Send to each recipient
for pref in preferences:
try:
email_notification = self.create_email_notification_record(
recipient=pref.email_address,
subject=f"⚠️ Bitcoin Monitor System Alert: {alert_title}",
notification_type='system'
)
self._send_single_email(
email_notification,
'system_alert.html',
context
)
except Exception as e:
logger.error(f"Failed to send system alert to {pref.email_address}: {e}")
logger.info(f"System alert sent to {preferences.count()} recipients")
except Exception as e:
logger.error(f"Error sending system alert: {e}")
def send_daily_digest(self):
"""Send daily digest email."""
try:
# Get recipients who want daily digest
preferences = self.get_notification_preferences().filter(
receive_daily_digest=True
)
if not preferences.exists():
logger.info("No recipients for daily digest")
return
# Get data for the past 24 hours
yesterday = datetime.now(timezone.utc) - timedelta(hours=24)
from monitor.models import BitcoinPrice, MarketAnalysis, SystemStatus
from django.db.models import Max, Min
# Get price stats
prices = BitcoinPrice.objects.filter(
timestamp__gte=yesterday
)
if prices.exists():
current_price = prices.latest('timestamp').price_usd
daily_high = prices.aggregate(Max('price_usd'))['price_usd__max']
daily_low = prices.aggregate(Min('price_usd'))['price_usd__min']
daily_change = ((float(daily_high) - float(daily_low)) / float(daily_low) * 100)
else:
current_price = daily_high = daily_low = 0
daily_change = 0
# Get market status
latest_analysis = MarketAnalysis.objects.filter(
period='hourly'
).order_by('-timestamp').first()
market_status = latest_analysis.status if latest_analysis else 'neutral'
# Get event count
events_count = MarketAnalysis.objects.filter(
is_event=True,
timestamp__gte=yesterday
).count()
# Get system stats
price_fetches = prices.count()
successful_fetches = SystemStatus.objects.filter(
is_healthy=True,
timestamp__gte=yesterday
).count()
total_checks = SystemStatus.objects.filter(
timestamp__gte=yesterday
).count()
uptime_percentage = (successful_fetches / total_checks * 100) if total_checks > 0 else 0
# Get today's events
events_today = []
for event in MarketAnalysis.objects.filter(
is_event=True,
timestamp__gte=yesterday
).order_by('-timestamp')[:5]:
events_today.append({
'type': event.event_type,
'price': float(event.current_price),
'time': event.timestamp,
})
# Prepare context
context = {
'date': datetime.now(timezone.utc),
'summary_period': 'Last 24 hours',
'market_status': market_status,
'current_price': float(current_price),
'daily_high': float(daily_high) if daily_high else 0,
'daily_low': float(daily_low) if daily_low else 0,
'daily_change': daily_change,
'events_count': events_count,
'price_fetches': price_fetches,
'uptime_percentage': uptime_percentage,
'events_today': events_today,
'market_insight': self._get_daily_market_insight(market_status),
}
# Send to each recipient
for pref in preferences:
try:
email_notification = self.create_email_notification_record(
recipient=pref.email_address,
subject=f"📊 Bitcoin Daily Digest - {datetime.now(timezone.utc).strftime('%b %d, %Y')}",
notification_type='digest'
)
self._send_single_email(
email_notification,
'daily_digest.html',
context
)
except Exception as e:
logger.error(f"Failed to send daily digest to {pref.email_address}: {e}")
logger.info(f"Daily digest sent to {preferences.count()} recipients")
except Exception as e:
logger.error(f"Error sending daily digest: {e}")
# Try to send system alert about the failure
self.send_system_alert(
alert_title="Daily Digest Failed",
alert_message=f"Failed to send daily digest: {str(e)}",
severity='error',
affected_component='email_service'
)
def send_test_email(self, recipient_email):
"""Send a test email to verify configuration."""
try:
email_notification = self.create_email_notification_record(
recipient=recipient_email,
subject="✅ Bitcoin Monitor - Test Email",
notification_type='test'
)
context = {
'test_message': 'This is a test email from Bitcoin Monitor.',
'timestamp': datetime.now(timezone.utc),
}
self._send_single_email(
email_notification,
'system_alert.html', # Reuse system template for test
context
)
return True, "Test email sent successfully"
except Exception as e:
logger.error(f"Error sending test email: {e}")
return False, str(e)
def _send_single_email(self, email_notification, template_name, context):
"""Send a single email and update notification record."""
try:
# Render email content
html_content, text_content = self.render_email_template(template_name, context)
# Create email
email = EmailMultiAlternatives(
subject=email_notification.subject,
body=text_content,
from_email=self.from_email,
to=[email_notification.recipient],
)
email.attach_alternative(html_content, "text/html")
# Send email
email.send()
# Update notification record
email_notification.content_html = html_content
email_notification.content_text = text_content
self.update_notification_status(email_notification, 'sent')
logger.info(f"Email sent to {email_notification.recipient}")
except Exception as e:
logger.error(f"Failed to send email to {email_notification.recipient}: {e}")
email_notification.retry_count += 1
if email_notification.retry_count < 3:
self.update_notification_status(
email_notification,
'retrying',
f"Retry {email_notification.retry_count}/3: {str(e)}"
)
else:
self.update_notification_status(
email_notification,
'failed',
f"Failed after 3 retries: {str(e)}"
)
raise
def _get_event_recommendation(self, analysis):
"""Get recommendation based on event type."""
if analysis.status == 'dip':
return "Price is significantly below yearly average. Consider buying opportunity."
elif analysis.status == 'peak':
return "Price is significantly above yearly average. Consider taking profits."
else:
return "Price has returned to normal range. Monitor for further changes."
def _get_troubleshooting_steps(self, severity):
"""Get troubleshooting steps based on severity."""
steps = [
"Check the admin panel for detailed error logs",
"Verify internet connection and API access",
"Check Redis and Celery worker status",
]
if severity == 'critical':
steps.append("Restart the monitoring services if necessary")
steps.append("Check database connection and disk space")
return steps
def _get_daily_market_insight(self, market_status):
"""Get daily market insight based on status."""
insights = {
'dip': "Market shows buying opportunity with prices below yearly average. "
"Monitor for further dips before entering.",
'peak': "Market at elevated levels. Consider profit-taking strategies "
"and set stop-loss orders.",
'neutral': "Market trading within normal range. Good time to review "
"portfolio and set alerts for future movements.",
}
return insights.get(market_status, "Monitor market for emerging trends.")

View File

@@ -0,0 +1,301 @@
import logging
import time
from datetime import datetime, timezone, timedelta
from typing import List, Dict, Optional
import requests
from django.db import transaction
from django.utils import timezone as django_timezone
from decimal import Decimal
from monitor.models import BitcoinPrice
logger = logging.getLogger(__name__)
class HistoricalDataFetcher:
"""Fetches historical Bitcoin price data."""
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',
})
def fetch_historical_data(self, days: int = 365) -> List[Dict]:
"""
Fetch historical Bitcoin data for specified number of days.
Args:
days: Number of days of historical data to fetch
Returns:
List of price data dictionaries
"""
try:
logger.info(f"Fetching {days} days of historical Bitcoin data...")
url = f"{self.base_url}/coins/bitcoin/market_chart"
params = {
'vs_currency': 'usd',
'days': days,
'interval': 'daily',
}
response = self.session.get(url, params=params, timeout=30)
response.raise_for_status()
data = response.json()
historical_data = []
# Process prices
for price_point in data.get('prices', []):
timestamp = datetime.fromtimestamp(price_point[0] / 1000, timezone.utc)
price = price_point[1]
historical_data.append({
'timestamp': timestamp,
'price_usd': price,
'volume': None,
'market_cap': None,
})
# Add volume data if available
volumes = data.get('total_volumes', [])
for i, (timestamp_ms, volume) in enumerate(volumes):
if i < len(historical_data):
historical_data[i]['volume'] = volume
# Add market cap data if available
market_caps = data.get('market_caps', [])
for i, (timestamp_ms, market_cap) in enumerate(market_caps):
if i < len(historical_data):
historical_data[i]['market_cap'] = market_cap
logger.info(f"Fetched {len(historical_data)} historical price points")
return historical_data
except requests.exceptions.RequestException as e:
logger.error(f"Request error fetching historical data: {e}")
return []
except Exception as e:
logger.error(f"Error fetching historical data: {e}")
return []
def fetch_historical_data_range(self, start_date: datetime, end_date: datetime) -> List[Dict]:
"""
Fetch historical data for a specific date range.
Note: CoinGecko API doesn't support arbitrary date ranges directly,
so we fetch maximum days and filter.
"""
# Calculate days between dates
days_difference = (end_date - start_date).days
# Fetch more data than needed to ensure we have the range
all_data = self.fetch_historical_data(days=days_difference + 100)
# Filter to date range
filtered_data = [
point for point in all_data
if start_date <= point['timestamp'] <= end_date
]
return filtered_data
def save_historical_data(self, historical_data: List[Dict], clear_existing: bool = False) -> Dict:
"""
Save historical data to database.
Args:
historical_data: List of price data dictionaries
clear_existing: Whether to clear existing data before saving
Returns:
Dictionary with statistics about the operation
"""
if not historical_data:
logger.warning("No historical data to save")
return {'saved': 0, 'skipped': 0, 'errors': 0}
try:
with transaction.atomic():
if clear_existing:
deleted_count, _ = BitcoinPrice.objects.all().delete()
logger.info(f"Cleared {deleted_count} existing price records")
saved_count = 0
skipped_count = 0
error_count = 0
for data_point in historical_data:
try:
# Check if price already exists for this timestamp
exists = BitcoinPrice.objects.filter(
timestamp=data_point['timestamp']
).exists()
if exists:
skipped_count += 1
continue
# Create BitcoinPrice object
BitcoinPrice.objects.create(
timestamp=data_point['timestamp'],
price_usd=Decimal(str(data_point['price_usd'])),
volume=Decimal(str(data_point['volume'])) if data_point.get('volume') else None,
market_cap=Decimal(str(data_point['market_cap'])) if data_point.get('market_cap') else None,
)
saved_count += 1
# Log progress every 50 records
if saved_count % 50 == 0:
logger.info(f"Saved {saved_count} historical records...")
except Exception as e:
error_count += 1
logger.error(f"Error saving data point {data_point.get('timestamp')}: {e}")
logger.info(f"Historical data saved: {saved_count} new, {skipped_count} existing, {error_count} errors")
return {
'saved': saved_count,
'skipped': skipped_count,
'errors': error_count,
'total': len(historical_data),
}
except Exception as e:
logger.error(f"Transaction error saving historical data: {e}")
return {'saved': 0, 'skipped': 0, 'errors': len(historical_data), 'total': len(historical_data)}
def generate_test_data(self, days: int = 30, base_price: float = 45000) -> List[Dict]:
"""
Generate synthetic test data for development.
Args:
days: Number of days of test data
base_price: Base price for the data
Returns:
List of synthetic price data
"""
import random
from datetime import timedelta
logger.info(f"Generating {days} days of synthetic test data...")
test_data = []
now = django_timezone.now()
for i in range(days * 24): # Generate hourly data
timestamp = now - timedelta(hours=i)
# Create realistic price fluctuations (±5%)
variation = random.uniform(0.95, 1.05)
price = base_price * variation
# Generate volume and market cap with some randomness
volume = random.uniform(20000000000, 40000000000)
market_cap = random.uniform(800000000000, 900000000000)
test_data.append({
'timestamp': timestamp,
'price_usd': round(price, 2),
'volume': round(volume, 2),
'market_cap': round(market_cap, 2),
})
# Reverse to have chronological order
test_data.reverse()
logger.info(f"Generated {len(test_data)} synthetic data points")
return test_data
def analyze_historical_data_quality(self, historical_data: List[Dict]) -> Dict:
"""
Analyze the quality of historical data.
Args:
historical_data: List of price data dictionaries
Returns:
Dictionary with quality metrics
"""
if not historical_data:
return {'error': 'No data to analyze'}
# Sort by timestamp
sorted_data = sorted(historical_data, key=lambda x: x['timestamp'])
timestamps = [d['timestamp'] for d in sorted_data]
prices = [d['price_usd'] for d in sorted_data]
# Calculate metrics
min_price = min(prices)
max_price = max(prices)
avg_price = sum(prices) / len(prices)
# Check for gaps in timestamps
time_gaps = []
for i in range(1, len(timestamps)):
gap = (timestamps[i] - timestamps[i-1]).total_seconds() / 3600 # hours
if gap > 24: # More than 1 day gap
time_gaps.append({
'from': timestamps[i-1],
'to': timestamps[i],
'gap_hours': gap,
})
# Check for missing values
missing_prices = sum(1 for d in sorted_data if d['price_usd'] is None)
missing_volumes = sum(1 for d in sorted_data if d.get('volume') is None)
missing_market_caps = sum(1 for d in sorted_data if d.get('market_cap') is None)
return {
'total_points': len(historical_data),
'date_range': {
'start': timestamps[0],
'end': timestamps[-1],
'days': (timestamps[-1] - timestamps[0]).days,
},
'price_stats': {
'min': min_price,
'max': max_price,
'average': avg_price,
'range_percent': ((max_price - min_price) / min_price * 100),
},
'data_quality': {
'missing_prices': missing_prices,
'missing_volumes': missing_volumes,
'missing_market_caps': missing_market_caps,
'time_gaps': len(time_gaps),
'time_gaps_details': time_gaps[:5], # First 5 gaps
},
'suggestions': self._generate_data_quality_suggestions({
'missing_prices': missing_prices,
'time_gaps': len(time_gaps),
'total_points': len(historical_data),
})
}
def _generate_data_quality_suggestions(self, metrics: Dict) -> List[str]:
"""Generate suggestions based on data quality metrics."""
suggestions = []
if metrics['missing_prices'] > 0:
suggestions.append(f"Found {metrics['missing_prices']} missing prices. Consider filling gaps.")
if metrics['time_gaps'] > 0:
suggestions.append(f"Found {metrics['time_gaps']} time gaps. Data may not be continuous.")
if metrics['total_points'] < 30:
suggestions.append("Less than 30 data points. Consider fetching more data.")
if not suggestions:
suggestions.append("Data quality looks good!")
return suggestions

173
monitor/tasks.py Normal file
View File

@@ -0,0 +1,173 @@
from monitor.services.email_service import EmailService
@shared_task
def send_event_notification_task(analysis_id):
"""Send email notification for a market event."""
try:
from monitor.models import MarketAnalysis
analysis = MarketAnalysis.objects.get(id=analysis_id)
if analysis.is_event:
email_service = EmailService()
email_service.send_event_alert(analysis)
logger.info(f"Event notification sent for analysis {analysis_id}")
return {'success': True, 'analysis_id': analysis_id}
else:
logger.info(f"Analysis {analysis_id} is not an event, skipping notification")
return {'success': False, 'reason': 'Not an event'}
except MarketAnalysis.DoesNotExist:
logger.error(f"Analysis {analysis_id} not found")
return {'success': False, 'error': 'Analysis not found'}
except Exception as e:
logger.error(f"Error sending event notification: {e}")
return {'success': False, 'error': str(e)}
@shared_task
def send_daily_digest_task():
"""Send daily digest email at 8 AM."""
try:
email_service = EmailService()
email_service.send_daily_digest()
logger.info("Daily digest email sent")
return {'success': True}
except Exception as e:
logger.error(f"Error sending daily digest: {e}")
# Try to send system alert about the failure
send_system_notification_task.delay(
alert_title="Daily Digest Failed",
alert_message=f"Failed to send daily digest: {str(e)}",
severity='error'
)
return {'success': False, 'error': str(e)}
@shared_task
def send_system_notification_task(alert_title, alert_message, severity='warning',
affected_component='system'):
"""Send system notification email."""
try:
email_service = EmailService()
email_service.send_system_alert(
alert_title=alert_title,
alert_message=alert_message,
severity=severity,
affected_component=affected_component
)
logger.info(f"System notification sent: {alert_title}")
return {'success': True}
except Exception as e:
logger.error(f"Error sending system notification: {e}")
return {'success': False, 'error': str(e)}
# Update the fetch_bitcoin_price_task to send system alerts on failure
@shared_task(bind=True, max_retries=3)
def fetch_bitcoin_price_task(self):
"""Fetch current Bitcoin price and save to database."""
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")
# Send system alert
send_system_notification_task.delay(
alert_title="Bitcoin Price Fetch Failed",
alert_message="Failed to fetch Bitcoin price from CoinGecko API. Check API status and internet connection.",
severity='warning',
affected_component='data_fetcher'
)
raise Exception("No price data received")
# ... rest of existing code ...
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,
)
# Send system alert
send_system_notification_task.delay(
alert_title="Bitcoin Price Task Error",
alert_message=f"Error in price fetch task: {str(e)}",
severity='error',
affected_component='celery_task'
)
# Retry the task
self.retry(exc=e, countdown=60)
return {
'success': False,
'error': str(e),
}
# Update run_hourly_analysis_task to send event notifications
@shared_task
def run_hourly_analysis_task():
"""Run hourly market analysis."""
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 send notification
if analysis.is_event:
logger.warning(
f"Market event detected: {analysis.event_type} "
f"at ${analysis.current_price}"
)
# Send email notification
send_event_notification_task.delay(analysis.id)
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}")
# Send system alert
send_system_notification_task.delay(
alert_title="Hourly Analysis Failed",
alert_message=f"Failed to run hourly analysis: {str(e)}",
severity='error',
affected_component='market_analyzer'
)
return {'success': False, 'error': str(e)}

View File

@@ -0,0 +1,199 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Bitcoin Monitor{% endblock %}</title>
<style>
/* Gruvbox color scheme matching dashboard */
:root {
--bg-primary: #282828;
--bg-secondary: #3c3836;
--bg-tertiary: #504945;
--text-primary: #ebdbb2;
--text-secondary: #d5c4a1;
--accent-red: #cc241d;
--accent-yellow: #d79921;
--accent-blue: #458588;
--accent-green: #98971a;
--accent-purple: #b16286;
}
body {
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace, Arial, sans-serif;
line-height: 1.6;
color: #333333;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.email-container {
background: white;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.email-header {
background: var(--bg-primary);
color: var(--text-primary);
padding: 25px;
text-align: center;
}
.email-header h1 {
margin: 0;
font-size: 24px;
font-weight: bold;
}
.email-header .subtitle {
color: var(--text-secondary);
margin-top: 8px;
font-size: 14px;
}
.email-body {
padding: 25px;
}
.alert-banner {
padding: 15px;
border-radius: 8px;
margin-bottom: 25px;
font-weight: bold;
text-align: center;
}
.alert-dip { background: #f8d7da; color: #721c24; border-left: 5px solid var(--accent-red); }
.alert-peak { background: #fff3cd; color: #856404; border-left: 5px solid var(--accent-yellow); }
.alert-system { background: #cce5ff; color: #004085; border-left: 5px solid var(--accent-blue); }
.alert-digest { background: #d4edda; color: #155724; border-left: 5px solid var(--accent-green); }
.metric-card {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
border-left: 4px solid var(--accent-purple);
}
.metric-value {
font-size: 28px;
font-weight: bold;
color: var(--accent-purple);
margin: 10px 0;
}
.metric-label {
color: #6c757d;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 1px;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin: 25px 0;
}
.stat-item {
text-align: center;
padding: 15px;
background: #e9ecef;
border-radius: 6px;
}
.stat-value {
font-size: 20px;
font-weight: bold;
color: var(--bg-primary);
}
.stat-label {
font-size: 12px;
color: #6c757d;
margin-top: 5px;
}
.action-button {
display: inline-block;
background: var(--accent-blue);
color: white;
padding: 12px 24px;
text-decoration: none;
border-radius: 6px;
font-weight: bold;
margin: 15px 0;
}
.recommendation {
background: #e8f4fd;
border-left: 4px solid var(--accent-blue);
padding: 15px;
margin: 20px 0;
border-radius: 0 6px 6px 0;
}
.footer {
background: var(--bg-secondary);
color: var(--text-secondary);
padding: 20px;
text-align: center;
font-size: 12px;
line-height: 1.5;
}
.footer a {
color: var(--accent-purple);
text-decoration: none;
}
.unsubscribe-link {
color: #6c757d !important;
font-size: 11px;
margin-top: 10px;
display: inline-block;
}
@media (max-width: 600px) {
body { padding: 10px; }
.email-body { padding: 15px; }
.stats-grid { grid-template-columns: 1fr; }
.metric-value { font-size: 24px; }
}
</style>
</head>
<body>
<div class="email-container">
<div class="email-header">
<h1>₿ Bitcoin Monitor</h1>
<div class="subtitle">Real-time Bitcoin Price Monitoring</div>
</div>
<div class="email-body">
{% block content %}{% endblock %}
</div>
<div class="footer">
<p>This email was sent by Bitcoin Monitor System.</p>
<p>
<a href="{{ dashboard_url }}">View Dashboard</a> |
<a href="{{ admin_url }}">Admin Panel</a>
</p>
<p>
<a href="{{ unsubscribe_url }}" class="unsubscribe-link">
Unsubscribe or manage preferences
</a>
</p>
<p style="margin-top: 15px; color: #999; font-size: 11px;">
Bitcoin Monitor &copy; {% now "Y" %} • Automated notifications
</p>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,100 @@
{% extends "emails/base.html" %}
{% block title %}Daily Digest - Bitcoin Monitor{% endblock %}
{% block content %}
<div class="alert-banner alert-digest">
📊 DAILY DIGEST: {{ date|date:"F d, Y" }}
</div>
<div style="text-align: center; margin-bottom: 30px;">
<h2 style="color: #343a40; margin-bottom: 5px;">24-Hour Summary</h2>
<p style="color: #6c757d; margin-top: 0;">{{ summary_period }}</p>
</div>
<div class="metric-card">
<div class="metric-label">Market Status</div>
<div class="metric-value" style="color:
{% if market_status == 'dip' %}#dc3545
{% elif market_status == 'peak' %}#ffc107
{% else %}#28a745{% endif %};">
{{ market_status|upper }}
</div>
<div style="color: #6c757d; font-size: 14px;">
{% if market_status == 'dip' %}Price below yearly average
{% elif market_status == 'peak' %}Price above yearly average
{% else %}Price within normal range{% endif %}
</div>
</div>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value">${{ current_price|floatformat:2 }}</div>
<div class="stat-label">Current Price</div>
</div>
<div class="stat-item">
<div class="stat-value">${{ daily_high|floatformat:2 }}</div>
<div class="stat-label">24h High</div>
</div>
<div class="stat-item">
<div class="stat-value">${{ daily_low|floatformat:2 }}</div>
<div class="stat-label">24h Low</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ daily_change|floatformat:1 }}%</div>
<div class="stat-label">24h Change</div>
</div>
</div>
<div style="margin: 30px 0;">
<h3 style="color: #343a40; margin-bottom: 15px;">📈 Market Activity</h3>
<div style="background: #f8f9fa; padding: 15px; border-radius: 6px; margin-bottom: 15px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
<span style="color: #6c757d;">Events Today:</span>
<span style="font-weight: bold;">{{ events_count }}</span>
</div>
<div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
<span style="color: #6c757d;">Price Fetches:</span>
<span style="font-weight: bold;">{{ price_fetches }}</span>
</div>
<div style="display: flex; justify-content: space-between;">
<span style="color: #6c757d;">System Uptime:</span>
<span style="font-weight: bold;">{{ uptime_percentage|floatformat:1 }}%</span>
</div>
</div>
{% if events_today %}
<div style="background: #e8f4fd; padding: 15px; border-radius: 6px; margin-top: 15px;">
<h4 style="margin-top: 0; color: #004085;">⚠️ Events Today</h4>
<ul style="margin-bottom: 0; padding-left: 20px;">
{% for event in events_today %}
<li>
<strong>{{ event.type|title }}</strong> at
${{ event.price|floatformat:2 }} ({{ event.time|time:"H:i" }})
</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
{% if market_insight %}
<div class="recommendation">
<h4 style="margin-top: 0; color: #004085;">📋 Market Insight</h4>
<p style="margin-bottom: 0;">{{ market_insight }}</p>
</div>
{% endif %}
<div style="margin-top: 30px; padding: 15px; background: #f8f9fa; border-radius: 8px; text-align: center;">
<p style="margin: 0; color: #6c757d;">
Next digest: Tomorrow at 08:00 UTC
</p>
</div>
<div style="text-align: center; margin: 30px 0;">
<a href="{{ dashboard_url }}" class="action-button">
View Full Dashboard
</a>
</div>
{% endblock %}

View File

@@ -0,0 +1,83 @@
{% extends "emails/base.html" %}
{% block title %}{{ alert_type }} Alert - Bitcoin Monitor{% endblock %}
{% block content %}
<div class="alert-banner alert-{{ alert_type }}">
{% if alert_type == 'dip' %}
🚨 DIP DETECTED: Price is {{ percent_change|floatformat:1 }}% below average
{% elif alert_type == 'peak' %}
⚡ PEAK DETECTED: Price is {{ percent_change|floatformat:1 }}% above average
{% else %}
MARKET EVENT DETECTED
{% endif %}
</div>
<div class="metric-card">
<div class="metric-label">Current Bitcoin Price</div>
<div class="metric-value">${{ current_price|floatformat:2 }}</div>
<div style="color: #6c757d; font-size: 14px;">
{% if alert_type == 'dip' %}📉 Below threshold{% else %}📈 Above threshold{% endif %}
</div>
</div>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value">{{ threshold_percent }}%</div>
<div class="stat-label">Threshold</div>
</div>
<div class="stat-item">
<div class="stat-value">${{ yearly_average|floatformat:2 }}</div>
<div class="stat-label">Yearly Average</div>
</div>
<div class="stat-item">
<div class="stat-value">${{ lower_threshold|floatformat:2 }}</div>
<div class="stat-label">Lower Bound</div>
</div>
<div class="stat-item">
<div class="stat-value">${{ upper_threshold|floatformat:2 }}</div>
<div class="stat-label">Upper Bound</div>
</div>
</div>
<div style="margin: 25px 0; padding: 20px; background: #f8f9fa; border-radius: 8px;">
<h3 style="margin-top: 0; color: #343a40;">Event Details</h3>
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; color: #6c757d;">Event Type:</td>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; font-weight: bold; text-align: right;">
{{ alert_type|upper }}
</td>
</tr>
<tr>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; color: #6c757d;">Detected At:</td>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; text-align: right;">
{{ detected_at|date:"M d, Y H:i" }} UTC
</td>
</tr>
<tr>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; color: #6c757d;">Price Deviation:</td>
<td style="padding: 8px 0; border-bottom: 1px solid #dee2e6; text-align: right; font-weight: bold;">
{{ percent_change|floatformat:1 }}%
</td>
</tr>
<tr>
<td style="padding: 8px 0; color: #6c757d;">Previous Status:</td>
<td style="padding: 8px 0; text-align: right;">{{ previous_status|default:"N/A"|title }}</td>
</tr>
</table>
</div>
{% if recommendation %}
<div class="recommendation">
<h4 style="margin-top: 0; color: #004085;">💡 Recommendation</h4>
<p style="margin-bottom: 0;">{{ recommendation }}</p>
</div>
{% endif %}
<div style="text-align: center; margin: 30px 0;">
<a href="{{ dashboard_url }}" class="action-button">
View Live Dashboard
</a>
</div>
{% endblock %}

View File

@@ -0,0 +1,52 @@
{% extends "emails/base.html" %}
{% block title %}System Alert - Bitcoin Monitor{% endblock %}
{% block content %}
<div class="alert-banner alert-system">
⚠️ SYSTEM ALERT: {{ alert_title }}
</div>
<div style="margin: 25px 0;">
<h3 style="color: #343a40; margin-top: 0;">Issue Details</h3>
<div style="background: #f8f9fa; padding: 15px; border-radius: 6px; border-left: 4px solid #dc3545;">
<p style="margin: 0; white-space: pre-wrap; font-family: monospace; font-size: 13px;">
{{ alert_message }}
</p>
</div>
</div>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value">{{ affected_component }}</div>
<div class="stat-label">Affected Component</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ severity|upper }}</div>
<div class="stat-label">Severity</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ occurred_at|date:"H:i" }}</div>
<div class="stat-label">Time (UTC)</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ error_code|default:"N/A" }}</div>
<div class="stat-label">Error Code</div>
</div>
</div>
<div style="margin: 25px 0; padding: 20px; background: #fff3cd; border-radius: 8px; border-left: 4px solid #ffc107;">
<h4 style="margin-top: 0; color: #856404;">🛠️ Troubleshooting Steps</h4>
<ul style="margin-bottom: 0; padding-left: 20px;">
{% for step in troubleshooting_steps %}
<li>{{ step }}</li>
{% endfor %}
</ul>
</div>
<div style="text-align: center; margin: 30px 0;">
<a href="{{ admin_url }}" class="action-button" style="background: #6c757d;">
Go to Admin Panel
</a>
</div>
{% endblock %}

View File

@@ -0,0 +1,108 @@
<!DOCTYPE html>
<html>
<head>
<title>Analysis Results</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header {
background: linear-gradient(135deg, #4e54c8 0%, #8f94fb 100%);
color: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
}
.success {
background: #d4edda;
color: #155724;
padding: 15px;
border-radius: 5px;
margin: 20px 0;
border: 1px solid #c3e6cb;
}
.analysis-card {
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 20px;
margin: 15px 0;
background: #f8f9fa;
}
.status-dip { border-left: 5px solid #dc3545; }
.status-peak { border-left: 5px solid #ffc107; }
.status-neutral { border-left: 5px solid #28a745; }
.btn {
display: inline-block;
padding: 10px 20px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 5px;
margin-right: 10px;
margin-top: 20px;
}
.btn:hover {
background: #0056b3;
}
.btn-secondary {
background: #6c757d;
}
.btn-secondary:hover {
background: #545b62;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>✓ Analysis Complete</h1>
<p>Market analysis has been successfully executed</p>
</div>
<div class="success">
<h3>{{ message }}</h3>
</div>
<h2>Analysis Results</h2>
{% for analysis in analyses %}
<div class="analysis-card status-{{ analysis.status }}">
<h3>{{ analysis.period|title }} Analysis</h3>
<p><strong>Status:</strong>
<span style="font-weight: bold;
{% if analysis.status == 'dip' %}color: #dc3545;
{% elif analysis.status == 'peak' %}color: #ffc107;
{% else %}color: #28a745;{% endif %}">
{{ analysis.status|upper }}
</span>
</p>
<p><strong>Current Price:</strong> ${{ analysis.current_price }}</p>
<p><strong>Average Price:</strong> ${{ analysis.average_price }}</p>
<p><strong>Threshold:</strong> {{ analysis.threshold_percent }}%</p>
<p><strong>Range:</strong> ${{ analysis.lower_threshold }} - ${{ analysis.upper_threshold }}</p>
{% if analysis.is_event %}
<p><strong>⚠️ Event Detected:</strong> {{ analysis.event_type|title }}</p>
{% endif %}
<p><small>Analyzed at: {{ analysis.timestamp }}</small></p>
</div>
{% endfor %}
<div style="margin-top: 30px;">
<a href="{% url 'view_analysis' %}" class="btn">View All Analyses</a>
<a href="/" class="btn btn-secondary">Back to Dashboard</a>
<a href="/admin/monitor/marketanalysis/" class="btn btn-secondary">Admin Panel</a>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,294 @@
<!DOCTYPE html>
<!-- Add this script to use API -->
<script>
// Fetch status from API
function fetchApiStatus() {
fetch('/api/status/')
.then(response => response.json())
.then(data => {
// Update status display
document.getElementById('current-price').textContent =
`$${data.current_price.toFixed(2)}`;
document.getElementById('current-status').textContent =
data.current_status.toUpperCase();
document.getElementById('current-status').className =
`status-${data.current_status}`;
// Update stats
document.getElementById('yearly-avg').textContent =
`$${data.yearly_average.toFixed(2)}`;
document.getElementById('yearly-min').textContent =
`$${data.yearly_min.toFixed(2)}`;
document.getElementById('yearly-max').textContent =
`$${data.yearly_max.toFixed(2)}`;
// Show/hide stale warnings
const staleWarning = document.getElementById('stale-warning');
if (data.stale_yearly || data.stale_hourly) {
staleWarning.style.display = 'block';
} else {
staleWarning.style.display = 'none';
}
})
.catch(error => {
console.error('Error fetching status:', error);
});
}
// Fetch events from API
function fetchApiEvents() {
fetch('/api/events/?limit=5')
.then(response => response.json())
.then(data => {
const eventsList = document.getElementById('events-list');
eventsList.innerHTML = '';
data.forEach(event => {
const li = document.createElement('li');
li.innerHTML = `
<strong>${event.event_type.replace('_', ' ').toUpperCase()}</strong>
at $${event.current_price.toFixed(2)}
(${new Date(event.timestamp).toLocaleTimeString()})
`;
eventsList.appendChild(li);
});
});
}
// Auto-refresh API data every 30 seconds
setInterval(() => {
fetchApiStatus();
fetchApiEvents();
}, 30000);
// Initial load
fetchApiStatus();
fetchApiEvents();
</script>
<html>
<head>
<title>Bitcoin Price Monitor</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header {
background: linear-gradient(135deg, #f7931a 0%, #8b4513 100%);
color: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
}
.stats-card {
background: #e9ecef;
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
}
{% if analysis %}
<div style="background: #e8f4fd; padding: 15px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #007bff;">
<h3>Latest Analysis</h3>
<p><strong>Status:</strong>
<span style="font-weight: bold;
{% if analysis.status == 'dip' %}color: #dc3545;
{% elif analysis.status == 'peak' %}color: #ffc107;
{% else %}color: #28a745;{% endif %}">
{{ analysis.status|upper }}
</span>
</p>
<p><strong>Average (Hourly):</strong> ${{ analysis.average_price|floatformat:2 }}</p>
<p><strong>Threshold:</strong> ±{{ analysis.threshold_percent }}%</p>
{% if analysis.is_event %}
<p style="color: #dc3545; font-weight: bold;">
{{ analysis.event_type|title }} event detected!
</p>
{% endif %}
<a href="{% url 'view_analysis' %}" style="color: #007bff; text-decoration: none;">View detailed analysis </a>
</div>
{% endif %}
.price-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.price-table th {
background: #343a40;
color: white;
padding: 12px;
text-align: left;
}
.price-table td {
padding: 12px;
border-bottom: 1px solid #dee2e6;
}
.price-table tr:hover {
background: #f8f9fa;
}
.btn {
background: #28a745;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin-right: 10px;
}
.btn:hover {
background: #218838;
}
.btn-fetch {
background: #007bff;
}
.btn-fetch:hover {
background: #0056b3;
}
.btn-admin {
background: #6c757d;
}
.btn-admin:hover {
background: #545b62;
}
.success {
color: #28a745;
padding: 10px;
background: #d4edda;
border-radius: 5px;
margin: 10px 0;
}
.error {
color: #dc3545;
padding: 10px;
background: #f8d7da;
border-radius: 5px;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>₿ Bitcoin Price Monitor</h1>
<p>Real-time Bitcoin price tracking and historical data</p>
</div>
<div class="stats-card">
<h2>Current Status</h2>
<p><strong>Latest Price:</strong> ${{ stats.latest_price }}</p>
<p><strong>Last Updated:</strong> {{ stats.latest_time }}</p>
<p><strong>Total Records:</strong> {{ stats.total_records }}</p>
<div>
<button class="btn btn-fetch" onclick="fetchPrice()">
Fetch Current Price
</button>
<button class="btn" onclick="location.reload()">
Refresh Data
</button>
<a href="/admin/monitor/bitcoinprice/" class="btn btn-admin">
Admin Panel
</a>
<a href="{% url 'run_analysis' %}" class="btn" style="background: #28a745;">
Run Analysis
</a>
<a href="{% url 'view_analysis' %}" class="btn" style="background: #17a2b8;">
View Analysis
</a>
</div>
<div id="message"></div>
</div>
<h2>Recent Prices (Last 10)</h2>
{% if prices %}
<table class="price-table">
<thead>
<tr>
<th>Timestamp</th>
<th>Price (USD)</th>
<th>Volume</th>
<th>Market Cap</th>
</tr>
</thead>
<tbody>
{% for price in prices %}
<tr>
<td>{{ price.timestamp }}</td>
<td>${{ price.price_usd }}</td>
<td>{% if price.volume %}${{ price.volume }}{% else %}-{% endif %}</td>
<td>{% if price.market_cap %}${{ price.market_cap }}{% else %}-{% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No price data available yet. Click "Fetch Current Price" to get started!</p>
{% endif %}
</div>
<div id="api-status-display" style="margin: 20px 0; padding: 15px; background: #f8f9fa; border-radius: 5px;">
<h3>API Status</h3>
<p><strong>Current Price:</strong> <span id="current-price">Loading...</span></p>
<p><strong>Status:</strong> <span id="current-status" class="status-neutral">Loading...</span></p>
<p><strong>Yearly Average:</strong> <span id="yearly-avg">Loading...</span></p>
<div id="stale-warning" style="display: none; color: #dc3545; padding: 10px; background: #f8d7da; border-radius: 5px;">
⚠️ Data may be stale. Last fetch was more than 1 hour ago.
</div>
</div>
<div style="margin: 20px 0;">
<h3>Recent Events</h3>
<ul id="events-list">
<li>Loading events...</li>
</ul>
</div>
<style>
.status-dip { color: #dc3545; font-weight: bold; }
.status-peak { color: #ffc107; font-weight: bold; }
.status-neutral { color: #28a745; font-weight: bold; }
</style>
<script>
function fetchPrice() {
const messageDiv = document.getElementById('message');
messageDiv.innerHTML = '<p>Fetching current price...</p>';
messageDiv.className = '';
fetch('/fetch-price/')
.then(response => response.json())
.then(data => {
if (data.success) {
messageDiv.innerHTML = `<p class="success">${data.message}</p>`;
// Reload the page after 2 seconds to show new data
setTimeout(() => {
location.reload();
}, 2000);
} else {
messageDiv.innerHTML = `<p class="error">${data.message}</p>`;
}
})
.catch(error => {
messageDiv.innerHTML = `<p class="error">Error: ${error}</p>`;
});
}
// Auto-refresh every 30 seconds
setInterval(() => {
const currentTime = new Date().toLocaleTimeString();
console.log(`Auto-refreshing at ${currentTime}`);
fetchPrice();
}, 30000);
</script>
</body>
</html>

View File

@@ -0,0 +1,195 @@
<!DOCTYPE html>
<html>
<head>
<title>Market Analysis</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1400px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header {
background: linear-gradient(135deg, #4e54c8 0%, #8f94fb 100%);
color: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
}
.summary-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin: 30px 0;
}
.summary-card {
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 20px;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.summary-card h3 {
margin-top: 0;
color: #495057;
border-bottom: 2px solid #e9ecef;
padding-bottom: 10px;
}
.status-badge {
display: inline-block;
padding: 5px 10px;
border-radius: 20px;
font-size: 12px;
font-weight: bold;
margin-left: 10px;
}
.status-dip { background: #dc3545; color: white; }
.status-peak { background: #ffc107; color: #212529; }
.status-neutral { background: #28a745; color: white; }
.analysis-table {
width: 100%;
border-collapse: collapse;
margin: 30px 0;
}
.analysis-table th {
background: #343a40;
color: white;
padding: 12px;
text-align: left;
}
.analysis-table td {
padding: 12px;
border-bottom: 1px solid #dee2e6;
}
.analysis-table tr:hover {
background: #f8f9fa;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 5px;
margin-right: 10px;
margin-bottom: 20px;
}
.btn:hover {
background: #0056b3;
}
.btn-run {
background: #28a745;
}
.btn-run:hover {
background: #218838;
}
.btn-secondary {
background: #6c757d;
}
.btn-secondary:hover {
background: #545b62;
}
.no-data {
text-align: center;
padding: 40px;
color: #6c757d;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📊 Market Analysis Dashboard</h1>
<p>Comprehensive Bitcoin market analysis across different time periods</p>
</div>
<div>
<a href="{% url 'run_analysis' %}" class="btn btn-run">Run New Analysis</a>
<a href="/" class="btn">Back to Dashboard</a>
<a href="/admin/monitor/marketanalysis/" class="btn btn-secondary">Admin Panel</a>
</div>
<h2>Latest Analysis Summary</h2>
{% if summary %}
<div class="summary-grid">
{% for period, data in summary.items %}
<div class="summary-card">
<h3>{{ period|title }} Analysis
<span class="status-badge status-{{ data.status }}">
{{ data.status|upper }}
</span>
</h3>
<p><strong>Current Price:</strong> ${{ data.current_price|floatformat:2 }}</p>
<p><strong>Average Price:</strong> ${{ data.average_price|floatformat:2 }}</p>
<p><strong>Threshold:</strong> {{ data.threshold_percent }}%</p>
{% if data.is_event %}
<p><strong>⚠️ Event Active</strong></p>
{% endif %}
</div>
{% endfor %}
</div>
{% else %}
<div class="no-data">
<h3>No analysis data available</h3>
<p>Click "Run New Analysis" to generate your first analysis report.</p>
</div>
{% endif %}
<h2>Recent Analyses</h2>
{% if all_analyses %}
<table class="analysis-table">
<thead>
<tr>
<th>Time</th>
<th>Period</th>
<th>Status</th>
<th>Current Price</th>
<th>Average Price</th>
<th>Threshold</th>
<th>Event</th>
</tr>
</thead>
<tbody>
{% for analysis in all_analyses %}
<tr>
<td>{{ analysis.timestamp|date:"M d, H:i" }}</td>
<td>{{ analysis.period }}</td>
<td>
<span class="status-badge status-{{ analysis.status }}">
{{ analysis.status|upper }}
</span>
</td>
<td>${{ analysis.current_price|floatformat:2 }}</td>
<td>${{ analysis.average_price|floatformat:2 }}</td>
<td>{{ analysis.threshold_percent }}%</td>
<td>
{% if analysis.is_event %}
<span style="color: #dc3545;">{{ analysis.event_type|title }}</span>
{% else %}
-
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div class="no-data">
<p>No analysis records found. Run an analysis to see results here.</p>
</div>
{% endif %}
</div>
</body>
</html>

3
monitor/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

207
monitor/views.py Normal file
View File

@@ -0,0 +1,207 @@
from django.shortcuts import render
from django.http import JsonResponse
from .models import BitcoinPrice
from datetime import datetime, timezone
import requests
def fetch_bitcoin_price(request):
"""Fetch current Bitcoin price from CoinGecko API and save it."""
try:
# Simple API call to CoinGecko
response = requests.get(
'https://api.coingecko.com/api/v3/simple/price',
params={'ids': 'bitcoin', 'vs_currencies': 'usd'}
)
data = response.json()
if 'bitcoin' in data:
price = data['bitcoin']['usd']
# Save to database
BitcoinPrice.objects.create(
price_usd=price,
timestamp=datetime.now(timezone.utc)
)
analyzer = MarketAnalyzer()
analyzer.analyze_market('hourly')
return JsonResponse({
'success': True,
'price': price,
'message': f'Price saved: ${price}'
})
else:
return JsonResponse({
'success': False,
'message': 'Failed to fetch price'
})
except Exception as e:
return JsonResponse({
'success': False,
'message': f'Error: {str(e)}'
})
from monitor.services.analyzer import MarketAnalyzer
from monitor.models import MarketAnalysis # Add this import
# Add new view functions
def run_analysis(request):
"""Run market analysis."""
analyzer = MarketAnalyzer(threshold_percent=15.0)
# Run analysis for different periods
analyses = []
for period in ['hourly', 'daily', 'yearly']:
analysis = analyzer.analyze_market(period)
if analysis:
analyses.append(analysis)
context = {
'analyses': analyses,
'message': f'Ran {len(analyses)} analyses successfully',
}
return render(request, 'monitor/analysis_result.html', context)
def view_analysis(request):
"""View latest market analysis."""
analyzer = MarketAnalyzer(threshold_percent=15.0)
# Get latest analyses
hourly = analyzer.get_latest_analysis('hourly')
daily = analyzer.get_latest_analysis('daily')
yearly = analyzer.get_latest_analysis('yearly')
# Get analysis summary
summary = analyzer.get_analysis_summary()
# Get all analyses for the table
all_analyses = MarketAnalysis.objects.all().order_by('-timestamp')[:20]
context = {
'hourly_analysis': hourly,
'daily_analysis': daily,
'yearly_analysis': yearly,
'summary': summary,
'all_analyses': all_analyses,
}
return render(request, 'monitor/view_analysis.html', context)
# Update bitcoin_data view to include analysis
def bitcoin_data(request):
"""Display Bitcoin data from database."""
# Get latest 10 prices
latest_prices = BitcoinPrice.objects.all()[:10]
# Calculate basic stats if we have data
if latest_prices:
latest_price = latest_prices[0]
stats = {
'latest_price': latest_price.price_usd,
'latest_time': latest_price.timestamp,
'total_records': BitcoinPrice.objects.count(),
}
# Get latest hourly analysis
analyzer = MarketAnalyzer()
hourly_analysis = analyzer.get_latest_analysis('hourly')
else:
stats = {
'latest_price': 'No data',
'latest_time': 'No data',
'total_records': 0,
}
hourly_analysis = None
return render(request, 'monitor/bitcoin_data.html', {
'prices': latest_prices,
'stats': stats,
'analysis': hourly_analysis, # Add analysis to context
})
# Add these imports at the top
# Add new view functions
def run_analysis(request):
"""Run market analysis."""
analyzer = MarketAnalyzer(threshold_percent=15.0)
# Run analysis for different periods
analyses = []
for period in ['hourly', 'daily', 'yearly']:
analysis = analyzer.analyze_market(period)
if analysis:
analyses.append(analysis)
context = {
'analyses': analyses,
'message': f'Ran {len(analyses)} analyses successfully',
}
return render(request, 'monitor/analysis_result.html', context)
def view_analysis(request):
"""View latest market analysis."""
analyzer = MarketAnalyzer(threshold_percent=15.0)
# Get latest analyses
hourly = analyzer.get_latest_analysis('hourly')
daily = analyzer.get_latest_analysis('daily')
yearly = analyzer.get_latest_analysis('yearly')
# Get analysis summary
summary = analyzer.get_analysis_summary()
# Get all analyses for the table
all_analyses = MarketAnalysis.objects.all().order_by('-timestamp')[:20]
context = {
'hourly_analysis': hourly,
'daily_analysis': daily,
'yearly_analysis': yearly,
'summary': summary,
'all_analyses': all_analyses,
}
return render(request, 'monitor/view_analysis.html', context)
# Update bitcoin_data view to include analysis
def bitcoin_data(request):
"""Display Bitcoin data from database."""
# Get latest 10 prices
latest_prices = BitcoinPrice.objects.all()[:10]
# Calculate basic stats if we have data
if latest_prices:
latest_price = latest_prices[0]
stats = {
'latest_price': latest_price.price_usd,
'latest_time': latest_price.timestamp,
'total_records': BitcoinPrice.objects.count(),
}
# Get latest hourly analysis
analyzer = MarketAnalyzer()
hourly_analysis = analyzer.get_latest_analysis('hourly')
else:
stats = {
'latest_price': 'No data',
'latest_time': 'No data',
'total_records': 0,
}
hourly_analysis = None
return render(request, 'monitor/bitcoin_data.html', {
'prices': latest_prices,
'stats': stats,
'analysis': hourly_analysis, # Add analysis to context
})