From 0ba75153f363ec1fe44ab31f3b2eba10254783d7 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 30 Aug 2021 19:36:14 -0400 Subject: [PATCH] trending fixes --- lbry/wallet/server/block_processor.py | 2 +- lbry/wallet/server/db/elasticsearch/search.py | 5 ++--- lbry/wallet/server/db/prefixes.py | 12 +++++++----- lbry/wallet/server/env.py | 15 ++++++++------- lbry/wallet/server/session.py | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index c7239abb8..728974125 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -520,7 +520,7 @@ class BlockProcessor: # add the spike for trending self.db_op_stack.append_op(self.db.prefix_db.trending_spike.pack_spike( - height, claim_hash, tx_num, nout, txo.amount - previous_amount, half_life=self.env.trending_half_life + height, claim_hash, tx_num, nout, txo.amount, half_life=self.env.trending_half_life )) def _add_support(self, height: int, txo: 'Output', tx_num: int, nout: int): diff --git a/lbry/wallet/server/db/elasticsearch/search.py b/lbry/wallet/server/db/elasticsearch/search.py index de1f6b59b..ee17113d2 100644 --- a/lbry/wallet/server/db/elasticsearch/search.py +++ b/lbry/wallet/server/db/elasticsearch/search.py @@ -178,9 +178,8 @@ class SearchIndex: }, slices=4, conflicts='proceed' ) self.logger.info("updated trending scores in %ims", int((time.perf_counter() - start) * 1000)) - - whale_decay_factor = 2 * (2.0 ** (-1 / self._trending_whale_half_life)) - decay_factor = 2 * (2.0 ** (-1 / self._trending_half_life)) + whale_decay_factor = 2.0 ** ((-1 / self._trending_whale_half_life) + 1) + decay_factor = 2.0 ** ((-1 / self._trending_half_life) + 1) decay_script = """ if (ctx._source.trending_score == null) { ctx._source.trending_score = 0.0; } if ((-0.1 <= ctx._source.trending_score) && (ctx._source.trending_score <= 0.1)) { diff --git a/lbry/wallet/server/db/prefixes.py b/lbry/wallet/server/db/prefixes.py index ddeb204f9..125210016 100644 --- a/lbry/wallet/server/db/prefixes.py +++ b/lbry/wallet/server/db/prefixes.py @@ -1363,12 +1363,14 @@ class TrendingSpikePrefixRow(PrefixRow): struct.Struct(b'>L20sLH').pack ] - def pack_spike(self, height: int, claim_hash: bytes, tx_num: int, position: int, amount: int, half_life: int, + @classmethod + def pack_spike(cls, height: int, claim_hash: bytes, tx_num: int, position: int, amount: int, half_life: int, depth: int = 0, subtract: bool = False) -> RevertablePut: - softened_change = (((amount * 1E-8) + 1E-8) ** 0.25).real - spike_mass = (-1.0 if subtract else 1.0) * softened_change * 2 * ((2.0 ** (-1 / half_life)) ** depth) - # trending_spike_height = self.height + delay_trending_spike(self.amount * 1E-8) - return RevertablePut(*self.pack_item(height, claim_hash, tx_num, position, spike_mass)) + softened_change = (((amount * 1E-8) + 1E-8) ** (1 / 4)) + spike_mass = softened_change * ((2.0 ** (-1 / half_life)) ** depth) + if subtract: + spike_mass = -spike_mass + return RevertablePut(*cls.pack_item(height, claim_hash, tx_num, position, spike_mass)) @classmethod def pack_key(cls, height: int, claim_hash: bytes, tx_num: int, position: int): diff --git a/lbry/wallet/server/env.py b/lbry/wallet/server/env.py index 6086c3ff6..d2de19254 100644 --- a/lbry/wallet/server/env.py +++ b/lbry/wallet/server/env.py @@ -5,7 +5,7 @@ # See the file "LICENCE" for information about the copyright # and warranty status of this software. - +import math import re import resource from os import environ @@ -39,12 +39,13 @@ class Env: self.obsolete(['UTXO_MB', 'HIST_MB', 'NETWORK']) self.db_dir = self.required('DB_DIRECTORY') self.db_engine = self.default('DB_ENGINE', 'leveldb') - self.trending_algorithms = [ - trending for trending in set(self.default('TRENDING_ALGORITHMS', 'zscore').split(' ')) if trending - ] - self.trending_half_life = float(self.string_amount('TRENDING_HALF_LIFE', "0.9")) - self.trending_whale_half_life = float(self.string_amount('TRENDING_WHALE_HALF_LIFE', "0.99")) - self.trending_whale_threshold = float(self.integer('TRENDING_WHALE_THRESHOLD', 10000)) + # self.trending_algorithms = [ + # trending for trending in set(self.default('TRENDING_ALGORITHMS', 'zscore').split(' ')) if trending + # ] + self.trending_half_life = math.log2(0.1 ** (1 / (3 + self.integer('TRENDING_DECAY_RATE', 48)))) + 1 + self.trending_whale_half_life = math.log2(0.1 ** (1 / (3 + self.integer('TRENDING_WHALE_DECAY_RATE', 24)))) + 1 + self.trending_whale_threshold = float(self.integer('TRENDING_WHALE_THRESHOLD', 10000)) * 1E8 + self.max_query_workers = self.integer('MAX_QUERY_WORKERS', None) self.individual_tag_indexes = self.boolean('INDIVIDUAL_TAG_INDEXES', True) self.track_metrics = self.boolean('TRACK_METRICS', False) diff --git a/lbry/wallet/server/session.py b/lbry/wallet/server/session.py index 4ee328621..767261213 100644 --- a/lbry/wallet/server/session.py +++ b/lbry/wallet/server/session.py @@ -897,7 +897,7 @@ class LBRYElectrumX(SessionBase): 'donation_address': env.donation_address, 'daily_fee': env.daily_fee, 'hash_function': 'sha256', - 'trending_algorithm': env.trending_algorithms[0] + 'trending_algorithm': 'variable_decay' }) async def server_features_async(self):