Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add email admin command #502

Merged
merged 2 commits into from Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions bims/management/commands/send_tracking_data_to_admins.py
@@ -0,0 +1,69 @@
# coding=utf-8
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.core.mail import mail_admins
from django.template.loader import get_template
from django.utils.timezone import now
from bims.conf import TRACK_PAGEVIEWS
from bims.models import Taxon, BiologicalCollectionRecord, Visitor, Pageview


class Command(BaseCommand):
help = "Script to send tracking data of BIMS site to Admins."

def add_arguments(self, parser):
parser.add_argument(
'--from',
dest='from',
help='Start date of tracking data',
)

parser.add_argument(
'--to',
dest='to',
help='End date of tracking data',
)

def handle(self, *args, **options):
date_to = now()
date_from = date_to - timedelta(days=7)
if options['from']:
date_from = options['from']

if options['to']:
date_to = options['to']

taxon_count = Taxon.objects.count()
collections_count = \
BiologicalCollectionRecord.objects.filter(validated=True).count()

html = get_template('tracking/email.html')
user_stats = Visitor.objects.user_stats(date_from, date_to)
visitor_stats = Visitor.objects.stats(date_from, date_to)
if TRACK_PAGEVIEWS:
pageview_stats = Pageview.objects.stats(date_from, date_to)
else:
pageview_stats = None
context = {
'user_stats': user_stats,
'visitor_stats': visitor_stats,
'pageview_stats': pageview_stats,
'taxon_count': taxon_count,
'collections_count': collections_count,
'date_from': date_from,
'date_to': date_to
}

html_content = html.render(context)

mail_admins(
'BIMS site activity report',
'Dear Admin\n'
'The following data is the activity report of the BIMS site '
'from {} to {}:\n'
'Current taxon count: {}\n'
'Current collections count: {}\n'
'Kind regards,\n'
'BIMS'.format(date_from, date_to, taxon_count, collections_count),
html_message=html_content
)
51 changes: 51 additions & 0 deletions bims/tasks/email_admins.py
@@ -0,0 +1,51 @@
# coding=utf-8
from celery import shared_task


@shared_task(name='bims.tasks.email_admins', queue='update')
def email_admins():
from datetime import timedelta
from django.core.mail import mail_admins
from django.template.loader import get_template
from django.utils.timezone import now
from bims.conf import TRACK_PAGEVIEWS
from bims.models import Taxon, BiologicalCollectionRecord, Visitor, \
Pageview

date_to = now()
date_from = date_to - timedelta(days=7)

taxon_count = Taxon.objects.count()
collections_count = \
BiologicalCollectionRecord.objects.filter(validated=True).count()

html = get_template('tracking/email.html')
user_stats = Visitor.objects.user_stats(date_from, date_to)
visitor_stats = Visitor.objects.stats(date_from, date_to)
if TRACK_PAGEVIEWS:
pageview_stats = Pageview.objects.stats(date_from, date_to)
else:
pageview_stats = None
context = {
'user_stats': user_stats,
'visitor_stats': visitor_stats,
'pageview_stats': pageview_stats,
'taxon_count': taxon_count,
'collections_count': collections_count,
'date_from': date_from,
'date_to': date_to
}

html_content = html.render(context)

mail_admins(
'BIMS site activity report',
'Dear Admin\n'
'The following data is the activity report of the BIMS site '
'from {} to {}:\n'
'Current taxon count: {}\n'
'Current collections count: {}\n'
'Kind regards,\n'
'BIMS'.format(date_from, date_to, taxon_count, collections_count),
html_message=html_content
)
126 changes: 126 additions & 0 deletions bims/templates/tracking/email.html
@@ -0,0 +1,126 @@
<!DOCTYPE html>
<html lang="en">
<body>
<p style="font-size: 16pt">Report statistics from {{ date_from }} to {{ date_to }}</p>
<h2>Database Statistics</h2>
<span style="font-size: 14pt;">Taxon records: <span style="padding-right: 2px;"><b>{{ taxon_count }}</b></span></span><br/>
<span style="font-size: 14pt;">Total validated collections: <span style="padding-right: 2px;"><b>{{ collections_count }}</b></span></span>
<br/>
<h2>Visitors</h2>
{% if visitor_stats.total %}
<table style="text-align: center; border: 1px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 1px solid black; padding: 5px 10px"></th>
<th style="border: 1px solid black; padding: 5px 10px">Total</th>
<th style="border: 1px solid black; padding: 5px 10px">Unique</th>
<th style="border: 1px solid black; padding: 5px 10px">% Returning Visitor</th>
<th style="border: 1px solid black; padding: 5px 10px">Avg. Time on Site</th>
{% if pageview_stats %}
<th style="border: 1px solid black; padding: 5px">Avg. Pages/Visit</th>
{% endif %}
</tr>
</thead>
<tbody>
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Registered</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.registered.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.registered.unique }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.registered.return_ratio|floatformat }}%</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.registered.time_on_site|default_if_none:"n/a" }}</td>
{% if pageview_stats %}
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.registered.pages_per_visit|floatformat|default:"n/a" }}</td>
{% endif %}
</tr>

{% if visitor_stats.guests %}
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Guests</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.guests.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.guests.unique }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.guests.return_ratio|floatformat }}%</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.guests.time_on_site|default_if_none:"n/a" }}</td>
{% if pageview_stats %}
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.guests.pages_per_visit|floatformat|default:"n/a" }}</td>
{% endif %}
</tr>
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Total</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.unique }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.return_ratio|floatformat }}%</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.time_on_site|default_if_none:"n/a" }}</td>
{% if pageview_stats %}
<td style="border: 1px solid black; padding: 5px 10px">{{ visitor_stats.pages_per_visit|floatformat|default:"n/a" }}</td>
{% endif %}
</tr>
{% endif %}
</tbody>
</table>
{% else %}
<p>No visitor stats available</p>
{% endif %}

<h2>Registered Users</h2>
{% if user_stats %}
<table style="text-align: center; border: 1px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 1px solid black; padding: 5px 10px"></th>
<th style="border: 1px solid black; padding: 5px 10px"># Visits</th>
<th style="border: 1px solid black; padding: 5px 10px">Avg. Time on Site</th>
<th style="border: 1px solid black; padding: 5px 10px">Avg. Pages/Visit</th>
</tr>
</thead>
<tbody>
{% for user in user_stats %}
<tr>
<th style="border: 1px solid black; padding: 5px 10px">{% firstof user.get_full_name user %}</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ user.visit_count }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ user.time_on_site|default_if_none:"n/a" }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ user.pages_per_visit|floatformat|default:"n/a" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No registered user stats available</p>
{% endif %}

{% if pageview_stats %}
<h2>Pageviews</h2>
{% if pageview_stats.total %}
<table style="text-align: center; border: 1px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 1px solid black; padding: 5px 10px"></th>
<th style="border: 1px solid black; padding: 5px 10px">Total</th>
<th style="border: 1px solid black; padding: 5px 10px">Unique</th>
</tr>
</thead>
<tbody>
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Registered</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.registered.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.registered.unique }}</td>
</tr>
{% if pageview_stats.guests %}
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Guests</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.guests.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.guests.unique }}</td>
</tr>
<tr>
<th style="border: 1px solid black; padding: 5px 10px">Total</th>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.total }}</td>
<td style="border: 1px solid black; padding: 5px 10px">{{ pageview_stats.unique }}</td>
</tr>
{% endif %}
</tbody>
</table>
{% else %}
<p>No pageview stats available</p>
{% endif %}
{% endif %}
</body>
</html>
4 changes: 4 additions & 0 deletions core/settings/celery_settings.py
Expand Up @@ -8,6 +8,10 @@
'task': 'bims.tasks.update_cluster',
'schedule': 18000.0, # update every 5 hours
},
'email_admins': {
'task': 'bims.tasks.email_admins',
'schedule': 604800.0, # send email to admins every 7 days
}
}

CELERY_TIMEZONE = 'UTC'
4 changes: 3 additions & 1 deletion core/settings/dev_docker.py
Expand Up @@ -5,7 +5,9 @@
ALLOWED_HOSTS = ['*',
u'0.0.0.0']

ADMINS = ()
ADMINS = (
('Anita Hapsari', 'anita@kartoza.com'),
)

# Set debug to True for development
DEBUG = True
Expand Down