Skip to content

Vis1hal/alerting-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Alerting & Notification Platform

A Django-based alerting and notification system that allows administrators to create and manage alerts with configurable visibility and automatic reminders, while providing users with the ability to interact with their alerts.

Features

  • Admin Alert Management: Create, update, and manage alerts with flexible visibility settings
  • User Alert Interface: View, read, and snooze personal alerts
  • Automated Reminders: Background tasks send reminders every 2 hours until snoozed or expired
  • Flexible Visibility: Target entire organization, specific teams, or individual users
  • Analytics Dashboard: Track alert engagement and delivery statistics
  • Extensible Architecture: Clean OOP design with strategy patterns for future enhancements

Technical Stack

  • Backend: Django 4.2+ with Django REST Framework
  • Database: SQLite (easily configurable for PostgreSQL/MySQL)
  • Background Tasks: Celery with Redis broker
  • Authentication: Token-based authentication
  • API: RESTful APIs with proper serialization

Quick Start

Prerequisites

  • Python 3.8+
  • Redis server (for Celery background tasks)

Installation

  1. Clone and setup project:
git clone <repository-url>
cd alerting_platform

# Create virtual environment
python -m venv alerting_env
source alerting_env/bin/activate  # Linux/Mac
# alerting_env\Scripts\activate    # Windows

# Install dependencies
pip install django djangorestframework celery redis django-cors-headers python-decouple
  1. Database setup:
python manage.py makemigrations users
python manage.py makemigrations alerts
python manage.py makemigrations notifications
python manage.py migrate
  1. Create superuser:
python manage.py createsuperuser
  1. Load test data:
python manage.py setup_test_data
  1. Start Redis (required for background tasks):
# Using Docker
docker run -p 6379:6379 redis

# Or install Redis locally and start it
redis-server
  1. Start Django server:
python manage.py runserver
  1. Start Celery worker (in separate terminal):
celery -A alerting_platform worker --loglevel=info
  1. Start Celery beat scheduler (in another terminal):
celery -A alerting_platform beat --loglevel=info

API Documentation

Authentication

All API endpoints require authentication. Get a token:

curl -X POST http://localhost:8000/api/auth/token/ \
     -H "Content-Type: application/json" \
     -d '{"username":"admin","password":"admin123"}'

Use the token in subsequent requests:

curl -H "Authorization: Token YOUR_TOKEN_HERE" http://localhost:8000/api/admin/alerts/

Admin APIs

Base URL: /api/admin/alerts/

Create Alert

POST /api/admin/alerts/
Content-Type: application/json
Authorization: Token YOUR_TOKEN

{
    "title": "System Maintenance",
    "message": "Scheduled maintenance tonight from 2 AM to 4 AM",
    "severity": "warning",
    "visibility_type": "organization",
    "start_time": "2024-01-01T00:00:00Z",
    "expiry_time": "2024-01-02T00:00:00Z",
    "reminders_enabled": true
}

List Alerts

GET /api/admin/alerts/
Authorization: Token YOUR_TOKEN

# Optional filters:
GET /api/admin/alerts/?severity=critical&is_active=true&visibility_type=team

Update Alert

PUT /api/admin/alerts/{id}/
Content-Type: application/json
Authorization: Token YOUR_TOKEN

{
    "title": "Updated Title",
    "is_active": false
}

Analytics

GET /api/admin/alerts/analytics/
Authorization: Token YOUR_TOKEN

User APIs

Get User Alerts

GET /api/user/alerts/
Authorization: Token YOUR_TOKEN

# Optional filters:
GET /api/user/alerts/?state=unread

Mark Alert as Read

POST /api/user/alerts/{alert_id}/read/
Authorization: Token YOUR_TOKEN

Mark Alert as Unread

POST /api/user/alerts/{alert_id}/unread/
Authorization: Token YOUR_TOKEN

Snooze Alert

POST /api/user/alerts/{alert_id}/snooze/
Authorization: Token YOUR_TOKEN

User Dashboard

GET /api/user/dashboard/
Authorization: Token YOUR_TOKEN

Test Data

The setup_test_data command creates:

Teams:

  • Engineering
  • Marketing
  • Operations

Users:

  • admin (admin123) - Administrator
  • john_eng (password123) - Engineering team member
  • sarah_eng (password123) - Engineering team member
  • mike_marketing (password123) - Marketing team member
  • lisa_ops (password123) - Operations team member

Sample Alerts:

  • Organization-wide maintenance notice
  • Team-specific security update
  • User-specific database migration notice

Architecture

Models

  • User: Extended Django user with team association and admin flag
  • Team: Organizational units for targeting alerts
  • Alert: Core alert entity with timing and targeting
  • UserAlertPreference: User-specific alert state and snooze management
  • NotificationDelivery: Audit log of sent notifications

Services

  • AlertService: Business logic for alert management and targeting
  • NotificationService: Handles delivery and reminder logic

Background Tasks

  • send_reminders: Runs every 2 hours, sends reminders to users with unread alerts
  • reset_daily_snoozes: Runs daily at midnight, resets day-long snoozes

Design Patterns

  • Strategy Pattern: Notification channels (in-app, email, SMS)
  • Service Layer: Business logic separation from views
  • Factory Pattern: Channel creation and management

Extending the System

Adding New Notification Channels

  1. Create new channel class:
# notifications/channels.py
class SlackChannel(NotificationChannel):
    def send(self, alert, user):
        # Implement Slack integration
        pass
    
    def get_channel_name(self):
        return 'slack'
  1. Register in factory:
# notifications/channels.py
class NotificationChannelFactory:
    _channels = {
        'in_app': InAppChannel,
        'email': EmailChannel,
        'sms': SMSChannel,
        'slack': SlackChannel,  # Add new channel
    }

Adding Custom Reminder Frequencies

Modify the Alert model and update the reminder logic in the Celery task.

Testing

Manual Testing

  1. Visit Django Admin: http://localhost:8000/admin/
  2. API Status: http://localhost:8000/api/status/
  3. DRF Browsable API: http://localhost:8000/api/admin/alerts/

API Testing with cURL

# Get token
TOKEN=$(curl -s -X POST http://localhost:8000/api/auth/token/ \
         -H "Content-Type: application/json" \
         -d '{"username":"admin","password":"admin123"}' | \
         python -c "import sys, json; print(json.load(sys.stdin)['token'])")

# Test admin APIs
curl -H "Authorization: Token $TOKEN" http://localhost:8000/api/admin/alerts/
curl -H "Authorization: Token $TOKEN" http://localhost:8000/api/admin/alerts/analytics/

# Test user APIs  
curl -H "Authorization: Token $TOKEN" http://localhost:8000/api/user/alerts/
curl -H "Authorization: Token $TOKEN" http://localhost:8000/api/user/dashboard/

Troubleshooting

Common Issues

403 Forbidden on admin endpoints: Ensure user has is_admin=True

# In Django shell
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(username='your_username')
user.is_admin = True
user.save()

Celery tasks not running: Ensure Redis is running and Celery worker/beat are started

No reminders being sent: Check Celery logs and verify alerts have reminders_enabled=True

Logs

  • Django: Check console output when running python manage.py runserver
  • Celery: Check worker and beat terminal outputs
  • Redis: Use redis-cli monitor to see Redis operations

Production Deployment

Environment Variables

# .env file
DEBUG=False
SECRET_KEY=your-production-secret-key
DATABASE_URL=postgresql://user:pass@localhost/dbname
REDIS_URL=redis://localhost:6379/0
ALLOWED_HOSTS=yourdomain.com

Database

Configure PostgreSQL or MySQL in settings for production use.

Static Files

python manage.py collectstatic

Process Management

Use supervisord or similar to manage Django, Celery worker, and Celery beat processes.

License

This project is created as a technical assessment and demonstration of Django development skills.

About

# πŸ”” Django Alerting & Notification Platform A production-ready enterprise alerting system built with Django and Django REST Framework. Designed to solve the common problem of missed organizational notifications through intelligent reminder systems and flexible user controls.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors