Metadata-Version: 2.4
Name: djinsight
Version: 0.3.1
Summary: MCP-first analytics for Django/Wagtail package for tracking page view statistics with Redis and async processing
Home-page: https://github.com/krystianmagdziarz/djinsight
Author: MDigital
Author-email: MDigital <kontakt@mdigital.com.pl>
Maintainer-email: MDigital <kontakt@mdigital.com.pl>
License: MIT License
        
        Copyright (c) 2023 Your Name
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/krystianmagdziarz/djinsight
Project-URL: Documentation, https://djinsight.readthedocs.io/
Project-URL: Repository, https://github.com/krystianmagdziarz/djinsight.git
Project-URL: Bug Tracker, https://github.com/krystianmagdziarz/djinsight/issues
Project-URL: Changelog, https://github.com/krystianmagdziarz/djinsight/blob/main/CHANGELOG.md
Keywords: django,wagtail,analytics,statistics,page-views,redis,celery
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.2
Classifier: Framework :: Django :: 4.0
Classifier: Framework :: Django :: 4.1
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Wagtail
Classifier: Framework :: Wagtail :: 3
Classifier: Framework :: Wagtail :: 4
Classifier: Framework :: Wagtail :: 5
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=3.2
Requires-Dist: wagtail>=3.0
Requires-Dist: redis>=4.0.0
Requires-Dist: celery>=5.0.0
Requires-Dist: django-redis>=5.0.0
Requires-Dist: django-environ>=0.9.0
Provides-Extra: dev
Requires-Dist: pytest>=6.0; extra == "dev"
Requires-Dist: pytest-django>=4.0; extra == "dev"
Requires-Dist: pytest-cov>=3.0; extra == "dev"
Requires-Dist: black>=22.0; extra == "dev"
Requires-Dist: flake8>=4.0; extra == "dev"
Requires-Dist: isort>=5.0; extra == "dev"
Requires-Dist: mypy>=0.900; extra == "dev"
Requires-Dist: factory-boy>=3.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=4.0; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=1.0; extra == "docs"
Provides-Extra: test
Requires-Dist: pytest>=6.0; extra == "test"
Requires-Dist: pytest-django>=4.0; extra == "test"
Requires-Dist: pytest-cov>=3.0; extra == "test"
Requires-Dist: factory-boy>=3.0; extra == "test"
Requires-Dist: coverage>=6.0; extra == "test"
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# djinsight

**MCP-first analytics for Django** - Expose your Django app statistics to Claude and other AI agents through Model Context Protocol.

[![PyPI version](https://badge.fury.io/py/djinsight.svg)](https://badge.fury.io/py/djinsight)
[![Python](https://img.shields.io/pypi/pyversions/djinsight.svg)](https://pypi.org/project/djinsight/)
[![Django](https://img.shields.io/badge/Django-3.2%20%7C%204.x%20%7C%205.x-green.svg)](https://www.djangoproject.com/)

## Why djinsight?

**Built for AI-first workflows.** djinsight is the first Django analytics package designed with MCP (Model Context Protocol) as a primary interface. Claude, other AI agents, or automation tools can query your app's analytics directly.

## Quick Start

```bash
pip install djinsight
```

### Option 1: Synchronous (No Redis/Celery)

**Direct database writes - simplest setup:**

```python
# settings.py
INSTALLED_APPS = ['djinsight']
MIDDLEWARE = ['djinsight.middleware.TrackingMiddleware']

DJINSIGHT = {
    'ENABLE_TRACKING': True,
    'USE_REDIS': False,  # Direct to database
    'USE_CELERY': False,
}
```

### Option 2: Async with Redis + Celery (Recommended)

**High-performance buffered writes:**

```python
# settings.py
INSTALLED_APPS = ['djinsight']
MIDDLEWARE = ['djinsight.middleware.TrackingMiddleware']

DJINSIGHT = {
    'ENABLE_TRACKING': True,
    'USE_REDIS': True,   # Buffer in Redis
    'USE_CELERY': True,  # Process with Celery
    'REDIS_HOST': 'localhost',
    'REDIS_PORT': 6379,
}
```

**Start Celery worker:**
```bash
celery -A your_project worker -l info
celery -A your_project beat -l info
```

### Register Models

```python
# blog/apps.py
from django.apps import AppConfig

class BlogConfig(AppConfig):
    name = 'blog'

    def ready(self):
        from djinsight.models import ContentTypeRegistry
        from blog.models import Article
        ContentTypeRegistry.register(Article)
```

### Use in Templates

```django
{% load djinsight_tags %}

{% stats metric="views" period="week" output="chart" %}
{% stats metric="unique_views" period="today" output="badge" %}
```

## Configuration Modes

| Feature | Synchronous | Async (Redis+Celery) |
|---------|-------------|---------------------|
| **Setup** | ✅ Simple | ⚙️ Requires Redis+Celery |
| **Dependencies** | None | Redis, Celery |
| **Performance** | Good | Excellent |
| **Request blocking** | ~5ms | <1ms |
| **Batch processing** | ❌ No | ✅ Yes |
| **Recommended for** | Small sites, dev | Production, high traffic |

**Switch modes anytime:**
```python
DJINSIGHT = {
    'USE_REDIS': False,  # False = sync, True = async
}
```

## MCP Integration (Claude Desktop)

**Works with both local and remote Django servers:**

1. **Create API key** in Django admin (`/admin/djinsight/mcpapikey/`)

2. **Install djinsight-mcp** (local package):

```bash
cd /path/to/djinsight/mcp-package
npm install
```

3. **Configure Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "djinsight": {
      "command": "node",
      "args": ["/path/to/djinsight/mcp-package/index.js"],
      "env": {
        "DJINSIGHT_URL": "http://localhost:8001",
        "DJINSIGHT_API_KEY": "YOUR_API_KEY_HERE"
      }
    }
  }
}
```

4. **Restart Claude Desktop**

**Available MCP tools:**
- `get_page_stats` - Get statistics for specific object  
- `get_top_pages` - Get top performing pages
- `get_period_stats` - Get time period statistics  
- `list_tracked_models` - List all tracked models

### Example Claude Interaction

```
You: "What are my top 5 blog articles this week?"

Claude: [Uses get_top_pages tool]
"Your top articles:
1. 'Django Performance Tips' - 1,247 views (823 unique)
2. 'Redis Caching Guide' - 891 views (654 unique)
3. 'API Design Patterns' - 743 views (521 unique)
4. 'Docker for Django' - 612 views (445 unique)
5. 'Testing Best Practices' - 502 views (389 unique)"

You: "Show me detailed stats for article #1"

Claude: [Uses get_page_stats + get_period_stats]
"Django Performance Tips (ID: 1):
- Total views: 3,452
- Unique visitors: 2,103
- First viewed: 2024-11-15
- Last viewed: 2 hours ago
- This week: 1,247 views"
```

## Universal Stats Tag

```django
{% stats metric="views" period="week" output="chart" chart_type="line" %}
```

**Parameters:**
- `metric`: `views`, `unique_views`
- `period`: `today`, `week`, `month`, `year`, `last_year`, `custom`, `total`
- `output`: `text`, `chart`, `json`, `widget`, `badge`
- `chart_type`: `line`, `bar`

## Extensibility

Everything is swappable via settings:

```python
DJINSIGHT = {
    'WIDGET_RENDERER': 'myapp.renderers.CustomRenderer',
    'CHART_RENDERER': 'myapp.renderers.CustomChartRenderer',
    'PROVIDER_CLASS': 'myapp.providers.PostgreSQLProvider',
    'MIDDLEWARE_CLASS': 'myapp.middleware.CustomTracking',
}
```

**Custom renderer:**

```python
from djinsight.renderers import BaseRenderer

class CustomRenderer(BaseRenderer):
    def render(self):
        data = self.get_data()
        return f"<div>Views: {data.get('total_views', 0)}</div>"
```

**Custom provider (replace Redis):**

```python
from djinsight.providers.base import BaseProvider

class PostgreSQLProvider(BaseProvider):
    async def record_view(self, event_data):
        # Direct PostgreSQL writes instead of Redis buffering
        pass
```

## Upgrading

See [MIGRATION_GUIDE.md](MIGRATION_GUIDE.md) for v0.1.x → v0.2.0 migration steps.

## License

MIT

## Links

- [Documentation](https://github.com/krystianmagdziarz/djinsight)
- [Issues](https://github.com/krystianmagdziarz/djinsight/issues)
- [CHANGELOG](CHANGELOG.md)
