-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.py
47 lines (42 loc) · 1.66 KB
/
actions.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
import json, sys
from housepy import util, strings, config, log
"""
These functions should not be web specific so that they can be importable.
"""
def insert(db, data):
for key in data.keys():
if type(key) is not str:
del data[key]
continue
clean_key = clean(key)
if key != clean_key:
data[clean_key] = data[key]
del data[key]
key = clean_key
data[key] = strings.as_numeric(data[key])
if 't_utc' not in data:
data['t_utc'] = util.timestamp()
data['date'] = util.datestring(data['t_utc'], tz=config['tz'])
log.info(json.dumps(data, indent=4))
entry_id = db.entries.insert_one(data).inserted_id
return entry_id
def retrieve(db, source, start, end, filters, page=None):
if filters == None:
filters = {}
sources = [clean(source) for source in source.split(",")]
start_t = 0 if start == "*" else util.timestamp(util.parse_date(start, tz=config['tz']))
end_t = min(2147483647, sys.maxsize) if end == "*" else util.timestamp(util.parse_date(end, tz=config['tz']))
template = {'t_utc': {'$gt': start_t, '$lt': end_t}, '$or': [{'source': source} for source in sources]}
template.update(filters)
log.info("QUERY %s" % template)
results = db.entries.find(template).sort('t_utc')
count = results.count()
if page is None:
page = (count // 100) + 1
skip = (page - 1) * 100
log.debug("page %s, skip %s" % (page, skip))
results = results.skip(skip).limit(100)
log.info("--> done")
return list(results), start_t, end_t, count, page
def clean(s):
return strings.slugify(strings.depunctuate(s, "_"))