-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_added.py
81 lines (67 loc) · 2.28 KB
/
db_added.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from time import time
from typing import Iterable, Sequence
from box import Box
from config import ADDED_SEARCH_RADIUS, MONGO_ADDED, SCORER_VERSION, VERSION
from latlon import LatLon
def contains_added(box: Box) -> bool:
p1 = box.point
p2 = box.point + box.size
return MONGO_ADDED.find_one({
"$and": [
{"position": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[p1.lon, p1.lat],
[p1.lon, p2.lat],
[p2.lon, p2.lat],
[p2.lon, p1.lat],
[p1.lon, p1.lat]
]
]
}
}
}},
{"scorer_version": {"$gte": SCORER_VERSION}}
]
}) is not None
def mask_not_added(positions: Iterable[LatLon]) -> Sequence[bool]:
result = []
for p in positions:
doc = MONGO_ADDED.find_one({
"$and": [
{"position": {
"$nearSphere": {
"$geometry": {
"type": "Point",
"coordinates": (p.lon, p.lat)
},
"$maxDistance": ADDED_SEARCH_RADIUS
}
}},
{"$or": [
{"scorer_version": {"$gte": SCORER_VERSION}},
{"reason": "added"}
]}
]
})
result.append(doc is None)
return tuple(result)
def mark_added(positions: Sequence[LatLon], **kwargs) -> Sequence[int]:
if not positions:
return ()
return MONGO_ADDED.insert_many([{
'timestamp': time(),
'position': (p.lon, p.lat),
'app_version': VERSION,
'scorer_version': SCORER_VERSION,
**kwargs,
} for p in positions]).inserted_ids
def get_added(*, reason: str, limit: int) -> Sequence[LatLon]:
docs = MONGO_ADDED.aggregate([
{"$match": {"reason": reason}},
{"$sample": {"size": limit}},
])
return tuple(LatLon(*reversed(doc['position'])) for doc in docs)