Skip to content

Commit

Permalink
Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 29, 2023
1 parent 01ca828 commit d42b03b
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 237 deletions.
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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tribler/core/components/database/db/layers/health_data_access_level.py#L25

Redefining name 'HealthInfo' from outer scope (line 6)
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,

Check notice on line 35 in src/tribler/core/components/database/db/layers/health_data_access_level.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tribler/core/components/database/db/layers/health_data_access_level.py#L35

Disallow trailing comma tuple

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from dataclasses import dataclass
from enum import IntEnum
from typing import Any, Callable, Iterator, List, Optional, Set
from typing import Callable, Iterator, List, Optional, Set

from pony import orm
from pony.orm import raw_sql
Expand All @@ -11,7 +11,7 @@

from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.torrent_checker.torrent_checker.dataclasses import HealthInfo

Check warning on line 13 in src/tribler/core/components/database/db/layers/knowledge_data_access_layer.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tribler/core/components/database/db/layers/knowledge_data_access_layer.py#L13

Unused HealthInfo imported from tribler.core.components.torrent_checker.torrent_checker.dataclasses
from tribler.core.utilities.pony_utils import TrackedDatabase, get_or_create
from tribler.core.utilities.pony_utils import get_or_create

CLOCK_START_VALUE = 0

Expand Down Expand Up @@ -64,13 +64,16 @@ class SimpleStatement:


class KnowledgeDataAccessLayer:
def __init__(self, filename: Optional[str] = None, *, create_tables: bool = True, **generate_mapping_kwargs):
self.instance = TrackedDatabase()
self.define_binding(self.instance)
self.instance.bind('sqlite', filename or ':memory:', create_db=True)
generate_mapping_kwargs['create_tables'] = create_tables
self.instance.generate_mapping(**generate_mapping_kwargs)
def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
self.db = None
self.instance = None

def apply(self, db):
self.db = db
self.instance = db.instance

return self.define_binding(self.instance)

@staticmethod
def define_binding(db):
Expand Down Expand Up @@ -124,7 +127,7 @@ class Resource(db.Entity):

subject_statements = orm.Set(lambda: Statement, reverse="subject")
object_statements = orm.Set(lambda: Statement, reverse="object")
health_info = orm.Set(lambda: HealthInfo, reverse="torrent")
health_info = orm.Set(lambda: db.HealthInfo, reverse="torrent")

orm.composite_key(name, type)

Expand All @@ -142,19 +145,7 @@ class StatementOp(db.Entity):

orm.composite_key(statement, peer)

class Misc(db.Entity): # pylint: disable=unused-variable
name = orm.PrimaryKey(str)
value = orm.Optional(str)

class HealthInfo(db.Entity):
id = orm.PrimaryKey(int, auto=True)

torrent = orm.Required(lambda: 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 Peer, Statement, Resource, StatementOp

def add_operation(self, operation: StatementOperation, signature: bytes, is_local_peer: bool = False,
is_auto_generated: bool = False, counter_increment: int = 1) -> bool:
Expand Down Expand Up @@ -224,28 +215,6 @@ def add_auto_generated_operation(self, subject_type: ResourceType, subject: str,
return self.add_operation(operation, signature=b'', is_local_peer=False, is_auto_generated=True,
counter_increment=SHOW_THRESHOLD)

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

@staticmethod
def _show_condition(s):
"""This function determines show condition for the statement"""
Expand Down Expand Up @@ -451,8 +420,7 @@ def get_operations_for_gossip(self, count: int = 10) -> Set[Entity]:
count=count
)

def shutdown(self) -> None:
self.instance.disconnect()


def _get_random_operations_by_condition(self, condition: Callable[[Entity], bool], count: int = 5,
attempts: int = 100) -> Set[Entity]:
Expand Down Expand Up @@ -480,11 +448,3 @@ def _get_random_operations_by_condition(self, condition: Callable[[Entity], bool
operations.add(operation)

return operations

def get_misc(self, key: str, default: Optional[str] = None) -> Optional[str]:
data = self.instance.Misc.get(name=key)
return data.value if data else default

def set_misc(self, key: str, value: Any):
key_value = get_or_create(self.instance.Misc, name=key)
key_value.value = str(value)
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')
Loading

0 comments on commit d42b03b

Please sign in to comment.