Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SystemLimit data model #35675

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions corehq/apps/dump_reload/tests/test_dump_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"pillowtop.DjangoPillowCheckpoint",
"pillowtop.KafkaCheckpoint",
"project_limits.DynamicRateDefinition",
"project_limits.SystemLimit",
"project_limits.RateLimitedTwoFactorLog",
"project_limits.PillowLagGaugeDefinition",
"registration.SelfSignupWorkflow",
Expand Down
7 changes: 7 additions & 0 deletions corehq/project_limits/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
DynamicRateDefinition,
PillowLagGaugeDefinition,
RateLimitedTwoFactorLog,
SystemLimit,
)


Expand All @@ -27,3 +28,9 @@ class PillowThrottleDefinitionAdmin(admin.ModelAdmin):
'max_value', 'average_value', 'is_enabled')
list_filter = ('key', 'is_enabled')
ordering = ('key',)


@admin.register(SystemLimit)
class SystemLimitAdmin(admin.ModelAdmin):
list_display = ('key', 'limit')
ordering = ('key',)
26 changes: 26 additions & 0 deletions corehq/project_limits/migrations/0004_systemlimit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.17 on 2025-01-30 23:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('project_limits', '0003_pillowlaggaugedefinition'),
]

operations = [
migrations.CreateModel(
name='SystemLimit',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=255)),
('limit', models.PositiveIntegerField()),
('domain', models.CharField(blank=True, default='', max_length=128)),
],
),
migrations.AddConstraint(
model_name='systemlimit',
constraint=models.UniqueConstraint(fields=('key', 'domain'), name='unique_key_per_domain_constraint'),
),
]
22 changes: 19 additions & 3 deletions corehq/project_limits/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models

import architect
from django.db import models
from field_audit import audit_fields

AVG = 'AVG'
MAX = 'MAX'
Expand Down Expand Up @@ -29,6 +29,7 @@ def delete(self, *args, **kwargs):

def _clear_caches(self):
from corehq.project_limits.rate_limiter import get_dynamic_rate_definition

get_dynamic_rate_definition.clear(self.key, {})


Expand All @@ -37,6 +38,7 @@ class GaugeDefinition(models.Model):
An abstract model to be used to define configuration to limit gauge values.
The model is used by GaugeLimiter class to decide weather to limit or not.
"""

key = models.CharField(max_length=512, blank=False, null=False, unique=True, db_index=True)
wait_for_seconds = models.IntegerField(null=False)
acceptable_value = models.FloatField(default=None, blank=True, null=True)
Expand All @@ -59,12 +61,12 @@ def _clear_caches(self):


class PillowLagGaugeDefinition(GaugeDefinition):

max_value = models.FloatField(default=None, blank=True, null=True)
average_value = models.FloatField(default=None, blank=True, null=True)

def _clear_caches(self):
from corehq.project_limits.gauge import get_pillow_throttle_definition

get_pillow_throttle_definition.clear(self.key)


Expand All @@ -80,3 +82,17 @@ class RateLimitedTwoFactorLog(models.Model):
window = models.CharField(max_length=15, null=False)
# largest input is 'number_rate_limited', 31 for headroom
status = models.CharField(max_length=31, null=False)


@audit_fields("limit")
class SystemLimit(models.Model):
key = models.CharField(max_length=255)
limit = models.PositiveIntegerField()
# the domain field is reserved for extreme cases since limits should apply globally in steady state
domain = models.CharField(max_length=128, blank=True, default="")

class Meta:
constraints = [models.UniqueConstraint(fields=['key', 'domain'], name='unique_key_per_domain_constraint')]

def __str__(self):
return f"{self.key}: {self.limit}"
1 change: 1 addition & 0 deletions migrations.lock
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ project_limits
0001_initial
0002_ratelimitedtwofactorlog
0003_pillowlaggaugedefinition
0004_systemlimit
registration
0001_initial
0002_alter_request_ip
Expand Down
Loading