"""Aggregate Kevin Hillstrom's 2008 MineThatData randomized email test.

Run: python analysis_hillstrom.py hillstorm_no_indices.csv.gz
The source archive is linked in the methods. This script exports only arm-level totals.
"""
from collections import defaultdict
import csv
from decimal import Decimal
import gzip
from hashlib import sha256
import json
from pathlib import Path
import sys


def analyze(path):
    groups = defaultdict(lambda: {'assigned':0,'visits':0,'purchases':0,'spend':Decimal('0')})
    reader = gzip.open if str(path).endswith('.gz') else open
    with reader(path,'rt',encoding='utf-8',newline='') as source:
        for row in csv.DictReader(source):
            values = groups[row['segment']]
            values['assigned'] += 1
            values['visits'] += int(row['visit'])
            values['purchases'] += int(row['conversion'])
            values['spend'] += Decimal(row['spend'])
    assert set(groups)=={'No E-Mail','Mens E-Mail','Womens E-Mail'}
    assert sum(g['assigned'] for g in groups.values())==64000
    for row in groups.values():
        row['visit_pct'] = round(100*row['visits']/row['assigned'],5)
        row['purchase_pct'] = round(100*row['purchases']/row['assigned'],5)
        row['spend_per_assigned'] = round(float(row['spend']/row['assigned']),5)
        row['spend'] = float(row['spend'])
    return {'sha256':sha256(Path(path).read_bytes()).hexdigest(), 'groups':dict(groups)}


if __name__=='__main__':
    print(json.dumps(analyze(sys.argv[1]),indent=2))
