I have two models relevant to this question. Each PowerSource has a maximum current (number of amps) that can flow through it before it becomes dangerous.
I want to allow people to utilize these power sources by scheduling Usage instances.
class PowerSource(models.Model): title = models.CharField(max_length=20, unique=True) max_amps = models.SmallIntegerField(default=0, help_text='Maximum capacity for this Power Source in amps') class Usage(models.Model): power_source = ForeignKey(PowerSource, on_delete=models.CASCADE) start_dt = models.DateTimeField() end_dt = models.DateTimeField() amps = models.SmallIntegerField(help_text='How much current, in amps, does this usage need?')
It's easy enough to check whether a newly placed Usage instance will put the PowerSource over the max_amps value (just get the sum of amps for each order that overlaps with the new Usage instance), but I need something a bit more complex:
Assuming there are multiple Usage instances with overlapping start/stop periods for a particular PowerSource, how can I efficiently identify all time periods when overall usage is at or above the 'max_amps' value for the PowerSource?
I'm pretty experienced with Django, but I don't even know where to start with something like this. Can this be done in the ORM?