Use the new improved anchel.nl JSON API

The /publiclist.json provides all the server event/downtime logs
in one file, so we don't have to do requests for every server anymore.
This commit is contained in:
miruka 2020-08-22 13:10:04 -04:00
parent 3e22694817
commit f327c99a99

View File

@ -115,7 +115,6 @@ class Backend:
self._sso_server_task: Optional[asyncio.Future] = None self._sso_server_task: Optional[asyncio.Future] = None
self._ping_tasks: Dict[str, asyncio.Future] = {} self._ping_tasks: Dict[str, asyncio.Future] = {}
self._stability_tasks: Dict[str, asyncio.Future] = {}
self.profile_cache: Dict[str, nio.ProfileGetResponse] = {} self.profile_cache: Dict[str, nio.ProfileGetResponse] = {}
self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \ self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \
@ -539,103 +538,58 @@ class Backend:
) )
async def _get_homeserver_stability( def _get_homeserver_stability(self, logs: List[Dict[str, Any]]) -> float:
self,
session: aiohttp.ClientSession,
homeserver_url: str,
uptimerobot_id: int,
) -> None:
api = "https://matrixservers.anchel.nl/api/getMonitor/wkMJmFGvo2?m={}"
response = await session.get(api.format(uptimerobot_id))
logs = (await response.json())["monitor"]["logs"]
stability = 100.0 stability = 100.0
for period in logs: for period in logs:
started_at = datetime.fromtimestamp(period["time"]) started_at = datetime.fromtimestamp(period["datetime"])
time_since_now = datetime.now() - started_at time_since_now = datetime.now() - started_at
if time_since_now.days > 30 or period["class"] != "danger": if time_since_now.days > 30 or period["type"] != 1: # 1 = downtime
continue continue
lasted_hours, lasted_mins = [ lasted_minutes = period["duration"] / 60
int(x.split()[0]) for x in period["duration"].split(", ")
]
lasted_mins += lasted_hours * 60
stability -= ( stability -= (
(lasted_mins * stability / 1000) / (lasted_minutes * stability / 1000) /
max(1, time_since_now.days / 3) max(1, time_since_now.days / 3)
) )
self.models["homeservers"][homeserver_url].stability = stability return stability
async def _add_homeserver_item(
self,
session: aiohttp.ClientSession,
homeserver_url: str,
uptimerobot_id: int,
**fields,
) -> Homeserver:
"""Add homeserver to our model & start info-gathering tasks."""
if not re.match(r"^https?://.+", homeserver_url):
homeserver_url = f"https://{homeserver_url}"
if fields.get("country") == "USA":
fields["country"] = "United States"
if homeserver_url in self._ping_tasks:
self._ping_tasks[homeserver_url].cancel()
if homeserver_url in self._stability_tasks:
self._stability_tasks[homeserver_url].cancel()
item = Homeserver(id=homeserver_url, **fields)
self.models["homeservers"][homeserver_url] = item
self._ping_tasks[homeserver_url] = asyncio.ensure_future(
self._ping_homeserver(session, homeserver_url),
)
self._stability_tasks[homeserver_url] = asyncio.ensure_future(
self._get_homeserver_stability(
session, item.id, uptimerobot_id,
),
)
return item
async def fetch_homeservers(self) -> None: async def fetch_homeservers(self) -> None:
"""Retrieve a list of public homeservers and add them to our model.""" """Retrieve a list of public homeservers and add them to our model."""
api_list = "https://publiclist.anchel.nl/staticlist.json" api_list = "https://publiclist.anchel.nl/publiclist.json"
tmout = aiohttp.ClientTimeout(total=20) tmout = aiohttp.ClientTimeout(total=20)
session = aiohttp.ClientSession(raise_for_status=True, timeout=tmout) session = aiohttp.ClientSession(raise_for_status=True, timeout=tmout)
response = await session.get(api_list) response = await session.get(api_list)
data = (await response.json())["staticlist"]
# Missing from the anchel.nl public list for server in (await response.json()):
await self._add_homeserver_item( homeserver_url = server["homeserver"]
session = session,
homeserver_url = "https://matrix-client.matrix.org",
name = "matrix.org",
site_url = "https://matrix.org",
country = "Cloudflare",
uptimerobot_id = 783115140,
)
for server in data: if homeserver_url.startswith("http://"): # insecure server
if server["homeserver"].startswith("http://"): # insecure server
continue continue
await self._add_homeserver_item( if not re.match(r"^https?://.+", homeserver_url):
session = session, homeserver_url = f"https://{homeserver_url}"
homeserver_url = server["homeserver"],
if server["country"] == "USA":
server["country"] = "United States"
if homeserver_url in self._ping_tasks:
self._ping_tasks[homeserver_url].cancel()
self.models["homeservers"][homeserver_url] = Homeserver(
id = homeserver_url,
name = server["name"], name = server["name"],
site_url = server["url"], site_url = server["url"],
country = server["country"], country = server["country"],
uptimerobot_id = server["uptrid"], stability =
self._get_homeserver_stability(server["monitor"]["logs"]),
)
self._ping_tasks[homeserver_url] = asyncio.ensure_future(
self._ping_homeserver(session, homeserver_url),
) )