-
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
25 changed files
with
333 additions
and
237 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/tribler/core/components/database/db/layers/health_data_access_level.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,57 @@ | ||
import datetime | ||
import logging | ||
|
||
from pony import orm | ||
|
||
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 | ||
|
||
|
||
class HealthDataAccessLayer: | ||
def __init__(self): | ||
self.logger = logging.getLogger(self.__class__.__name__) | ||
self.db = None | ||
self.instance = None | ||
|
||
def apply(self, db): | ||
self.db = db.instance | ||
self.instance = db.instance | ||
|
||
return self.define_binding(self.instance) | ||
|
||
@staticmethod | ||
def define_binding(db): | ||
class HealthInfo(db.Entity): | ||
Check warning on line 25 in src/tribler/core/components/database/db/layers/health_data_access_level.py Codacy Production / Codacy Static Code Analysissrc/tribler/core/components/database/db/layers/health_data_access_level.py#L25
|
||
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 | ||
last_check = orm.Required(datetime.datetime, default=datetime.datetime.utcnow) | ||
|
||
return HealthInfo, | ||
|
||
def add_torrent_health(self, torrent_health: HealthInfo): | ||
torrent = get_or_create( | ||
self.instance.Resource, | ||
name=torrent_health.infohash_hex, | ||
type=ResourceType.TORRENT | ||
) | ||
|
||
health_info_entity = get_or_create( | ||
self.instance.HealthInfo, | ||
torrent=torrent | ||
) | ||
|
||
health_info_entity.seeders = torrent_health.seeders | ||
health_info_entity.leechers = torrent_health.leechers | ||
health_info_entity.source = torrent_health.source | ||
health_info_entity.last_check = datetime.datetime.utcfromtimestamp(torrent_health.last_check) | ||
|
||
def get_torrent_health(self, infohash: str): | ||
if torrent := self.instance.Resource.get(name=infohash, type=ResourceType.TORRENT): | ||
return self.instance.HealthInfo.get(torrent=torrent) | ||
return None |
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
26 changes: 26 additions & 0 deletions
26
src/tribler/core/components/database/db/layers/tests/test_health_data_access_level.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,26 @@ | ||
import time | ||
|
||
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): | ||
|
||
@db_session | ||
def test_add_torrent_health(self): | ||
""" Test that add_torrent_health works as expected""" | ||
health_info = HealthInfo( | ||
infohash=b'0' * 20, | ||
seeders=10, | ||
leechers=20, | ||
last_check=int(time.time()), | ||
self_checked=True, | ||
source=Source.POPULARITY_COMMUNITY | ||
) | ||
|
||
self.db.health.add_torrent_health(health_info) | ||
|
||
assert self.db.health.get_torrent_health(health_info.infohash_hex) # add fields validation | ||
assert not self.db.health.get_torrent_health('missed hash') |
Oops, something went wrong.