#!/usr/bin/env python

import os.path
import time
import json

import numpy
import random

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "docbow_project.settings")

from django.contrib.auth.models import User
from django.test.client import RequestFactory

from docbow_project.docbow.models import FileType
from docbow_project.docbow.views import inbox_view, outbox_view, send_file


def stat(seq):
    return {
        'min': min(seq),
        'max': max(seq),
        'mean': numpy.mean(seq),
        'median': numpy.median(seq),
        'std': numpy.std(seq),
    }


rf = RequestFactory()
paths = [('/inbox/', inbox_view, {}), ('/outbox/', outbox_view, {})]
filetypes = list(FileType.objects.all())
random.shuffle(filetypes)
for file_type in filetypes[:10]:
    paths.append(('/inbox/%s/' % file_type.id, send_file, {'file_type_id': file_type.id}))
data = []
for path, view, kwargs in paths:
    get_request = rf.get(path)
    get_request.session = {}
    print('Testing view', path, 'for all users:')
    seq = []
    for user in User.objects.all():
        get_request.user = user
        now = time.time()
        response = view(get_request, **kwargs)
        if hasattr(response, 'render'):
            response.render()
        str(response)
        duration = time.time() - now
        seq.append(duration * 1000)
    data.append(stat(seq))
    data[-1]['path'] = path

with file('vix.js', 'w') as f:
    f.write(
        '''var vix = 
'''
    )
    f.write(json.dumps(data, indent=4))
    f.write(';')
