Skip to content

Commit

Permalink
Filtered out 'bad' mirrors. Also added a sorting mechanism that uses …
Browse files Browse the repository at this point in the history
…the mirrors 'score' rather than just the URL name. This will emulate the reflector.service/rankmirrors behavior and thus reducing the need to re-rank the mirrors.
  • Loading branch information
Torxed committed Jul 30, 2024
1 parent ee61693 commit 0483de2
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions archinstall/lib/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .networking import fetch_data_from_url
from .output import warn, FormattedOutput
from .storage import storage
from .models.mirrors import MirrorStatusListV3
from .models.mirrors import MirrorStatusListV3, MirrorStatusEntryV3

if TYPE_CHECKING:
_: Any
Expand Down Expand Up @@ -282,23 +282,43 @@ def select_custom_mirror(prompt: str = '', preset: List[CustomMirror] = []):
return custom_mirrors


def _parse_mirror_list(mirrorlist: str) -> Dict[str, List[str]]:
def sort_mirror_list(mirror_list :List[MirrorStatusEntryV3], sorting_element :str = "url") -> List[MirrorStatusEntryV3]:
return sorted(mirror_list, key=lambda item: getattr(item, sorting_element))


def _parse_mirror_list(mirrorlist: str, sorting_element :str = "url") -> Dict[str, List[str]]:
mirror_status = MirrorStatusListV3(**json.loads(mirrorlist))

mirror_list: Dict[str, List[str]] = {}
sorting_placeholder: Dict[str, List[MirrorStatusEntryV3]] = {}

for mirror in mirror_status.urls:
# We filter out mirrors that have bad criteria values
if any([
mirror.active is False, # Disabled by mirror-list admins
mirror.last_sync is None, # Has not synced recently
# mirror.score (error rate) over time reported from backend: https://github.com/archlinux/archweb/blob/31333d3516c91db9a2f2d12260bd61656c011fd1/mirrors/utils.py#L111C22-L111C66
(mirror.score is None or mirror.score >= 100),
]):
continue

if mirror.country == "":
# TODO: This should be removed once RFC!29 is merged and completed
# Until then, there are mirrors which lacks data in the backend
# and there is no way of knowing where they're located.
# So we have to assume world-wide
mirror.country = "Worldwide"

if mirror.url.startswith('http'):
if mirror.country == "":
# TODO: This should be removed once RFC!29 is merged and completed
# Until then, there are mirrors which lacks data in the backend
# and there is no way of knowing where they're located.
# So we have to assume world-wide
mirror.country = "Worldwide"
sorting_placeholder.setdefault(mirror.country, []).append(mirror)

mirror_list.setdefault(mirror.country, []).append(mirror.url)
sorted_by_element: Dict[str, List[str]] = dict({
region: [
mirror.url for mirror in sort_mirror_list(unsorted_mirrors, sorting_element=sorting_element)
]
for region, unsorted_mirrors in sorted(sorting_placeholder.items(), key=lambda item: item[0])
})

return mirror_list
return sorted_by_element


def list_mirrors() -> Dict[str, List[str]]:
Expand All @@ -315,9 +335,4 @@ def list_mirrors() -> Dict[str, List[str]]:
warn(f'Could not fetch an active mirror-list: {err}')
return regions

regions = _parse_mirror_list(mirrorlist)
sorted_regions = {}
for region, urls in regions.items():
sorted_regions[region] = sorted(urls, reverse=True)

return sorted_regions
return _parse_mirror_list(mirrorlist, sorting_element="score")

0 comments on commit 0483de2

Please sign in to comment.