-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute-summaries.py
50 lines (41 loc) · 1.68 KB
/
compute-summaries.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
from pymongo import MongoClient
from bson.son import SON
import datetime, json, copy, re
client = MongoClient('mongodb://database/argo')
db = client.argo
def get_timestamp_range(db, collection_name):
collection = db[collection_name]
# Find the earliest timestamp
filter = {}
earliest_doc = collection.find_one(filter, sort=[("timestamp", 1)])
if earliest_doc and "timestamp" in earliest_doc:
earliest_timestamp = earliest_doc["timestamp"]
else:
return None, None # Return None if no timestamps are found
# Find the latest timestamp or current time, whichever is earlier
filter = {}
latest_doc = collection.find_one(filter, sort=[("timestamp", -1)])
current_time = datetime.datetime.utcnow()
if latest_doc and "timestamp" in latest_doc:
latest_timestamp = min(latest_doc["timestamp"], current_time)
else:
latest_timestamp = current_time # If no documents, default to current time
# Convert timestamps to ISO 8601 format
try:
earliest_iso = earliest_timestamp.isoformat() + "Z"
latest_iso = latest_timestamp.isoformat() + "Z"
return earliest_iso, latest_iso
except:
return None, None
startDate, endDate = get_timestamp_range(db, 'glodap')
entry = {"metagroups": ["id"], "startDate": startDate, "endDate": endDate}
rldoc = db.summaries.find_one({"_id": 'ratelimiter'})
if rldoc:
rldoc['metadata']['glodap'] = entry
else:
rldoc = {"_id": "ratelimiter", "metadata": {"glodap": entry}}
try:
db.summaries.replace_one({"_id": 'ratelimiter'}, rldoc, upsert=True)
except BaseException as err:
print('error: db write failure')
print(err)