dancerfly / django-brambling

Event website manager, specifically designed for dance weekends or other events with multiple simultaneous tracks of classes.
BSD 3-Clause "New" or "Revised" License
11 stars 3 forks source link

Dancerfly-wide report generation #928

Open melinath opened 5 years ago

melinath commented 5 years ago

It's useful to have data on how Dancerfly is doing. We should have a standard way to generate these kinds of reports. For now I'll keep scripts here for generating reports.

melinath commented 5 years ago
# published events run by orgs that have previous events
# published events run by orgs that don't have previous events

from django.db.models import Count
from django.db import connection
truncate_date = connection.ops.date_trunc_sql('month', 'start_date')

from collections import OrderedDict

from brambling.models import Event
events = Event.objects.annotate(txn_count=Count('transaction')).filter(is_published=True, txn_count__gt=0, api_type='live').extra({'month': truncate_date}).order_by('month')
data = OrderedDict()
seen_orgs = set()

import datetime
for year in range(2015, 2020):
    for month in range(1, 13):
        first_day = datetime.date(year, month, 1)
        data[first_day.strftime('%Y-%m-%d')] = {
            'repeat': 0,
            'new': 0,
        }

for event in events:
    month_data = data[event.month.strftime('%Y-%m-%d')]
    if event.organization_id in seen_orgs:
        month_data['repeat'] += 1
    else:
        month_data['new'] += 1
        seen_orgs.add(event.organization_id)

import csv
from io import BytesIO

out = BytesIO()
writer = csv.writer(out)
writer.writerow(['Month', 'New events', 'Repeat events'])
for month, month_data in data.items():
    _ = writer.writerow([month, month_data['new'], month_data['repeat']])

out.seek(0)
print out.read()
melinath commented 5 years ago
from django.db.models import Sum, Count
from django.db import connection
from brambling.models import Transaction
truncate_date = connection.ops.date_trunc_sql('month', 'timestamp')
txns = Transaction.objects.extra({'month': truncate_date})
x = txns.values_list('month').annotate(Sum('amount'), Sum('application_fee'), Sum('processing_fee'), Count('id')).order_by('month')
import csv
from io import BytesIO
out = BytesIO()
writer = csv.writer(out)
writer.writerow(['Month', 'Amount', 'Application fee', 'Processing fee', 'Count'])

for txn in x:
 _ = writer.writerow([txn[0].strftime('%Y-%m-%d'), txn[1], txn[2], txn[3], txn[4]])

out.seek(0)
print out.read()