summaryrefslogtreecommitdiffstats
path: root/lib/mongo.py
blob: b466df15107c7fe346ddff950c007351b3e0f781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#-*- coding: utf-8 -*-

import pymongo
from bunch import Bunch

connection = pymongo.Connection('localhost', 27017)

def get_emails_thread(table, start_email, thread):
    db = connection[table]
    db.mails.create_index('Date')
    db.mails.ensure_index('Date')
    db.mails.create_index('In-Reply-To')
    db.mails.ensure_index('In-Reply-To')
    db.mails.create_index('Message-ID')
    db.mails.ensure_index('Message-ID')
    for el in db.mails.find({'In-Reply-To': start_email['Message-ID']},
        sort=[('Date', pymongo.DESCENDING)]):
        thread.append(el)
        get_emails_thread(el, thread)
    return thread


def get_archives(table, start, end):
    db = connection[table]
    db.mails.create_index('Date')
    db.mails.ensure_index('Date')
    db.mails.create_index('In-Reply-To')
    db.mails.ensure_index('In-Reply-To')
    # Beginning of thread == No 'In-Reply-To' header
    archives = []
    for el in db.mails.find({'In-Reply-To': {'$exists':False},
            "Date": {"$gte": start, "$lt": end}}, 
            sort=[('Date', pymongo.DESCENDING)]):
        archives.append(el)
    return archives


def get_thread(table, start, end):
    db = connection[table]
    db.mails.create_index('Date')
    db.mails.ensure_index('Date')
    db.mails.create_index('In-Reply-To')
    db.mails.ensure_index('In-Reply-To')
    # Beginning of thread == No 'In-Reply-To' header
    archives = Bunch()
    for el in db.mails.find({'In-Reply-To': {'$exists':False},
            "Date": {"$gte": start, "$lt": end}}, 
            sort=[('Date', pymongo.DESCENDING)]):
        thread = get_emails_thread(el, [el])
        #print el['Subject'], len(thread)
        archives[el['Subject']] = thread
    return archives


def get_thread_length(table, thread_id):
    db = connection[table]
    db.mails.create_index('Thread-ID')
    db.mails.ensure_index('Thread-ID')
    return db.mails.find({'Thread-ID': thread_id}).count()


def get_thread_participants(table, thread_id):
    db = connection[table]
    db.mails.create_index('Thread-ID')
    db.mails.ensure_index('Thread-ID')
    authors = set()
    for mail in db.mails.find({'Thread-ID': thread_id}):
        authors.add(mail['From'])
    return len(authors)


def get_archives_length(table):
    db = connection[table]
    db.mails.create_index('Date')
    db.mails.ensure_index('Date')
    archives = {}
    for date in db.mails.distinct('Date'):
        if date.year in archives:
            archives[date.year].add(date.month)
        else:
            archives[date.year] = set([date.month])
    for key in archives:
        archives[key] = list(archives[key])
    return archives