~daisy-pluckers/oops-repository/trunk

« back to all changes in this revision

Viewing changes to oopsrepository/oopses.py

  • Committer: Robert Collins
  • Date: 2011-04-03 11:14:12 UTC
  • Revision ID: robert@canonical.com-20110403111412-6s3jf6w2v38jsnjs
Add an insert method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
"""basic operations on oopses in the db."""
8
8
 
 
9
import json
9
10
import time
 
11
import uuid
10
12
 
11
13
import pycassa
12
14
from pycassa.index import create_index_expression, LT, create_index_clause
17
19
 
18
20
def prune(config):
19
21
    """Remove OOPSES that are over 30 days old."""
20
 
    # TODO: use a batch mutator.
21
22
    pool = pycassa.connect(config['keyspace'])
22
23
    dayoops_cf = pycassa.ColumnFamily(pool, 'DayOOPS')
23
24
    oops_cf = pycassa.ColumnFamily(pool, 'OOPS')
51
52
    batch = dayoops_cf.batch()
52
53
    map(batch.remove, days)
53
54
    batch.send()
 
55
 
 
56
 
 
57
def insert(config, oopsid, oops_json):
 
58
    """Insert an OOPS into the system.
 
59
    
 
60
    :return: The day which the oops was filed under.
 
61
    """
 
62
    pool = pycassa.connect(config['keyspace'])
 
63
    dayoops_cf = pycassa.ColumnFamily(pool, 'DayOOPS')
 
64
    oops_cf = pycassa.ColumnFamily(pool, 'OOPS')
 
65
    day_key = time.strftime('%Y%m%d', time.gmtime())
 
66
    now_uuid = uuid.uuid1()
 
67
    # make sure the oops report is a json dict, and break out each key to a
 
68
    # separate column. For now, rather than worrying about typed column values
 
69
    # we just coerce them all to strings.
 
70
    oops_dict = json.loads(oops_json)
 
71
    assert isinstance(oops_dict, dict)
 
72
    insert_dict = {}
 
73
    for key, value in oops_dict.items():
 
74
        insert_dict[key] = json.dumps(value)
 
75
    oops_cf.insert(oopsid, insert_dict)
 
76
    dayoops_cf.insert(day_key, {now_uuid:oopsid})
 
77
    return day_key