Browse Source

Implement the notion of "real share"

Taking into account the "wasted part" of resources, that have to be split
accross the different users.
Jocelyn Delande 9 years ago
parent
commit
4b20885fae
2 changed files with 37 additions and 1 deletions
  1. 24 1
      costs/models.py
  2. 13 0
      costs/tests/test_models.py

+ 24 - 1
costs/models.py

@@ -50,6 +50,9 @@ class AbstractItem(models.Model):
 
 
         return existing_uses_sum
         return existing_uses_sum
 
 
+    def unused(self):
+        return 1-self.used()
+
     class Meta:
     class Meta:
         abstract = True
         abstract = True
 
 
@@ -105,12 +108,32 @@ class AbstractUse(models.Model):
                 raise ValidationError(
                 raise ValidationError(
                     "Cannot use more than 100% of {})".format(self.resource))
                     "Cannot use more than 100% of {})".format(self.resource))
 
 
+    def real_share(self):
+        """The share, + wasted space share
+
+        Taking into account that the unused space is
+        wasted and has to be divided among actual users
+        """
+        return (
+            self.share +
+            (self.share/self.resource.used())*self.resource.unused()
+        )
+
+    def value_share(self):
+        return self.resource.price*self.real_share()
+
+    def unit_value_share(self):
+        if self.service.subscriptions_count == 0:
+            return 0
+        else:
+            return self.value_share()/self.service.subscriptions_count
+
 
 
 class CostUse(AbstractUse):
 class CostUse(AbstractUse):
     resource = models.ForeignKey(Cost)
     resource = models.ForeignKey(Cost)
 
 
     def cost_share(self):
     def cost_share(self):
-        return self.share*self.resource.price
+        return self.real_share()*self.resource.price
 
 
     def unit_cost_share(self):
     def unit_cost_share(self):
         subscriptions_count = self.service.subscriptions_count
         subscriptions_count = self.service.subscriptions_count

+ 13 - 0
costs/tests/test_models.py

@@ -81,3 +81,16 @@ class AbstractUseTests(TestCase):
         u1.save()
         u1.save()
         u1.full_clean()
         u1.full_clean()
         u1.save()
         u1.save()
+
+    def test_real_shares(self):
+        u1 = CostUse.objects.create(
+            service=self.hosting_service,
+            resource=self.datacenter_cost,
+            share=0.4)
+        u2 = CostUse.objects.create(
+            service=self.hosting_service,
+            resource=self.datacenter_cost,
+            share=0.2)
+
+        self.assertEqual(u1.real_share() + u2.real_share(), 1)
+        self.assertEqual(u1.share/u2.share, u1.real_share()/u2.real_share())