From 6978bcc5edaa19ef1d91d783a72479aff5ef4831 Mon Sep 17 00:00:00 2001 From: db0 Date: Thu, 22 Jun 2023 16:29:58 +0200 Subject: [PATCH] orphan control --- overseer/apis/v1/endorsements.py | 2 +- overseer/apis/v1/guarantees.py | 11 +++++++++-- overseer/classes/instance.py | 14 +++++++++++++- overseer/database/functions.py | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/overseer/apis/v1/endorsements.py b/overseer/apis/v1/endorsements.py index a3ae03f..0093ef6 100644 --- a/overseer/apis/v1/endorsements.py +++ b/overseer/apis/v1/endorsements.py @@ -38,7 +38,7 @@ class Endorsements(Resource): @api.marshal_with(models.response_model_model_Whitelist_get, code=200, description='Instances', skip_none=True) @api.response(404, 'Instance not registered', models.response_model_error) def get(self, domain): - '''Display all endorsements given by a specific domain + '''Display all endorsements received by a specific domain ''' self.args = self.get_parser.parse_args() instance = database.find_instance_by_domain(domain) diff --git a/overseer/apis/v1/guarantees.py b/overseer/apis/v1/guarantees.py index 776ca9a..daffa16 100644 --- a/overseer/apis/v1/guarantees.py +++ b/overseer/apis/v1/guarantees.py @@ -103,6 +103,7 @@ class Guarantees(Resource): orphan_ids = database.get_guarantee_chain(target_instance.id) for orphan in database.get_instances_by_ids(orphan_ids): pm_instance(orphan.domain, f"Phew! You guarantor chain has been repaired as {instance.domain} has guaranteed for {domain}.") + orphan.unset_as_orphan() logger.info(f"{instance.domain} Guaranteed for {domain}") return {"message":'Changed'}, 200 @@ -141,9 +142,15 @@ class Guarantees(Resource): db.session.delete(endorsement) db.session.delete(guarantee) db.session.commit() - pm_instance(target_instance.domain, f"Attention! You guarantor instance {instance.domain} has withdrawn their backing.\n\nIMPORTANT: All your guarantees will be deleted unless you manage to find a new guarantor within 24hours!") + pm_instance(target_instance.domain, + f"Attention! You guarantor instance {instance.domain} has withdrawn their backing.\n\n" + "IMPORTANT: You are still considered guaranteed for the next 24hours, but you cannot further endorse or guarantee others." + "If you find a new guarantor then your guarantees will be reactivated!.\n\n" + "Note that if you do not find a guarantor within 7 days, all your endorsements will be removed." + ) orphan_ids = database.get_guarantee_chain(target_instance.id) for orphan in database.get_instances_by_ids(orphan_ids): - pm_instance(orphan.domain, f"Attention! You guarantor chain has been b broken because {instance.domain} has withdrawn their backing from {domain}.\n\nIMPORTANT: All your guarantees will be deleted unless the chain is repaired or you find a new guarantor within 24hours!") + pm_instance(orphan.domain, f"Attention! You guarantor chain has been broken because {instance.domain} has withdrawn their backing from {domain}.\n\nIMPORTANT: All your guarantees will be deleted unless the chain is repaired or you find a new guarantor within 24hours!") + orphan.set_as_oprhan() logger.info(f"{instance.domain} Withdrew guarantee from {domain}") return {"message":'Changed'}, 200 \ No newline at end of file diff --git a/overseer/classes/instance.py b/overseer/classes/instance.py index 66f3cd4..c88b21b 100644 --- a/overseer/classes/instance.py +++ b/overseer/classes/instance.py @@ -19,6 +19,7 @@ class Guarantee(db.Model): guarantor_instance = db.relationship("Instance", back_populates="guarantees", foreign_keys=[guarantor_id]) guaranteed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), unique=True, nullable=False) guaranteed_instance = db.relationship("Instance", back_populates="guarantors", foreign_keys=[guaranteed_id]) + created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) class Endorsement(db.Model): @@ -29,6 +30,7 @@ class Endorsement(db.Model): approving_instance = db.relationship("Instance", back_populates="approvals", foreign_keys=[approving_id]) endorsed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False) endorsed_instance = db.relationship("Instance", back_populates="endorsements", foreign_keys=[endorsed_id]) + created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) class Instance(db.Model): @@ -39,6 +41,7 @@ class Instance(db.Model): api_key = db.Column(db.String(100), unique=True, nullable=False, index=True) created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + oprhan_since = db.Column(db.DateTime, nullable=True) open_registrations = db.Column(db.Boolean, unique=False, nullable=False, index=True) email_verify = db.Column(db.Boolean, unique=False, nullable=False, index=True) @@ -80,4 +83,13 @@ class Instance(db.Model): def get_guarantor_domain(self): guarantor = self.get_guarantor() - return guarantor.domain if guarantor else None \ No newline at end of file + return guarantor.domain if guarantor else None + + def set_as_oprhan(self): + self.oprhan_since = datetime.utcnow() + db.session.commit() + + def unset_as_orphan(self): + self.oprhan_since = None + db.session.commit() + \ No newline at end of file diff --git a/overseer/database/functions.py b/overseer/database/functions.py index 18ed57a..04fb7de 100644 --- a/overseer/database/functions.py +++ b/overseer/database/functions.py @@ -21,6 +21,11 @@ def get_all_instances(min_endorsements = 0, min_guarantors = 1): joinedload(Instance.endorsements), ).group_by( Instance.id + ).filter( + or_( + Instance.oprhan_since == None, + Instance.oprhan_since > datetime.utcnow() - timedelta(hours=24) + ) ).having( db.func.count(Instance.endorsements) >= min_endorsements, ).having(