-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
159 additions
and
91 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/tribler/core/components/database/db/layers/health_data_access_layer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import logging | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from pony import orm | ||
|
||
from tribler.core.components.database.db.layers.knowledge_data_access_layer import KnowledgeDataAccessLayer | ||
from tribler.core.components.torrent_checker.torrent_checker.dataclasses import HealthInfo | ||
from tribler.core.upgrade.tags_to_knowledge.previous_dbs.knowledge_db import ResourceType | ||
from tribler.core.utilities.pony_utils import get_or_create | ||
|
||
|
||
# pylint: disable=redefined-outer-name | ||
|
||
|
||
class HealthDataAccessLayer: | ||
def __init__(self, knowledge_layer: KnowledgeDataAccessLayer): | ||
self.logger = logging.getLogger(self.__class__.__name__) | ||
self.instance = knowledge_layer.instance | ||
self.Resource = knowledge_layer.Resource | ||
self.TorrentHealth, self.Tracker, self.get_torrent_health = self.define_binding(self.instance) | ||
|
||
@staticmethod | ||
def define_binding(db): | ||
class TorrentHealth(db.Entity): | ||
id = orm.PrimaryKey(int, auto=True) | ||
|
||
torrent = orm.Required(lambda: db.Resource, index=True) | ||
|
||
seeders = orm.Required(int, default=0) | ||
leechers = orm.Required(int, default=0) | ||
source = orm.Required(int, default=0) # Source enum | ||
tracker = orm.Optional(lambda: Tracker) | ||
last_check = orm.Required(datetime, default=datetime.utcnow) | ||
|
||
class Tracker(db.Entity): | ||
id = orm.PrimaryKey(int, auto=True) | ||
|
||
url = orm.Required(str, unique=True) | ||
last_check = orm.Optional(datetime) | ||
alive = orm.Required(bool, default=True) | ||
failures = orm.Required(int, default=0) | ||
|
||
torrents = orm.Set(lambda: db.Resource) | ||
torrent_health_set = orm.Set(lambda: TorrentHealth, reverse='tracker') | ||
|
||
def get_torrent_health(infohash: str) -> Optional[TorrentHealth]: | ||
if torrent := db.Resource.get(name=infohash, type=ResourceType.TORRENT): | ||
return TorrentHealth.get(torrent=torrent) | ||
return None | ||
|
||
return TorrentHealth, Tracker, get_torrent_health | ||
|
||
def add_torrent_health(self, health_info: HealthInfo): | ||
torrent = get_or_create( | ||
self.Resource, | ||
name=health_info.infohash_hex, | ||
type=ResourceType.TORRENT | ||
) | ||
|
||
torrent_health = get_or_create( | ||
self.TorrentHealth, | ||
torrent=torrent | ||
) | ||
|
||
torrent_health.seeders = health_info.seeders | ||
torrent_health.leechers = health_info.leechers | ||
if health_info.tracker: | ||
torrent_health.tracker = get_or_create( | ||
self.Tracker, | ||
url=health_info.tracker | ||
) | ||
|
||
torrent_health.source = health_info.source | ||
torrent_health.last_check = datetime.utcfromtimestamp(health_info.last_check) |
56 changes: 0 additions & 56 deletions
56
src/tribler/core/components/database/db/layers/health_data_access_level.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/tribler/core/components/database/db/layers/tests/test_health_data_access_layer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import os | ||
import time | ||
from datetime import datetime | ||
|
||
from tribler.core.components.database.db.layers.knowledge_data_access_layer import ResourceType | ||
from tribler.core.components.database.db.tests.test_tribler_database import TestTriblerDatabase | ||
from tribler.core.components.torrent_checker.torrent_checker.dataclasses import HealthInfo, Source | ||
from tribler.core.utilities.pony_utils import db_session | ||
|
||
|
||
# pylint: disable=protected-access | ||
class TestHealthAccessLayer(TestTriblerDatabase): | ||
|
||
@staticmethod | ||
def create_health_info(tracker=''): | ||
return HealthInfo( | ||
infohash=os.urandom(40), | ||
seeders=10, | ||
leechers=20, | ||
last_check=int(time.time()), | ||
self_checked=True, | ||
source=Source.TRACKER, | ||
tracker=tracker | ||
) | ||
|
||
@db_session | ||
def test_add_torrent_health_no_tracker(self): | ||
""" Test that add_torrent_health works as expected""" | ||
info = self.create_health_info() | ||
self.db.health.add_torrent_health(info) | ||
|
||
health = self.db.health.get_torrent_health(info.infohash_hex) | ||
|
||
assert health.torrent.name == info.infohash_hex | ||
assert health.torrent.type == ResourceType.TORRENT | ||
assert health.seeders == info.seeders | ||
assert health.leechers == info.leechers | ||
assert health.last_check == datetime.utcfromtimestamp(info.last_check) | ||
assert health.source == info.source | ||
|
||
assert not health.tracker | ||
|
||
assert not self.db.health.get_torrent_health('missed hash') | ||
|
||
@db_session | ||
def test_add_torrent_health_with_trackers(self): | ||
""" Test that add_torrent_health considers trackers""" | ||
|
||
# first add single HealthInfo with tracker | ||
info = self.create_health_info(tracker='tracker1') | ||
self.db.health.add_torrent_health(info) | ||
health = self.db.health.get_torrent_health(info.infohash_hex) | ||
|
||
assert health.tracker.url == info.tracker | ||
|
||
# then add another HealthInfo with the same tracker | ||
self.db.health.add_torrent_health( | ||
self.create_health_info(tracker='tracker1') | ||
) | ||
|
||
assert len(self.db.Tracker.select()) == 1 | ||
assert len(self.db.TorrentHealth.select()) == 2 | ||
|
||
# then add another HealthInfo with the different tracker | ||
self.db.health.add_torrent_health( | ||
self.create_health_info(tracker='tracker2') | ||
) | ||
|
||
assert len(self.db.Tracker.select()) == 2 | ||
assert len(self.db.TorrentHealth.select()) == 3 |
26 changes: 0 additions & 26 deletions
26
src/tribler/core/components/database/db/layers/tests/test_health_data_access_level.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters