Merge pull request #2765 from eggplantbren/master

fixed some issues with AR trending
This commit is contained in:
Lex Berezhny 2020-02-09 17:53:27 -05:00 committed by GitHub
commit dadc004dd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,16 +24,23 @@ DECAY_PER_RENORM = DECAY**(RENORM_INTERVAL)
TRENDING_LOG = True
# Stubs
def install(connection):
"""
Install the AR trending algorithm.
"""
check_trending_values(connection)
if TRENDING_LOG:
f = open("trending_ar.log", "a")
f.close()
# Stub
CREATE_TREND_TABLE = ""
def check_trending_values(connection):
"""
If the trending values appear to be based on the standard algorithm,
If the trending values appear to be based on the zscore algorithm,
reset them. This will allow resyncing from a standard snapshot.
"""
c = connection.cursor()
@ -53,9 +60,6 @@ def check_trending_values(connection):
COMMIT;""")
print("done.")
# Create the index
c.execute("create index if not exists claim_id_only_idx on claim (claim_id);")
def spike_height(trending_score, x, x_old, time_boost=1.0):
"""
@ -71,8 +75,9 @@ def spike_height(trending_score, x, x_old, time_boost=1.0):
# Softened change in amount counts more for minnows
if delta > 0.0:
multiplier = 1.0/math.sqrt(x + 1.0)
softened_change_in_amount *= multiplier
if trending_score >= 0.0:
multiplier = 0.1/((trending_score/time_boost + softened_change_in_amount) + 1.0)
softened_change_in_amount *= multiplier
else:
softened_change_in_amount *= -1.0
@ -106,14 +111,14 @@ class TrendingData:
# Have all claims been read from db yet?
self.initialised = False
def insert_claim_from_load(self, claim_id, trending_score, total_amount):
def insert_claim_from_load(self, claim_hash, trending_score, total_amount):
assert not self.initialised
self.claims[claim_id] = {"trending_score": trending_score,
"total_amount": total_amount,
"changed": False}
self.claims[claim_hash] = {"trending_score": trending_score,
"total_amount": total_amount,
"changed": False}
def update_claim(self, claim_id, total_amount, time_boost=1.0):
def update_claim(self, claim_hash, total_amount, time_boost=1.0):
"""
Update trending data for a claim, given its new total amount.
"""
@ -121,8 +126,8 @@ class TrendingData:
# Extract existing total amount and trending score
# or use starting values if the claim is new
if claim_id in self.claims:
old_state = copy.deepcopy(self.claims[claim_id])
if claim_hash in self.claims:
old_state = copy.deepcopy(self.claims[claim_hash])
else:
old_state = {"trending_score": 0.0,
"total_amount": 0.0,
@ -138,9 +143,9 @@ class TrendingData:
old_state["total_amount"],
time_boost)
trending_score = old_state["trending_score"] + spike
self.claims[claim_id] = {"total_amount": total_amount,
"trending_score": trending_score,
"changed": True}
self.claims[claim_hash] = {"total_amount": total_amount,
"trending_score": trending_score,
"changed": True}
@ -170,13 +175,12 @@ def test_trending():
# One global instance
# pylint: disable=C0103
trending_data = TrendingData()
f = open("trending_ar.log", "w")
f.close()
def run(db, height, final_height, recalculate_claim_hashes):
if height < final_height - 5*HALF_LIFE:
trending_log("Skipping AR trending at block {h}.\n".format(h=height))
return
start = time.time()
@ -212,7 +216,7 @@ def run(db, height, final_height, recalculate_claim_hashes):
if not trending_data.initialised:
# On fresh launch
for row in db.execute("""
SELECT claim_id, trending_mixed,
SELECT claim_hash, trending_mixed,
(amount + support_amount)
AS total_amount
FROM claim;
@ -221,7 +225,7 @@ def run(db, height, final_height, recalculate_claim_hashes):
trending_data.initialised = True
else:
for row in db.execute(f"""
SELECT claim_id,
SELECT claim_hash,
(amount + support_amount)
AS total_amount
FROM claim
@ -248,7 +252,7 @@ def run(db, height, final_height, recalculate_claim_hashes):
trending_log("{n} scores to write...".format(n=len(the_list)))
db.executemany("UPDATE claim SET trending_mixed=? WHERE claim_id=?;",
db.executemany("UPDATE claim SET trending_mixed=? WHERE claim_hash=?;",
the_list)
trending_log("done.\n")