/
statsdb.py
64 lines (52 loc) · 1.98 KB
/
statsdb.py
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
"""Interface to Open Library stats database.
The stats table in the openlibrary database is of the following schema:
CREATE TABLE stats (
id serial primary key,
key text unique,
type text,
timestamp timestamp without time zone,
json text
);
see schema.py for more details.
"""
import logging
import web
import simplejson
import datetime
logger = logging.getLogger("openlibrary.statsdb")
@web.memoize
def get_db():
return web.database(**web.config.db_parameters)
def add_entry(key, data, timestamp=None):
"""Adds a new entry to the stats table.
If an entry is already present in the table, a warn message is logged
and no changes will be made to the database.
"""
jsontext = simplejson.dumps(data)
timestamp = timestamp or datetime.datetime.utcnow()
t = timestamp.isoformat()
db = get_db()
result = db.query("SELECT * FROM stats WHERE key=$key", vars=locals())
if result:
logger.warn("Failed to add stats entry with key %r. An entry is already present.")
else:
db.insert("stats", type='loan', key=key, created=t, updated=t, json=jsontext)
def get_entry(key):
result = db.query("SELECT * FROM stats WHERE key=$key", vars=locals())
if result:
return result[0]
def update_entry(key, data, timestamp=None):
"""Updates an already existing entry in the stats table.
If there is no entry with the given key, a new one will be added
after logging a warn message.
"""
jsontext = simplejson.dumps(data)
timestamp = timestamp or datetime.datetime.utcnow()
t = timestamp.isoformat()
db = get_db()
result = db.query("SELECT * FROM stats WHERE key=$key", vars=locals())
if result:
db.update("stats", json=jsontext, updated=t, where="key=$key", vars=locals())
else:
logger.warn("stats entry with key %r doesn't exist to update. adding new entry...", key)
db.insert("stats", type='loan', key=key, created=t, updated=t, json=jsontext)