Implement asset rules as Asset Group children objects

This commit is contained in:
2023-02-13 20:45:23 +00:00
parent b48af50620
commit 6ff5f718ba
18 changed files with 565 additions and 349 deletions

View File

@@ -46,6 +46,24 @@ AGGREGATION_CHOICES = (
("avg_sentiment", "Average sentiment"),
)
STATUS_CHOICES = (
(0, "No data"),
(1, "No match"),
(2, "Bullish"),
(3, "Bearish"),
(4, "No aggregation"),
(5, "Not in bounds"),
(6, "Always allow"),
(7, "Always deny"),
)
MAPPING_CHOICES = (
(6, "Always allow"),
(7, "Always deny"),
(2, "Bullish"),
(3, "Bearish"),
)
class Plan(models.Model):
name = models.CharField(max_length=255, unique=True)
@@ -416,16 +434,17 @@ class AssetGroup(models.Model):
description = models.TextField(null=True, blank=True)
# Dict like {"RUB": True, "USD": False}
allowed = models.JSONField(null=True, blank=True, default=dict)
# allowed = models.JSONField(null=True, blank=True, default=dict)
webhook_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
aggregation = models.CharField(
choices=AGGREGATION_CHOICES, max_length=255, default="none"
)
when_no_data = models.IntegerField(choices=MAPPING_CHOICES, default=7)
when_no_match = models.IntegerField(choices=MAPPING_CHOICES, default=6)
when_no_aggregation = models.IntegerField(choices=MAPPING_CHOICES, default=6)
when_not_in_bounds = models.IntegerField(choices=MAPPING_CHOICES, default=6)
trigger_below = models.FloatField(null=True, blank=True)
trigger_above = models.FloatField(null=True, blank=True)
when_bullish = models.IntegerField(choices=MAPPING_CHOICES, default=2)
when_bearish = models.IntegerField(choices=MAPPING_CHOICES, default=3)
def __str__(self):
return f"{self.name}"
@@ -435,20 +454,24 @@ class AssetGroup(models.Model):
"""
Get the total number of matches for this group.
"""
if isinstance(self.allowed, dict):
truthy_values = [x for x in self.allowed.values() if x is True]
return f"{len(truthy_values)}/{len(self.allowed)}"
asset_rule_total = AssetRule.objects.filter(group=self).count()
asset_rule_positive = AssetRule.objects.filter(group=self, status=2).count()
return f"{asset_rule_positive}/{asset_rule_total}"
# class AssetRestriction(models.Model):
# user = models.ForeignKey(User, on_delete=models.CASCADE)
# name = models.CharField(max_length=255)
# description = models.TextField(null=True, blank=True)
# pairs = models.CharField(max_length=4096, null=True, blank=True)
# pairs_parsed = models.JSONField(null=True, blank=True, default=list)
class AssetRule(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
asset = models.CharField(max_length=64)
group = models.ForeignKey(AssetGroup, on_delete=models.CASCADE)
aggregation = models.CharField(
choices=AGGREGATION_CHOICES, max_length=255, default="none"
)
value = models.FloatField(null=True, blank=True)
original_status = models.IntegerField(choices=STATUS_CHOICES, default=0)
status = models.IntegerField(choices=STATUS_CHOICES, default=0)
trigger_below = models.FloatField(null=True, blank=True)
trigger_above = models.FloatField(null=True, blank=True)
# webhook_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
# group = models.ForeignKey(
# AssetGroup, on_delete=models.CASCADE, null=True, blank=True
# )
# Ensure that the asset is unique per group
class Meta:
unique_together = ("asset", "group")