Skip to content

Commit

Permalink
Make Pymongo tz aware
Browse files Browse the repository at this point in the history
  • Loading branch information
ppigazzini committed Apr 17, 2024
1 parent 660432b commit 3e1b582
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 44 deletions.
26 changes: 9 additions & 17 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import fishtest.stats.stat_util
from bson.binary import Binary
from bson.codec_options import CodecOptions
from bson.objectid import ObjectId
from fishtest.actiondb import ActionDb
from fishtest.schemas import RUN_VERSION, nn_schema, runs_schema
Expand Down Expand Up @@ -85,7 +86,8 @@ def __init__(self, db_name="fishtest_new"):
# MongoDB server is assumed to be on the same machine, if not user should
# use ssh with port forwarding to access the remote host.
self.conn = MongoClient(os.getenv("FISHTEST_HOST") or "localhost")
self.db = self.conn[db_name]
codec_options = CodecOptions(tz_aware=True, tzinfo=timezone.utc)
self.db = self.conn[db_name].with_options(codec_options=codec_options)
self.userdb = UserDb(self.db)
self.actiondb = ActionDb(self.db)
self.workerdb = WorkerDb(self.db)
Expand Down Expand Up @@ -149,8 +151,8 @@ def new_run(
):
if start_time is None:
start_time = datetime.now(timezone.utc)
else:
start_time = start_time.replace(tzinfo=timezone.utc)
# else:
# start_time = start_time.replace(tzinfo=timezone.utc)

run_args = {
"base_tag": base_tag,
Expand Down Expand Up @@ -464,10 +466,7 @@ def scavenge(self, run):
task_id = -1
for task in run["tasks"]:
task_id += 1
if (
task["active"]
and task["last_updated"].replace(tzinfo=timezone.utc) < old
):
if task["active"] and task["last_updated"] < old:
task["active"] = False
dead_task = True
print(
Expand Down Expand Up @@ -507,9 +506,7 @@ def get_machines(self):
task["worker_info"]
| {
"last_updated": (
task["last_updated"].replace(tzinfo=timezone.utc)
if task.get("last_updated")
else None
task["last_updated"] if task.get("last_updated") else None
),
"run": run,
"task_id": task_id,
Expand Down Expand Up @@ -852,9 +849,7 @@ def priority(run): # lower is better
for task in active_tasks:
task_name = worker_name(task["worker_info"], short=True)
if my_name == task_name:
last_update = (
now - task["last_updated"].replace(tzinfo=timezone.utc)
).seconds
last_update = (now - task["last_updated"]).seconds
# 120 = period of heartbeat in worker.
if last_update <= 120:
error = 'Request_task: There is already a worker running with name "{}" which sent an update {} seconds ago'.format(
Expand Down Expand Up @@ -1406,10 +1401,7 @@ def purge_run(self, run, p=0.001, res=7.0, iters=1):
return "Can only purge completed run"

now = datetime.now(timezone.utc)
if (
"start_time" not in run
or (now - run["start_time"].replace(tzinfo=timezone.utc)).days > 30
):
if "start_time" not in run or (now - run["start_time"]).days > 30:
return "Run too old to be purged"
# Do not revive failed runs
if run.get("failed", False):
Expand Down
5 changes: 2 additions & 3 deletions server/fishtest/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@


def first_test_before_last(x):
# Pymongo is not timezone aware. Assume dates are UTC.
f = x["first_test"]["date"].replace(tzinfo=timezone.utc)
l = x["last_test"]["date"].replace(tzinfo=timezone.utc)
f = x["first_test"]["date"]
l = x["last_test"]["date"]
if f <= l:
return True
else:
Expand Down
5 changes: 2 additions & 3 deletions server/fishtest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,8 @@ def post_in_fishcooking_results(run):


def diff_date(date):
utc_date = date.replace(tzinfo=timezone.utc)
if utc_date != datetime.min.replace(tzinfo=timezone.utc):
diff = datetime.now(timezone.utc) - utc_date
if date != datetime.min.replace(tzinfo=timezone.utc):
diff = datetime.now(timezone.utc) - date
else:
diff = timedelta.max
return diff
Expand Down
9 changes: 3 additions & 6 deletions server/fishtest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,7 @@ def validate_modify(request, run):
raise home(request)

now = datetime.now(timezone.utc)
if (
"start_time" not in run
or (now - run["start_time"].replace(tzinfo=timezone.utc)).days > 30
):
if "start_time" not in run or (now - run["start_time"]).days > 30:
request.session.flash("Run too old to be modified", "error")
raise home(request)

Expand Down Expand Up @@ -1565,8 +1562,8 @@ def tests_view(request):
if task["active"]:
active += 1
cores += task["worker_info"]["concurrency"]
last_updated = task.get("last_updated", datetime.min).replace(
tzinfo=timezone.utc
last_updated = task.get(
"last_updated", datetime.min.replace(tzinfo=timezone.utc)
)
task["last_updated"] = last_updated

Expand Down
8 changes: 4 additions & 4 deletions server/tests/test_nn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from datetime import datetime
from datetime import datetime, timezone

from util import get_rundb
from vtjson import ValidationError
Expand All @@ -15,9 +15,9 @@ def setUp(self):
self.rundb = get_rundb()
self.name = "nn-0000000000a0.nnue"
self.user = "user00"
self.first_test = datetime(2024, 1, 1)
self.last_test = datetime(2024, 3, 24)
self.last_test_old = datetime(2023, 3, 24)
self.first_test = datetime(2024, 1, 1, tzinfo=timezone.utc)
self.last_test = datetime(2024, 3, 24, tzinfo=timezone.utc)
self.last_test_old = datetime(2023, 3, 24, tzinfo=timezone.utc)
self.run_id = "64e74776a170cb1f26fa3930"

def tearDown(self):
Expand Down
18 changes: 9 additions & 9 deletions server/utils/delta_update_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def process_run(run, info):

info_user = info[t_username]
info_user["last_updated"] = max(
info_user["last_updated"].replace(tzinfo=timezone.utc),
task.get("last_updated", datetime.min).replace(tzinfo=timezone.utc),
info_user["last_updated"],
task.get("last_updated", datetime.min.replace(tzinfo=timezone.utc)),
)
info_user["cpu_hours"] += float(
num_games * int(run["args"].get("threads", 1)) * tc / (60 * 60)
Expand Down Expand Up @@ -131,7 +131,7 @@ def update_info(rundb, clear_stats, deltas, info_total, info_top_month):
skip_count += 1

# Update info_top_month with finished runs having start_time in the last 30 days
if (now - run["start_time"].replace(tzinfo=timezone.utc)).days < 30:
if (now - run["start_time"]).days < 30:
try:
process_run(run, info_top_month)
except Exception as e:
Expand Down Expand Up @@ -200,18 +200,18 @@ def cleanup_users(rundb):
update = True
if update:
rundb.userdb.save_user(u)
if "registration_time" not in u or u["registration_time"].replace(
tzinfo=timezone.utc
) < datetime.now(timezone.utc) - timedelta(days=28):
if "registration_time" not in u or u["registration_time"] < datetime.now(
timezone.utc
) - timedelta(days=28):
idle[u["username"]] = u
for u in rundb.userdb.user_cache.find():
if u["username"] in idle:
del idle[u["username"]]
for u in idle.values():
# A safe guard against deleting long time users
if "registration_time" not in u or u["registration_time"].replace(
tzinfo=timezone.utc
) < datetime.now(timezone.utc) - timedelta(days=38):
if "registration_time" not in u or u["registration_time"] < datetime.now(
timezone.utc
) - timedelta(days=38):
print("Warning: Found old user to delete:", str(u["_id"]))
else:
print("Delete:", str(u["_id"]))
Expand Down
4 changes: 2 additions & 2 deletions server/utils/purge_pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def purge_pgn(rundb, finished, deleted, days):
not deleted
and finished
and tc_regex.match(run["args"]["tc"])
and run["last_updated"].replace(tzinfo=timezone.utc) > cutoff_date_ltc
) or run["last_updated"].replace(tzinfo=timezone.utc) > cutoff_date
and run["last_updated"] > cutoff_date_ltc
) or run["last_updated"] > cutoff_date

if keep:
kept_runs += 1
Expand Down

0 comments on commit 3e1b582

Please sign in to comment.