Skip to content

Commit 399f06c

Browse files
HafizHafiz
authored andcommitted
[change] Replace third-party JSONField with native models.JSONField and optimize indexing
1 parent 331fca2 commit 399f06c

7 files changed

Lines changed: 40 additions & 41 deletions

File tree

openwisp_monitoring/check/base/models.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from collections import OrderedDict
2-
31
from django.contrib.contenttypes.fields import GenericForeignKey
42
from django.contrib.contenttypes.models import ContentType
53
from django.db import models
64
from django.utils.functional import cached_property
75
from django.utils.module_loading import import_string
86
from django.utils.translation import gettext_lazy as _
9-
from jsonfield import JSONField
107

118
from openwisp_utils.base import TimeStampedEditableModel
129

@@ -37,13 +34,11 @@ class AbstractCheck(TimeStampedEditableModel):
3734
db_index=True,
3835
max_length=128,
3936
)
40-
params = JSONField(
37+
params = models.JSONField(
4138
_("parameters"),
4239
default=dict,
4340
blank=True,
4441
help_text=_("parameters needed to perform the check"),
45-
load_kwargs={"object_pairs_hook": OrderedDict},
46-
dump_kwargs={"indent": 4},
4742
)
4843

4944
class Meta:

openwisp_monitoring/check/migrations/0001_initial_squashed_0002_check_unique_together.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Generated by Django 3.1.2 on 2020-11-18 12:55
22

3-
import collections
43
import uuid
54

65
import django.db.models.deletion
76
import django.utils.timezone
8-
import jsonfield.fields
97
import model_utils.fields
108
import swapper
119
from django.db import migrations, models
@@ -67,12 +65,10 @@ class Migration(migrations.Migration):
6765
),
6866
(
6967
"params",
70-
jsonfield.fields.JSONField(
68+
models.JSONField(
7169
blank=True,
7270
default=dict,
73-
dump_kwargs={"indent": 4},
7471
help_text="parameters needed to perform the check",
75-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
7672
verbose_name="parameters",
7773
),
7874
),

openwisp_monitoring/monitoring/base/models.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from django.contrib.auth import get_user_model
1212
from django.contrib.contenttypes.fields import GenericForeignKey
1313
from django.contrib.contenttypes.models import ContentType
14+
from django.contrib.postgres.indexes import GinIndex
1415
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1516
from django.core.validators import MaxValueValidator
1617
from django.db import IntegrityError, models
1718
from django.utils import timezone
1819
from django.utils.translation import gettext_lazy as _
19-
from jsonfield import JSONField
2020
from openwisp_notifications.signals import notify
2121
from pytz import timezone as tz
2222
from pytz import utc
@@ -91,20 +91,16 @@ class AbstractMetric(TimeStampedEditableModel):
9191
)
9292
object_id = models.CharField(max_length=36, db_index=True, blank=True, null=True)
9393
content_object = GenericForeignKey("content_type", "object_id")
94-
main_tags = JSONField(
94+
main_tags = models.JSONField(
9595
_("main tags"),
9696
default=dict,
9797
blank=True,
98-
load_kwargs={"object_pairs_hook": OrderedDict},
99-
dump_kwargs={"indent": 4},
10098
db_index=True,
10199
)
102-
extra_tags = JSONField(
100+
extra_tags = models.JSONField(
103101
_("extra tags"),
104102
default=dict,
105103
blank=True,
106-
load_kwargs={"object_pairs_hook": OrderedDict},
107-
dump_kwargs={"indent": 4},
108104
)
109105
# NULL means the health has yet to be assessed
110106
is_healthy = models.BooleanField(default=None, null=True, blank=True, db_index=True)
@@ -120,6 +116,10 @@ class Meta:
120116
"object_id",
121117
"main_tags",
122118
)
119+
indexes = [
120+
GinIndex(fields=["main_tags"], name="main_tags_gin_idx"),
121+
GinIndex(fields=["extra_tags"], name="extra_tags_gin_idx"),
122+
]
123123

124124
def __str__(self):
125125
obj = self.content_object

openwisp_monitoring/monitoring/migrations/0004_metric_main_and_extra_tags.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Generated by Django 3.2.12 on 2022-04-23 17:50
22

3-
import collections
4-
5-
import jsonfield.fields
6-
from django.db import migrations
3+
from django.db import migrations, models
74

85

96
class Migration(migrations.Migration):
@@ -16,23 +13,19 @@ class Migration(migrations.Migration):
1613
migrations.AddField(
1714
model_name="metric",
1815
name="main_tags",
19-
field=jsonfield.fields.JSONField(
16+
field=models.JSONField(
2017
blank=True,
2118
db_index=True,
2219
default=dict,
23-
dump_kwargs={"indent": 4},
24-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
2520
verbose_name="main tags",
2621
),
2722
),
2823
migrations.AddField(
2924
model_name="metric",
3025
name="extra_tags",
31-
field=jsonfield.fields.JSONField(
26+
field=models.JSONField(
3227
blank=True,
3328
default=dict,
34-
dump_kwargs={"indent": 4},
35-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
3629
verbose_name="extra tags",
3730
),
3831
),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.db import migrations
2+
3+
class Migration(migrations.Migration):
4+
dependencies = [
5+
('monitoring', '0012_migrate_signal_metrics'),
6+
]
7+
8+
operations = [
9+
migrations.RunSQL(
10+
sql='DROP INDEX IF EXISTS monitoring_metric_main_tags_2d8550ae_like; '
11+
'DROP INDEX IF EXISTS monitoring_metric_main_tags_2d8550ae;',
12+
reverse_sql=migrations.RunSQL.noop
13+
),
14+
migrations.RunSQL(
15+
sql='ALTER TABLE monitoring_metric ALTER COLUMN main_tags TYPE jsonb USING main_tags::jsonb; '
16+
'ALTER TABLE monitoring_metric ALTER COLUMN extra_tags TYPE jsonb USING extra_tags::jsonb;',
17+
reverse_sql='ALTER TABLE monitoring_metric ALTER COLUMN main_tags TYPE text; '
18+
'ALTER TABLE monitoring_metric ALTER COLUMN extra_tags TYPE text;'
19+
),
20+
migrations.RunSQL(
21+
sql='CREATE INDEX IF NOT EXISTS main_tags_gin_idx ON monitoring_metric USING gin (main_tags); '
22+
'CREATE INDEX IF NOT EXISTS extra_tags_gin_idx ON monitoring_metric USING gin (extra_tags);',
23+
reverse_sql='DROP INDEX IF EXISTS main_tags_gin_idx; DROP INDEX IF EXISTS extra_tags_gin_idx;'
24+
),
25+
]

tests/openwisp2/sample_check/migrations/0001_initial.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Generated by Django 3.0.5 on 2020-05-27 03:04
22

3-
import collections
43
import uuid
54

65
import django.db.models.deletion
76
import django.utils.timezone
8-
import jsonfield.fields
97
import model_utils.fields
108
from django.db import migrations, models
119

@@ -74,12 +72,10 @@ class Migration(migrations.Migration):
7472
),
7573
(
7674
"params",
77-
jsonfield.fields.JSONField(
75+
models.JSONField(
7876
blank=True,
7977
default=dict,
80-
dump_kwargs={"indent": 4},
8178
help_text="parameters needed to perform the check",
82-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
8379
verbose_name="parameters",
8480
),
8581
),

tests/openwisp2/sample_monitoring/migrations/0001_initial.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Generated by Django 3.0.5 on 2020-05-27 03:04
22

3-
import collections
43
import uuid
54

65
import django
76
import django.core.validators
87
import django.db.models.deletion
98
import django.utils.timezone
10-
import jsonfield
119
import model_utils.fields
1210
import swapper
1311
from django.db import migrations, models
@@ -104,22 +102,18 @@ class Migration(migrations.Migration):
104102
),
105103
(
106104
"main_tags",
107-
jsonfield.fields.JSONField(
105+
models.JSONField(
108106
blank=True,
109107
db_index=True,
110108
default=dict,
111-
dump_kwargs={"indent": 4},
112-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
113109
verbose_name="main tags",
114110
),
115111
),
116112
(
117113
"extra_tags",
118-
jsonfield.fields.JSONField(
114+
models.JSONField(
119115
blank=True,
120116
default=dict,
121-
dump_kwargs={"indent": 4},
122-
load_kwargs={"object_pairs_hook": collections.OrderedDict},
123117
verbose_name="extra tags",
124118
),
125119
),

0 commit comments

Comments
 (0)