def index(request):
    db = Database()
    dbmx = DatabaseMX()

    report = dict(
        total_mails=dbmx.count_mails(),
        total_malwares=dbmx.count_malwares(),
        total_attachments=dbmx.count_attachments(),
        total_urls=dbmx.count_urls(),
        mails_have_malwares=dbmx.get_mails_have_malwares(),

        total_samples=db.count_samples(),
        total_tasks=db.count_tasks(),
        states_count={},
        estimate_hour=None,
        estimate_day=None
    )
    
    states = (
        TASK_PENDING,
        TASK_RUNNING,
        TASK_COMPLETED,
        TASK_RECOVERED,
        TASK_REPORTED,
        TASK_FAILED_ANALYSIS,
        TASK_FAILED_PROCESSING,
        TASK_FAILED_REPORTING
    )

    for state in states:
        report["states_count"][state] = db.count_tasks(state)

    offset = None

    # For the following stats we're only interested in completed tasks.
    tasks = db.list_tasks(offset=offset, status=TASK_COMPLETED)
    tasks += db.list_tasks(offset=offset, status=TASK_REPORTED)

    if tasks:
        # Get the time when the first task started.
        started = min(timestamp(task.started_on) for task in tasks)

        # Get the time when the last task completed.
        completed = max(timestamp(task.completed_on) for task in tasks)

        # Get the amount of tasks that actually completed.
        finished = len(tasks)

        hourly = 60 * 60 * finished / (completed - started)

        report["estimate_hour"] = int(hourly)
        report["estimate_day"] = int(24 * hourly)

    return render_to_response("cuckoomx/index.html",
                              {"report" : report},
                              context_instance=RequestContext(request))