microblog.pub/core/remote.py

39 lines
872 B
Python
Raw Normal View History

2019-08-07 21:27:57 +00:00
from urllib.parse import urlparse
from core.db import _Q
from core.db import update_one_remote
from utils import now
def server(url: str) -> str:
return urlparse(url).netloc
def _update(url: str, replace: _Q) -> None:
update_one_remote({"server": server(url)}, replace, upsert=True)
# TODO(tsileo): track receive (and the user agent to help debug issues)
2019-08-07 21:27:57 +00:00
def track_successful_send(url: str) -> None:
now_ = now()
_update(
url,
{
"$inc": {"successful_send": 1},
2019-08-08 22:08:22 +00:00
"$set": {
"last_successful_contact": now_,
"last_successful_send": now_,
"last_contact": now_,
},
2019-08-07 21:27:57 +00:00
},
)
return None
def track_failed_send(url: str) -> None:
now_ = now()
_update(url, {"$inc": {"failed_send": 1}, "$set": {"last_contact": now_}})
return None